GCC Code Coverage Report


Directory: ./
File: src/PGenericSocket_impl.h
Date: 2025-12-02 14:33:23
Exec Total Coverage
Lines: 61 75 81.3%
Functions: 11 12 91.7%
Branches: 16 28 57.1%

Line Branch Exec Source
1 /***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5 ****************************************/
6
7 #ifndef __PGENERIC_SOCKET_H_IMPL__
8 #define __PGENERIC_SOCKET_H_IMPL__
9
10 #include "PGenericSocket.h"
11
12 ///Default constructor of PGenericSocket
13 /** @param mode : Mode of the Socket (no mock, mock, mock_record)
14 */
15 template<typename _TBackend, typename _TMockBackend>
16
1/1
✓ Branch 0 (3→4) taken 4 times.
4 PGenericSocket<_TBackend, _TMockBackend>::PGenericSocket(PSocketMode::PSocketMode mode){
17 4 initialisationPGenericSocket(mode);
18 4 }
19
20 ///Destructor of PGenericSocket
21 template<typename _TBackend, typename _TMockBackend>
22 8 PGenericSocket<_TBackend, _TMockBackend>::~PGenericSocket(){
23 4 close();
24 8 }
25
26 ///Create a client socket
27 /** @param backend : instanciated backend of the socket
28 * @param mockBackend : instanciated backend of the mock socket
29 * @param socketParam : parameters of the socket (host, port, send/recv timeout)
30 * @param param : extra parameters of the backend
31 * @param mockPrefix : prefix where to find/save the mock
32 * @param mockParam : extra parameters of the mock
33 */
34 template<typename _TBackend, typename _TMockBackend>
35 2 bool PGenericSocket<_TBackend, _TMockBackend>::createClientSocket(_TBackend & backend, _TMockBackend & mockBackend, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam){
36 2 bool b(true);
37 2 b &= mockBackend.createClientSocket(p_mockSocket, socketParam, mockParam);
38 2 b &= backend.createClientSocket(p_socket, socketParam, param);
39 2 p_mockSocket.setMockPrefix(mockPrefix);
40 2 p_mockSocket.setIsMockRecord(p_mode == PSocketMode::MOCK_RECORD);
41 2 return b;
42 }
43
44 ///Create a server socket
45 /** @param backend : instanciated backend of the socket
46 * @param mockBackend : instanciated backend of the mock socket
47 * @param socketParam : parameters of the socket (host, port, send/recv timeout)
48 * @param param : extra parameters of the backend
49 * @param mockPrefix : prefix where to find/save the mock
50 * @param mockParam : extra parameters of the mock
51 */
52 template<typename _TBackend, typename _TMockBackend>
53 2 bool PGenericSocket<_TBackend, _TMockBackend>::createServerSocket(_TBackend & backend, _TMockBackend & mockBackend, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam){
54 2 bool b(true);
55 2 b &= mockBackend.createServerSocket(p_mockSocket, socketParam, mockParam);
56 2 b &= backend.createServerSocket(p_socket, socketParam, param);
57 2 p_mockSocket.setMockPrefix(mockPrefix);
58 2 p_mockSocket.setIsMockRecord(p_mode == PSocketMode::MOCK_RECORD);
59 2 return b;
60 }
61
62 ///Set the mode of the generic socket
63 /** @param mode : mode of the PGenericSocket (NO_MOCK, MOCK, MOCK_RECORD)
64 */
65 template<typename _TBackend, typename _TMockBackend>
66 void PGenericSocket<_TBackend, _TMockBackend>::setMode(PSocketMode::PSocketMode mode){
67 p_mode = mode;
68 p_mockSocket.setIsMockRecord(mode == PSocketMode::MOCK_RECORD);
69 }
70
71 ///Send message on the given socket
72 /** @param msg : message to be sent
73 * @param flag : flags to be used to send the message (BLOCK, NON_BLOCK, etc)
74 * @return PSendStatus::PSendStatus
75 */
76 template<typename _TBackend, typename _TMockBackend>
77 10 PSendStatus::PSendStatus PGenericSocket<_TBackend, _TMockBackend>::sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag){
78 10 PSendStatus::PSendStatus sendStatus = PSendStatus::OK;
79 //This is normal to call the mock backend on a send, because it will check if the send message is the expected one
80 //by respected to the recorded mock
81
1/2
✓ Branch 0 (2→3) taken 10 times.
✗ Branch 1 (2→8) not taken.
10 if(p_mode != PSocketMode::NO_MOCK){
82 10 typename _TMockBackend::Message msgMock;
83
1/1
✓ Branch 0 (4→5) taken 10 times.
10 _TBackend::msgToMock(msgMock, msg);
84
1/1
✓ Branch 0 (5→6) taken 10 times.
10 sendStatus = p_mockSocket.sendMsg(msgMock, flag);
85 10 }
86
87 // If we dont test if b is true, we will always send the message even if the mock backend is not connected
88 // I don't know if it is a good idea to do so
89 // If we do that, we might have somme issues if the user tries to end a message. The mock may give an error that will not be catch
90 // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
91
92
2/4
✓ Branch 0 (8→9) taken 10 times.
✗ Branch 1 (8→12) not taken.
✓ Branch 2 (9→10) taken 10 times.
✗ Branch 3 (9→12) not taken.
10 if(p_mode != PSocketMode::MOCK && sendStatus == PSendStatus::OK){
93 10 sendStatus = p_socket.sendMsg(msg, flag);
94 }
95 10 return sendStatus;
96 }
97
98 ///Receive message from the given socket
99 /** @param msg : message to be received
100 * @param flag : flags to be used to send the message (BLOCK, NON_BLOCK, etc)
101 * @return PRecvStatus::PRecvStatus
102 */
103 template<typename _TBackend, typename _TMockBackend>
104 10 PRecvStatus::PRecvStatus PGenericSocket<_TBackend, _TMockBackend>::recvMsg(typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag){
105 10 PRecvStatus::PRecvStatus recvStatus = PRecvStatus::OK;
106
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 10 times.
10 if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
107 recvStatus = p_socket.recvMsg(msg, flag);
108
1/2
✓ Branch 0 (5→6) taken 10 times.
✗ Branch 1 (5→11) not taken.
10 }else if(p_mode == PSocketMode::MOCK){ //Mock mode
109 10 typename _TMockBackend::Message msgMock;
110
1/1
✓ Branch 0 (7→8) taken 10 times.
10 recvStatus = p_mockSocket.recvMsg(msgMock, flag);
111
1/1
✓ Branch 0 (8→9) taken 10 times.
10 _TBackend::mockToMsg(msg, msgMock);
112 10 }else{ //Mock record mode
113 recvStatus = p_socket.recvMsg(msg, flag);
114 if(recvStatus == PRecvStatus::OK){
115 typename _TMockBackend::Message msgMock;
116 _TBackend::msgToMock(msgMock, msg);
117 recvStatus = p_mockSocket.recvMsg(msgMock, flag);
118 }else{
119 typename _TMockBackend::Message emptyMsg;
120 recvStatus = p_mockSocket.recvMsg(emptyMsg, flag);
121 }
122 }
123 10 return recvStatus;
124 }
125
126 ///Close the socket
127 template<typename _TBackend, typename _TMockBackend>
128 8 void PGenericSocket<_TBackend, _TMockBackend>::close(){
129 8 p_mockSocket.close();
130 8 p_socket.close();
131 8 }
132
133 ///Say if the Socket is connected
134 /** @return true if the current socket is connected (or both in record mode)
135 */
136 template<typename _TBackend, typename _TMockBackend>
137 4 bool PGenericSocket<_TBackend, _TMockBackend>::isConnected() const{
138 4 bool b(true);
139
1/2
✓ Branch 0 (2→3) taken 4 times.
✗ Branch 1 (2→5) not taken.
4 if(p_mode != PSocketMode::NO_MOCK){
140 4 b &= p_mockSocket.isConnected();
141 }
142
2/2
✓ Branch 0 (5→6) taken 3 times.
✓ Branch 1 (5→8) taken 1 times.
4 if(p_mode != PSocketMode::MOCK){
143 3 b &= p_socket.isConnected();
144 }
145 4 return b;
146 }
147
148 /// @brief Wait until the socket is connected
149 /// @param refreshTimer : time in µs between two checks
150 /// @param nbRetry : number of retries before giving up
151 /// @return true if the socket is connected, false otherwise
152 template<typename _TBackend, typename _TMockBackend>
153 2 bool PGenericSocket<_TBackend, _TMockBackend>::waitUntilConnection(size_t refreshTimer, size_t nbRetry) const {
154 2 size_t counter(0);
155 2 bool b(false);
156
3/4
✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 2 times.
✓ Branch 2 (7→3) taken 2 times.
✗ Branch 3 (7→8) not taken.
4 while(!b && counter < nbRetry) {
157 2 b = isConnected();
158 //FIXME : use the clock backend instead of usleep
159 2 usleep(refreshTimer);
160 2 ++counter;
161 }
162 2 return b;
163 }
164
165 ///Initialisation function of the class PGenericSocket
166 /** @param mode : Mode of the Socket (no mock, mock, mock_record)
167 */
168 template<typename _TBackend, typename _TMockBackend>
169 4 void PGenericSocket<_TBackend, _TMockBackend>::initialisationPGenericSocket(PSocketMode::PSocketMode mode){
170 4 p_mode = mode;
171 4 }
172
173
174 #endif
175
176
177
178