| 1 | /* | 
|---|
| 2 | * SpecificFragmentController.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Aug 27, 2012 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef SPECIFICFRAGMENTCONTROLLER_HPP_ | 
|---|
| 9 | #define SPECIFICFRAGMENTCONTROLLER_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | // include config.h | 
|---|
| 13 | #ifdef HAVE_CONFIG_H | 
|---|
| 14 | #include <config.h> | 
|---|
| 15 | #endif | 
|---|
| 16 |  | 
|---|
| 17 | #include "JobMarket/Controller/FragmentController.hpp" | 
|---|
| 18 | #include "JobMarket/types.hpp" | 
|---|
| 19 | #include "JobMarket/Results/FragmentResult.hpp" | 
|---|
| 20 |  | 
|---|
| 21 | #include <boost/function.hpp> | 
|---|
| 22 | #include <map> | 
|---|
| 23 | #include <string> | 
|---|
| 24 | #include <vector> | 
|---|
| 25 |  | 
|---|
| 26 | /** This class extends FragmentController by some commodity functions used | 
|---|
| 27 | * within FragmentationAutomationAction. | 
|---|
| 28 | * | 
|---|
| 29 | */ | 
|---|
| 30 | class SpecificFragmentController : public FragmentController | 
|---|
| 31 | { | 
|---|
| 32 | public: | 
|---|
| 33 | SpecificFragmentController(boost::asio::io_service &_io_service); | 
|---|
| 34 | virtual ~SpecificFragmentController() | 
|---|
| 35 | {} | 
|---|
| 36 |  | 
|---|
| 37 | typedef boost::function<void (const size_t,const size_t)> update_handler_t; | 
|---|
| 38 |  | 
|---|
| 39 | void setUpdateHandler(update_handler_t _handler) | 
|---|
| 40 | { | 
|---|
| 41 | handler = _handler; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void requestIds(const size_t numberjobs); | 
|---|
| 45 | virtual void waitforResults(const size_t NoExpectedResults)=0; | 
|---|
| 46 | void RunService(const std::string message); | 
|---|
| 47 |  | 
|---|
| 48 | /// getter and setter | 
|---|
| 49 |  | 
|---|
| 50 | void setHost(const std::string &_host) { host = _host; } | 
|---|
| 51 | void setPort(const std::string &_port) { port = _port; } | 
|---|
| 52 |  | 
|---|
| 53 | protected: | 
|---|
| 54 |  | 
|---|
| 55 | /** Container for the results received from the server. | 
|---|
| 56 | * | 
|---|
| 57 | * These are received bit by bit until all jobs are calculated. | 
|---|
| 58 | * This struct takes of the waiting. | 
|---|
| 59 | * | 
|---|
| 60 | */ | 
|---|
| 61 | template <typename T> | 
|---|
| 62 | struct ResultContainer { | 
|---|
| 63 | /** cycle to wait for results | 
|---|
| 64 | * | 
|---|
| 65 | * \param NoExpectedResults number of expected results | 
|---|
| 66 | * \param io_service service used for waiting | 
|---|
| 67 | * \param callback ref to call controller functions | 
|---|
| 68 | */ | 
|---|
| 69 | void waitforResults( | 
|---|
| 70 | const size_t NoExpectedResults, | 
|---|
| 71 | boost::asio::io_service &io_service, | 
|---|
| 72 | SpecificFragmentController &callback); | 
|---|
| 73 |  | 
|---|
| 74 | /** Internal function to receive results if some are present. | 
|---|
| 75 | * | 
|---|
| 76 | * \param callback ref to call controller functions | 
|---|
| 77 | * \return number of received results | 
|---|
| 78 | */ | 
|---|
| 79 | size_t receiveResults(SpecificFragmentController &callback); | 
|---|
| 80 |  | 
|---|
| 81 | /** Extracts specific Data type from received vector of FragmentResults. | 
|---|
| 82 | * | 
|---|
| 83 | * @param results results to extract specific Data type from | 
|---|
| 84 | * @param fragmentData on return array filled with extracted specific Data type | 
|---|
| 85 | */ | 
|---|
| 86 | void ConvertFragmentResultTo( | 
|---|
| 87 | const std::vector<FragmentResult::ptr> &results, | 
|---|
| 88 | std::vector<T> &fragmentData); | 
|---|
| 89 |  | 
|---|
| 90 | /** Clears all internally stored results. | 
|---|
| 91 | * | 
|---|
| 92 | */ | 
|---|
| 93 | void clear() | 
|---|
| 94 | { IdData.clear(); } | 
|---|
| 95 |  | 
|---|
| 96 | //!> internal container for the received data | 
|---|
| 97 | std::map<JobId_t, T> IdData; | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | protected: | 
|---|
| 101 | //!> hostname of the server to control | 
|---|
| 102 | std::string host; | 
|---|
| 103 | //!> port of the server to control | 
|---|
| 104 | std::string port; | 
|---|
| 105 | //!> reference to io_service for internal purposes | 
|---|
| 106 | boost::asio::io_service &io_service; | 
|---|
| 107 | //!> update handler that is called in waitforResults() | 
|---|
| 108 | update_handler_t handler; | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 | #include "SpecificFragmentController_ResultContainer_impl.hpp" | 
|---|
| 112 |  | 
|---|
| 113 | #endif /* SPECIFICFRAGMENTCONTROLLER_HPP_ */ | 
|---|