Directory: | ./ |
---|---|
File: | src/Daemon/BaseDaemon.h |
Date: | 2025-03-14 12:18:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 25 | 29 | 86.2% |
Branches: | 32 | 58 | 55.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #ifndef __BASE_DAEMON_H__ | ||
8 | #define __BASE_DAEMON_H__ | ||
9 | |||
10 | #include <functional> | ||
11 | |||
12 | #include "PPath.h" | ||
13 | #include "PLog.h" | ||
14 | #include "OptionParser.h" | ||
15 | #include "representation_def.h" | ||
16 | #include "FunctionCall.h" | ||
17 | #include "DataFunctionCall.h" | ||
18 | #include "DataFunctionClassCall.h" | ||
19 | |||
20 | ///@brief Daemon which help communication between processes and thread | ||
21 | class BaseDaemon{ | ||
22 | public: | ||
23 | BaseDaemon(); | ||
24 | virtual ~BaseDaemon(); | ||
25 | |||
26 | bool parseArgument(int argc, char** argv); | ||
27 | bool load(const PPath & fileName, const PString & daemonName); | ||
28 | |||
29 | ///Add a callable method of the Daemon | ||
30 | /** @param function : method to be added | ||
31 | * @param name : name of the method which can be called | ||
32 | * @return true on success, false otherwise | ||
33 | */ | ||
34 | template<typename _Callable> | ||
35 | 3 | bool addCallableMethod(_Callable && function, const PString & name){ | |
36 | 3 | std::map<PString, AbstractFunction*>::iterator it(p_mapCallableMethod.find(name)); | |
37 | 3 | if(it != p_mapCallableMethod.end()){ | |
38 | 1 | getLog().getLogError() << "BaseDaemon::addCallableMethod : function '"<<name<<"' already added" << std::endl; | |
39 | 1 | return false; | |
40 | } | ||
41 | 2 | p_mapCallableMethod[name] = new FunctionCall(function, name); | |
42 | 2 | return true; | |
43 | } | ||
44 | |||
45 | bool callMethod(Data & result, const PString & name, const Data & parameter); | ||
46 | void clearCallableMethod(); | ||
47 | |||
48 | void stop(); | ||
49 | OptionParser & getOptionParser(); | ||
50 | PLog & getLog(); | ||
51 | bool isDaemonExist(const PString & name) const; | ||
52 | |||
53 | void addMessageToConfirm(const Message & message); | ||
54 | bool getMessageToConfirm(Message & message, size_t id) const; | ||
55 | void removeConfirmedMessage(size_t id, time_t currentTime); | ||
56 | size_t getMessageId(); | ||
57 | |||
58 | ///Add a callable method of the Daemon | ||
59 | /** @param function : method to be added | ||
60 | * @return true on success, false otherwise | ||
61 | */ | ||
62 | template<typename _Data> | ||
63 | 6 | bool addDataCallableMethod(bool(&function)(const _Data &)){ | |
64 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
6 | PString prototype(phoenix_getTypeToStr<_Data>()); |
65 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
6 | std::cout << "BaseDaemon::addDataCallableMethod : prototype = " << prototype << std::endl; |
66 |
1/1✓ Branch 1 taken 3 times.
|
6 | std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype)); |
67 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
6 | if(it != p_mapDataFunction.end()){ |
68 | ✗ | getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl; | |
69 | ✗ | return false; | |
70 | } | ||
71 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
6 | getLog().getLogDebug() << "BaseDaemon::addDataCallableMethod : add method to process data '"<<prototype<<"'" << std::endl; |
72 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
6 | p_mapDataFunction[prototype] = new DataFunctionCall(function, prototype); |
73 | 6 | return true; | |
74 | 6 | } | |
75 | |||
76 | ///Add a callable method of the Daemon | ||
77 | /** @param function : method to be added | ||
78 | * @param persistentData : data used each time the method is called | ||
79 | * @return true on success, false otherwise | ||
80 | */ | ||
81 | template<typename _Class, typename _Data> | ||
82 | 1 | bool addDataCallableMethod(bool(&function)(_Class&, const _Data &), _Class& persistentData){ | |
83 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | PString prototype(phoenix_getTypeToStr<_Data>()); |
84 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | std::cout << "BaseDaemon::addDataCallableMethod : prototype = " << prototype << std::endl; |
85 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype)); |
86 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if(it != p_mapDataFunction.end()){ |
87 | ✗ | getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl; | |
88 | ✗ | return false; | |
89 | } | ||
90 |
6/6✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
|
1 | getLog().getLogDebug() << "BaseDaemon::addDataCallableMethod : add method to process data '"<<prototype<<"'" << std::endl; |
91 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | p_mapDataFunction[prototype] = new DataFunctionClassCall(function, persistentData, prototype); |
92 | 1 | return true; | |
93 | 1 | } | |
94 | |||
95 | bool processData(const Data & data); | ||
96 | |||
97 | protected: | ||
98 | ///Configuration of the curent Daemon | ||
99 | DaemonConfig p_config; | ||
100 | ///Map of the other Daemon of the Swarm | ||
101 | MapDaemonConfig p_mapDaemon; | ||
102 | ///Logger of the current Daemon | ||
103 | PLog p_log; | ||
104 | ///True if the current BaseDaemon is running | ||
105 | bool p_isRun; | ||
106 | |||
107 | private: | ||
108 | void initialisationBaseDaemon(); | ||
109 | |||
110 | ///Option parser of the Daemon | ||
111 | OptionParser p_optionParser; | ||
112 | ///Map of callable method of the Daemon | ||
113 | std::map<PString, AbstractFunction*> p_mapCallableMethod; | ||
114 | ///Map of messages which have to be confirmed by destination Daemon | ||
115 | std::map<size_t, Message> p_mapMessageToBeConfirmed; | ||
116 | ///Id counter of the message of the current Daemon | ||
117 | size_t p_messageId; | ||
118 | |||
119 | ///Map of methods which have to be called when recieving data | ||
120 | std::map<PString, AbstractDataFunction*> p_mapDataFunction; | ||
121 | }; | ||
122 | |||
123 | #endif | ||
124 | |||
125 |