| 1 | /*
|
|---|
| 2 | * FragmentController.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Nov 27, 2011
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef FRAGMENTCONTROLLER_HPP_
|
|---|
| 9 | #define FRAGMENTCONTROLLER_HPP_
|
|---|
| 10 |
|
|---|
| 11 | // include config.h
|
|---|
| 12 | #ifdef HAVE_CONFIG_H
|
|---|
| 13 | #include <config.h>
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | #include <boost/asio.hpp>
|
|---|
| 17 | #include <string>
|
|---|
| 18 | #include <vector>
|
|---|
| 19 |
|
|---|
| 20 | #include "Connection.hpp"
|
|---|
| 21 | #include "FragmentJob.hpp"
|
|---|
| 22 | #include "FragmentResult.hpp"
|
|---|
| 23 |
|
|---|
| 24 | #include "Controller/Commands/Operation.hpp"
|
|---|
| 25 |
|
|---|
| 26 | /** The FragmentController sends bunches of jobs to a FragmentScheduler,
|
|---|
| 27 | * waits for their calculation and is called when they are done. Then,
|
|---|
| 28 | * he loads the bunch of results from the Scheduler.
|
|---|
| 29 | *
|
|---|
| 30 | * While the FragmentScheduler and FragmentWorker rather act on their own
|
|---|
| 31 | * this is the piece to implant into the user software to allow for
|
|---|
| 32 | * communication with the Server/Worker duo to perform the calculation
|
|---|
| 33 | * of the fragments on distant computers.
|
|---|
| 34 | */
|
|---|
| 35 | class FragmentController
|
|---|
| 36 | {
|
|---|
| 37 | public:
|
|---|
| 38 | FragmentController(boost::asio::io_service& io_service, const std::string& _host, const std::string& _service);
|
|---|
| 39 | ~FragmentController();
|
|---|
| 40 |
|
|---|
| 41 | class ReceiveJobsOperation : public Operation {
|
|---|
| 42 | public:
|
|---|
| 43 | /// Constructor for class ReceiveJobsOperation.
|
|---|
| 44 | ReceiveJobsOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 45 | Operation(_connection, _host, _service) {}
|
|---|
| 46 | /// Destructor for class ReceiveJobsOperation
|
|---|
| 47 | ~ReceiveJobsOperation() {}
|
|---|
| 48 |
|
|---|
| 49 | public:
|
|---|
| 50 | /// Placing receive jobs operations into an io_service
|
|---|
| 51 | virtual void operator()();
|
|---|
| 52 |
|
|---|
| 53 | /// Handle completion of a calculate operation.
|
|---|
| 54 | void handle_connect_calc(const boost::system::error_code& e,
|
|---|
| 55 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 56 |
|
|---|
| 57 | /// Callback function when bunch of jobs have been sent.
|
|---|
| 58 | void handle_SendJobs(const boost::system::error_code& e);
|
|---|
| 59 |
|
|---|
| 60 | /// Handle completion of an operation.
|
|---|
| 61 | void handle_FinishOperation(const boost::system::error_code& e);
|
|---|
| 62 |
|
|---|
| 63 | /// internal function to connect to server and send jobs for calculation
|
|---|
| 64 | void connect_calc();
|
|---|
| 65 |
|
|---|
| 66 | /// Setter for jobs
|
|---|
| 67 | void addJobs(const std::vector<FragmentJob> &jobs);
|
|---|
| 68 |
|
|---|
| 69 | /// Getter for number of size of jobs
|
|---|
| 70 | size_t getPresentJobs() const;
|
|---|
| 71 |
|
|---|
| 72 | protected:
|
|---|
| 73 | /// bunch of jobs
|
|---|
| 74 | std::vector<FragmentJob> jobs;
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | struct CheckResultsOperation : public Operation {
|
|---|
| 78 | public:
|
|---|
| 79 | /// Constructor for class CheckResultsOperation.
|
|---|
| 80 | CheckResultsOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 81 | Operation(_connection, _host, _service),
|
|---|
| 82 | doneJobs(0)
|
|---|
| 83 | {}
|
|---|
| 84 | /// Destructor for class CheckResultsOperation
|
|---|
| 85 | ~CheckResultsOperation() {}
|
|---|
| 86 |
|
|---|
| 87 | public:
|
|---|
| 88 | // placing receive jobs operations into an io_service
|
|---|
| 89 | virtual void operator()();
|
|---|
| 90 |
|
|---|
| 91 | /// Handle completion of a CheckResults operation.
|
|---|
| 92 | void handle_connect_check(const boost::system::error_code& e,
|
|---|
| 93 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 94 |
|
|---|
| 95 | /// Callback function when doneJobs have been received.
|
|---|
| 96 | void handle_ReceiveDoneJobs(const boost::system::error_code& e);
|
|---|
| 97 |
|
|---|
| 98 | /// Handle completion of an operation.
|
|---|
| 99 | void handle_FinishOperation(const boost::system::error_code& e);
|
|---|
| 100 |
|
|---|
| 101 | /// internal function to connect to server and check done jobs
|
|---|
| 102 | void connect_check();
|
|---|
| 103 |
|
|---|
| 104 | /// Getter for doneJobs
|
|---|
| 105 | size_t getDoneJobs() const;
|
|---|
| 106 |
|
|---|
| 107 | protected:
|
|---|
| 108 | /// currently calculated results
|
|---|
| 109 | size_t doneJobs;
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | struct SendResultsOperation : public Operation {
|
|---|
| 113 | public:
|
|---|
| 114 | /// Constructor for class SendResultsOperation.
|
|---|
| 115 | SendResultsOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 116 | Operation(_connection, _host, _service) {}
|
|---|
| 117 | /// Destructor for class SendResultsOperation
|
|---|
| 118 | ~SendResultsOperation() {}
|
|---|
| 119 |
|
|---|
| 120 | public:
|
|---|
| 121 | // placing receive jobs operations into an io_service
|
|---|
| 122 | virtual void operator()();
|
|---|
| 123 |
|
|---|
| 124 | /// Handle completion of a GetResults operation.
|
|---|
| 125 | void handle_connect_get(const boost::system::error_code& e,
|
|---|
| 126 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 127 |
|
|---|
| 128 | /// Callback function when results are about to be received.
|
|---|
| 129 | void handle_ReceivingResults(const boost::system::error_code& e);
|
|---|
| 130 |
|
|---|
| 131 | /// Callback function when results have been received.
|
|---|
| 132 | void handle_ReceivedResults(const boost::system::error_code& e);
|
|---|
| 133 |
|
|---|
| 134 | /// internal function to connect to server and receive calculated results
|
|---|
| 135 | void connect_get();
|
|---|
| 136 |
|
|---|
| 137 | /// Getter for results
|
|---|
| 138 | std::vector<FragmentResult> getResults();
|
|---|
| 139 |
|
|---|
| 140 | protected:
|
|---|
| 141 | /// bunch of results
|
|---|
| 142 | std::vector<FragmentResult> results;
|
|---|
| 143 | };
|
|---|
| 144 |
|
|---|
| 145 | struct ShutdownOperation : public Operation {
|
|---|
| 146 | public:
|
|---|
| 147 | /// Constructor for class ShutdownOperation.
|
|---|
| 148 | ShutdownOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 149 | Operation(_connection, _host, _service) {}
|
|---|
| 150 | /// Destructor for class ShutdownOperation
|
|---|
| 151 | ~ShutdownOperation() {}
|
|---|
| 152 |
|
|---|
| 153 | public:
|
|---|
| 154 | // placing receive jobs operations into an io_service
|
|---|
| 155 | virtual void operator()();
|
|---|
| 156 |
|
|---|
| 157 | /// Handle completion of a Shutdown operation.
|
|---|
| 158 | void handle_connect_shutdown(const boost::system::error_code& e,
|
|---|
| 159 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 160 |
|
|---|
| 161 | /// internal function to connect to server and receive calculated results
|
|---|
| 162 | void connect_shutdown();
|
|---|
| 163 |
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | protected:
|
|---|
| 167 | /// The Connection to the server.
|
|---|
| 168 | Connection connection_;
|
|---|
| 169 |
|
|---|
| 170 | public:
|
|---|
| 171 |
|
|---|
| 172 | ReceiveJobsOperation recjobs;
|
|---|
| 173 | CheckResultsOperation checkres;
|
|---|
| 174 | SendResultsOperation sendres;
|
|---|
| 175 | ShutdownOperation shutdown;
|
|---|
| 176 |
|
|---|
| 177 | // get the exit flag of the last operations
|
|---|
| 178 | size_t getExitflag() const
|
|---|
| 179 | {
|
|---|
| 180 | if (recjobs.getExitflag() != 0)
|
|---|
| 181 | return recjobs.getExitflag();
|
|---|
| 182 | if (checkres.getExitflag() != 0)
|
|---|
| 183 | return checkres.getExitflag();
|
|---|
| 184 | if (sendres.getExitflag() != 0)
|
|---|
| 185 | return sendres.getExitflag();
|
|---|
| 186 | if (shutdown.getExitflag() != 0)
|
|---|
| 187 | return shutdown.getExitflag();
|
|---|
| 188 | return 0;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /// place number of jobs into this controller
|
|---|
| 192 | void addJobs(const std::vector<FragmentJob> &jobs)
|
|---|
| 193 | {
|
|---|
| 194 | recjobs.addJobs(jobs);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /// get the results for the current jobs
|
|---|
| 198 | std::vector<FragmentResult> getResults()
|
|---|
| 199 | {
|
|---|
| 200 | return sendres.getResults();
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /// Getter for doneJobs
|
|---|
| 204 | size_t getDoneJobs() const
|
|---|
| 205 | {
|
|---|
| 206 | return checkres.getDoneJobs();
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /// Getter for number of jobs in the queue
|
|---|
| 210 | size_t getPresentJobs() const
|
|---|
| 211 | {
|
|---|
| 212 | return recjobs.getPresentJobs();
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | private:
|
|---|
| 216 | /// host name of server
|
|---|
| 217 | const std::string host;
|
|---|
| 218 |
|
|---|
| 219 | // service to connect to to
|
|---|
| 220 | const std::string service;
|
|---|
| 221 | };
|
|---|
| 222 |
|
|---|
| 223 | #endif /* FRAGMENTCONTROLLER_HPP_ */
|
|---|