| [db03d9] | 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 | /** The FragmentController sends bunches of jobs to a FragmentScheduler,
|
|---|
| 25 | * waits for their calculation and is called when they are done. Then,
|
|---|
| 26 | * he loads the bunch of results from the Scheduler.
|
|---|
| 27 | *
|
|---|
| 28 | * While the FragmentScheduler and FragmentWorker rather act on their own
|
|---|
| 29 | * this is the piece to implant into the user software to allow for
|
|---|
| 30 | * communication with the Server/Worker duo to perform the calculation
|
|---|
| 31 | * of the fragments on distant computers.
|
|---|
| 32 | */
|
|---|
| 33 | class FragmentController
|
|---|
| 34 | {
|
|---|
| 35 | public:
|
|---|
| [5adb84] | 36 | FragmentController(boost::asio::io_service& io_service, const std::string& _host, const std::string& _service);
|
|---|
| [db03d9] | 37 | ~FragmentController();
|
|---|
| 38 |
|
|---|
| [5adb84] | 39 | class Operation {
|
|---|
| 40 | public:
|
|---|
| 41 | Operation(Connection &_connection, const std::string& _host, const std::string& _service);
|
|---|
| 42 | virtual ~Operation();
|
|---|
| 43 | public:
|
|---|
| 44 | /// The Connection to the server.
|
|---|
| 45 | Connection &connection_;
|
|---|
| [db03d9] | 46 |
|
|---|
| [5adb84] | 47 | // virtual function pointer to the operation to do
|
|---|
| 48 | virtual void operator()() = 0;
|
|---|
| [3c4a5e] | 49 |
|
|---|
| [5adb84] | 50 | /// Handle completion of an operation.
|
|---|
| 51 | void handle_FinishOperation(const boost::system::error_code& e);
|
|---|
| [778abb] | 52 |
|
|---|
| [5adb84] | 53 | enum Exitflag_t {
|
|---|
| 54 | OkFlag = 0,
|
|---|
| 55 | ErrorFlag = 255
|
|---|
| 56 | };
|
|---|
| [0196c6] | 57 |
|
|---|
| [5adb84] | 58 | // get the exit flag of the last operations
|
|---|
| 59 | size_t getExitflag() const
|
|---|
| 60 | {
|
|---|
| 61 | return Exitflag;
|
|---|
| 62 | }
|
|---|
| [778abb] | 63 |
|
|---|
| [5adb84] | 64 | protected:
|
|---|
| 65 | /// internal function to resolve host name and ip address
|
|---|
| 66 | boost::asio::ip::tcp::resolver::iterator getEndpointIterator();
|
|---|
| [db03d9] | 67 |
|
|---|
| [5adb84] | 68 | /// internal function to disconnect from server
|
|---|
| 69 | void disconnect();
|
|---|
| [3c4a5e] | 70 |
|
|---|
| [5adb84] | 71 | protected:
|
|---|
| 72 | /// host name of server
|
|---|
| 73 | const std::string host;
|
|---|
| [778abb] | 74 |
|
|---|
| [5adb84] | 75 | // service to connect to to
|
|---|
| 76 | const std::string service;
|
|---|
| [778abb] | 77 |
|
|---|
| [5adb84] | 78 | /// flag of operation to give on program exit
|
|---|
| 79 | enum Exitflag_t Exitflag;
|
|---|
| [db03d9] | 80 | };
|
|---|
| 81 |
|
|---|
| [5adb84] | 82 | class ReceiveJobsOperation : public Operation {
|
|---|
| 83 | public:
|
|---|
| 84 | /// Constructor for class ReceiveJobsOperation.
|
|---|
| 85 | ReceiveJobsOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 86 | Operation(_connection, _host, _service) {}
|
|---|
| 87 | /// Destructor for class ReceiveJobsOperation
|
|---|
| 88 | ~ReceiveJobsOperation() {}
|
|---|
| [db03d9] | 89 |
|
|---|
| [5adb84] | 90 | public:
|
|---|
| 91 | /// Placing receive jobs operations into an io_service
|
|---|
| 92 | virtual void operator()();
|
|---|
| [778abb] | 93 |
|
|---|
| [5adb84] | 94 | /// Handle completion of a calculate operation.
|
|---|
| 95 | void handle_connect_calc(const boost::system::error_code& e,
|
|---|
| 96 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| [0196c6] | 97 |
|
|---|
| [5adb84] | 98 | /// Callback function when bunch of jobs have been sent.
|
|---|
| 99 | void handle_SendJobs(const boost::system::error_code& e);
|
|---|
| [778abb] | 100 |
|
|---|
| [5adb84] | 101 | /// Handle completion of an operation.
|
|---|
| 102 | void handle_FinishOperation(const boost::system::error_code& e);
|
|---|
| [3c4a5e] | 103 |
|
|---|
| [5adb84] | 104 | /// internal function to connect to server and send jobs for calculation
|
|---|
| 105 | void connect_calc();
|
|---|
| [3c4a5e] | 106 |
|
|---|
| [5adb84] | 107 | /// Setter for jobs
|
|---|
| 108 | void addJobs(const std::vector<FragmentJob> &jobs);
|
|---|
| [dba6d1] | 109 |
|
|---|
| [5adb84] | 110 | /// Getter for number of size of jobs
|
|---|
| 111 | size_t getPresentJobs() const;
|
|---|
| 112 |
|
|---|
| 113 | protected:
|
|---|
| 114 | /// bunch of jobs
|
|---|
| 115 | std::vector<FragmentJob> jobs;
|
|---|
| 116 | };
|
|---|
| [db03d9] | 117 |
|
|---|
| [5adb84] | 118 | struct CheckResultsOperation : public Operation {
|
|---|
| 119 | public:
|
|---|
| 120 | /// Constructor for class CheckResultsOperation.
|
|---|
| 121 | CheckResultsOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 122 | Operation(_connection, _host, _service),
|
|---|
| 123 | doneJobs(0)
|
|---|
| 124 | {}
|
|---|
| 125 | /// Destructor for class CheckResultsOperation
|
|---|
| 126 | ~CheckResultsOperation() {}
|
|---|
| [db03d9] | 127 |
|
|---|
| [5adb84] | 128 | public:
|
|---|
| 129 | // placing receive jobs operations into an io_service
|
|---|
| 130 | virtual void operator()();
|
|---|
| [3c4a5e] | 131 |
|
|---|
| [5adb84] | 132 | /// Handle completion of a CheckResults operation.
|
|---|
| 133 | void handle_connect_check(const boost::system::error_code& e,
|
|---|
| 134 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| [778abb] | 135 |
|
|---|
| [5adb84] | 136 | /// Callback function when doneJobs have been received.
|
|---|
| 137 | void handle_ReceiveDoneJobs(const boost::system::error_code& e);
|
|---|
| [0196c6] | 138 |
|
|---|
| [5adb84] | 139 | /// Handle completion of an operation.
|
|---|
| 140 | void handle_FinishOperation(const boost::system::error_code& e);
|
|---|
| [db03d9] | 141 |
|
|---|
| [5adb84] | 142 | /// internal function to connect to server and check done jobs
|
|---|
| 143 | void connect_check();
|
|---|
| 144 |
|
|---|
| 145 | /// Getter for doneJobs
|
|---|
| 146 | size_t getDoneJobs() const;
|
|---|
| 147 |
|
|---|
| 148 | protected:
|
|---|
| 149 | /// currently calculated results
|
|---|
| 150 | size_t doneJobs;
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | struct SendResultsOperation : public Operation {
|
|---|
| 154 | public:
|
|---|
| 155 | /// Constructor for class SendResultsOperation.
|
|---|
| 156 | SendResultsOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 157 | Operation(_connection, _host, _service) {}
|
|---|
| 158 | /// Destructor for class SendResultsOperation
|
|---|
| 159 | ~SendResultsOperation() {}
|
|---|
| 160 |
|
|---|
| 161 | public:
|
|---|
| 162 | // placing receive jobs operations into an io_service
|
|---|
| 163 | virtual void operator()();
|
|---|
| 164 |
|
|---|
| 165 | /// Handle completion of a GetResults operation.
|
|---|
| 166 | void handle_connect_get(const boost::system::error_code& e,
|
|---|
| 167 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 168 |
|
|---|
| 169 | /// Callback function when results are about to be received.
|
|---|
| 170 | void handle_ReceivingResults(const boost::system::error_code& e);
|
|---|
| 171 |
|
|---|
| 172 | /// Callback function when results have been received.
|
|---|
| 173 | void handle_ReceivedResults(const boost::system::error_code& e);
|
|---|
| 174 |
|
|---|
| 175 | /// internal function to connect to server and receive calculated results
|
|---|
| 176 | void connect_get();
|
|---|
| 177 |
|
|---|
| 178 | /// Getter for results
|
|---|
| 179 | std::vector<FragmentResult> getResults();
|
|---|
| 180 |
|
|---|
| 181 | protected:
|
|---|
| 182 | /// bunch of results
|
|---|
| 183 | std::vector<FragmentResult> results;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | struct ShutdownOperation : public Operation {
|
|---|
| 187 | public:
|
|---|
| 188 | /// Constructor for class ShutdownOperation.
|
|---|
| 189 | ShutdownOperation(Connection &_connection, const std::string& _host, const std::string& _service) :
|
|---|
| 190 | Operation(_connection, _host, _service) {}
|
|---|
| 191 | /// Destructor for class ShutdownOperation
|
|---|
| 192 | ~ShutdownOperation() {}
|
|---|
| 193 |
|
|---|
| 194 | public:
|
|---|
| 195 | // placing receive jobs operations into an io_service
|
|---|
| 196 | virtual void operator()();
|
|---|
| 197 |
|
|---|
| 198 | /// Handle completion of a Shutdown operation.
|
|---|
| 199 | void handle_connect_shutdown(const boost::system::error_code& e,
|
|---|
| 200 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 201 |
|
|---|
| 202 | /// internal function to connect to server and receive calculated results
|
|---|
| 203 | void connect_shutdown();
|
|---|
| 204 |
|
|---|
| 205 | };
|
|---|
| 206 |
|
|---|
| 207 | protected:
|
|---|
| [db03d9] | 208 | /// The Connection to the server.
|
|---|
| 209 | Connection connection_;
|
|---|
| 210 |
|
|---|
| [5adb84] | 211 | public:
|
|---|
| [db03d9] | 212 |
|
|---|
| [5adb84] | 213 | ReceiveJobsOperation recjobs;
|
|---|
| 214 | CheckResultsOperation checkres;
|
|---|
| 215 | SendResultsOperation sendres;
|
|---|
| 216 | ShutdownOperation shutdown;
|
|---|
| 217 |
|
|---|
| 218 | // get the exit flag of the last operations
|
|---|
| 219 | size_t getExitflag() const
|
|---|
| 220 | {
|
|---|
| 221 | if (recjobs.getExitflag() != 0)
|
|---|
| 222 | return recjobs.getExitflag();
|
|---|
| 223 | if (checkres.getExitflag() != 0)
|
|---|
| 224 | return checkres.getExitflag();
|
|---|
| 225 | if (sendres.getExitflag() != 0)
|
|---|
| 226 | return sendres.getExitflag();
|
|---|
| 227 | if (shutdown.getExitflag() != 0)
|
|---|
| 228 | return shutdown.getExitflag();
|
|---|
| 229 | return 0;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /// place number of jobs into this controller
|
|---|
| 233 | void addJobs(const std::vector<FragmentJob> &jobs)
|
|---|
| 234 | {
|
|---|
| 235 | recjobs.addJobs(jobs);
|
|---|
| 236 | }
|
|---|
| [db03d9] | 237 |
|
|---|
| [5adb84] | 238 | /// get the results for the current jobs
|
|---|
| 239 | std::vector<FragmentResult> getResults()
|
|---|
| 240 | {
|
|---|
| 241 | return sendres.getResults();
|
|---|
| 242 | }
|
|---|
| [db03d9] | 243 |
|
|---|
| [5adb84] | 244 | /// Getter for doneJobs
|
|---|
| 245 | size_t getDoneJobs() const
|
|---|
| 246 | {
|
|---|
| 247 | return checkres.getDoneJobs();
|
|---|
| 248 | }
|
|---|
| [3c4a5e] | 249 |
|
|---|
| [5adb84] | 250 | /// Getter for number of jobs in the queue
|
|---|
| 251 | size_t getPresentJobs() const
|
|---|
| 252 | {
|
|---|
| 253 | return recjobs.getPresentJobs();
|
|---|
| 254 | }
|
|---|
| [778abb] | 255 |
|
|---|
| [5adb84] | 256 | private:
|
|---|
| 257 | /// host name of server
|
|---|
| 258 | const std::string host;
|
|---|
| 259 |
|
|---|
| 260 | // service to connect to to
|
|---|
| 261 | const std::string service;
|
|---|
| [db03d9] | 262 | };
|
|---|
| 263 |
|
|---|
| 264 | #endif /* FRAGMENTCONTROLLER_HPP_ */
|
|---|