| 1 | /*
 | 
|---|
| 2 |  * \file PoolWorker.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * This file strongly follows the Serialization example from the boost::asio
 | 
|---|
| 5 |  * library (see client.cpp).
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *  Created on: Feb 28, 2012
 | 
|---|
| 8 |  *      Author: heber
 | 
|---|
| 9 |  */
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef POOLWORKER_HPP_
 | 
|---|
| 12 | #define POOLWORKER_HPP_
 | 
|---|
| 13 | 
 | 
|---|
| 14 | // include config.h
 | 
|---|
| 15 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 16 | #include <config.h>
 | 
|---|
| 17 | #endif
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <boost/asio.hpp>
 | 
|---|
| 20 | #include <boost/function.hpp>
 | 
|---|
| 21 | #include <vector>
 | 
|---|
| 22 | #include "Connection.hpp"
 | 
|---|
| 23 | #include "ExitflagContainer.hpp"
 | 
|---|
| 24 | #include "Jobs/FragmentJob.hpp"
 | 
|---|
| 25 | #include "Operations/OperationQueue.hpp"
 | 
|---|
| 26 | #include "Listener.hpp"
 | 
|---|
| 27 | #include "WorkerAddress.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** Receives a job from Server to execute and return FragmentResult.
 | 
|---|
| 30 |  *
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | class PoolWorker : public ExitflagContainer
 | 
|---|
| 33 | {
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |   /// Constructor starts the asynchronous connect operation.
 | 
|---|
| 36 |   PoolWorker(
 | 
|---|
| 37 |       boost::asio::io_service& io_service,
 | 
|---|
| 38 |       const std::string& host,
 | 
|---|
| 39 |       const std::string& service,
 | 
|---|
| 40 |       const std::string& listenhost,
 | 
|---|
| 41 |       const std::string& listenservice);
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   /** Returns the flag of the handled operation.
 | 
|---|
| 44 |    *
 | 
|---|
| 45 |    */
 | 
|---|
| 46 |   size_t getFlag() const
 | 
|---|
| 47 |   {
 | 
|---|
| 48 |     if (PoolListener.getExitflag())
 | 
|---|
| 49 |       return PoolListener.getExitflag();
 | 
|---|
| 50 |     if (getExitflag())
 | 
|---|
| 51 |       return getExitflag();
 | 
|---|
| 52 |     return 0;
 | 
|---|
| 53 |   }
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   void WorkOnJob(FragmentJob::ptr &job);
 | 
|---|
| 56 |   void removeFromPool();
 | 
|---|
| 57 |   void shutdown(int sig);
 | 
|---|
| 58 |   void shutdown();
 | 
|---|
| 59 |   void finish();
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   class PoolListener_t : public Listener
 | 
|---|
| 62 |   {
 | 
|---|
| 63 |   public:
 | 
|---|
| 64 |     PoolListener_t(
 | 
|---|
| 65 |         boost::asio::io_service& io_service,
 | 
|---|
| 66 |         unsigned short port,
 | 
|---|
| 67 |         PoolWorker &_callback) :
 | 
|---|
| 68 |       Listener(io_service, port),
 | 
|---|
| 69 |       callback(_callback)
 | 
|---|
| 70 |     {}
 | 
|---|
| 71 |     virtual ~PoolListener_t() {}
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   protected:
 | 
|---|
| 74 |     /// Handle completion of a accept controller operation.
 | 
|---|
| 75 |     void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |     /// Controller callback function when job has been sent.
 | 
|---|
| 78 |     void handle_ReceiveJob(const boost::system::error_code& e, connection_ptr conn);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   private:
 | 
|---|
| 81 |     //!> callback reference to PoolWorker for handling the job
 | 
|---|
| 82 |     PoolWorker &callback;
 | 
|---|
| 83 | 
 | 
|---|
| 84 |     //!> current job
 | 
|---|
| 85 |     FragmentJob::ptr job;
 | 
|---|
| 86 |   };
 | 
|---|
| 87 | 
 | 
|---|
| 88 | private:
 | 
|---|
| 89 |   //!> reference to io_service which we use for connections
 | 
|---|
| 90 |   boost::asio::io_service& io_service;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   //!> The listener for the WorkerPool
 | 
|---|
| 93 |   PoolListener_t PoolListener;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   //!> address of this worker
 | 
|---|
| 96 |   const WorkerAddress MyAddress;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   //!> address of the server we work for
 | 
|---|
| 99 |   WorkerAddress ServerAddress;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   //!> The Connection to the server for the stored operations
 | 
|---|
| 102 |   Connection connection_;
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   //!> bound function as callback when operation fails
 | 
|---|
| 105 |   boost::function<void ()> failed;
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   //!> internal queue for all asynchronous operations
 | 
|---|
| 108 |   OperationQueue OpQueue;
 | 
|---|
| 109 | };
 | 
|---|
| 110 | 
 | 
|---|
| 111 | #endif /* POOLWORKER_HPP_ */
 | 
|---|