1 | /*
|
---|
2 | * FragmentScheduler.hpp
|
---|
3 | *
|
---|
4 | * Created on: Oct 19, 2011
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef FRAGMENTSCHEDULER_HPP_
|
---|
9 | #define FRAGMENTSCHEDULER_HPP_
|
---|
10 |
|
---|
11 | // include config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <vector>
|
---|
17 | #include <boost/asio.hpp>
|
---|
18 | #include <boost/function.hpp>
|
---|
19 |
|
---|
20 | #include "Connection.hpp"
|
---|
21 | #include "ControllerChoices.hpp"
|
---|
22 | #include "FragmentQueue.hpp"
|
---|
23 | #include "GlobalJobId.hpp"
|
---|
24 | #include "Jobs/FragmentJob.hpp"
|
---|
25 | #include "Listener.hpp"
|
---|
26 | #include "Results/FragmentResult.hpp"
|
---|
27 | #include "types.hpp"
|
---|
28 |
|
---|
29 | /** FragmentScheduler serves FragmentJobs to Workers and accepts commands from
|
---|
30 | * a Controller.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | class FragmentScheduler
|
---|
34 | {
|
---|
35 | public:
|
---|
36 | /// Constructor opens the acceptor and starts waiting for the first incoming
|
---|
37 | /// Connection.
|
---|
38 | FragmentScheduler(boost::asio::io_service& io_service, unsigned short workerport, unsigned short controllerport);
|
---|
39 |
|
---|
40 | private:
|
---|
41 | class WorkerListener_t : public Listener
|
---|
42 | {
|
---|
43 | public:
|
---|
44 | WorkerListener_t(
|
---|
45 | boost::asio::io_service& io_service,
|
---|
46 | unsigned short port,
|
---|
47 | FragmentQueue &_JobsQueue) :
|
---|
48 | Listener(io_service, port),
|
---|
49 | JobsQueue(_JobsQueue),
|
---|
50 | result( new FragmentResult(JobId::NoJob) )
|
---|
51 | {}
|
---|
52 | virtual ~WorkerListener_t() {}
|
---|
53 |
|
---|
54 | protected:
|
---|
55 | /// Handle completion of a accept worker operation.
|
---|
56 | void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
|
---|
57 |
|
---|
58 | /// Worker callback function when job has been sent.
|
---|
59 | void handle_SendJobtoWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
60 |
|
---|
61 | /// Worker callback function when result has been received.
|
---|
62 | void handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
63 | private:
|
---|
64 | //!> reference to external FragmentQueue containing jobs to work on
|
---|
65 | FragmentQueue & JobsQueue;
|
---|
66 |
|
---|
67 | /// result that is received from the client.
|
---|
68 | FragmentResult::ptr result;
|
---|
69 |
|
---|
70 | // static entity to indicate to clients that the queue is empty.
|
---|
71 | static FragmentJob::ptr NoJob;
|
---|
72 | };
|
---|
73 |
|
---|
74 | class ControllerListener_t : public Listener
|
---|
75 | {
|
---|
76 | public:
|
---|
77 | ControllerListener_t(
|
---|
78 | boost::asio::io_service& io_service,
|
---|
79 | unsigned short port,
|
---|
80 | FragmentQueue &_JobsQueue,
|
---|
81 | boost::function<void ()> _initiateWorkerSocket) :
|
---|
82 | Listener(io_service, port),
|
---|
83 | JobsQueue(_JobsQueue),
|
---|
84 | jobInfo((size_t)2, 0),
|
---|
85 | choice(NoOperation),
|
---|
86 | globalId(0),
|
---|
87 | initiateWorkerSocket(_initiateWorkerSocket)
|
---|
88 | {}
|
---|
89 | virtual ~ControllerListener_t() {}
|
---|
90 |
|
---|
91 | protected:
|
---|
92 | /// Handle completion of a accept controller operation.
|
---|
93 | void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
|
---|
94 |
|
---|
95 | /// Handle completion of controller operation to read choice
|
---|
96 | void handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn);
|
---|
97 |
|
---|
98 | /// Controller callback function when job has been sent.
|
---|
99 | void handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn);
|
---|
100 |
|
---|
101 | /// Controller callback function when checking on state of results.
|
---|
102 | void handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn);
|
---|
103 |
|
---|
104 | /// Controller callback function when checking on state of results.
|
---|
105 | void handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn);
|
---|
106 |
|
---|
107 | /// Controller callback function when result has been received.
|
---|
108 | void handle_SendResults(const boost::system::error_code& e, connection_ptr conn);
|
---|
109 |
|
---|
110 | private:
|
---|
111 | //!> reference to external FragmentQueue containing jobs to work on
|
---|
112 | FragmentQueue & JobsQueue;
|
---|
113 |
|
---|
114 | /// bunch of jobs received from controller before placed in JobsQueue
|
---|
115 | std::vector<FragmentJob::ptr> jobs;
|
---|
116 |
|
---|
117 | /// number of jobs that are waiting to be and are calculated, required for returning status
|
---|
118 | std::vector<size_t> jobInfo;
|
---|
119 |
|
---|
120 | // choice
|
---|
121 | enum ControllerChoices choice;
|
---|
122 |
|
---|
123 | // TODO: replace this instance by a IdPool.
|
---|
124 | //!> global id to give next available job id
|
---|
125 | GlobalJobId globalId;
|
---|
126 |
|
---|
127 | //!> callback function to tell that worker socket should be enabled
|
---|
128 | boost::function<void ()> initiateWorkerSocket;
|
---|
129 | };
|
---|
130 |
|
---|
131 | private:
|
---|
132 | //!> Listener instance that waits for a worker
|
---|
133 | WorkerListener_t WorkerListener;
|
---|
134 |
|
---|
135 | //!> Listener instance that waits for a controller
|
---|
136 | ControllerListener_t ControllerListener;
|
---|
137 |
|
---|
138 | /// Queue with data to be sent to each client.
|
---|
139 | FragmentQueue JobsQueue;
|
---|
140 |
|
---|
141 | public:
|
---|
142 | /** Getter for Exitflag.
|
---|
143 | *
|
---|
144 | * @return Exitflag of operations
|
---|
145 | */
|
---|
146 | size_t getExitflag() const
|
---|
147 | {
|
---|
148 | if (WorkerListener.getExitflag() != 0)
|
---|
149 | return WorkerListener.getExitflag();
|
---|
150 | if (ControllerListener.getExitflag() != 0)
|
---|
151 | return ControllerListener.getExitflag();
|
---|
152 | return 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | };
|
---|
156 |
|
---|
157 | #endif /* FRAGMENTSCHEDULER_HPP_ */
|
---|