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