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