| 1 | /* | 
|---|
| 2 | * SpecificFragmentController_ReceiveResultContainer_impl.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Aug 27, 2012 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef SPECIFICFRAGMENTCONTROLLER_RECEIVERESULTCONTAINER_IMPL_HPP_ | 
|---|
| 9 | #define SPECIFICFRAGMENTCONTROLLER_RECEIVERESULTCONTAINER_IMPL_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | // include config.h | 
|---|
| 13 | #ifdef HAVE_CONFIG_H | 
|---|
| 14 | #include <config.h> | 
|---|
| 15 | #endif | 
|---|
| 16 |  | 
|---|
| 17 | #include "SpecificFragmentController.hpp" | 
|---|
| 18 |  | 
|---|
| 19 | #include "CodePatterns/Assert.hpp" | 
|---|
| 20 | #include "CodePatterns/toString.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include <numeric> | 
|---|
| 23 |  | 
|---|
| 24 | template <typename T> | 
|---|
| 25 | size_t SpecificFragmentController::ReceiveResultContainer<T>::receiveResults( | 
|---|
| 26 | SpecificFragmentController &callback) | 
|---|
| 27 | { | 
|---|
| 28 | // receive (and remove the respective ids) | 
|---|
| 29 | callback.receiveResults(callback.host, callback.port); | 
|---|
| 30 | callback.RunService("Requesting results"); | 
|---|
| 31 | std::vector<FragmentResult::ptr> fragmentresults = callback.getReceivedResults(); | 
|---|
| 32 |  | 
|---|
| 33 | // convert | 
|---|
| 34 | std::vector<T> fragmentData; | 
|---|
| 35 | SpecificFragmentController::ReceiveResultContainer<T>::ConvertFragmentResultTo( | 
|---|
| 36 | fragmentresults, | 
|---|
| 37 | fragmentData); | 
|---|
| 38 |  | 
|---|
| 39 | // insert into map | 
|---|
| 40 | SpecificFragmentController::ReceiveResultContainer<T>::insertResults( | 
|---|
| 41 | fragmentresults, | 
|---|
| 42 | fragmentData); | 
|---|
| 43 |  | 
|---|
| 44 | return fragmentData.size(); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | template <typename T> | 
|---|
| 48 | void SpecificFragmentController::ReceiveResultContainer<T>::waitforResults( | 
|---|
| 49 | const size_t NoExpectedResults, | 
|---|
| 50 | boost::asio::io_service &io_service, | 
|---|
| 51 | SpecificFragmentController &callback) | 
|---|
| 52 | { | 
|---|
| 53 | // wait but receive all results that are already done | 
|---|
| 54 | size_t NoReceivedResults = 0; | 
|---|
| 55 | while ((NoReceivedResults != NoExpectedResults) | 
|---|
| 56 | && (callback.getExitflag() == 0)) { | 
|---|
| 57 | // wait a bit | 
|---|
| 58 | boost::asio::deadline_timer timer(io_service); | 
|---|
| 59 | timer.expires_from_now(boost::posix_time::milliseconds(500)); | 
|---|
| 60 | timer.wait(); | 
|---|
| 61 | // then request status | 
|---|
| 62 | callback.checkResults(callback.host, callback.port); | 
|---|
| 63 | callback.RunService("Checking on results"); | 
|---|
| 64 | if (callback.getExitflag() != 0) | 
|---|
| 65 | break; | 
|---|
| 66 |  | 
|---|
| 67 | const std::pair<size_t, size_t> JobStatus = callback.getJobStatus(); | 
|---|
| 68 | const size_t NoCalculatedResults = JobStatus.second; | 
|---|
| 69 | // if some are done, get them | 
|---|
| 70 | if (NoCalculatedResults != 0) { | 
|---|
| 71 | NoReceivedResults += receiveResults(callback); | 
|---|
| 72 | callback.handler(NoReceivedResults, NoExpectedResults); | 
|---|
| 73 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #" << NoReceivedResults << " of " << NoExpectedResults << " jobs are calculated so far."); | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | #endif /* SPECIFICFRAGMENTCONTROLLER_RECEIVERESULTCONTAINER_IMPL_HPP_ */ | 
|---|