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