Directory: | ./ |
---|---|
File: | tmp_project/PhoenixSocket/src/PMockBackend.cpp |
Date: | 2025-03-14 12:18:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 64 | 78 | 82.1% |
Branches: | 38 | 60 | 63.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <sstream> | ||
8 | |||
9 | #include "data_stream_check_value.h" | ||
10 | #include "PMockBackend.h" | ||
11 | |||
12 | ///Check given value compare to the reference size | ||
13 | /** @param testName : name of the current test | ||
14 | * @param vecData : vector of data to be checked | ||
15 | * @param vecReferenceData : vector of reference data | ||
16 | * @return true on success, false otherwise | ||
17 | */ | ||
18 | template<> | ||
19 | 2 | bool checkValue<DataStreamType>(const std::string & testName, const std::vector<DataStreamType> & vecData, const std::vector<DataStreamType> & vecReferenceData){ | |
20 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if(vecData.size() != vecReferenceData.size()){ |
21 | ✗ | std::cout << "checkValue<DataStreamType> : "<<testName<<" => vecData.size(" << vecData.size() << ") != vecReferenceData.size("<<vecReferenceData.size()<<")" << std::endl; | |
22 | ✗ | return false; | |
23 | } | ||
24 | 2 | bool b(true); | |
25 |
5/6✓ Branch 1 taken 170 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 170 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 170 times.
✓ Branch 6 taken 2 times.
|
172 | for(size_t i(0lu); i < vecData.size() && b; ++i){ |
26 | 170 | b &= vecData[i] == vecReferenceData[i]; | |
27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
|
170 | if(!b){ |
28 | ✗ | std::cout << "checkValue<DataStreamType> : "<<testName<<" => vecData["<<i<<"](" << (int)vecData[i] << ") != vecReferenceData["<<i<<"]("<<(int)vecReferenceData[i]<<")" << std::endl; | |
29 | } | ||
30 | } | ||
31 | // if(b){std::cout << "checkValue : "<<testName<<" => Ok"<<std::endl;} | ||
32 | // else{std::cout << "checkValue : "<<testName<<" => WRONG!!!!"<<std::endl;} | ||
33 | 2 | return b; | |
34 | } | ||
35 | |||
36 | ///Default constructor of PMockBackend | ||
37 | ✗ | PMockBackend::PMockBackend(){ | |
38 | |||
39 | } | ||
40 | |||
41 | ///Create param for a client socket | ||
42 | /** @param address : address of the server to be connected to | ||
43 | * @param port : port to be used | ||
44 | * @param isMockRecord : true if the mock is in record mode | ||
45 | * @param mockDirectory : directory where to find/save mocks | ||
46 | * @return corresponding parameters | ||
47 | */ | ||
48 | 19 | PMockBackend::Param PMockBackend::client(const std::string & address, size_t port, bool isMockRecord, const std::string & mockDirectory){ | |
49 | 19 | PMockBackend::Param param; | |
50 |
1/1✓ Branch 1 taken 19 times.
|
19 | param.address = address; |
51 | 19 | param.port = port; | |
52 | 19 | param.isMockRecord = isMockRecord; | |
53 |
1/1✓ Branch 1 taken 19 times.
|
19 | param.mockDirectory = mockDirectory; |
54 | 19 | return param; | |
55 | } | ||
56 | |||
57 | ///Create param for a server socket | ||
58 | /** @param address : address of the server to be connected to | ||
59 | * @param port : port to be used | ||
60 | * @param isMockRecord : true if the mock is in record mode | ||
61 | * @param mockDirectory : directory where to find/save mocks | ||
62 | * @return corresponding parameters | ||
63 | */ | ||
64 | 8 | PMockBackend::Param PMockBackend::server(const std::string & address, size_t port, bool isMockRecord, const std::string & mockDirectory){ | |
65 | 8 | return PMockBackend::client(address, port, isMockRecord, mockDirectory); | |
66 | } | ||
67 | |||
68 | ///Create a client socket | ||
69 | /** @param[out] socket : socket to be created | ||
70 | * @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
71 | * @return true if the socket has been created, false otherwise | ||
72 | */ | ||
73 | 15 | bool PMockBackend::createClientSocket(PMockBackend::Socket & socket, const PMockParam & param){ | |
74 |
1/1✓ Branch 1 taken 15 times.
|
15 | socket.param = param; |
75 | 15 | socket.eventIndex = 0lu; | |
76 |
1/1✓ Branch 1 taken 15 times.
|
15 | std::stringstream socketFileName; |
77 |
2/3✓ Branch 1 taken 15 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
15 | if(param.mockDirectory != ""){ |
78 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
|
15 | socketFileName << param.mockDirectory << "/"; |
79 | } | ||
80 |
4/4✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
|
15 | socketFileName << param.address << "_" << param.port << ".pmockbackend"; |
81 |
1/1✓ Branch 1 taken 15 times.
|
15 | socket.fileNameMessage = socketFileName.str(); |
82 | 15 | bool b(true); | |
83 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
|
15 | if(!socket.param.isMockRecord){ //If we are not in record mode, we load all the exchange |
84 |
1/1✓ Branch 1 taken 13 times.
|
13 | b &= data_load(socket.fileNameMessage, socket.vecMessage); |
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if(!b){ |
86 | ✗ | std::cerr << "PMockBackend::createClientSocket : cannot load file '"<<socket.fileNameMessage<<"'" << std::endl; | |
87 | } | ||
88 | } | ||
89 | 15 | return b; | |
90 | 15 | } | |
91 | |||
92 | ///Create a client socket | ||
93 | /** @param[out] socket : socket to be created | ||
94 | * @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
95 | * @return true if the socket has been created, false otherwise | ||
96 | */ | ||
97 | 6 | bool PMockBackend::createServerSocket(PMockBackend::Socket & socket, const PMockParam & param){ | |
98 | 6 | return createClientSocket(socket, param); | |
99 | } | ||
100 | |||
101 | ///Send message on the given socket | ||
102 | /** @param socket : socket to be used | ||
103 | * @param msg : message to be sent | ||
104 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
105 | * @return true on success, false otherwise | ||
106 | */ | ||
107 | 22 | bool PMockBackend::send(PMockBackend::Socket & socket, const PMockBackend::Message & msg, PSendFlag::PSendFlag flag){ | |
108 | 22 | bool b(true); | |
109 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
|
22 | if(socket.param.isMockRecord){ //If we record all events |
110 | 20 | socket.vecMessage.push_back(msg); | |
111 | }else{ | ||
112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if(socket.vecMessage.size() == 0lu){ |
113 | ✗ | std::cerr << "PMockBackend::send : empty vector of message" << std::endl; | |
114 | ✗ | return false; | |
115 | } | ||
116 | //Let's pick the current message | ||
117 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if(socket.eventIndex >= socket.vecMessage.size()){ |
118 | ✗ | socket.eventIndex = 0lu; //Let's loop if all the events were used | |
119 | } | ||
120 | //Let's check if the current message matches the message we are supposed to send | ||
121 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::stringstream strEvent; |
122 |
1/1✓ Branch 1 taken 2 times.
|
2 | strEvent << socket.eventIndex; |
123 |
3/3✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
|
2 | b &= checkValue("PMockBackend::send : event " + strEvent.str(), msg, socket.vecMessage[socket.eventIndex]); |
124 | 2 | } | |
125 | 22 | ++socket.eventIndex; | |
126 | 22 | return b; | |
127 | } | ||
128 | |||
129 | ///Recieve message from the given socket | ||
130 | /** @param socket : socket to be used | ||
131 | * @param msg : message to be recieved | ||
132 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
133 | * @return true on success, false otherwise | ||
134 | */ | ||
135 | 28 | bool PMockBackend::recv(PMockBackend::Socket & socket, PMockBackend::Message & msg, PRecvFlag::PRecvFlag flag){ | |
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if(socket.param.isMockRecord){ //If we record all events |
137 | ✗ | socket.vecMessage.push_back(msg); | |
138 | }else{ | ||
139 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if(socket.vecMessage.size() == 0lu){ |
140 | ✗ | std::cerr << "PMockBackend::recv : empty vector of message" << std::endl; | |
141 | ✗ | return false; | |
142 | } | ||
143 | //Let's pick the current message | ||
144 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if(socket.eventIndex >= socket.vecMessage.size()){ |
145 | ✗ | socket.eventIndex = 0lu; //Let's loop if all the events were used | |
146 | } | ||
147 | 28 | msg = socket.vecMessage[socket.eventIndex]; | |
148 | } | ||
149 | 28 | ++socket.eventIndex; | |
150 | 28 | return true; | |
151 | } | ||
152 | |||
153 | ///Resize a message | ||
154 | /** @param[out] msg : message to be resized | ||
155 | * @param sizeMsg : new size of the message | ||
156 | */ | ||
157 | 22 | void PMockBackend::msgResize(PMockBackend::Message & msg, size_t sizeMsg){ | |
158 | 22 | msg.resize(sizeMsg); | |
159 | 22 | } | |
160 | |||
161 | ///Get the size of a message | ||
162 | /** @param msg : message to be used | ||
163 | * @return size of the message in bytes | ||
164 | */ | ||
165 | 28 | size_t PMockBackend::msgSize(const PMockBackend::Message & msg){ | |
166 | 28 | return msg.size(); | |
167 | } | ||
168 | |||
169 | ///Get the data of a message | ||
170 | /** @param msg : message to be used | ||
171 | * @return data of the message in bytes | ||
172 | */ | ||
173 | ✗ | const DataStreamIter PMockBackend::msgData(const PMockBackend::Message & msg){ | |
174 | ✗ | return (const DataStreamIter)msg.data(); | |
175 | } | ||
176 | |||
177 | ///Get the data of a message | ||
178 | /** @param msg : message to be used | ||
179 | * @return data of the message in bytes | ||
180 | */ | ||
181 | 48 | DataStreamIter PMockBackend::msgData(PMockBackend::Message & msg){ | |
182 | 48 | return msg.data(); | |
183 | } | ||
184 | |||
185 | ///Close the given socket | ||
186 | /** @param[out] socket : socket to be closed | ||
187 | */ | ||
188 | 15 | void PMockBackend::close(PMockBackend::Socket & socket){ | |
189 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
|
15 | if(socket.param.isMockRecord){ |
190 | 2 | data_save(socket.fileNameMessage, socket.vecMessage); | |
191 | } | ||
192 | 15 | } | |
193 | |||
194 | ///Say if the given socket is connected | ||
195 | /** @param socket : socket to be checked | ||
196 | * @return true if the socket is connected, false otherwise | ||
197 | */ | ||
198 | 2 | bool PMockBackend::isConnected(const PMockBackend::Socket & socket){ | |
199 | 2 | return true; | |
200 | } | ||
201 | |||
202 | |||
203 |