PhoenixSwarm  3.5.0
Library to ease communication between daemons
Loading...
Searching...
No Matches
BaseDaemon.h
Go to the documentation of this file.
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
18#include "daemon_load_config.h"
19#include "representation_def.h"
20#include "FunctionCall.h"
21#include "DataFunctionCall.h"
23
25
29#define SWARM_MAKE_SLOT(C,X,D) static PUncastableBool slot_##X(C & self, const D & _data){\
30 return self.X(_data);\
31}
32
34
36#define SWARM_SLOT(X) slot_##X
37
38typedef std::map<PString, std::map<PString, StatAccumulator> > MapDaemonStatAccumulator;
39typedef std::map<PString, StatAccumulator> MapStatAccumulator;
40
48 PRecvFlag::PRecvFlag recvFlag{PRecvFlag::NON_BLOCK};
50 PSendFlag::PSendFlag sendFlag{PSendFlag::NON_BLOCK};
51};
52
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
68
72 template<typename _Callable>
73 bool addCallableMethod(_Callable && function, const PString & name){
74 std::map<PString, AbstractFunction*>::iterator it(p_mapCallableMethod.find(name));
75 if(it != p_mapCallableMethod.end()){
76 getLog().getLogError() << "BaseDaemon::addCallableMethod : function '"<<name<<"' already added" << std::endl;
77 return false;
78 }
79 p_mapCallableMethod[name] = new FunctionCall(function, name);
80 return true;
81 }
82
83 bool callMethod(Data & result, const PString & name, const Data & parameter);
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
98
104 template<typename _Data>
105 bool addDataCallableMethod(PUncastableBool(&function)(const _Data &), size_t nbBin, float histLowerBound, float histUpperBound){
106 PString prototype(phoenix_getTypeToStr<_Data>());
107 std::cout << "BaseDaemon::addDataCallableMethod : prototype = " << prototype << std::endl;
108 std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype));
109 if(it != p_mapDataFunction.end()){
110 getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl;
111 return false;
112 }
113 getLog().getLogDebug() << "BaseDaemon::addDataCallableMethod : add method to process data '"<<prototype<<"'" << std::endl;
114 p_mapDataFunction[prototype] = new DataFunctionCall(function, prototype);
115 p_config.getDaemonStatAccumulator().getMapStatComputing()[prototype] = createNewStat(nbBin, histLowerBound, histUpperBound);
116 return true;
117 }
118
120
127 template<typename _Class, typename _Data>
128 bool addDataCallableMethod(PUncastableBool(&function)(_Class &, const _Data &), _Class & persistentData, size_t nbBin, float histLowerBound, float histUpperBound){
129 PString prototype(phoenix_getTypeToStr<_Data>());
130 std::cout << "BaseDaemon::addDataCallableMethod : prototype = " << prototype << std::endl;
131 std::map<PString, AbstractDataFunction*>::iterator it(p_mapDataFunction.find(prototype));
132 if(it != p_mapDataFunction.end()){
133 getLog().getLogError() << "BaseDaemon::addDataCallableMethod : function to process data '"<<prototype<<"' already added" << std::endl;
134 return false;
135 }
136 getLog().getLogDebug() << "BaseDaemon::addDataCallableMethod : add method to process data '"<<prototype<<"'" << std::endl;
137 p_mapDataFunction[prototype] = new DataFunctionClassCall(function, persistentData, prototype);
138 p_config.getDaemonStatAccumulator().getMapStatComputing()[prototype] = createNewStat(nbBin, histLowerBound, histUpperBound);
139 return true;
140 }
141
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
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
166 PLog p_log;
173
174 private:
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
180 OptionParser p_optionParser;
182 std::map<PString, AbstractFunction*> p_mapCallableMethod;
184 std::map<size_t, Message> p_mapMessageToBeConfirmed;
187
189 std::map<PString, AbstractDataFunction*> p_mapDataFunction;
190};
191
192#endif
193
std::map< PString, std::map< PString, StatAccumulator > > MapDaemonStatAccumulator
Definition BaseDaemon.h:38
std::map< PString, StatAccumulator > MapStatAccumulator
Definition BaseDaemon.h:39
Abstract function definition which will be callable in Daemon.
PLog p_log
Logger of the current Daemon.
Definition BaseDaemon.h:166
void clearCallableMethod()
Clear the map of callable methods.
bool p_isRun
True if the current BaseDaemon is running.
Definition BaseDaemon.h:168
void loadConfigFromNode(const ConfigNode &dico, const PString &daemonName)
Load configuration form a node of ConfigNode.
bool isDaemonExist(const PString &name) const
Say if a neighbour Daemon does exist.
OptionParser p_optionParser
Option parser of the Daemon.
Definition BaseDaemon.h:180
void updateStatAccumulator(StatAccumulator &stat, float value)
Update a computing statistic with a new value.
size_t p_messageId
Id counter of the message of the current Daemon.
Definition BaseDaemon.h:186
OptionParser & getOptionParser()
Return the OptionParser of the current BaseDaemon.
std::map< PString, StatAccumulator > createNewCommunicationStatMap(const PString &dataType, size_t nbBin, float histLowerBound, float histUpperBound)
Create a new map of communication statistic.
bool p_isFullMockRecord
True if the daemon has to be executed in mock record mode for socket and clock.
Definition BaseDaemon.h:172
bool getMessageToConfirm(Message &message, size_t id) const
Get a message to confirm by id if it exists.
VecStat fillVecStat(const StatAccumulator &accumulator, time_t startTimestamp, time_t endTimestamp)
Fill a VecStat from a StatAccumulator to send to the DamonStat.
bool processData(const Data &data)
Process given data with the proper method.
PLog & getLogger()
Get the logger of the current BaseDaemon.
bool load(const PString &configFileContent, const PString &daemonName, ConfigFormat::ConfigFormat format)
Load the Json configuration which define all BaseDaemons of the Swarm.
DaemonMainConfig p_mainConfig
Main configuration of the Daemon.
Definition BaseDaemon.h:160
bool p_isFullMock
True if the daemon has to be executed in mock mode for socket and clock.
Definition BaseDaemon.h:170
void initialisationBaseDaemon()
Initialisation function of the class BaseDaemon.
bool addCallableMethod(_Callable &&function, const PString &name)
Add a callable method of the Daemon.
Definition BaseDaemon.h:73
MapDaemonConfig & getMapDaemonConfig()
Get the map of all Daemon configurations.
DaemonConfig & getConfig()
Get the configuration of the current BaseDaemon.
bool callMethod(Data &result, const PString &name, const Data &parameter)
Call an added callable method by name.
std::map< PString, AbstractFunction * > p_mapCallableMethod
Map of callable method of the Daemon.
Definition BaseDaemon.h:182
DaemonMainConfig & getMainConfig()
Get the main configuration of the swarm.
void addMessageToConfirm(const Message &message)
Add a message to confirm.
virtual void addCallMethod()
Do the addCallableMethod here.
virtual bool extraLoad(const ConfigNode *config)
Extra call on load for the current Daemon.
AbstractDataFunction * getDataFunction(const Data &data)
Get the data function associated with the given data.
bool addDataCallableMethod(PUncastableBool(&function)(_Class &, const _Data &), _Class &persistentData, size_t nbBin, float histLowerBound, float histUpperBound)
Add a callable method of the Daemon.
Definition BaseDaemon.h:128
void fillDaemonStat(DaemonStat &stat, time_t startTimestamp, time_t endTimestamp)
Fill the DaemonStat with the current statistics of the daemon.
PLog & getLog()
Get the log of the current BaseDaemon.
void clearStat()
Clear all the statistics of the daemon.
void stop()
Stops the BaseDaemon.
std::map< PString, AbstractDataFunction * > p_mapDataFunction
Map of methods which have to be called when receiving data.
Definition BaseDaemon.h:189
void checkMessageTimeout(time_t currentTime)
Check if a message has reached the timeout.
DaemonConfig p_config
Configuration of the curent Daemon.
Definition BaseDaemon.h:162
MapDaemonConfig p_mapDaemon
Map of the other Daemon of the Swarm.
Definition BaseDaemon.h:164
size_t getMessageId()
Get current message id.
void getCommunicationStat(const PString &destName, const PString &dataType, float latency, size_t nbBin, float histLowerBound, float histUpperBound)
Add a communication statistic.
bool addDataCallableMethod(PUncastableBool(&function)(const _Data &), size_t nbBin, float histLowerBound, float histUpperBound)
Add a callable method of the Daemon.
Definition BaseDaemon.h:105
BaseDaemon()
Default constructor of BaseDaemon.
StatAccumulator createNewStat(size_t nbBin, float histLowerBound, float histUpperBound)
Create a new clear computing statistic.
std::map< size_t, Message > p_mapMessageToBeConfirmed
Map of messages which have to be confirmed by destination Daemon.
Definition BaseDaemon.h:184
const std::map< size_t, Message > & getMapMessageToBeConfirmed() const
Get the map of message to be confirmed.
void processConfirmedMessage(size_t id, time_t currentTime)
Process confirmed message.
virtual ~BaseDaemon()
Destructor of BaseDaemon.
bool parseArgument(int argc, char **argv)
Parse arguments given to the BaseDaemon with command line.
Describe a Daemon of the Swarm.
Statistics of a Daemon.
Function which can be called in a Daemon.
Function which can be called in a Daemon.
Basic Data exchanged in the swarm.
Function which can be called in a Daemon.
Message exchanged by Daemons.
Accumulator of event occurence to build swarm statistics over a time period.
General statistics in the swarm.
ConfigFormat
Type of the Message which can be exchanged by Daemon.
std::map< PString, DaemonConfig > MapDaemonConfig
Main Daemon configuration which drives timeouts and flags of send and recv calls.
Definition BaseDaemon.h:42
PSendFlag::PSendFlag sendFlag
Send flag to be used for every send of the Daemon.
Definition BaseDaemon.h:50
int sendTimeoutMs
Send time out in miliseconds.
Definition BaseDaemon.h:46
int recvTimeoutMs
Recv time out in miliseconds.
Definition BaseDaemon.h:44
PRecvFlag::PRecvFlag recvFlag
Recv flag to be used for every recv of the Daemon.
Definition BaseDaemon.h:48