| 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 "Operations/Workers/EnrollInPoolOperation.hpp" | 
|---|
| 25 | #include "Operations/Workers/RemoveFromPoolOperation.hpp" | 
|---|
| 26 | #include "Operations/Workers/SubmitResultOperation.hpp" | 
|---|
| 27 | #include "Listener.hpp" | 
|---|
| 28 | #include "WorkerAddress.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | /** Receives a job from Server to execute and return FragmentResult. | 
|---|
| 31 | * | 
|---|
| 32 | */ | 
|---|
| 33 | class PoolWorker | 
|---|
| 34 | { | 
|---|
| 35 | public: | 
|---|
| 36 | /// Constructor starts the asynchronous connect operation. | 
|---|
| 37 | PoolWorker( | 
|---|
| 38 | boost::asio::io_service& io_service, | 
|---|
| 39 | const std::string& host, | 
|---|
| 40 | const std::string& service, | 
|---|
| 41 | const std::string& listenhost, | 
|---|
| 42 | const std::string& listenservice); | 
|---|
| 43 |  | 
|---|
| 44 | /** Returns the flag of the handled operation. | 
|---|
| 45 | * | 
|---|
| 46 | */ | 
|---|
| 47 | size_t getFlag() const | 
|---|
| 48 | { | 
|---|
| 49 | return submitOp.getFlag(); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void WorkOnJob(FragmentJob::ptr &job); | 
|---|
| 53 | void removeFromPool(); | 
|---|
| 54 | void shutdown(int sig); | 
|---|
| 55 | void shutdown(); | 
|---|
| 56 |  | 
|---|
| 57 | class PoolListener_t : public Listener | 
|---|
| 58 | { | 
|---|
| 59 | public: | 
|---|
| 60 | PoolListener_t( | 
|---|
| 61 | boost::asio::io_service& io_service, | 
|---|
| 62 | unsigned short port, | 
|---|
| 63 | PoolWorker &_callback) : | 
|---|
| 64 | Listener(io_service, port), | 
|---|
| 65 | callback(_callback) | 
|---|
| 66 | {} | 
|---|
| 67 | virtual ~PoolListener_t() {} | 
|---|
| 68 |  | 
|---|
| 69 | protected: | 
|---|
| 70 | /// Handle completion of a accept controller operation. | 
|---|
| 71 | void handle_Accept(const boost::system::error_code& e, connection_ptr conn); | 
|---|
| 72 |  | 
|---|
| 73 | /// Controller callback function when job has been sent. | 
|---|
| 74 | void handle_ReceiveJob(const boost::system::error_code& e, connection_ptr conn); | 
|---|
| 75 |  | 
|---|
| 76 | private: | 
|---|
| 77 | //!> callback reference to PoolWorker for handling the job | 
|---|
| 78 | PoolWorker &callback; | 
|---|
| 79 |  | 
|---|
| 80 | //!> current job | 
|---|
| 81 | FragmentJob::ptr job; | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 | private: | 
|---|
| 85 | //!> reference to io_service which we use for connections | 
|---|
| 86 | boost::asio::io_service& io_service; | 
|---|
| 87 |  | 
|---|
| 88 | //!> The listener for the WorkerPool | 
|---|
| 89 | PoolListener_t PoolListener; | 
|---|
| 90 |  | 
|---|
| 91 | //!> address of this worker | 
|---|
| 92 | const WorkerAddress address; | 
|---|
| 93 |  | 
|---|
| 94 | //!> The Connection to the server for the stored operations | 
|---|
| 95 | Connection connection_; | 
|---|
| 96 |  | 
|---|
| 97 | //!> operation that handles obtaining a job | 
|---|
| 98 | EnrollInPoolOperation enrollOp; | 
|---|
| 99 |  | 
|---|
| 100 | //!> operation that handles submitting job's result | 
|---|
| 101 | SubmitResultOperation submitOp; | 
|---|
| 102 |  | 
|---|
| 103 | //!> internally bound function such that host and service don't have to be stored, submits result | 
|---|
| 104 | boost::function<void ()> submitresult; | 
|---|
| 105 |  | 
|---|
| 106 | //!> operation that handles removal from pool | 
|---|
| 107 | RemoveFromPoolOperation removeOp; | 
|---|
| 108 |  | 
|---|
| 109 | //!> internally bound function such that host and service don't have to be stored, submits result | 
|---|
| 110 | boost::function<void ()> removeme; | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | #endif /* POOLWORKER_HPP_ */ | 
|---|