Directory: | ./ |
---|---|
File: | src/Daemon/Daemon_impl.h |
Date: | 2025-03-27 14:50:11 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 70 | 80 | 87.5% |
Branches: | 89 | 134 | 66.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #ifndef __DAEMON_H_IMPL__ | ||
8 | #define __DAEMON_H_IMPL__ | ||
9 | |||
10 | #include "phoenix_assert.h" | ||
11 | #include "Daemon.h" | ||
12 | |||
13 | ///Default constructor of Daemon | ||
14 | template<class _TBackend> | ||
15 | 8 | Daemon<_TBackend>::Daemon() | |
16 |
1/1✓ Branch 2 taken 8 times.
|
8 | :BaseDaemon() |
17 | { | ||
18 | 8 | initialisationDaemon(); | |
19 | 8 | } | |
20 | |||
21 | ///Destructor of Daemon | ||
22 | template<class _TBackend> | ||
23 | 16 | Daemon<_TBackend>::~Daemon(){ | |
24 | |||
25 | } | ||
26 | |||
27 | ///Set the mode of the sockets of the SocketManager | ||
28 | /** @param mode : mode of the socket manager (NO_MOCK, MOCK, MOCK_RECORD) | ||
29 | */ | ||
30 | template<class _TBackend> | ||
31 | 8 | void Daemon<_TBackend>::setSocketMode(PSocketMode::PSocketMode mode){ | |
32 | 8 | p_backend.socketManager.setMode(mode); | |
33 | 8 | } | |
34 | |||
35 | ///Set the mode of the clock | ||
36 | /** @param mode : mode of the clock (NO_MOCK, MOCK, MOCK_RECORD) | ||
37 | */ | ||
38 | template<class _TBackend> | ||
39 | 8 | void Daemon<_TBackend>::setClockMode(PClockMode::PClockMode mode){ | |
40 | 8 | p_backend.clock.setMode(mode); | |
41 | 8 | } | |
42 | |||
43 | ///Run the Daemon | ||
44 | /** @return true if the run was a success, false otherwise | ||
45 | */ | ||
46 | template<class _TBackend> | ||
47 | 6 | bool Daemon<_TBackend>::run(){ | |
48 | 6 | p_isRun = true; | |
49 | //Let's initialise all connexions | ||
50 | 6 | initialisationDaemonSocket(); | |
51 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 6 times.
|
30 | while(p_isRun){ |
52 | //Let's get the last recieved message | ||
53 | //If there is not message, we treat the rest of the loop | ||
54 |
1/1✓ Branch 1 taken 12 times.
|
12 | Message message; |
55 |
4/4✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 4 times.
|
12 | if(p_backend.socketManager.recvData("pull", message, PRecvFlag::NON_BLOCK)){ |
56 |
1/1✓ Branch 1 taken 8 times.
|
8 | processInputMessage(message); |
57 | } | ||
58 |
1/1✓ Branch 1 taken 12 times.
|
12 | extraLoopProcessing(); |
59 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
12 | checkMesageTimeout(p_backend.clock.now()); |
60 | } | ||
61 | //Let's execute a last command before stop | ||
62 | 6 | executeOnStop(); | |
63 | 6 | return true; | |
64 | } | ||
65 | |||
66 | ///Recieve data from other Daemon | ||
67 | /** @param senderName : name of the sender of the data | ||
68 | * @param data : sent data | ||
69 | * @return true on success, false otherwise (maybe recieved method does not exist) | ||
70 | */ | ||
71 | template<class _TBackend> | ||
72 | bool Daemon<_TBackend>::recieveData(const PString & senderName, const Data & data){ | ||
73 | //Let's find in the map of reciver methods which corresponds to the given Data type | ||
74 | |||
75 | //And then, let's call it | ||
76 | |||
77 | return false; | ||
78 | } | ||
79 | |||
80 | ///Send message to other Daemon | ||
81 | /** @param message : message to be sent | ||
82 | * @return true on success, false otherwise (maybe recieved method does not exist) | ||
83 | */ | ||
84 | template<class _TBackend> | ||
85 | 2 | bool Daemon<_TBackend>::sendMessage(const Message & message){ | |
86 | 2 | bool b(true); | |
87 | 2 | const PVecString & vecDestination = message.getVecRecver(); | |
88 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | for(PVecString::const_iterator it(vecDestination.begin()); it != vecDestination.end(); ++it){ |
89 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | b &= sendMessage(*it, message); |
90 | } | ||
91 | 2 | return b; | |
92 | } | ||
93 | |||
94 | ///Send message to other Daemon | ||
95 | /** @param destinationName : name of the destination Daemon of the message | ||
96 | * @param message : message to be sent | ||
97 | * @return true on success, false otherwise (maybe recieved method does not exist) | ||
98 | */ | ||
99 | template<class _TBackend> | ||
100 | 5 | bool Daemon<_TBackend>::sendMessage(const PString & destinationName, const Message & message){ | |
101 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
|
5 | if(message.getIsConfirmationNeeded()){ |
102 | 3 | addMessageToConfirm(message); | |
103 | } | ||
104 | 5 | bool b(p_backend.socketManager.sendData(destinationName, message, PSendFlag::NON_BLOCK)); | |
105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if(!b){ |
106 | ✗ | getLog().getLogError() << "Daemon<_TBackend>::sendMessage : cannot send message to Daemon '"<<destinationName<<"'" << std::endl; | |
107 | } | ||
108 | 5 | return b; | |
109 | } | ||
110 | |||
111 | ///Send data to other Daemon (specialization for Data) | ||
112 | /** @param destinationName : name of the destination Daemon of the data | ||
113 | * @param data : data to be sent | ||
114 | * @param isConfirmationNeeded : true if a confirmation is needed | ||
115 | * @return true on success, false otherwise (maybe recieved method does not exist) | ||
116 | */ | ||
117 | template<class _TBackend> | ||
118 | 3 | bool Daemon<_TBackend>::sendData(const PString & destinationName, const Data & data, bool isConfirmationNeeded){ | |
119 |
1/1✓ Branch 1 taken 3 times.
|
3 | Message message; |
120 | //TODO : I think we have to call chrono and now clock but I do not have time for this now | ||
121 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | message.setSendTime(p_backend.clock.now()); |
122 |
1/1✓ Branch 1 taken 3 times.
|
3 | message.setData(data); |
123 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | message.setId(getMessageId()); |
124 |
1/1✓ Branch 1 taken 3 times.
|
3 | message.setIsConfirmationNeeded(isConfirmationNeeded); |
125 |
1/1✓ Branch 1 taken 3 times.
|
3 | message.setType(MessageType::RESULT_DATA); |
126 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | message.setSender(p_config.getName()); |
127 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | message.getVecRecver().push_back(destinationName); |
128 |
1/1✓ Branch 1 taken 3 times.
|
6 | return sendMessage(destinationName, message); |
129 | 3 | } | |
130 | |||
131 | ///Computing Method for each event loop (when recieving message from other Daemon) | ||
132 | template<class _TBackend> | ||
133 | 3 | void Daemon<_TBackend>::extraLoopProcessing(){} | |
134 | |||
135 | ///Method which is called on stop of the Daemon | ||
136 | template<class _TBackend> | ||
137 | 6 | void Daemon<_TBackend>::executeOnStop(){} | |
138 | |||
139 | ///Initialisation function of the class Daemon | ||
140 | template<class _TBackend> | ||
141 | 8 | void Daemon<_TBackend>::initialisationDaemon(){ | |
142 | |||
143 | 8 | } | |
144 | |||
145 | ///Initialise the Daemon Sockets | ||
146 | template<class _TBackend> | ||
147 | 6 | void Daemon<_TBackend>::initialisationDaemonSocket(){ | |
148 | //Let's send when we are a client and recv when we are a server | ||
149 | //By doing this way, we have one server per Daemon and as many clients as other Daemon | ||
150 | 6 | getLog().getLogInfo() << "Daemon<_TBackend>::initialisationDaemonSocket() : initialise pull socket" << std::endl; | |
151 |
13/13✓ Branch 2 taken 6 times.
✓ Branch 6 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 14 taken 6 times.
✓ Branch 18 taken 6 times.
✓ Branch 21 taken 6 times.
✓ Branch 24 taken 6 times.
✓ Branch 27 taken 6 times.
✓ Branch 30 taken 6 times.
✓ Branch 33 taken 6 times.
✓ Branch 36 taken 6 times.
✓ Branch 39 taken 6 times.
✓ Branch 42 taken 6 times.
|
6 | phoenix_assert(p_backend.socketManager.addServerSocket("pull", |
152 | _TBackend::SocketBackend::server(p_config.getHostName(), p_config.getRecievingPort()), | ||
153 | _TBackend::SocketMock::server(p_config.getHostName(), p_config.getRecievingPort(), p_backend.socketManager.getMode() == PSocketMode::MOCK_RECORD, "./"))); | ||
154 | |||
155 | //Now let's initialise connexions to other | ||
156 |
2/2✓ Branch 3 taken 11 times.
✓ Branch 4 taken 6 times.
|
17 | for(MapDaemonConfig::iterator it(p_mapDaemon.begin()); it != p_mapDaemon.end(); ++it){ |
157 |
7/7✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 11 taken 11 times.
✓ Branch 14 taken 11 times.
✓ Branch 17 taken 11 times.
✓ Branch 20 taken 11 times.
|
11 | getLog().getLogInfo() << "Daemon<_TBackend>::initialisationDaemonSocket() : initialise send socket for neighbour Daemon '"<<it->second.getName()<<"'" << std::endl; |
158 |
13/13✓ Branch 2 taken 11 times.
✓ Branch 6 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 14 taken 11 times.
✓ Branch 19 taken 11 times.
✓ Branch 23 taken 11 times.
✓ Branch 26 taken 11 times.
✓ Branch 30 taken 11 times.
✓ Branch 34 taken 11 times.
✓ Branch 37 taken 11 times.
✓ Branch 41 taken 11 times.
✓ Branch 44 taken 11 times.
✓ Branch 47 taken 11 times.
|
11 | phoenix_assert(p_backend.socketManager.addClientSocket(it->second.getName(), |
159 | _TBackend::SocketBackend::client(it->second.getHostName(), it->second.getRecievingPort()), | ||
160 | _TBackend::SocketMock::client(it->second.getHostName(), it->second.getRecievingPort(), p_backend.socketManager.getMode() == PSocketMode::MOCK_RECORD, "./"))); | ||
161 | } | ||
162 | 6 | } | |
163 | |||
164 | ///Process an input message | ||
165 | /** @param[out] message : recieved Message to be processed | ||
166 | */ | ||
167 | template<class _TBackend> | ||
168 | 8 | void Daemon<_TBackend>::processInputMessage(Message & message){ | |
169 | //Check is the message needs a confirmation | ||
170 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if(message.getIsConfirmationNeeded()){ |
171 | ✗ | Message confirmation; | |
172 | //We do not need to change the time of the message because it has to be in the clock reference of the sender | ||
173 | ✗ | confirmation.setSendTime(message.getSendTime()); | |
174 | ✗ | confirmation.setId(message.getId()); | |
175 | ✗ | confirmation.setSender(p_config.getName()); | |
176 | ✗ | confirmation.setType(MessageType::MESSAGE_CONFIRMATION); | |
177 | ✗ | confirmation.getVecRecver().push_back(message.getSender()); | |
178 | ✗ | sendMessage(confirmation); | |
179 | } | ||
180 | |||
181 | //Let's check is we recieve a stop message | ||
182 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
|
8 | if(message.getType() == MessageType::STOP){stop();} |
183 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | else if(message.getType() == MessageType::PING){ |
184 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | Message pong; |
185 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | pong.setSendTime(message.getSendTime()); |
186 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | pong.setId(message.getId()); |
187 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | pong.setSender(p_config.getName()); |
188 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | pong.setType(MessageType::PONG); |
189 |
3/6✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
1 | pong.getVecRecver().push_back(message.getSender()); // change the assert to have correct comportement (log) |
190 |
5/10✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
|
1 | phoenix_assert(sendMessage(pong)); // TODO remove the assert |
191 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | }else if(message.getType() == MessageType::MESSAGE_CONFIRMATION){ |
192 | ✗ | removeConfirmedMessage(message.getId(), p_backend.clock.now()); | |
193 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | }else if(message.getType() == MessageType::RESULT_DATA){ |
194 | ✗ | processData(message.getData()); | |
195 | } | ||
196 | 8 | } | |
197 | |||
198 | |||
199 | #endif | ||
200 | |||
201 | |||
202 | |||
203 |