| 1 | /*
 | 
|---|
| 2 |  * \file PoolWorker.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * This file strongly follows the Serialization example from the boost::asio
 | 
|---|
| 5 |  * library (see client.cpp).
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *  Created on: Feb 28, 2012
 | 
|---|
| 8 |  *      Author: heber
 | 
|---|
| 9 |  */
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef POOLWORKER_HPP_
 | 
|---|
| 12 | #define POOLWORKER_HPP_
 | 
|---|
| 13 | 
 | 
|---|
| 14 | // include config.h
 | 
|---|
| 15 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 16 | #include <config.h>
 | 
|---|
| 17 | #endif
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <boost/asio.hpp>
 | 
|---|
| 20 | #include <boost/function.hpp>
 | 
|---|
| 21 | #include <vector>
 | 
|---|
| 22 | #include "Connection.hpp"
 | 
|---|
| 23 | #include "Jobs/FragmentJob.hpp"
 | 
|---|
| 24 | #include "Controller/Commands/EnrollInPoolOperation.hpp"
 | 
|---|
| 25 | #include "Controller/Commands/SubmitResultOperation.hpp"
 | 
|---|
| 26 | #include "Listener.hpp"
 | 
|---|
| 27 | #include "WorkerAddress.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** Receives a job from Server to execute and return FragmentResult.
 | 
|---|
| 30 |  *
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | class PoolWorker
 | 
|---|
| 33 | {
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |   /// Constructor starts the asynchronous connect operation.
 | 
|---|
| 36 |   PoolWorker(
 | 
|---|
| 37 |       boost::asio::io_service& io_service,
 | 
|---|
| 38 |       const std::string& host,
 | 
|---|
| 39 |       const std::string& service,
 | 
|---|
| 40 |       const std::string& listenhost,
 | 
|---|
| 41 |       const std::string& listenservice);
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   /** Returns the flag of the handled operation.
 | 
|---|
| 44 |    *
 | 
|---|
| 45 |    */
 | 
|---|
| 46 |   size_t getFlag() const
 | 
|---|
| 47 |   {
 | 
|---|
| 48 |     return submitOp.getFlag();
 | 
|---|
| 49 |   }
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   void WorkOnJob(FragmentJob::ptr &job);
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   class PoolListener_t : public Listener
 | 
|---|
| 54 |   {
 | 
|---|
| 55 |   public:
 | 
|---|
| 56 |     PoolListener_t(
 | 
|---|
| 57 |         boost::asio::io_service& io_service,
 | 
|---|
| 58 |         unsigned short port,
 | 
|---|
| 59 |         PoolWorker &_callback) :
 | 
|---|
| 60 |       Listener(io_service, port),
 | 
|---|
| 61 |       callback(_callback)
 | 
|---|
| 62 |     {}
 | 
|---|
| 63 |     virtual ~PoolListener_t() {}
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   protected:
 | 
|---|
| 66 |     /// Handle completion of a accept controller operation.
 | 
|---|
| 67 |     void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |     /// Controller callback function when job has been sent.
 | 
|---|
| 70 |     void handle_ReceiveJob(const boost::system::error_code& e, connection_ptr conn);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   private:
 | 
|---|
| 73 |     //!> callback reference to PoolWorker for handling the job
 | 
|---|
| 74 |     PoolWorker &callback;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     //!> current job
 | 
|---|
| 77 |     FragmentJob::ptr job;
 | 
|---|
| 78 |   };
 | 
|---|
| 79 | 
 | 
|---|
| 80 | private:
 | 
|---|
| 81 |   //!> The Connection to the server.
 | 
|---|
| 82 |   Connection connection_;
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   //!> The listener for the WorkerPool
 | 
|---|
| 85 |   PoolListener_t PoolListener;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   //!> address of this worker
 | 
|---|
| 88 |   const WorkerAddress address;
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   //!> operation that handles obtaining a job
 | 
|---|
| 91 |   EnrollInPoolOperation enrollOp;
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   //!> operation that handles submitting job's result
 | 
|---|
| 94 |   SubmitResultOperation submitOp;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   //!> internally bound function such that host and service don't have to be stored, submits result
 | 
|---|
| 97 |   boost::function<void ()> submitresult;
 | 
|---|
| 98 | };
 | 
|---|
| 99 | 
 | 
|---|
| 100 | #endif /* POOLWORKER_HPP_ */
 | 
|---|