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