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