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