GCC Code Coverage Report


Directory: ./
File: src/Daemon/BaseDaemon.h
Date: 2025-03-27 14:50:11
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 void checkMesageTimeout(time_t currentTime);
57 size_t getMessageId();
58
59 ///Add a callable method of the Daemon
60 /** @param function : method to be added
61 * @return true on success, false otherwise
62 */
63 template<typename _Data>
64 6 bool addDataCallableMethod(bool(&function)(const _Data &)){
65
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 PString prototype(phoenix_getTypeToStr<_Data>());
66
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;
67
1/1
✓ Branch 1 taken 3 times.
6 std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype));
68
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
6 if(it != p_mapDataFunction.end()){
69 getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl;
70 return false;
71 }
72
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;
73
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);
74 6 return true;
75 6 }
76
77 ///Add a callable method of the Daemon
78 /** @param function : method to be added
79 * @param persistentData : data used each time the method is called
80 * @return true on success, false otherwise
81 */
82 template<typename _Class, typename _Data>
83 1 bool addDataCallableMethod(bool(&function)(_Class&, const _Data &), _Class& persistentData){
84
2/2
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
1 PString prototype(phoenix_getTypeToStr<_Data>());
85
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;
86
1/1
✓ Branch 1 taken 1 times.
1 std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype));
87
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if(it != p_mapDataFunction.end()){
88 getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl;
89 return false;
90 }
91
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;
92
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);
93 1 return true;
94 1 }
95
96 bool processData(const Data & data);
97
98 protected:
99 ///Configuration of the curent Daemon
100 DaemonConfig p_config;
101 ///Map of the other Daemon of the Swarm
102 MapDaemonConfig p_mapDaemon;
103 ///Logger of the current Daemon
104 PLog p_log;
105 ///True if the current BaseDaemon is running
106 bool p_isRun;
107
108 private:
109 void initialisationBaseDaemon();
110
111 ///Option parser of the Daemon
112 OptionParser p_optionParser;
113 ///Map of callable method of the Daemon
114 std::map<PString, AbstractFunction*> p_mapCallableMethod;
115 ///Map of messages which have to be confirmed by destination Daemon
116 std::map<size_t, Message> p_mapMessageToBeConfirmed;
117 ///Id counter of the message of the current Daemon
118 size_t p_messageId;
119
120 ///Map of methods which have to be called when recieving data
121 std::map<PString, AbstractDataFunction*> p_mapDataFunction;
122 };
123
124 #endif
125
126