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 |
|
---|
19 | #include "Connection.hpp"
|
---|
20 | #include "ControllerChoices.hpp"
|
---|
21 | #include "FragmentQueue.hpp"
|
---|
22 | #include "GlobalJobId.hpp"
|
---|
23 | #include "Jobs/FragmentJob.hpp"
|
---|
24 | #include "Results/FragmentResult.hpp"
|
---|
25 | #include "types.hpp"
|
---|
26 |
|
---|
27 | /** FragmentScheduler serves FragmentJobs to Workers.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | class FragmentScheduler
|
---|
31 | {
|
---|
32 | public:
|
---|
33 | /// Constructor opens the acceptor and starts waiting for the first incoming
|
---|
34 | /// Connection.
|
---|
35 | FragmentScheduler(boost::asio::io_service& io_service, unsigned short workerport, unsigned short controllerport);
|
---|
36 |
|
---|
37 | enum Exitflag_t{
|
---|
38 | OkFlag = 0,
|
---|
39 | QueueError = 128,
|
---|
40 | ControllerErrorFlag = 254,
|
---|
41 | WorkerErrorFlag = 255
|
---|
42 | };
|
---|
43 |
|
---|
44 | /** Getter for Exitflag.
|
---|
45 | *
|
---|
46 | * @return Exitflag of operations
|
---|
47 | */
|
---|
48 | size_t getExitflag() const
|
---|
49 | {
|
---|
50 | return Exitflag;
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected:
|
---|
54 | /// Handle completion of a accept worker operation.
|
---|
55 | void handle_AcceptWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
56 |
|
---|
57 | /// Handle completion of a accept controller operation.
|
---|
58 | void handle_AcceptController(const boost::system::error_code& e, connection_ptr conn);
|
---|
59 |
|
---|
60 | /// Handle completion of controller operation to read choice
|
---|
61 | void handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn);
|
---|
62 |
|
---|
63 | /// Worker callback function when job has been sent.
|
---|
64 | void handle_SendJobtoWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
65 |
|
---|
66 | /// Worker callback function when result has been received.
|
---|
67 | void handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
68 |
|
---|
69 | /// Controller callback function when job has been sent.
|
---|
70 | void handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn);
|
---|
71 |
|
---|
72 | private:
|
---|
73 |
|
---|
74 | /// Controller callback function when checking on state of results.
|
---|
75 | void handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn);
|
---|
76 |
|
---|
77 | /// Controller callback function when checking on state of results.
|
---|
78 | void handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn);
|
---|
79 |
|
---|
80 | /// Controller callback function when result has been received.
|
---|
81 | void handle_SendResults(const boost::system::error_code& e, connection_ptr conn);
|
---|
82 |
|
---|
83 | /// internal function to prepare worker connections
|
---|
84 | void initiateWorkerSocket();
|
---|
85 |
|
---|
86 | /// internal function to prepare controller connections
|
---|
87 | void initiateControllerSocket();
|
---|
88 |
|
---|
89 | private:
|
---|
90 | /// The acceptor object used to accept incoming worker socket connections.
|
---|
91 | boost::asio::ip::tcp::acceptor worker_acceptor_;
|
---|
92 |
|
---|
93 | /// The acceptor object used to accept incoming controller socket connections.
|
---|
94 | boost::asio::ip::tcp::acceptor controller_acceptor_;
|
---|
95 |
|
---|
96 | /// result that is received from the client.
|
---|
97 | FragmentResult::ptr result;
|
---|
98 |
|
---|
99 | /// bunch of jobs received from controller before placed in JobsQueue
|
---|
100 | std::vector<FragmentJob::ptr> jobs;
|
---|
101 |
|
---|
102 | /// number of jobs that are waiting to be and are calculated, required for returning status
|
---|
103 | std::vector<size_t> jobInfo;
|
---|
104 |
|
---|
105 | // choice
|
---|
106 | enum ControllerChoices choice;
|
---|
107 |
|
---|
108 | /// Queue with data to be sent to each client.
|
---|
109 | FragmentQueue JobsQueue;
|
---|
110 |
|
---|
111 | // static entity to indicate to clients that the queue is empty.
|
---|
112 | static FragmentJob::ptr NoJob;
|
---|
113 |
|
---|
114 | // TODO: replace this instance by a IdPool.
|
---|
115 | //!> global id to give next available job id
|
---|
116 | GlobalJobId globalId;
|
---|
117 |
|
---|
118 | // Exit flag on program exit
|
---|
119 | enum Exitflag_t Exitflag;
|
---|
120 | };
|
---|
121 |
|
---|
122 | #endif /* FRAGMENTSCHEDULER_HPP_ */
|
---|