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