PhoenixSocket  1.0.0
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
PMockBackend.cpp
Go to the documentation of this file.
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
13
17bool phoenix_loadMockSocket(const std::string & prefix, const std::string & host, size_t port, PVecMockMessage & vecMessage){
18 std::stringstream socketFileName;
19 socketFileName << prefix << host << "_" << port << ".pmockbackend";
20 return data_load(socketFileName.str(), vecMessage);
21}
22
24
28bool phoenix_saveMockSocket(const std::string & prefix, const std::string & host, size_t port, const PVecMockMessage & vecMessage){
29 std::stringstream socketFileName;
30 socketFileName << prefix << host << "_" << port << ".pmockbackend";
31 return data_save(socketFileName.str(), vecMessage);
32}
33
35
40template<>
41bool checkValue<DataStreamType>(const std::string & testName, const std::vector<DataStreamType> & vecData, const std::vector<DataStreamType> & vecReferenceData){
42 if(vecData.size() != vecReferenceData.size()){
43 std::cout << "checkValue<DataStreamType> : "<<testName<<" => vecData.size(" << vecData.size() << ") != vecReferenceData.size("<<vecReferenceData.size()<<")" << std::endl;
44 return false;
45 }
46 bool b(true);
47 for(size_t i(0lu); i < vecData.size() && b; ++i){
48 b &= vecData[i] == vecReferenceData[i];
49 if(!b){
50 std::cout << "checkValue<DataStreamType> : "<<testName<<" => vecData["<<i<<"](" << (int)vecData[i] << ") != vecReferenceData["<<i<<"]("<<(int)vecReferenceData[i]<<")" << std::endl;
51 }
52 }
53// if(b){std::cout << "checkValue : "<<testName<<" => Ok"<<std::endl;}
54// else{std::cout << "checkValue : "<<testName<<" => WRONG!!!!"<<std::endl;}
55 return b;
56}
57
62
64
70
72
77
79
85bool PMockBackend::createClientSocket(PMockBackend::Socket & socket, const std::string & address, size_t port, const PMockParam & param){
86 socket.eventIndex = 0lu;
87 std::stringstream socketFileName;
88 if(socket.param.mockPrefix != ""){
89 socketFileName << socket.param.mockPrefix;
90 }
91 socketFileName << address << "_" << port << ".pmockbackend";
92 socket.fileNameMessage = socketFileName.str();
93 bool b(true);
94 if(!socket.param.isMockRecord){ //If we are not in record mode, we load all the exchange
95 b &= data_load(socket.fileNameMessage, socket.vecMessage);
96 if(!b){
97 std::cerr << "PMockBackend::createClientSocket : cannot load file '"<<socket.fileNameMessage<<"'" << std::endl;
98 }
99 }
100 return b;
101}
102
104
108bool PMockBackend::createServerSocket(PMockBackend::Socket & socket, const std::string & address, size_t port, const PMockParam & param){
109 return createClientSocket(socket, address, port, param);
110}
111
113
116void PMockBackend::setMockPrefix(Socket & socket, const std::string & mockPrefix){
117 socket.param.mockPrefix = mockPrefix;
118}
119
121
124void PMockBackend::setIsMockRecord(Socket & socket, bool isMockRecord){
125 socket.param.isMockRecord = isMockRecord;
126}
127
128
130
136 bool b(true);
137 if(socket.param.isMockRecord){ //If we record all events
138 socket.vecMessage.push_back(msg);
139 }else{
140 if(socket.vecMessage.size() == 0lu){
141 std::cerr << "PMockBackend::send : empty vector of message" << std::endl;
142 return false;
143 }
144 //Let's pick the current message
145 if(socket.eventIndex >= socket.vecMessage.size()){
146 socket.eventIndex = 0lu; //Let's loop if all the events were used
147 }
148 //Let's check if the current message matches the message we are supposed to send
149 std::stringstream strEvent;
150 strEvent << socket.eventIndex;
151 b &= checkValue("PMockBackend::send : event " + strEvent.str(), msg, socket.vecMessage[socket.eventIndex]);
152 }
153 ++socket.eventIndex;
154 return b;
155}
156
158
164 if(socket.param.isMockRecord){ //If we record all events
165 socket.vecMessage.push_back(msg);
166 }else{
167 if(socket.vecMessage.size() == 0lu){
168 std::cerr << "PMockBackend::recv : empty vector of message" << std::endl;
169 return false;
170 }
171 //Let's pick the current message
172 if(socket.eventIndex >= socket.vecMessage.size()){
173 socket.eventIndex = 0lu; //Let's loop if all the events were used
174 }
175 msg = socket.vecMessage[socket.eventIndex];
176 }
177 ++socket.eventIndex;
178 return true;
179}
180
182
186 msg.resize(sizeMsg);
187}
188
190
194 return msg.size();
195}
196
198
201const DataStreamIter PMockBackend::msgData(const PMockBackend::Message & msg){
202 return (const DataStreamIter)msg.data();
203}
204
206
210 return msg.data();
211}
212
214
217 if(socket.param.isMockRecord){
218 data_save(socket.fileNameMessage, socket.vecMessage);
219 }
220}
221
223
227 return true;
228}
229
231
234void PMockBackend::msgToMock(DataStreamMsg & mockMsg, const PMockBackend::Message & msg){
235 size_t dataSize(PMockBackend::msgSize(msg));
236 mockMsg.resize(dataSize);
237 memcpy(mockMsg.data(), PMockBackend::msgData(msg), dataSize);
238}
239
241
244void PMockBackend::mockToMsg(PMockBackend::Message & msg, DataStreamMsg & mockMsg){
245 size_t dataSize(mockMsg.size());
246 PMockBackend::msgResize(msg, dataSize);
247 memcpy(PMockBackend::msgData(msg), mockMsg.data(), dataSize);
248}
249
250
bool checkValue< DataStreamType >(const std::string &testName, const std::vector< DataStreamType > &vecData, const std::vector< DataStreamType > &vecReferenceData)
Check given value compare to the reference size.
bool phoenix_loadMockSocket(const std::string &prefix, const std::string &host, size_t port, PVecMockMessage &vecMessage)
Load a mock of socket.
bool phoenix_saveMockSocket(const std::string &prefix, const std::string &host, size_t port, const PVecMockMessage &vecMessage)
Save a mock of socket.
static void msgToMock(DataStreamMsg &mockMsg, const Message &msg)
Copy current backend message data into mock message.
static const DataStreamIter msgData(const Message &msg)
Get the data of a message.
static bool send(Socket &socket, const Message &msg, PSendFlag::PSendFlag flag)
Send message on the given socket.
PMockBackend()
Default constructor of PMockBackend.
static void msgResize(Message &msg, size_t sizeMsg)
Resize a message.
static bool isConnected(const Socket &socket)
Say if the given socket is connected.
static Param server()
Create param for a server socket.
static void setIsMockRecord(Socket &socket, bool isMockRecord)
Set the mock prefix (where to find/save it)
PMockParam Param
Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketM...
static void setMockPrefix(Socket &socket, const std::string &mockPrefix)
Set the mock prefix (where to find/save it)
static void mockToMsg(Message &msg, DataStreamMsg &mockMsg)
Copy mock message data into current backend message.
static bool recv(Socket &socket, Message &msg, PRecvFlag::PRecvFlag flag)
Recieve message from the given socket.
PMockSocket Socket
Define the socket of the backend used by the PAbstractSocketManager.
static void close(Socket &socket)
Close the given socket.
static bool createClientSocket(Socket &socket, const std::string &address, size_t port, const PMockParam &param)
Create a client socket.
static Param client()
Create param for a client socket.
static size_t msgSize(const Message &msg)
Get the size of a message.
static bool createServerSocket(Socket &socket, const std::string &address, size_t port, const PMockParam &param)
Create a client socket.
DataStreamMsg Message
Define the type of message used by the PAbstractSocketManager.
PRecvFlag
describe the recieving flag of the Socket
Definition PSocketFlag.h:20
PSendFlag
describe the sending flag of the Socket
Definition PSocketFlag.h:12
std::vector< DataStreamMsg > PVecMockMessage
Vector of messages.
Set of parameters to be passed to create a socket with mock backend.
std::string mockPrefix
Prefix where to find/save mock files.
bool isMockRecord
True to record the activity of the backend durring real use to make reusable mock.
PMockParam param
Parameters of the mock.
std::string fileNameMessage
Name of the file to read messages.
PVecMockMessage vecMessage
Vector of messages.
size_t eventIndex
Index of the current message.