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