1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2011 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * FragmentQueue.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 19, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "FragmentQueue.hpp"
|
---|
23 |
|
---|
24 | #include "CodePatterns/Assert.hpp"
|
---|
25 |
|
---|
26 | FragmentResult FragmentQueue::NoResult(-1);
|
---|
27 | FragmentResult FragmentQueue::ResultDelivered(-2);
|
---|
28 |
|
---|
29 | /** Constructor for class FragmentQueue.
|
---|
30 | *
|
---|
31 | */
|
---|
32 | FragmentQueue::FragmentQueue()
|
---|
33 | {}
|
---|
34 |
|
---|
35 | /** Destructor for class FragmentQueue.
|
---|
36 | *
|
---|
37 | */
|
---|
38 | FragmentQueue::~FragmentQueue()
|
---|
39 | {}
|
---|
40 |
|
---|
41 | /** Pushes a FragmentJob into the internal queue for delivery to server.
|
---|
42 | *
|
---|
43 | * \note we throw assertion when jobid has already been used.
|
---|
44 | *
|
---|
45 | * \param job job to enter into queue
|
---|
46 | */
|
---|
47 | void FragmentQueue::pushJob(FragmentJob &job)
|
---|
48 | {
|
---|
49 | ASSERT(job.getId() != (JobId_t)-1,
|
---|
50 | "FragmentQueue::pushJob() - job to push has no valid id.");
|
---|
51 | ASSERT(!results.count(job.getId()),
|
---|
52 | "FragmentQueue::pushJob() - job id "+toString(job.getId())+" has already been used.");
|
---|
53 | jobs.push_back(job);
|
---|
54 | results.insert( std::make_pair(job.getId(), NoResult));
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Queries whether a job has already been finished and the result is present.
|
---|
58 | *
|
---|
59 | * \param jobid id of job to query
|
---|
60 | * \return true - result is present, false - result is not present
|
---|
61 | */
|
---|
62 | bool FragmentQueue::isResultPresent(JobId_t jobid) const
|
---|
63 | {
|
---|
64 | ResultMap::const_iterator iter = results.find(jobid);
|
---|
65 | return ((iter != results.end())
|
---|
66 | && (iter->second != NoResult)
|
---|
67 | && (iter->second != ResultDelivered));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Delivers result for a finished job.
|
---|
71 | *
|
---|
72 | * \note we throw assertion if not present
|
---|
73 | *
|
---|
74 | * \param jobid id of job
|
---|
75 | * \return result for job of given \a jobid
|
---|
76 | */
|
---|
77 | FragmentResult FragmentQueue::getResult(JobId_t jobid)
|
---|
78 | {
|
---|
79 | ResultMap::iterator iter = results.find(jobid);
|
---|
80 | ASSERT(iter != results.end(),
|
---|
81 | "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
|
---|
82 | ASSERT(iter->second != NoResult,
|
---|
83 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has not arrived yet.");
|
---|
84 | ASSERT(iter->second != ResultDelivered,
|
---|
85 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered.");
|
---|
86 | /// store result
|
---|
87 | FragmentResult result = iter->second;
|
---|
88 | /// mark as delivered in map
|
---|
89 | iter->second = ResultDelivered;
|
---|
90 | /// and return result
|
---|
91 | return result;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Pushes a result for a finished job.
|
---|
95 | *
|
---|
96 | * \note we throw assertion if job already has result or is not known.
|
---|
97 | *
|
---|
98 | * \param result result of job to store
|
---|
99 | */
|
---|
100 | void FragmentQueue::pushResult(FragmentResult &result)
|
---|
101 | {
|
---|
102 | /// check for presence
|
---|
103 | ResultMap::iterator iter = results.find(result.getId());
|
---|
104 | ASSERT(iter != results.end(),
|
---|
105 | "FragmentQueue::pushResult() - job "+toString(result.getId())+" is not known to us.");
|
---|
106 | ASSERT(iter->second == NoResult,
|
---|
107 | "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(result.getId())+".");
|
---|
108 | /// and overwrite NoResult in found entry
|
---|
109 | iter->second = result;
|
---|
110 | }
|
---|