/* * FragmentController.hpp * * Created on: Nov 27, 2011 * Author: heber */ #ifndef FRAGMENTCONTROLLER_HPP_ #define FRAGMENTCONTROLLER_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "Connection.hpp" #include "FragmentJob.hpp" #include "FragmentResult.hpp" /** The FragmentController sends bunches of jobs to a FragmentScheduler, * waits for their calculation and is called when they are done. Then, * he loads the bunch of results from the Scheduler. * * While the FragmentScheduler and FragmentWorker rather act on their own * this is the piece to implant into the user software to allow for * communication with the Server/Worker duo to perform the calculation * of the fragments on distant computers. */ class FragmentController { public: FragmentController(boost::asio::io_service& io_service, const std::string& _host, const std::string& _service); ~FragmentController(); class Operation { public: Operation(Connection &_connection, const std::string& _host, const std::string& _service); virtual ~Operation(); public: /// The Connection to the server. Connection &connection_; // virtual function pointer to the operation to do virtual void operator()() = 0; /// Handle completion of an operation. void handle_FinishOperation(const boost::system::error_code& e); enum Exitflag_t { OkFlag = 0, ErrorFlag = 255 }; // get the exit flag of the last operations size_t getExitflag() const { return Exitflag; } protected: /// internal function to resolve host name and ip address boost::asio::ip::tcp::resolver::iterator getEndpointIterator(); /// internal function to disconnect from server void disconnect(); protected: /// host name of server const std::string host; // service to connect to to const std::string service; /// flag of operation to give on program exit enum Exitflag_t Exitflag; }; class ReceiveJobsOperation : public Operation { public: /// Constructor for class ReceiveJobsOperation. ReceiveJobsOperation(Connection &_connection, const std::string& _host, const std::string& _service) : Operation(_connection, _host, _service) {} /// Destructor for class ReceiveJobsOperation ~ReceiveJobsOperation() {} public: /// Placing receive jobs operations into an io_service virtual void operator()(); /// Handle completion of a calculate operation. void handle_connect_calc(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); /// Callback function when bunch of jobs have been sent. void handle_SendJobs(const boost::system::error_code& e); /// Handle completion of an operation. void handle_FinishOperation(const boost::system::error_code& e); /// internal function to connect to server and send jobs for calculation void connect_calc(); /// Setter for jobs void addJobs(const std::vector &jobs); /// Getter for number of size of jobs size_t getPresentJobs() const; protected: /// bunch of jobs std::vector jobs; }; struct CheckResultsOperation : public Operation { public: /// Constructor for class CheckResultsOperation. CheckResultsOperation(Connection &_connection, const std::string& _host, const std::string& _service) : Operation(_connection, _host, _service), doneJobs(0) {} /// Destructor for class CheckResultsOperation ~CheckResultsOperation() {} public: // placing receive jobs operations into an io_service virtual void operator()(); /// Handle completion of a CheckResults operation. void handle_connect_check(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); /// Callback function when doneJobs have been received. void handle_ReceiveDoneJobs(const boost::system::error_code& e); /// Handle completion of an operation. void handle_FinishOperation(const boost::system::error_code& e); /// internal function to connect to server and check done jobs void connect_check(); /// Getter for doneJobs size_t getDoneJobs() const; protected: /// currently calculated results size_t doneJobs; }; struct SendResultsOperation : public Operation { public: /// Constructor for class SendResultsOperation. SendResultsOperation(Connection &_connection, const std::string& _host, const std::string& _service) : Operation(_connection, _host, _service) {} /// Destructor for class SendResultsOperation ~SendResultsOperation() {} public: // placing receive jobs operations into an io_service virtual void operator()(); /// Handle completion of a GetResults operation. void handle_connect_get(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); /// Callback function when results are about to be received. void handle_ReceivingResults(const boost::system::error_code& e); /// Callback function when results have been received. void handle_ReceivedResults(const boost::system::error_code& e); /// internal function to connect to server and receive calculated results void connect_get(); /// Getter for results std::vector getResults(); protected: /// bunch of results std::vector results; }; struct ShutdownOperation : public Operation { public: /// Constructor for class ShutdownOperation. ShutdownOperation(Connection &_connection, const std::string& _host, const std::string& _service) : Operation(_connection, _host, _service) {} /// Destructor for class ShutdownOperation ~ShutdownOperation() {} public: // placing receive jobs operations into an io_service virtual void operator()(); /// Handle completion of a Shutdown operation. void handle_connect_shutdown(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); /// internal function to connect to server and receive calculated results void connect_shutdown(); }; protected: /// The Connection to the server. Connection connection_; public: ReceiveJobsOperation recjobs; CheckResultsOperation checkres; SendResultsOperation sendres; ShutdownOperation shutdown; // get the exit flag of the last operations size_t getExitflag() const { if (recjobs.getExitflag() != 0) return recjobs.getExitflag(); if (checkres.getExitflag() != 0) return checkres.getExitflag(); if (sendres.getExitflag() != 0) return sendres.getExitflag(); if (shutdown.getExitflag() != 0) return shutdown.getExitflag(); return 0; } /// place number of jobs into this controller void addJobs(const std::vector &jobs) { recjobs.addJobs(jobs); } /// get the results for the current jobs std::vector getResults() { return sendres.getResults(); } /// Getter for doneJobs size_t getDoneJobs() const { return checkres.getDoneJobs(); } /// Getter for number of jobs in the queue size_t getPresentJobs() const { return recjobs.getPresentJobs(); } private: /// host name of server const std::string host; // service to connect to to const std::string service; }; #endif /* FRAGMENTCONTROLLER_HPP_ */