GCC Code Coverage Report


Directory: ./
File: src/Daemon/BaseDaemon.h
Date: 2026-01-15 15:35:36
Exec Total Coverage
Lines: 27 31 87.1%
Functions: 11 11 100.0%
Branches: 57 80 71.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 "OptionParser.h"
13 #include "parser_toml.h"
14 #include "PSocketFlag.h"
15 #include "PUncastableBool.h"
16
17 #include "daemon_config_exception.h"
18 #include "daemon_load_config.h"
19 #include "representation_def.h"
20 #include "FunctionCall.h"
21 #include "DataFunctionCall.h"
22 #include "DataFunctionClassCall.h"
23
24 ///Make a callable slot for the application
25 /** @param C : name of the class to define the slot
26 * @param X : name of the method to be called with this slot
27 * @param D : data to be used
28 */
29 #define SWARM_MAKE_SLOT(C,X,D) static PUncastableBool slot_##X(C & self, const D & _data){\
30 return self.X(_data);\
31 }
32
33 ///Call a phoenix slot
34 /** @param X : name of the base function to be called by the slot
35 */
36 #define SWARM_SLOT(X) slot_##X
37
38 typedef std::map<PString, std::map<PString, StatAccumulator> > MapDaemonStatAccumulator;
39 typedef std::map<PString, StatAccumulator> MapStatAccumulator;
40
41 ///@brief Main Daemon configuration which drives timeouts and flags of send and recv calls
42 struct DaemonMainConfig{
43 ///Recv time out in miliseconds
44 int recvTimeoutMs{-1};
45 ///Send time out in miliseconds
46 int sendTimeoutMs{-1};
47 ///Recv flag to be used for every recv of the Daemon
48 PRecvFlag::PRecvFlag recvFlag{PRecvFlag::NON_BLOCK};
49 ///Send flag to be used for every send of the Daemon
50 PSendFlag::PSendFlag sendFlag{PSendFlag::NON_BLOCK};
51 };
52
53 ///@brief Daemon which help communication between processes and thread
54 class BaseDaemon{
55 public:
56 BaseDaemon();
57 virtual ~BaseDaemon();
58
59 bool parseArgument(int argc, char** argv);
60 void loadConfigFromNode(const ConfigNode & dico, const PString & daemonName);
61 bool load(const PString & configFileContent, const PString & daemonName, ConfigFormat::ConfigFormat format);
62 bool load(const PPath & fileName, const PString & daemonName);
63
64 virtual bool extraLoad(const ConfigNode * config);
65 virtual void addCallMethod();
66
67 ///Add a callable method of the Daemon
68 /** @param function : method to be added
69 * @param name : name of the method which can be called
70 * @return true on success, false otherwise
71 */
72 template<typename _Callable>
73 3 bool addCallableMethod(_Callable && function, const PString & name){
74
1/1
✓ Branch 0 (2→3) taken 3 times.
3 std::map<PString, AbstractFunction*>::iterator it(p_mapCallableMethod.find(name));
75
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→13) taken 2 times.
3 if(it != p_mapCallableMethod.end()){
76
6/6
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 2 (7→8) taken 1 times.
✓ Branch 4 (8→9) taken 1 times.
✓ Branch 6 (9→10) taken 1 times.
✓ Branch 8 (10→11) taken 1 times.
✓ Branch 10 (11→12) taken 1 times.
1 getLog().getLogError() << "BaseDaemon::addCallableMethod : function '"<<name<<"' already added" << std::endl;
77 1 return false;
78 }
79
4/7
✓ Branch 0 (13→14) taken 2 times.
✓ Branch 2 (14→15) taken 2 times.
✓ Branch 4 (15→16) taken 2 times.
✗ Branch 6 (16→17) not taken.
✓ Branch 7 (16→18) taken 2 times.
✗ Branch 8 (21→22) not taken.
✗ Branch 9 (21→23) not taken.
2 p_mapCallableMethod[name] = new FunctionCall(function, name);
80 2 return true;
81 }
82
83 bool callMethod(Data & result, const PString & name, const Data & parameter);
84 void clearCallableMethod();
85
86 void stop();
87 OptionParser & getOptionParser();
88 PLog & getLog();
89 bool isDaemonExist(const PString & name) const;
90
91 void addMessageToConfirm(const Message & message);
92 bool getMessageToConfirm(Message & message, size_t id) const;
93 void processConfirmedMessage(size_t id, time_t currentTime);
94 void checkMessageTimeout(time_t currentTime);
95 size_t getMessageId();
96
97 ///Add a callable method of the Daemon
98 /** @param function : method to be added
99 * @param nbBin : number of bin in the histogram
100 * @param histLowerBound : lower bound of the histogram
101 * @param histUpperBound : upper bound of the histogram
102 * @return true on success, false otherwise
103 */
104 template<typename _Data>
105 2 bool addDataCallableMethod(PUncastableBool(&function)(const _Data &), size_t nbBin, float histLowerBound, float histUpperBound){
106
2/2
✓ Branch 0 (2→3) taken 2 times.
✓ Branch 2 (3→4) taken 2 times.
2 PString prototype(phoenix_getTypeToStr<_Data>());
107
3/3
✓ Branch 0 (5→6) taken 2 times.
✓ Branch 2 (6→7) taken 2 times.
✓ Branch 4 (7→8) taken 2 times.
2 std::cout << "BaseDaemon::addDataCallableMethod : prototype = " << prototype << std::endl;
108
1/1
✓ Branch 0 (8→9) taken 2 times.
2 std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype));
109
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→19) taken 2 times.
2 if(it != p_mapDataFunction.end()){
110 getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl;
111 return false;
112 }
113
6/6
✓ Branch 0 (19→20) taken 2 times.
✓ Branch 2 (20→21) taken 2 times.
✓ Branch 4 (21→22) taken 2 times.
✓ Branch 6 (22→23) taken 2 times.
✓ Branch 8 (23→24) taken 2 times.
✓ Branch 10 (24→25) taken 2 times.
2 getLog().getLogDebug() << "BaseDaemon::addDataCallableMethod : add method to process data '"<<prototype<<"'" << std::endl;
114
4/7
✓ Branch 0 (25→26) taken 2 times.
✓ Branch 2 (26→27) taken 2 times.
✓ Branch 4 (27→28) taken 2 times.
✗ Branch 6 (28→29) not taken.
✓ Branch 7 (28→30) taken 2 times.
✗ Branch 8 (43→44) not taken.
✗ Branch 9 (43→45) not taken.
2 p_mapDataFunction[prototype] = new DataFunctionCall(function, prototype);
115
5/5
✓ Branch 0 (30→31) taken 2 times.
✓ Branch 2 (31→32) taken 2 times.
✓ Branch 4 (32→33) taken 2 times.
✓ Branch 6 (33→34) taken 2 times.
✓ Branch 8 (34→35) taken 2 times.
2 p_config.getDaemonStatAccumulator().getMapStatComputing()[prototype] = createNewStat(nbBin, histLowerBound, histUpperBound);
116 2 return true;
117 2 }
118
119 ///Add a callable method of the Daemon
120 /** @param function : method to be added
121 * @param persistentData : data used each time the method is called
122 * @param nbBin : number of bin in the histogram
123 * @param histLowerBound : lower bound of the histogram
124 * @param histUpperBound : upper bound of the histogram
125 * @return true on success, false otherwise
126 */
127 template<typename _Class, typename _Data>
128 7 bool addDataCallableMethod(PUncastableBool(&function)(_Class &, const _Data &), _Class & persistentData, size_t nbBin, float histLowerBound, float histUpperBound){
129
2/2
✓ Branch 0 (2→3) taken 7 times.
✓ Branch 2 (3→4) taken 7 times.
7 PString prototype(phoenix_getTypeToStr<_Data>());
130
3/3
✓ Branch 0 (5→6) taken 7 times.
✓ Branch 2 (6→7) taken 7 times.
✓ Branch 4 (7→8) taken 7 times.
7 std::cout << "BaseDaemon::addDataCallableMethod : prototype = " << prototype << std::endl;
131
1/1
✓ Branch 0 (8→9) taken 7 times.
7 std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype));
132
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→19) taken 7 times.
7 if(it != p_mapDataFunction.end()){
133 getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl;
134 return false;
135 }
136
6/6
✓ Branch 0 (19→20) taken 7 times.
✓ Branch 2 (20→21) taken 7 times.
✓ Branch 4 (21→22) taken 7 times.
✓ Branch 6 (22→23) taken 7 times.
✓ Branch 8 (23→24) taken 7 times.
✓ Branch 10 (24→25) taken 7 times.
7 getLog().getLogDebug() << "BaseDaemon::addDataCallableMethod : add method to process data '"<<prototype<<"'" << std::endl;
137
4/7
✓ Branch 0 (25→26) taken 7 times.
✓ Branch 2 (26→27) taken 7 times.
✓ Branch 4 (27→28) taken 7 times.
✗ Branch 6 (28→29) not taken.
✓ Branch 7 (28→30) taken 7 times.
✗ Branch 8 (43→44) not taken.
✗ Branch 9 (43→45) not taken.
7 p_mapDataFunction[prototype] = new DataFunctionClassCall(function, persistentData, prototype);
138
5/5
✓ Branch 0 (30→31) taken 7 times.
✓ Branch 2 (31→32) taken 7 times.
✓ Branch 4 (32→33) taken 7 times.
✓ Branch 6 (33→34) taken 7 times.
✓ Branch 8 (34→35) taken 7 times.
7 p_config.getDaemonStatAccumulator().getMapStatComputing()[prototype] = createNewStat(nbBin, histLowerBound, histUpperBound);
139 7 return true;
140 7 }
141
142 AbstractDataFunction* getDataFunction(const Data & data);
143 bool processData(const Data & data);
144
145 void updateStatAccumulator(StatAccumulator & stat, float value);
146 StatAccumulator createNewStat(size_t nbBin, float histLowerBound, float histUpperBound);
147 const std::map<size_t, Message> & getMapMessageToBeConfirmed() const;
148 void clearStat();
149
150 DaemonConfig & getConfig();
151 MapDaemonConfig & getMapDaemonConfig();
152 DaemonMainConfig & getMainConfig();
153 PLog & getLogger();
154
155 protected:
156 VecStat fillVecStat(const StatAccumulator & accumulator, time_t startTimestamp, time_t endTimestamp);
157 void fillDaemonStat(DaemonStat & stat, time_t startTimestamp, time_t endTimestamp);
158
159 ///Main configuration of the Daemon
160 DaemonMainConfig p_mainConfig;
161 ///Configuration of the curent Daemon
162 DaemonConfig p_config;
163 ///Map of the other Daemon of the Swarm
164 MapDaemonConfig p_mapDaemon;
165 ///Logger of the current Daemon
166 PLog p_log;
167 ///True if the current BaseDaemon is running
168 bool p_isRun;
169 ///True if the daemon has to be executed in mock mode for socket and clock
170 bool p_isFullMock;
171 ///True if the daemon has to be executed in mock record mode for socket and clock
172 bool p_isFullMockRecord;
173
174 private:
175 void initialisationBaseDaemon();
176 std::map<PString, StatAccumulator> createNewCommunicationStatMap(const PString & dataType, size_t nbBin, float histLowerBound, float histUpperBound);
177 void getCommunicationStat(const PString & destName, const PString & dataType, float latency, size_t nbBin, float histLowerBound, float histUpperBound);
178
179 ///Option parser of the Daemon
180 OptionParser p_optionParser;
181 ///Map of callable method of the Daemon
182 std::map<PString, AbstractFunction*> p_mapCallableMethod;
183 ///Map of messages which have to be confirmed by destination Daemon
184 std::map<size_t, Message> p_mapMessageToBeConfirmed;
185 ///Id counter of the message of the current Daemon
186 size_t p_messageId;
187
188 ///Map of methods which have to be called when receiving data
189 std::map<PString, AbstractDataFunction*> p_mapDataFunction;
190 };
191
192 #endif
193
194