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 | 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 param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
28 | * @param mockParam : parameters of mock backend | ||
29 | * @return true if the socket has been created, false otherwise | ||
30 | */ | ||
31 | template<typename _TBackend, typename _TMockBackend> | ||
32 | bool PGenericSocket<_TBackend, _TMockBackend>::createClientSocket(const typename _TBackend::Param & param, const typename _TMockBackend::Param & mockParam){ | ||
33 | bool b(true); | ||
34 | if(p_mode != PSocketMode::NO_MOCK){ | ||
35 | b &= _TMockBackend::createClientSocket(p_mockSocket, mockParam); | ||
36 | } | ||
37 | if(p_mode != PSocketMode::MOCK){ | ||
38 | b &= _TBackend::createClientSocket(p_socket, param); | ||
39 | } | ||
40 | return b; | ||
41 | } | ||
42 | |||
43 | ///Create a server socket | ||
44 | /** @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
45 | * @param mockParam : parameters of mock backend | ||
46 | * @return true if the socket has been created, false otherwise | ||
47 | */ | ||
48 | template<typename _TBackend, typename _TMockBackend> | ||
49 | bool PGenericSocket<_TBackend, _TMockBackend>::createServerSocket(const typename _TBackend::Param & param, const typename _TMockBackend::Param & mockParam){ | ||
50 | bool b(true); | ||
51 | if(p_mode != PSocketMode::NO_MOCK){ | ||
52 | b &= _TMockBackend::createServerSocket(p_mockSocket, mockParam); | ||
53 | } | ||
54 | if(p_mode != PSocketMode::MOCK){ | ||
55 | b &= _TBackend::createServerSocket(p_socket, param); | ||
56 | } | ||
57 | return b; | ||
58 | } | ||
59 | |||
60 | ///Create a client socket | ||
61 | /** @param host : connection host | ||
62 | * @param port : connection port | ||
63 | * @param param : extra parameters of the backend | ||
64 | * @param mockPrefix : prefix where to find/save the mock | ||
65 | * @param mockParam : extra parameters of the mock | ||
66 | */ | ||
67 | template<typename _TBackend, typename _TMockBackend> | ||
68 | 2 | bool PGenericSocket<_TBackend, _TMockBackend>::createClientSocket(const std::string & host, size_t port, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam){ | |
69 | 2 | bool b(true); | |
70 | 2 | _TMockBackend::setMockPrefix(p_mockSocket, mockPrefix); | |
71 | 2 | _TMockBackend::setIsMockRecord(p_mockSocket, p_mode == PSocketMode::MOCK_RECORD); | |
72 | 2 | b &= _TMockBackend::createClientSocket(p_mockSocket, host, port, mockParam); | |
73 | 2 | b &= _TBackend::createClientSocket(p_socket, host, port, param); | |
74 | |||
75 | 2 | return b; | |
76 | } | ||
77 | |||
78 | ///Create a server socket | ||
79 | /** @param host : connection host | ||
80 | * @param port : connection port | ||
81 | * @param param : extra parameters of the backend | ||
82 | * @param mockPrefix : prefix where to find/save the mock | ||
83 | * @param mockParam : extra parameters of the mock | ||
84 | */ | ||
85 | template<typename _TBackend, typename _TMockBackend> | ||
86 | 2 | bool PGenericSocket<_TBackend, _TMockBackend>::createServerSocket(const std::string & host, size_t port, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam){ | |
87 | 2 | bool b(true); | |
88 | 2 | _TMockBackend::setMockPrefix(p_mockSocket, mockPrefix); | |
89 | 2 | _TMockBackend::setIsMockRecord(p_mockSocket, p_mode == PSocketMode::MOCK_RECORD); | |
90 | 2 | b &= _TMockBackend::createServerSocket(p_mockSocket, host, port, mockParam); | |
91 | 2 | b &= _TBackend::createServerSocket(p_socket, host, port, param); | |
92 | |||
93 | 2 | return b; | |
94 | } | ||
95 | |||
96 | ///Set the mode of the generic socket | ||
97 | /** @param mode : mode of the PGenericSocket (NO_MOCK, MOCK, MOCK_RECORD) | ||
98 | */ | ||
99 | template<typename _TBackend, typename _TMockBackend> | ||
100 | ✗ | void PGenericSocket<_TBackend, _TMockBackend>::setMode(PSocketMode::PSocketMode mode){ | |
101 | ✗ | p_mode = mode; | |
102 | ✗ | _TMockBackend::setIsMockRecord(p_mockSocket, mode == PSocketMode::MOCK_RECORD); | |
103 | ✗ | } | |
104 | |||
105 | ///Send message on the given socket | ||
106 | /** @param msg : message to be sent | ||
107 | * @param flag : flags to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
108 | * @return true on success, false otherwise | ||
109 | */ | ||
110 | template<typename _TBackend, typename _TMockBackend> | ||
111 | 10 | bool PGenericSocket<_TBackend, _TMockBackend>::sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag){ | |
112 | 10 | bool b(true); | |
113 | //This is normal to call the mock backend on a send, because it will check if the send message is the expected one | ||
114 | //by respected to the recorded mock | ||
115 |
1/2✓ Branch 0 (2→3) taken 10 times.
✗ Branch 1 (2→8) not taken.
|
10 | if(p_mode != PSocketMode::NO_MOCK){ |
116 | 10 | typename _TMockBackend::Message vecTmp; | |
117 |
1/1✓ Branch 0 (4→5) taken 10 times.
|
10 | _TBackend::msgToMock(vecTmp, msg); |
118 |
1/1✓ Branch 0 (5→6) taken 10 times.
|
10 | b &= _TMockBackend::send(p_mockSocket, vecTmp, flag); |
119 | 10 | } | |
120 | |||
121 | // If we dont test if b is true, we will always send the message even if the mock backend is not connected | ||
122 | // I don't know if it is a good idea to do so | ||
123 | // 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 | ||
124 | // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not.... | ||
125 | |||
126 |
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 && b){ |
127 | 10 | b &= _TBackend::send(p_socket, msg, flag); | |
128 | } | ||
129 | 10 | return b; | |
130 | } | ||
131 | |||
132 | ///Recieve message from the given socket | ||
133 | /** @param msg : message to be recieved | ||
134 | * @param flag : flags to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
135 | * @return true on success, false otherwise | ||
136 | */ | ||
137 | template<typename _TBackend, typename _TMockBackend> | ||
138 | 10 | bool PGenericSocket<_TBackend, _TMockBackend>::recvMsg(typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag){ | |
139 | 10 | bool b(true); | |
140 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 10 times.
|
10 | if(p_mode == PSocketMode::NO_MOCK){ //Normal mode |
141 | ✗ | b &= _TBackend::recv(p_socket, msg, flag); | |
142 |
1/2✓ Branch 0 (5→6) taken 10 times.
✗ Branch 1 (5→12) not taken.
|
10 | }else if(p_mode == PSocketMode::MOCK){ //Mock mode |
143 | 10 | typename _TMockBackend::Message msgMock; | |
144 |
1/1✓ Branch 0 (7→8) taken 10 times.
|
10 | b &= _TMockBackend::recv(p_mockSocket, msgMock, flag); |
145 |
1/2✓ Branch 0 (8→9) taken 10 times.
✗ Branch 1 (8→10) not taken.
|
10 | if(b){ |
146 |
1/1✓ Branch 0 (9→10) taken 10 times.
|
10 | _TBackend::mockToMsg(msg, msgMock); |
147 | } | ||
148 | 10 | }else{ //Mock record mode | |
149 | ✗ | b &= _TBackend::recv(p_socket, msg, flag); | |
150 | ✗ | if(b){ | |
151 | ✗ | typename _TMockBackend::Message msgMock; | |
152 | ✗ | _TBackend::msgToMock(msgMock, msg); | |
153 | ✗ | b &= _TMockBackend::recv(p_mockSocket, msgMock, flag); | |
154 | ✗ | } | |
155 | else { | ||
156 | ✗ | typename _TMockBackend::Message empty_msg; | |
157 | ✗ | b &= _TMockBackend::recv(p_mockSocket, empty_msg, flag); | |
158 | ✗ | } | |
159 | } | ||
160 | 10 | return b; | |
161 | } | ||
162 | |||
163 | ///Close the socket | ||
164 | template<typename _TBackend, typename _TMockBackend> | ||
165 | 8 | void PGenericSocket<_TBackend, _TMockBackend>::close(){ | |
166 | 8 | _TMockBackend::close(p_mockSocket); | |
167 | 8 | _TBackend::close(p_socket); | |
168 | 8 | } | |
169 | |||
170 | ///Say if the Socket is connected | ||
171 | /** @return true if the current socket is connected (or both in record mode) | ||
172 | */ | ||
173 | template<typename _TBackend, typename _TMockBackend> | ||
174 | 2 | bool PGenericSocket<_TBackend, _TMockBackend>::isConnected() const{ | |
175 | 2 | bool b(true); | |
176 |
1/2✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→5) not taken.
|
2 | if(p_mode != PSocketMode::NO_MOCK){ |
177 | 2 | b &= _TMockBackend::isConnected(p_mockSocket); | |
178 | } | ||
179 |
1/2✓ Branch 0 (5→6) taken 2 times.
✗ Branch 1 (5→8) not taken.
|
2 | if(p_mode != PSocketMode::MOCK){ |
180 | 2 | b &= _TBackend::isConnected(p_socket); | |
181 | } | ||
182 | 2 | return b; | |
183 | } | ||
184 | |||
185 | ///Initialisation function of the class PGenericSocket | ||
186 | /** @param mode : Mode of the Socket (no mock, mock, mock_record) | ||
187 | */ | ||
188 | template<typename _TBackend, typename _TMockBackend> | ||
189 | 4 | void PGenericSocket<_TBackend, _TMockBackend>::initialisationPGenericSocket(PSocketMode::PSocketMode mode){ | |
190 | 4 | p_mode = mode; | |
191 | 4 | } | |
192 | |||
193 | |||
194 | #endif | ||
195 | |||
196 | |||
197 | |||
198 |