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/RemoveFromPoolOperation.hpp"
|
---|
26 | #include "Controller/Commands/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 |
|
---|
56 | class PoolListener_t : public Listener
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | PoolListener_t(
|
---|
60 | boost::asio::io_service& io_service,
|
---|
61 | unsigned short port,
|
---|
62 | PoolWorker &_callback) :
|
---|
63 | Listener(io_service, port),
|
---|
64 | callback(_callback)
|
---|
65 | {}
|
---|
66 | virtual ~PoolListener_t() {}
|
---|
67 |
|
---|
68 | protected:
|
---|
69 | /// Handle completion of a accept controller operation.
|
---|
70 | void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
|
---|
71 |
|
---|
72 | /// Controller callback function when job has been sent.
|
---|
73 | void handle_ReceiveJob(const boost::system::error_code& e, connection_ptr conn);
|
---|
74 |
|
---|
75 | private:
|
---|
76 | //!> callback reference to PoolWorker for handling the job
|
---|
77 | PoolWorker &callback;
|
---|
78 |
|
---|
79 | //!> current job
|
---|
80 | FragmentJob::ptr job;
|
---|
81 | };
|
---|
82 |
|
---|
83 | private:
|
---|
84 | //!> reference to io_service which we use for connections
|
---|
85 | boost::asio::io_service& io_service;
|
---|
86 |
|
---|
87 | //!> The listener for the WorkerPool
|
---|
88 | PoolListener_t PoolListener;
|
---|
89 |
|
---|
90 | //!> address of this worker
|
---|
91 | const WorkerAddress address;
|
---|
92 |
|
---|
93 | //!> The Connection to the server for the stored operations
|
---|
94 | Connection connection_;
|
---|
95 |
|
---|
96 | //!> operation that handles obtaining a job
|
---|
97 | EnrollInPoolOperation enrollOp;
|
---|
98 |
|
---|
99 | //!> operation that handles submitting job's result
|
---|
100 | SubmitResultOperation submitOp;
|
---|
101 |
|
---|
102 | //!> internally bound function such that host and service don't have to be stored, submits result
|
---|
103 | boost::function<void ()> submitresult;
|
---|
104 |
|
---|
105 | //!> operation that handles removal from pool
|
---|
106 | RemoveFromPoolOperation removeOp;
|
---|
107 |
|
---|
108 | //!> internally bound function such that host and service don't have to be stored, submits result
|
---|
109 | boost::function<void ()> removeme;
|
---|
110 | };
|
---|
111 |
|
---|
112 | #endif /* POOLWORKER_HPP_ */
|
---|