| 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(!results.count(job.getId()),
 | 
|---|
| 50 |       "FragmentQueue::pushJob() - job id "+toString(job.getId())+" has already been used.");
 | 
|---|
| 51 |   jobs.push_back(job);
 | 
|---|
| 52 |   results.insert( std::make_pair(job.getId(), NoResult));
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /** Queries whether a job has already been finished and the result is present.
 | 
|---|
| 56 |  *
 | 
|---|
| 57 |  * \param jobid id of job to query
 | 
|---|
| 58 |  * \return true - result is present, false - result is not present
 | 
|---|
| 59 |  */
 | 
|---|
| 60 | bool FragmentQueue::isResultPresent(JobId_t jobid) const
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   ResultMap::const_iterator iter = results.find(jobid);
 | 
|---|
| 63 |   return ((iter != results.end())
 | 
|---|
| 64 |       && (iter->second != NoResult)
 | 
|---|
| 65 |       && (iter->second != ResultDelivered));
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | /** Delivers result for a finished job.
 | 
|---|
| 69 |  *
 | 
|---|
| 70 |  * \note we throw assertion if not present
 | 
|---|
| 71 |  *
 | 
|---|
| 72 |  * \param jobid id of job
 | 
|---|
| 73 |  * \return result for job of given \a jobid
 | 
|---|
| 74 |  */
 | 
|---|
| 75 | FragmentResult FragmentQueue::getResult(JobId_t jobid)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   ResultMap::iterator iter = results.find(jobid);
 | 
|---|
| 78 |   ASSERT(iter != results.end(),
 | 
|---|
| 79 |       "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
 | 
|---|
| 80 |   ASSERT(iter->second != NoResult,
 | 
|---|
| 81 |       "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has not arrived yet.");
 | 
|---|
| 82 |   ASSERT(iter->second != ResultDelivered,
 | 
|---|
| 83 |       "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered.");
 | 
|---|
| 84 |   /// store result
 | 
|---|
| 85 |   FragmentResult result = iter->second;
 | 
|---|
| 86 |   /// mark as delivered in map
 | 
|---|
| 87 |   iter->second = ResultDelivered;
 | 
|---|
| 88 |   /// and return result
 | 
|---|
| 89 |   return result;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | /** Pushes a result for a finished job.
 | 
|---|
| 93 |  *
 | 
|---|
| 94 |  * \note we throw assertion if job already has result or is not known.
 | 
|---|
| 95 |  *
 | 
|---|
| 96 |  * \param jobid id of job whose result is pushed
 | 
|---|
| 97 |  * \param result result of job to store
 | 
|---|
| 98 |  */
 | 
|---|
| 99 | void FragmentQueue::pushResult(const JobId_t jobid, FragmentResult &result)
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   /// check for presence
 | 
|---|
| 102 |   ResultMap::iterator iter = results.find(jobid);
 | 
|---|
| 103 |   ASSERT(iter != results.end(),
 | 
|---|
| 104 |       "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
 | 
|---|
| 105 |   ASSERT(iter->second == NoResult,
 | 
|---|
| 106 |       "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(jobid)+".");
 | 
|---|
| 107 |   /// and overwrite NoResult in found entry
 | 
|---|
| 108 |   iter->second = result;
 | 
|---|
| 109 | }
 | 
|---|