Directory: | ./ |
---|---|
File: | tmp_project/PhoenixSocket/src/PGenericSocket.h |
Date: | 2025-03-14 12:18:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 34 | 50 | 68.0% |
Branches: | 26 | 66 | 39.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 __PGENERIC_SOCKET_H__ | ||
8 | #define __PGENERIC_SOCKET_H__ | ||
9 | |||
10 | #include "PSocketMode.h" | ||
11 | #include "PSocketFlag.h" | ||
12 | #include "phoenix_mock_socket.h" | ||
13 | |||
14 | ///@brief Abstract socket which has a mock mode to avoid heavy socket backend for unit tests | ||
15 | template<typename _TBackend, typename _TMockBackend> | ||
16 | class PGenericSocket{ | ||
17 | public: | ||
18 | PGenericSocket(PSocketMode::PSocketMode mode); | ||
19 | virtual ~PGenericSocket(); | ||
20 | |||
21 | bool createClientSocket(const typename _TBackend::Param & param, const typename _TMockBackend::Param & mockParam); | ||
22 | bool createServerSocket(const typename _TBackend::Param & param, const typename _TMockBackend::Param & mockParam); | ||
23 | |||
24 | ///Send message on the given socket | ||
25 | /** @param data : data to be sent | ||
26 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
27 | * @return true on success, false otherwise | ||
28 | */ | ||
29 | template<typename U> | ||
30 | 22 | bool sendData(const U & data, PSendFlag::PSendFlag flag){ | |
31 | 22 | size_t dataSize(data_size<U>(data)); | |
32 | 22 | bool b(true); | |
33 | |||
34 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10 times.
|
22 | if(p_mode != PSocketMode::NO_MOCK){ |
35 | 12 | typename _TMockBackend::Message msg; | |
36 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | _TMockBackend::msgResize(msg, dataSize); |
37 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | DataStreamIter iter = _TMockBackend::msgData(msg); |
38 |
2/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | if(data_message_save<U>(iter, data)){ //Save the message |
39 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | b &= _TMockBackend::send(p_mockSocket, msg, flag); |
40 | }else{ | ||
41 | ✗ | b = false; | |
42 | } | ||
43 | 12 | } | |
44 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12 times.
|
22 | if(p_mode != PSocketMode::MOCK){ |
45 | 10 | typename _TBackend::Message msg; | |
46 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | _TBackend::msgResize(msg, dataSize); |
47 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | DataStreamIter iter = _TBackend::msgData(msg); |
48 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
10 | if(data_message_save<U>(iter, data)){ //Save the message |
49 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | b &= _TBackend::send(p_socket, msg, flag); |
50 | }else{ | ||
51 | ✗ | b = false; | |
52 | } | ||
53 | 10 | } | |
54 | 22 | return b; | |
55 | } | ||
56 | |||
57 | bool sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag); | ||
58 | |||
59 | ///Recieve message from the given socket | ||
60 | /** @param data : data to be recieved | ||
61 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
62 | * @return true on success, false otherwise | ||
63 | */ | ||
64 | template<typename U> | ||
65 | 28 | bool recvData(U & data, PRecvFlag::PRecvFlag flag){ | |
66 | 28 | bool b(true); | |
67 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 18 times.
|
28 | if(p_mode == PSocketMode::NO_MOCK){ //Normal mode |
68 | 10 | typename _TBackend::Message msg; | |
69 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | b &= _TBackend::recv(p_socket, msg, flag); |
70 | //If the message is empty we cannot initialise the given data, so, this is an error | ||
71 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | b &= _TBackend::msgSize(msg) != 0lu; |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if(b){ |
73 | ✗ | DataStreamIter iter = _TBackend::msgData(msg); | |
74 | ✗ | b &= data_message_load<U>(iter, data); | |
75 | } | ||
76 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
28 | }else if(p_mode == PSocketMode::MOCK){ //Mock mode |
77 | 18 | typename _TMockBackend::Message msg; | |
78 |
1/1✓ Branch 1 taken 18 times.
|
18 | b &= _TMockBackend::recv(p_mockSocket, msg, flag); |
79 | //If the message is empty we cannot initialise the given data, so, this is an error | ||
80 |
1/1✓ Branch 1 taken 18 times.
|
18 | b &= _TMockBackend::msgSize(msg) != 0lu; |
81 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | if(b){ |
82 |
1/1✓ Branch 1 taken 16 times.
|
16 | DataStreamIter iter = _TMockBackend::msgData(msg); |
83 |
1/1✓ Branch 1 taken 16 times.
|
16 | b &= data_message_load<U>(iter, data); |
84 | } | ||
85 | 18 | }else{ //Mock record mode | |
86 | ✗ | typename _TBackend::Message msg; | |
87 | ✗ | b &= _TBackend::recv(p_socket, msg, flag); | |
88 | //If the message is empty we cannot initialise the given data, so, this is an error | ||
89 | ✗ | b &= _TBackend::msgSize(msg) != 0lu; | |
90 | ✗ | if(b){ | |
91 | ✗ | DataStreamIter iter = _TBackend::msgData(msg); | |
92 | ✗ | b &= data_message_load<U>(iter, data); | |
93 | ✗ | if(b){ | |
94 | //Let's convert the message into the mock backend | ||
95 | ✗ | typename _TMockBackend::Message msgMock; | |
96 | ✗ | size_t dataSize(_TBackend::msgSize(msg)); | |
97 | ✗ | _TMockBackend::msgResize(msgMock, dataSize); | |
98 | ✗ | memcpy(_TMockBackend::msgData(msgMock), _TBackend::msgData(msg), dataSize); | |
99 | ✗ | b &= _TMockBackend::recv(p_mockSocket, msgMock, flag); | |
100 | } | ||
101 | } | ||
102 | } | ||
103 | 28 | return b; | |
104 | } | ||
105 | |||
106 | bool recvMsg(typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag); | ||
107 | |||
108 | void close(); | ||
109 | bool isConnected() const; | ||
110 | |||
111 | private: | ||
112 | void initialisationPGenericSocket(PSocketMode::PSocketMode mode); | ||
113 | |||
114 | ///Mode of the Socket (no mock, mock, mock_record) | ||
115 | PSocketMode::PSocketMode p_mode; | ||
116 | |||
117 | ///Socket to be used with the classical backend | ||
118 | typename _TBackend::Socket p_socket; | ||
119 | ///Socket to be used with the mock backend | ||
120 | typename _TMockBackend::Socket p_mockSocket; | ||
121 | }; | ||
122 | |||
123 | #include "PGenericSocket_impl.h" | ||
124 | |||
125 | |||
126 | #endif | ||
127 | |||
128 |