PhoenixSocket  1.0.0
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
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.
 
typedef PMockParam Param
 Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketManager.
 
typedef PMockSocket Socket
 Define the socket of the backend used by the PAbstractSocketManager.
 

Public Member Functions

 PMockBackend ()
 Default constructor of PMockBackend.
 

Static Public Member Functions

static Param client ()
 Create param for a client socket.
 
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 bool createServerSocket (Socket &socket, const std::string &address, size_t port, const PMockParam &param)
 Create a client socket.
 
static bool isConnected (const Socket &socket)
 Say if the given socket is connected.
 
static void mockToMsg (Message &msg, DataStreamMsg &mockMsg)
 Copy mock message data into current backend message.
 
static const DataStreamIter msgData (const Message &msg)
 Get the data of a message.
 
static DataStreamIter msgData (Message &msg)
 Get the data of a message.
 
static void msgResize (Message &msg, size_t sizeMsg)
 Resize a message.
 
static size_t msgSize (const Message &msg)
 Get the size of a message.
 
static void msgToMock (DataStreamMsg &mockMsg, const Message &msg)
 Copy current backend message data into mock message.
 
static bool recv (Socket &socket, Message &msg, PRecvFlag::PRecvFlag flag)
 Recieve message from the given socket.
 
static bool send (Socket &socket, const Message &msg, PSendFlag::PSendFlag flag)
 Send message on the given socket.
 
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)
 
static void setMockPrefix (Socket &socket, const std::string &mockPrefix)
 Set the mock prefix (where to find/save it)
 

Detailed Description

Backend to use Mock library with PAbtractSocket.

Definition at line 38 of file PMockBackend.h.

Member Typedef Documentation

◆ Message

typedef DataStreamMsg PMockBackend::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 59 of file PMockBackend.cpp.

59 {
60
61}

Member Function Documentation

◆ client()

PMockBackend::Param PMockBackend::client ( )
static

Create param for a client socket.

Returns
corresponding parameters

Definition at line 66 of file PMockBackend.cpp.

66 {
68 return param;
69}
PMockParam Param
Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketM...

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 216 of file PMockBackend.cpp.

216 {
217 if(socket.param.isMockRecord){
218 data_save(socket.fileNameMessage, socket.vecMessage);
219 }
220}
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.

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

◆ createClientSocket()

bool PMockBackend::createClientSocket ( PMockBackend::Socket & socket,
const std::string & address,
size_t port,
const PMockParam & param )
static

Create a client socket.

Parameters
[out]socket: socket to be created
address: address of the server, the client has to connect to
port: port to be used for the connection
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 85 of file PMockBackend.cpp.

85 {
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}
std::string mockPrefix
Prefix where to find/save mock files.
size_t eventIndex
Index of the current message.

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

Referenced by createServerSocket().

+ Here is the caller graph for this function:

◆ createServerSocket()

bool PMockBackend::createServerSocket ( PMockBackend::Socket & socket,
const std::string & address,
size_t port,
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 108 of file PMockBackend.cpp.

108 {
109 return createClientSocket(socket, address, port, param);
110}
static bool createClientSocket(Socket &socket, const std::string &address, size_t port, 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 226 of file PMockBackend.cpp.

226 {
227 return true;
228}

◆ 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 244 of file PMockBackend.cpp.

244 {
245 size_t dataSize(mockMsg.size());
246 PMockBackend::msgResize(msg, dataSize);
247 memcpy(PMockBackend::msgData(msg), mockMsg.data(), dataSize);
248}
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 201 of file PMockBackend.cpp.

201 {
202 return (const DataStreamIter)msg.data();
203}

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 209 of file PMockBackend.cpp.

209 {
210 return msg.data();
211}

◆ 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 185 of file PMockBackend.cpp.

185 {
186 msg.resize(sizeMsg);
187}

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 193 of file PMockBackend.cpp.

193 {
194 return msg.size();
195}

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 234 of file PMockBackend.cpp.

234 {
235 size_t dataSize(PMockBackend::msgSize(msg));
236 mockMsg.resize(dataSize);
237 memcpy(mockMsg.data(), PMockBackend::msgData(msg), dataSize);
238}
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 163 of file PMockBackend.cpp.

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

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 135 of file PMockBackend.cpp.

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

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

◆ server()

PMockBackend::Param PMockBackend::server ( )
static

Create param for a server socket.

Returns
corresponding parameters

Definition at line 74 of file PMockBackend.cpp.

74 {
75 return PMockBackend::client();
76}
static Param client()
Create param for a client socket.

References client().

+ Here is the call graph for this function:

◆ setIsMockRecord()

void PMockBackend::setIsMockRecord ( Socket & socket,
bool isMockRecord )
static

Set the mock prefix (where to find/save it)

Parameters
[out]socket: socket to be modified
isMockRecord: true if the mock has to be recorded

Definition at line 124 of file PMockBackend.cpp.

124 {
125 socket.param.isMockRecord = isMockRecord;
126}

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

◆ setMockPrefix()

void PMockBackend::setMockPrefix ( Socket & socket,
const std::string & mockPrefix )
static

Set the mock prefix (where to find/save it)

Parameters
[out]socket: socket to be modified
mockPrefix: prefix of the mock to find or write it

Definition at line 116 of file PMockBackend.cpp.

116 {
117 socket.param.mockPrefix = mockPrefix;
118}

References PMockParam::mockPrefix, and PMockSocket::param.


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