7#ifndef __DAEMON_H_IMPL__
8#define __DAEMON_H_IMPL__
10#include "phoenix_assert.h"
16template<
class _TBackend>
24template<
class _TBackend>
32template<
class _TBackend>
36 case PSocketMode::MOCK:
40 case PSocketMode::MOCK_RECORD:
54template<
class _TBackend>
58 case PClockMode::MOCK:
62 case PClockMode::MOCK_RECORD:
76template<
class _TBackend>
78 if(recvStatus == PRecvStatus::SOCKET_NOT_AVAILABLE){
80 p_log.getLogWarning() <<
"Daemon::checkRecvStatus : Socket not available" << std::endl;
82 if(recvStatus == PRecvStatus::SIGNAL_INTERRUPTION){
85 if(recvStatus == PRecvStatus::BROKEN_BACKEND){
88 if(recvStatus == PRecvStatus::BROKEN_SOCKET){
91 if(recvStatus == PRecvStatus::CANNOT_DESERIALIZE_DATA){
101template<
class _TBackend>
103 if(sendStatus == PSendStatus::SOCKET_NOT_AVAILABLE){
106 if(sendStatus == PSendStatus::NO_ROUTE_TO_RECEIVER){
109 if(sendStatus == PSendStatus::SIGNAL_INTERRUPTION){
110 p_log.criticalAndThrow<
Phoenix::SocketStatusException>(
"Daemon::checkSendStatus : Socket Backend caught a signal, aborting.",
"Socket Backend caught a signal, aborting.");
112 if(sendStatus == PSendStatus::BROKEN_BACKEND){
115 if(sendStatus == PSendStatus::BROKEN_SOCKET){
118 if(sendStatus == PSendStatus::CANNOT_SERIALIZE_DATA){
126template<
class _TBackend>
139 if(recvStatus == PRecvStatus::OK){
144 time_t currentTime =
p_backend.clock.now();
159template<
class _TBackend>
162 const PVecString & vecDestination = message.
getVecRecver();
163 for(PVecString::const_iterator it(vecDestination.begin()); it != vecDestination.end(); ++it){
174template<
class _TBackend>
189template<
class _TBackend>
203template<
class _TBackend>
207template<
class _TBackend>
211template<
class _TBackend>
217template<
class _TBackend>
233 getLog().getLogInfo() <<
"Daemon<_TBackend>::initialisationDaemonSocket() : initialise pull socket for daemon '" <<
p_config.getName() <<
"'" << std::endl;
234 phoenix_assert(
p_backend.socketManager.addServerSocket(
"pull",
235 PSocketParam{p_config.getHostName(), p_config.getReceivingPort(), p_mainConfig.recvTimeoutMs, p_mainConfig.sendTimeoutMs},
p_backend.extraServerParam,
240 if(it->second.getName() ==
p_config.getName()){
continue;}
241 getLog().getLogInfo() <<
"Daemon<_TBackend>::initialisationDaemonSocket() : initialise send socket for neighbour Daemon '"<<it->second.getName()<<
"'" << std::endl;
242 phoenix_assert(
p_backend.socketManager.addClientSocket(it->second.getName(),
243 PSocketParam{it->second.getHostName(), it->second.getReceivingPort(), p_mainConfig.recvTimeoutMs, p_mainConfig.sendTimeoutMs},
p_backend.extraClientParam,
247 getLog().getLogInfo() <<
"Daemon<_TBackend>::initialisationDaemonSocket() : all connection to neighbours Daemon initialised" << std::endl;
254template<
class _TBackend>
274 if(function == NULL){
277 time_t functionStartTime =
p_backend.clock.now();
279 getLog().getLogError() <<
"Daemon<_TBackend>::processInputMessage : cannot process data of type '"<<message.
getData().
getType()<<
"' from Daemon '"<<message.
getSender()<<
"'" << std::endl;
282 time_t functionEllapsedTime =
p_backend.clock.now() - functionStartTime;
283 getLog().getLogDebug() <<
"Daemon<_TBackend>::processInputMessage : function '"<<message.
getData().
getType()<<
"' processed data from daemon '"<<message.
getSender()<<
"' in "<<functionEllapsedTime<<
" nanoseconds" << std::endl;
293template<
class _TBackend>
302 getLog().getLogDebug() <<
"Daemon::sendStatToStatDaemon() : sending statistics to Stat Daemon '"<<
p_config.getStatDaemonName()<<
"'" << std::endl;
Exception for daemon configuration errors.
Exception for socket status errors.
Abstract function definition which will be callable in Daemon.
virtual bool call(PLog &log, const Swarm::Data &data)=0
bool p_isRun
True if the current BaseDaemon is running.
void updateStatAccumulator(StatAccumulator &stat, float value)
Update a computing statistic with a new value.
void addMessageToConfirm(const Swarm::Message &message)
Add a message to confirm.
bool p_isFullMock
True if the daemon has to be executed in mock mode for socket and clock.
void fillDaemonStat(Swarm::Stat &stat, time_t startTimestamp, time_t endTimestamp)
Fill the Stat with the current statistics of the daemon.
PLog p_log
Logger of the current Daemon.
PLog & getLog()
Get the log of the current BaseDaemon.
void clearStat()
Clear all the statistics of the daemon.
void stop()
Stops the BaseDaemon.
void checkMessageTimeout(time_t currentTime)
Check if a message has reached the timeout.
size_t getMessageId()
Get current message id.
bool p_isFullMockRecord
True if the daemon has to be executed in mock record mode for socket and clock.
BaseDaemon()
Default constructor of BaseDaemon.
DaemonConfig p_config
Configuration of the curent Daemon.
Swarm::AbstractDataFunction * getDataFunction(const Swarm::Data &data)
Get the data function associated with the given data.
MapDaemonConfig p_mapDaemon
Map of the other Daemon of the Swarm.
void processConfirmedMessage(size_t id, time_t currentTime)
Process confirmed message.
DaemonMainConfig p_mainConfig
Main configuration of the Daemon.
virtual void extraLoopProcessing()
Computing Method for each event loop (when receiving message from other Daemon)
bool sendMessage(const Swarm::Message &message)
Send message to other Daemon.
void initialisationDaemonSocket()
Initialise the Daemon Sockets.
Daemon()
Default constructor of Daemon.
virtual void executeOnStop()
Method which is called on stop of the Daemon.
bool sendData(const PString &destinationName, const Swarm::Data &data, bool isConfirmationNeeded)
Send data to other Daemon (specialization for Data)
void setClockMode(PClockMode::PClockMode mode)
Set the mode of the clock.
void initialisationDaemon()
Initialisation function of the class Daemon.
void setSocketMode(PSocketMode::PSocketMode mode)
Set the mode of the sockets of the SocketManager.
void checkSendStatus(PSendStatus::PSendStatus sendStatus)
Check the status of recv sockets.
PTimer p_statTimer
Timer to send statistics to the Stat Daemon.
void processInputMessage(Swarm::Message &message)
Process an input message.
PRecvStatus::PRecvStatus checkRecvStatus(PRecvStatus::PRecvStatus recvStatus)
Check the status of recv sockets.
bool sendValue(const PString &destinationName, const T &data, bool isConfirmationNeeded=true)
Send data to other Daemon.
_TBackend p_backend
Full backend of the Daemon (for Socket and clock)
time_t p_startTimestamp
Start timestamp of the current accumulation of statistics.
bool run()
Run the Daemon.
virtual ~Daemon()
Destructor of Daemon.
void sendStatToStatDaemon(time_t currentTime)
Send the statistics of the Daemon to the Stat Daemon.
Basic Data exchanged in the swarm.
const PString & getType() const
Gets the type of the Data.
Message exchanged by Daemons.
const PString & getSender() const
Gets the sender of the Message.
void setData(const Swarm::Data &data)
Sets the data of the Message.
void setType(const MessageType::MessageType &type)
Sets the type of the Message.
const std::vector< PString > & getVecRecver() const
Gets the vecRecver of the Message.
void setSendTime(const time_t &sendTime)
Sets the sendTime of the Message.
const Swarm::Data & getData() const
Gets the data of the Message.
void setSender(const PString &sender)
Sets the sender of the Message.
size_t getId() const
Gets the id of the Message.
bool getIsConfirmationNeeded() const
Gets the isConfirmationNeeded of the Message.
void setIsConfirmationNeeded(bool isConfirmationNeeded)
Sets the isConfirmationNeeded of the Message.
void setId(size_t id)
Sets the id of the Message.
const MessageType::MessageType & getType() const
Gets the type of the Message.
const time_t & getSendTime() const
Gets the sendTime of the Message.
void setName(const PString &name)
Sets the name of the Stat.