| 1 | /*
|
|---|
| 2 | * WorkerPool.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: 22.02.2012
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef WORKERPOOL_HPP_
|
|---|
| 9 | #define WORKERPOOL_HPP_
|
|---|
| 10 |
|
|---|
| 11 | // include config.h
|
|---|
| 12 | #ifdef HAVE_CONFIG_H
|
|---|
| 13 | #include <config.h>
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | #include <map>
|
|---|
| 17 | #include <set>
|
|---|
| 18 | #include <string>
|
|---|
| 19 |
|
|---|
| 20 | #include "CodePatterns/Observer/Observable.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "WorkerAddress.hpp"
|
|---|
| 23 |
|
|---|
| 24 | class FragmentQueue;
|
|---|
| 25 | class WorkerPoolTest;
|
|---|
| 26 |
|
|---|
| 27 | /** Class WorkerPool contains a number of PoolWorkers that connect to it
|
|---|
| 28 | * and wait for jobs to be sent to them. The class manages this pool of
|
|---|
| 29 | * workers.
|
|---|
| 30 | *
|
|---|
| 31 | */
|
|---|
| 32 | class WorkerPool : public Observable
|
|---|
| 33 | {
|
|---|
| 34 | //!> grant unit test access
|
|---|
| 35 | friend class WorkerPoolTest;
|
|---|
| 36 | public:
|
|---|
| 37 | WorkerPool();
|
|---|
| 38 | ~WorkerPool();
|
|---|
| 39 |
|
|---|
| 40 | enum NotificationType {
|
|---|
| 41 | WorkerIdle,
|
|---|
| 42 | WorkerAdded,
|
|---|
| 43 | WorkerRemoved,
|
|---|
| 44 | NotificationType_MAX // denotes the maximum of available notification types
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | bool addWorker(const WorkerAddress &address);
|
|---|
| 48 | bool presentInPool(const WorkerAddress &address) const;
|
|---|
| 49 | bool presentIdleWorkers() const {
|
|---|
| 50 | return !idle_queue.empty();
|
|---|
| 51 | }
|
|---|
| 52 | WorkerAddress getNextIdleWorker();
|
|---|
| 53 | bool isWorkerBusy(const WorkerAddress &address) const;
|
|---|
| 54 | bool removeWorker(const WorkerAddress& address);
|
|---|
| 55 | void unmarkWorkerBusy(const WorkerAddress &address);
|
|---|
| 56 |
|
|---|
| 57 | // this is currently for the passing time until Worker pool is fully operable
|
|---|
| 58 |
|
|---|
| 59 | //!> typedef of the priority in the idle queue of a worker
|
|---|
| 60 | typedef size_t priority_t;
|
|---|
| 61 |
|
|---|
| 62 | //!> typedef for the worker queue being a map with priority and address of worker
|
|---|
| 63 | typedef std::multimap<priority_t, WorkerAddress> Idle_Queue_t;
|
|---|
| 64 |
|
|---|
| 65 | Idle_Queue_t::iterator getIdleWorker(const WorkerAddress &address);
|
|---|
| 66 | void markWorkerBusy(Idle_Queue_t::iterator &iter);
|
|---|
| 67 |
|
|---|
| 68 | private:
|
|---|
| 69 | //!> typedef for the worker queue being a map with priority and address of worker
|
|---|
| 70 | typedef std::map<WorkerAddress, priority_t> Busy_Queue_t;
|
|---|
| 71 |
|
|---|
| 72 | //!> typedef for the pool of workers being a set to keep only unique addresses
|
|---|
| 73 | typedef std::set<WorkerAddress> Pool_t;
|
|---|
| 74 |
|
|---|
| 75 | private:
|
|---|
| 76 | void removeAllWorkers();
|
|---|
| 77 |
|
|---|
| 78 | private:
|
|---|
| 79 | //!> static for defining default priority for new workers
|
|---|
| 80 | static priority_t default_priority;
|
|---|
| 81 |
|
|---|
| 82 | //!> empty address in case queue is idle
|
|---|
| 83 | static WorkerAddress emptyAddress;
|
|---|
| 84 |
|
|---|
| 85 | //!> pool of all worker addresses to connect to and work on jobs
|
|---|
| 86 | Pool_t pool;
|
|---|
| 87 |
|
|---|
| 88 | //!> pool of worker addresses to connect to and work on jobs
|
|---|
| 89 | Idle_Queue_t idle_queue;
|
|---|
| 90 |
|
|---|
| 91 | //!> pool of worker addresses to connect to and work on jobs
|
|---|
| 92 | mutable Busy_Queue_t busy_queue;
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | #endif /* WORKERPOOL_HPP_ */
|
|---|