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