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