| 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 |
|
|---|
| 19 | #include "Connection.hpp"
|
|---|
| 20 | #include "FragmentJob.hpp"
|
|---|
| 21 | #include "FragmentQueue.hpp"
|
|---|
| 22 | #include "FragmentResult.hpp"
|
|---|
| 23 |
|
|---|
| 24 | /** FragmentScheduler serves FragmentJobs to Workers.
|
|---|
| 25 | *
|
|---|
| 26 | */
|
|---|
| 27 | class FragmentScheduler
|
|---|
| 28 | {
|
|---|
| 29 | public:
|
|---|
| 30 | /// Constructor opens the acceptor and starts waiting for the first incoming
|
|---|
| 31 | /// Connection.
|
|---|
| 32 | FragmentScheduler(boost::asio::io_service& io_service, unsigned short workerport, unsigned short controllerport);
|
|---|
| 33 |
|
|---|
| 34 | enum Exitflag_t{
|
|---|
| 35 | OkFlag = 0,
|
|---|
| 36 | ControllerErrorFlag = 254,
|
|---|
| 37 | WorkerErrorFlag = 255
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | /** Getter for Exitflag.
|
|---|
| 41 | *
|
|---|
| 42 | * @return Exitflag of operations
|
|---|
| 43 | */
|
|---|
| 44 | size_t getExitflag() const
|
|---|
| 45 | {
|
|---|
| 46 | return Exitflag;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | protected:
|
|---|
| 50 | /// Handle completion of a accept worker operation.
|
|---|
| 51 | void handle_AcceptWorker(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 52 |
|
|---|
| 53 | /// Handle completion of a accept controller operation.
|
|---|
| 54 | void handle_AcceptController(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 55 |
|
|---|
| 56 | /// Worker callback function when job has been sent.
|
|---|
| 57 | void handle_SendJobtoWorker(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 58 |
|
|---|
| 59 | /// Worker callback function when result has been received.
|
|---|
| 60 | void handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 61 |
|
|---|
| 62 | /// Controller callback function when job has been sent.
|
|---|
| 63 | void handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 64 |
|
|---|
| 65 | private:
|
|---|
| 66 |
|
|---|
| 67 | /// Controller callback function when checking on state of results.
|
|---|
| 68 | void handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn);
|
|---|
| 69 |
|
|---|
| 70 | /// internal function to prepare worker connections
|
|---|
| 71 | void initiateWorkerSocket();
|
|---|
| 72 |
|
|---|
| 73 | /// internal function to prepare controller connections
|
|---|
| 74 | void initiateControllerSocket();
|
|---|
| 75 |
|
|---|
| 76 | private:
|
|---|
| 77 | /// The acceptor object used to accept incoming worker socket connections.
|
|---|
| 78 | boost::asio::ip::tcp::acceptor worker_acceptor_;
|
|---|
| 79 |
|
|---|
| 80 | /// The acceptor object used to accept incoming controller socket connections.
|
|---|
| 81 | boost::asio::ip::tcp::acceptor controller_acceptor_;
|
|---|
| 82 |
|
|---|
| 83 | /// result that is received from the client.
|
|---|
| 84 | FragmentResult result;
|
|---|
| 85 |
|
|---|
| 86 | /// bunch of jobs received from controller before placed in JobsQueue
|
|---|
| 87 | std::vector<FragmentJob> jobs;
|
|---|
| 88 |
|
|---|
| 89 | /// number of jobs that are calculated, required for returning status
|
|---|
| 90 | size_t doneJobs;
|
|---|
| 91 |
|
|---|
| 92 | /// Queue with data to be sent to each client.
|
|---|
| 93 | FragmentQueue JobsQueue;
|
|---|
| 94 |
|
|---|
| 95 | // static entity to indicate to clients that the queue is empty.
|
|---|
| 96 | static FragmentJob NoJob;
|
|---|
| 97 |
|
|---|
| 98 | // Exit flag on program exit
|
|---|
| 99 | enum Exitflag_t Exitflag;
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | #endif /* FRAGMENTSCHEDULER_HPP_ */
|
|---|