| 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::NoResultQueued(-2); | 
|---|
| 28 | FragmentResult FragmentQueue::ResultDelivered(-3); | 
|---|
| 29 |  | 
|---|
| 30 | /** Constructor for class FragmentQueue. | 
|---|
| 31 | * | 
|---|
| 32 | */ | 
|---|
| 33 | FragmentQueue::FragmentQueue() | 
|---|
| 34 | {} | 
|---|
| 35 |  | 
|---|
| 36 | /** Destructor for class FragmentQueue. | 
|---|
| 37 | * | 
|---|
| 38 | */ | 
|---|
| 39 | FragmentQueue::~FragmentQueue() | 
|---|
| 40 | {} | 
|---|
| 41 |  | 
|---|
| 42 | /** Checks whether there are jobs in the queue at all. | 
|---|
| 43 | * \return true - jobs present, false - queue is empty | 
|---|
| 44 | */ | 
|---|
| 45 | bool FragmentQueue::isJobPresent() const | 
|---|
| 46 | { | 
|---|
| 47 | return !jobs.empty(); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | /** Pushes a FragmentJob into the internal queue for delivery to server. | 
|---|
| 51 | * | 
|---|
| 52 | * \note we throw assertion when jobid has already been used. | 
|---|
| 53 | * | 
|---|
| 54 | * \param job job to enter into queue | 
|---|
| 55 | */ | 
|---|
| 56 | void FragmentQueue::pushJob(FragmentJob &job) | 
|---|
| 57 | { | 
|---|
| 58 | ASSERT(job.getId() != (JobId_t)-1, | 
|---|
| 59 | "FragmentQueue::pushJob() - job to push has no valid id."); | 
|---|
| 60 | ASSERT(!results.count(job.getId()), | 
|---|
| 61 | "FragmentQueue::pushJob() - job id "+toString(job.getId())+" has already been used."); | 
|---|
| 62 | results.insert( std::make_pair(job.getId(), NoResult)); | 
|---|
| 63 | jobs.push_back(job); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | /** Pops top-most FragmentJob from internal queue. | 
|---|
| 67 | * | 
|---|
| 68 | * From here on, we expect a result in FragmentQueue::results. | 
|---|
| 69 | * | 
|---|
| 70 | * \return job topmost job from queue | 
|---|
| 71 | */ | 
|---|
| 72 | FragmentJob FragmentQueue::popJob() | 
|---|
| 73 | { | 
|---|
| 74 | ASSERT(jobs.size(), | 
|---|
| 75 | "FragmentQueue::popJob() - there are no jobs on the queue."); | 
|---|
| 76 | FragmentJob job = jobs.front(); | 
|---|
| 77 | ResultMap::iterator iter = results.find(job.getId()); | 
|---|
| 78 | ASSERT(iter != results.end(), | 
|---|
| 79 | "FragmentQueue::popJob() - for job "+toString(job.getId())+" no result place has been stored."); | 
|---|
| 80 | iter->second = NoResultQueued; | 
|---|
| 81 | jobs.pop_front(); | 
|---|
| 82 | return job; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | /** Queries whether a job has already been finished and the result is present. | 
|---|
| 86 | * | 
|---|
| 87 | * \param jobid id of job to query | 
|---|
| 88 | * \return true - result is present, false - result is not present | 
|---|
| 89 | */ | 
|---|
| 90 | bool FragmentQueue::isResultPresent(JobId_t jobid) const | 
|---|
| 91 | { | 
|---|
| 92 | ResultMap::const_iterator iter = results.find(jobid); | 
|---|
| 93 | return ((iter != results.end()) | 
|---|
| 94 | && (iter->second != NoResult) | 
|---|
| 95 | && (iter->second != NoResultQueued) | 
|---|
| 96 | && (iter->second != ResultDelivered)); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | /** Delivers result for a finished job. | 
|---|
| 100 | * | 
|---|
| 101 | * \note we throw assertion if not present | 
|---|
| 102 | * | 
|---|
| 103 | * \param jobid id of job | 
|---|
| 104 | * \return result for job of given \a jobid | 
|---|
| 105 | */ | 
|---|
| 106 | FragmentResult FragmentQueue::getResult(JobId_t jobid) | 
|---|
| 107 | { | 
|---|
| 108 | ResultMap::iterator iter = results.find(jobid); | 
|---|
| 109 | ASSERT(iter != results.end(), | 
|---|
| 110 | "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us."); | 
|---|
| 111 | ASSERT(iter->second != NoResult, | 
|---|
| 112 | "FragmentQueue::pushResult() - job "+toString(jobid)+" has not been request for calculation yet."); | 
|---|
| 113 | ASSERT(iter->second != NoResultQueued, | 
|---|
| 114 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s calculation is underway but not result has arrived yet."); | 
|---|
| 115 | ASSERT(iter->second != ResultDelivered, | 
|---|
| 116 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered."); | 
|---|
| 117 | /// store result | 
|---|
| 118 | FragmentResult result = iter->second; | 
|---|
| 119 | /// mark as delivered in map | 
|---|
| 120 | iter->second = ResultDelivered; | 
|---|
| 121 | /// and return result | 
|---|
| 122 | return result; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | /** Pushes a result for a finished job. | 
|---|
| 126 | * | 
|---|
| 127 | * \note we throw assertion if job already has result or is not known. | 
|---|
| 128 | * | 
|---|
| 129 | * \param result result of job to store | 
|---|
| 130 | */ | 
|---|
| 131 | void FragmentQueue::pushResult(FragmentResult &result) | 
|---|
| 132 | { | 
|---|
| 133 | /// check for presence | 
|---|
| 134 | ResultMap::iterator iter = results.find(result.getId()); | 
|---|
| 135 | ASSERT(iter != results.end(), | 
|---|
| 136 | "FragmentQueue::pushResult() - job "+toString(result.getId())+" is not known to us."); | 
|---|
| 137 | ASSERT(iter->second == NoResultQueued, | 
|---|
| 138 | "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(result.getId())+"."); | 
|---|
| 139 | /// and overwrite NoResult in found entry | 
|---|
| 140 | iter->second = result; | 
|---|
| 141 | } | 
|---|