PhoenixZMQ  2.0.0
Library which integrates zeromq use in Phoenix
PMockBackend Class Reference

Backend to use Mock library with PAbtractSocket. More...

#include <PMockBackend.h>

Public Types

typedef DataStreamMsg Message
 Define the type of message used by the PAbstractSocketManager. More...
 
typedef PMockParam Param
 Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketManager. More...
 
typedef PMockSocket Socket
 Define the socket of the backend used by the PAbstractSocketManager. More...
 

Public Member Functions

 PMockBackend ()
 Default constructor of PMockBackend. More...
 

Static Public Member Functions

static Param client (const std::string &address, size_t port, bool isMockRecord, const std::string &mockDirectory)
 Create param for a client socket. More...
 
static void close (Socket &socket)
 Close the given socket. More...
 
static bool createClientSocket (Socket &socket, const PMockParam &param)
 Create a client socket. More...
 
static bool createServerSocket (Socket &socket, const PMockParam &param)
 Create a client socket. More...
 
static bool isConnected (const Socket &socket)
 Say if the given socket is connected. More...
 
static void mockToMsg (Message &msg, DataStreamMsg &mockMsg)
 Copy mock message data into current backend message. More...
 
static const DataStreamIter msgData (const Message &msg)
 Get the data of a message. More...
 
static DataStreamIter msgData (Message &msg)
 Get the data of a message. More...
 
static void msgResize (Message &msg, size_t sizeMsg)
 Resize a message. More...
 
static size_t msgSize (const Message &msg)
 Get the size of a message. More...
 
static void msgToMock (DataStreamMsg &mockMsg, const Message &msg)
 Copy current backend message data into mock message. More...
 
static bool recv (Socket &socket, Message &msg, PRecvFlag::PRecvFlag flag)
 Recieve message from the given socket. More...
 
static bool send (Socket &socket, const Message &msg, PSendFlag::PSendFlag flag)
 Send message on the given socket. More...
 
static Param server (const std::string &address, size_t port, bool isMockRecord, const std::string &mockDirectory)
 Create param for a server socket. More...
 

Detailed Description

Backend to use Mock library with PAbtractSocket.

Definition at line 38 of file PMockBackend.h.

Member Typedef Documentation

◆ Message

Define the type of message used by the PAbstractSocketManager.

Definition at line 43 of file PMockBackend.h.

◆ Param

Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketManager.

Definition at line 45 of file PMockBackend.h.

◆ Socket

Define the socket of the backend used by the PAbstractSocketManager.

Definition at line 41 of file PMockBackend.h.

Constructor & Destructor Documentation

◆ PMockBackend()

PMockBackend::PMockBackend ( )

Default constructor of PMockBackend.

Definition at line 37 of file PMockBackend.cpp.

37  {
38 
39 }

Member Function Documentation

◆ client()

PMockBackend::Param PMockBackend::client ( const std::string &  address,
size_t  port,
bool  isMockRecord,
const std::string &  mockDirectory 
)
static

Create param for a client socket.

Parameters
address: address of the server to be connected to
port: port to be used
isMockRecord: true if the mock is in record mode
mockDirectory: directory where to find/save mocks
Returns
corresponding parameters

Definition at line 48 of file PMockBackend.cpp.

48  {
49  PMockBackend::Param param;
50  param.address = address;
51  param.port = port;
52  param.isMockRecord = isMockRecord;
53  param.mockDirectory = mockDirectory;
54  return param;
55 }
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

References PMockParam::address, PMockParam::isMockRecord, PMockParam::mockDirectory, and PMockParam::port.

Referenced by server().

+ Here is the caller graph for this function:

◆ close()

void PMockBackend::close ( PMockBackend::Socket socket)
static

Close the given socket.

Parameters
[out]socket: socket to be closed

Definition at line 188 of file PMockBackend.cpp.

188  {
189  if(socket.param.isMockRecord){
190  data_save(socket.fileNameMessage, socket.vecMessage);
191  }
192 }
bool data_save(FILE *iter, const T &data)
Save data in a message.
Definition: data_file.h:18
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

References data_save(), PMockSocket::fileNameMessage, PMockParam::isMockRecord, PMockSocket::param, and PMockSocket::vecMessage.

+ Here is the call graph for this function:

◆ createClientSocket()

bool PMockBackend::createClientSocket ( PMockBackend::Socket socket,
const PMockParam param 
)
static

Create a client socket.

Parameters
[out]socket: socket to be created
param: extra customisable parameters for the creation of the socket (depends on the backend)
Returns
true if the socket has been created, false otherwise

Definition at line 73 of file PMockBackend.cpp.

73  {
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 }
bool data_load(FILE *iter, T &data)
Load data from a message.
Definition: data_file.h:39
size_t eventIndex
Index of the current message.
Definition: PMockBackend.h:34

References PMockParam::address, data_load(), PMockSocket::eventIndex, PMockSocket::fileNameMessage, PMockParam::isMockRecord, PMockParam::mockDirectory, PMockSocket::param, PMockParam::port, and PMockSocket::vecMessage.

Referenced by createServerSocket().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createServerSocket()

bool PMockBackend::createServerSocket ( PMockBackend::Socket socket,
const PMockParam param 
)
static

Create a client socket.

Parameters
[out]socket: socket to be created
param: extra customisable parameters for the creation of the socket (depends on the backend)
Returns
true if the socket has been created, false otherwise

Definition at line 97 of file PMockBackend.cpp.

97  {
98  return createClientSocket(socket, param);
99 }
static bool createClientSocket(Socket &socket, const PMockParam &param)
Create a client socket.

References createClientSocket().

+ Here is the call graph for this function:

◆ isConnected()

bool PMockBackend::isConnected ( const Socket socket)
static

Say if the given socket is connected.

Parameters
socket: socket to be checked
Returns
true if the socket is connected, false otherwise

Definition at line 198 of file PMockBackend.cpp.

198  {
199  return true;
200 }

◆ mockToMsg()

void PMockBackend::mockToMsg ( PMockBackend::Message msg,
DataStreamMsg mockMsg 
)
static

Copy mock message data into current backend message.

Parameters
[out]msg: message of the current backend to be converted
mockMsg: mock message

Definition at line 216 of file PMockBackend.cpp.

216  {
217  size_t dataSize(mockMsg.size());
218  PMockBackend::msgResize(msg, dataSize);
219  memcpy(PMockBackend::msgData(msg), mockMsg.data(), dataSize);
220 }
static const DataStreamIter msgData(const Message &msg)
Get the data of a message.
static void msgResize(Message &msg, size_t sizeMsg)
Resize a message.

References msgData(), and msgResize().

+ Here is the call graph for this function:

◆ msgData() [1/2]

const DataStreamIter PMockBackend::msgData ( const Message msg)
static

Get the data of a message.

Parameters
msg: message to be used
Returns
data of the message in bytes

Definition at line 173 of file PMockBackend.cpp.

173  {
174  return (const DataStreamIter)msg.data();
175 }
DataStreamType * DataStreamIter

Referenced by mockToMsg(), and msgToMock().

+ Here is the caller graph for this function:

◆ msgData() [2/2]

DataStreamIter PMockBackend::msgData ( PMockBackend::Message msg)
static

Get the data of a message.

Parameters
msg: message to be used
Returns
data of the message in bytes

Definition at line 181 of file PMockBackend.cpp.

181  {
182  return msg.data();
183 }

◆ msgResize()

void PMockBackend::msgResize ( PMockBackend::Message msg,
size_t  sizeMsg 
)
static

Resize a message.

Parameters
[out]msg: message to be resized
sizeMsg: new size of the message

Definition at line 157 of file PMockBackend.cpp.

157  {
158  msg.resize(sizeMsg);
159 }

Referenced by mockToMsg().

+ Here is the caller graph for this function:

◆ msgSize()

size_t PMockBackend::msgSize ( const Message msg)
static

Get the size of a message.

Parameters
msg: message to be used
Returns
size of the message in bytes

Definition at line 165 of file PMockBackend.cpp.

165  {
166  return msg.size();
167 }

Referenced by msgToMock().

+ Here is the caller graph for this function:

◆ msgToMock()

void PMockBackend::msgToMock ( DataStreamMsg mockMsg,
const Message msg 
)
static

Copy current backend message data into mock message.

Parameters
[out]mockMsg: mock message
msg: message of the current backend to be converted

Definition at line 206 of file PMockBackend.cpp.

206  {
207  size_t dataSize(PMockBackend::msgSize(msg));
208  mockMsg.resize(dataSize);
209  memcpy(mockMsg.data(), PMockBackend::msgData(msg), dataSize);
210 }
static size_t msgSize(const Message &msg)
Get the size of a message.

References msgData(), and msgSize().

+ Here is the call graph for this function:

◆ recv()

bool PMockBackend::recv ( PMockBackend::Socket socket,
PMockBackend::Message msg,
PRecvFlag::PRecvFlag  flag 
)
static

Recieve message from the given socket.

Parameters
socket: socket to be used
msg: message to be recieved
flag: flag to be used to send the message (BLOCK, NON_BLOCK, etc)
Returns
true on success, false otherwise

Definition at line 135 of file PMockBackend.cpp.

135  {
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 }

References PMockSocket::eventIndex, PMockParam::isMockRecord, PMockSocket::param, and PMockSocket::vecMessage.

◆ send()

bool PMockBackend::send ( PMockBackend::Socket socket,
const Message msg,
PSendFlag::PSendFlag  flag 
)
static

Send message on the given socket.

Parameters
socket: socket to be used
msg: message to be sent
flag: flag to be used to send the message (BLOCK, NON_BLOCK, etc)
Returns
true on success, false otherwise

Definition at line 107 of file PMockBackend.cpp.

107  {
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 }
bool checkValue(const std::string &testName, const T &givenSize, const T &referenceSize)
Check given value compare to the reference value.

References checkValue(), PMockSocket::eventIndex, PMockParam::isMockRecord, PMockSocket::param, and PMockSocket::vecMessage.

+ Here is the call graph for this function:

◆ server()

PMockBackend::Param PMockBackend::server ( const std::string &  address,
size_t  port,
bool  isMockRecord,
const std::string &  mockDirectory 
)
static

Create param for a server socket.

Parameters
address: address of the server to be connected to
port: port to be used
isMockRecord: true if the mock is in record mode
mockDirectory: directory where to find/save mocks
Returns
corresponding parameters

Definition at line 64 of file PMockBackend.cpp.

64  {
65  return PMockBackend::client(address, port, isMockRecord, mockDirectory);
66 }
static Param client(const std::string &address, size_t port, bool isMockRecord, const std::string &mockDirectory)
Create param for a client socket.

References client().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: