| 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 free job ids have been sent.
|
|---|
| 150 | void handle_SendIds(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 151 |
|
|---|
| 152 | /// Controller callback function when result has been received.
|
|---|
| 153 | void handle_SendResults(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 154 |
|
|---|
| 155 | private:
|
|---|
| 156 | //!> reference to external FragmentQueue containing jobs to work on
|
|---|
| 157 | FragmentQueue & JobsQueue;
|
|---|
| 158 |
|
|---|
| 159 | //!> bunch of jobs received from controller before placed in JobsQueue
|
|---|
| 160 | std::vector<FragmentJob::ptr> jobs;
|
|---|
| 161 |
|
|---|
| 162 | //!> number of jobs that are waiting to be and are calculated, required for returning status
|
|---|
| 163 | std::vector<size_t> jobInfo;
|
|---|
| 164 |
|
|---|
| 165 | //!> number of job ids request from controller;
|
|---|
| 166 | size_t NumberIds;
|
|---|
| 167 |
|
|---|
| 168 | //!> choice
|
|---|
| 169 | enum ControllerChoices choice;
|
|---|
| 170 |
|
|---|
| 171 | //!> bound function to shutdown all sockets
|
|---|
| 172 | boost::function<void ()> shutdownAllSockets;
|
|---|
| 173 |
|
|---|
| 174 | // TODO: replace this instance by a IdPool.
|
|---|
| 175 | //!> global id to give next available job id
|
|---|
| 176 | GlobalJobId globalId;
|
|---|
| 177 | };
|
|---|
| 178 |
|
|---|
| 179 | private:
|
|---|
| 180 | //!> static entity to indicate to clients that the queue is empty.
|
|---|
| 181 | static FragmentJob::ptr NoJob;
|
|---|
| 182 |
|
|---|
| 183 | //!> reference to the io_service which we use for connections
|
|---|
| 184 | boost::asio::io_service& io_service;
|
|---|
| 185 |
|
|---|
| 186 | //!> Queue with data to be sent to each client.
|
|---|
| 187 | FragmentQueue JobsQueue;
|
|---|
| 188 |
|
|---|
| 189 | //!> Pool of Workers
|
|---|
| 190 | WorkerPool pool;
|
|---|
| 191 |
|
|---|
| 192 | //!> Listener instance that waits for a worker
|
|---|
| 193 | WorkerListener_t WorkerListener;
|
|---|
| 194 |
|
|---|
| 195 | //!> Listener instance that waits for a controller
|
|---|
| 196 | ControllerListener_t ControllerListener;
|
|---|
| 197 |
|
|---|
| 198 | public:
|
|---|
| 199 | /** Getter for Exitflag.
|
|---|
| 200 | *
|
|---|
| 201 | * @return Exitflag of operations
|
|---|
| 202 | */
|
|---|
| 203 | size_t getExitflag() const
|
|---|
| 204 | {
|
|---|
| 205 | if (WorkerListener.getExitflag() != 0)
|
|---|
| 206 | return WorkerListener.getExitflag();
|
|---|
| 207 | if (ControllerListener.getExitflag() != 0)
|
|---|
| 208 | return ControllerListener.getExitflag();
|
|---|
| 209 | return 0;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | private:
|
|---|
| 213 | //!> Connection for sending jobs to workers
|
|---|
| 214 | Connection connection;
|
|---|
| 215 |
|
|---|
| 216 | //!> internal operation to send jobs to workers
|
|---|
| 217 | mutable SendJobToWorkerOperation sendJobOp;
|
|---|
| 218 |
|
|---|
| 219 | //!> internal operation to shutdown a worker
|
|---|
| 220 | ShutdownWorkerOperation shutdownWorkerOp;
|
|---|
| 221 | };
|
|---|
| 222 |
|
|---|
| 223 | #endif /* FRAGMENTSCHEDULER_HPP_ */
|
|---|