PhoenixZMQ  2.0.0
Library which integrates zeromq use in Phoenix
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 
10 #include "PMockBackend.h"
11 
13 
18 template<>
19 bool checkValue<DataStreamType>(const std::string & testName, const std::vector<DataStreamType> & vecData, const std::vector<DataStreamType> & vecReferenceData){
20  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  bool b(true);
25  for(size_t i(0lu); i < vecData.size() && b; ++i){
26  b &= vecData[i] == vecReferenceData[i];
27  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  return b;
34 }
35 
38 
39 }
40 
42 
48 PMockBackend::Param PMockBackend::client(const std::string & address, size_t port, bool isMockRecord, const std::string & mockDirectory){
49  PMockBackend::Param param;
50  param.address = address;
51  param.port = port;
52  param.isMockRecord = isMockRecord;
53  param.mockDirectory = mockDirectory;
54  return param;
55 }
56 
58 
64 PMockBackend::Param PMockBackend::server(const std::string & address, size_t port, bool isMockRecord, const std::string & mockDirectory){
65  return PMockBackend::client(address, port, isMockRecord, mockDirectory);
66 }
67 
69 
74  socket.param = param;
75  socket.eventIndex = 0lu;
76  std::stringstream socketFileName;
77  if(param.mockDirectory != ""){
78  socketFileName << param.mockDirectory << "/";
79  }
80  socketFileName << param.address << "_" << param.port << ".pmockbackend";
81  socket.fileNameMessage = socketFileName.str();
82  bool b(true);
83  if(!socket.param.isMockRecord){ //If we are not in record mode, we load all the exchange
84  b &= data_load(socket.fileNameMessage, socket.vecMessage);
85  if(!b){
86  std::cerr << "PMockBackend::createClientSocket : cannot load file '"<<socket.fileNameMessage<<"'" << std::endl;
87  }
88  }
89  return b;
90 }
91 
93 
98  return createClientSocket(socket, param);
99 }
100 
102 
108  bool b(true);
109  if(socket.param.isMockRecord){ //If we record all events
110  socket.vecMessage.push_back(msg);
111  }else{
112  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  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  std::stringstream strEvent;
122  strEvent << socket.eventIndex;
123  b &= checkValue("PMockBackend::send : event " + strEvent.str(), msg, socket.vecMessage[socket.eventIndex]);
124  }
125  ++socket.eventIndex;
126  return b;
127 }
128 
130 
136  if(socket.param.isMockRecord){ //If we record all events
137  socket.vecMessage.push_back(msg);
138  }else{
139  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  if(socket.eventIndex >= socket.vecMessage.size()){
145  socket.eventIndex = 0lu; //Let's loop if all the events were used
146  }
147  msg = socket.vecMessage[socket.eventIndex];
148  }
149  ++socket.eventIndex;
150  return true;
151 }
152 
154 
157 void PMockBackend::msgResize(PMockBackend::Message & msg, size_t sizeMsg){
158  msg.resize(sizeMsg);
159 }
160 
162 
166  return msg.size();
167 }
168 
170 
174  return (const DataStreamIter)msg.data();
175 }
176 
178 
182  return msg.data();
183 }
184 
186 
189  if(socket.param.isMockRecord){
190  data_save(socket.fileNameMessage, socket.vecMessage);
191  }
192 }
193 
195 
199  return true;
200 }
201 
203 
207  size_t dataSize(PMockBackend::msgSize(msg));
208  mockMsg.resize(dataSize);
209  memcpy(mockMsg.data(), PMockBackend::msgData(msg), dataSize);
210 }
211 
213 
217  size_t dataSize(mockMsg.size());
218  PMockBackend::msgResize(msg, dataSize);
219  memcpy(PMockBackend::msgData(msg), mockMsg.data(), dataSize);
220 }
221 
222 
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.
static void msgToMock(DataStreamMsg &mockMsg, const Message &msg)
Copy current backend message data into mock message.
static bool createClientSocket(Socket &socket, const PMockParam &param)
Create a client socket.
static const DataStreamIter msgData(const Message &msg)
Get the data of a message.
static Param client(const std::string &address, size_t port, bool isMockRecord, const std::string &mockDirectory)
Create param for a client socket.
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 bool createServerSocket(Socket &socket, const PMockParam &param)
Create a client socket.
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.
static void close(Socket &socket)
Close the given socket.
static Param server(const std::string &address, size_t port, bool isMockRecord, const std::string &mockDirectory)
Create param for a server socket.
static size_t msgSize(const Message &msg)
Get the size of a message.
DataStreamMsg Message
Define the type of message used by the PAbstractSocketManager.
Definition: PMockBackend.h:43
bool data_load(FILE *iter, T &data)
Load data from a message.
Definition: data_file.h:39
bool data_save(FILE *iter, const T &data)
Save data in a message.
Definition: data_file.h:18
bool checkValue(const std::string &testName, const T &givenSize, const T &referenceSize)
Check given value compare to the reference value.
std::vector< DataStreamType > DataStreamMsg
DataStreamType * DataStreamIter
PRecvFlag
describe the recieving flag of the Socket
Definition: PSocketFlag.h:20
PSendFlag
describe the sending flag of the Socket
Definition: PSocketFlag.h:12
Set of parameters to be passed to create a socket with mock backend.
Definition: PMockBackend.h:14
size_t port
Connection port.
Definition: PMockBackend.h:18
std::string address
Host address.
Definition: PMockBackend.h:16
std::string mockDirectory
Directory where to find/save mock files.
Definition: PMockBackend.h:22
bool isMockRecord
True to record the activity of the backend durring real use to make reusable mock.
Definition: PMockBackend.h:20
Data to be used to handle the mock socket.
Definition: PMockBackend.h:26
PMockParam param
Parameters of the mock.
Definition: PMockBackend.h:28
std::string fileNameMessage
Name of the file to read messages.
Definition: PMockBackend.h:30
PVecMockMessage vecMessage
Vector of messages.
Definition: PMockBackend.h:32
size_t eventIndex
Index of the current message.
Definition: PMockBackend.h:34