PhoenixSocket  6.1.0
Library which integrates socket 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
19bool phoenix_initializeMockSocket(PMockSocket & mock, const PSocketParam & socketParam, const std::string & prefix, const PMockParam & extraParam){
20 mock.setIsMockRecord(true);
21 bool b = mock.createClientSocket(socketParam, extraParam);
22 mock.setMockPrefix(prefix);
23 return b;
24}
25
30
35
37
40bool PMockSocket::createClientSocket(const PSocketParam & socketParam, const PMockSocketGenerator::Param & extraParam){
41 p_socketParam = socketParam;
42 p_extraParam = extraParam;
43
44 return true;
45}
46
48
51bool PMockSocket::createServerSocket(const PSocketParam & socketParam, const PMockSocketGenerator::Param & extraParam){
52 return createClientSocket(socketParam, extraParam);
53}
54
56
58void PMockSocket::setMockPrefix(const std::string & mockPrefix){
59 std::stringstream socketFileName;
60 socketFileName << mockPrefix << p_socketParam.hostname << "_" << p_socketParam.port << ".pmockbackend";
61 p_mock.setFileName(socketFileName.str());
62}
63
65
67void PMockSocket::setIsMockRecord(bool isMockRecord){
68 p_mock.setIsRecord(isMockRecord);
69}
70
72
77 if(p_mock.getIsRecord()){
78 p_mock.append(msg);
79 }else{
80 check_stream_assert(p_mock.checkCurrentValue("PMockSocket::sendMsg", msg));
81 }
82 return PSendStatus::OK;
83}
84
86
91 if(p_mock.getIsRecord()){
92 p_mock.append(msg);
93 return PRecvStatus::OK;
94 }else{
95 p_mock.getCurrentValue(msg);
96 if(msg.size() == 0lu){ //If the message is empty, it is not recieved
98 }else{
99 return PRecvStatus::OK;
100 }
101 }
102}
103
105
108 return true;
109}
110
113 p_mock.close();
114}
115
116
117
122
124
130
132
137
139
145 return socket.createClientSocket(socketParam, param);
146}
147
149
155 return createClientSocket(socket, socketParam, param);
156}
157
159
163 msg.resize(sizeMsg);
164}
165
167
171 return msg.size();
172}
173
175
179 return (const DataStreamIter)msg.data();
180}
181
183
187 return msg.data();
188}
189
191
194void PMockSocketGenerator::msgToMock(DataStreamMsg & mockMsg, const PMockSocketGenerator::Message & msg){
195 size_t dataSize(PMockSocketGenerator::msgSize(msg));
196 mockMsg.resize(dataSize);
197 memcpy(mockMsg.data(), PMockSocketGenerator::msgData(msg), dataSize);
198}
199
201
205 size_t dataSize(mockMsg.size());
206 PMockSocketGenerator::msgResize(msg, dataSize);
207 memcpy(PMockSocketGenerator::msgData(msg), mockMsg.data(), dataSize);
208}
209
210
bool phoenix_initializeMockSocket(PMockSocket &mock, const PSocketParam &socketParam, const std::string &prefix, const PMockParam &extraParam)
Create a mock backend.
static size_t msgSize(const Message &msg)
Get the size of a message.
PMockParam Param
Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketM...
bool createServerSocket(Socket &socket, const PSocketParam &socketParam, const PMockParam &param)
Create a client socket.
bool createClientSocket(Socket &socket, const PSocketParam &socketParam, const PMockParam &param)
Create a client socket.
static void msgToMock(DataStreamMsg &mockMsg, const Message &msg)
Copy current backend message data into mock message.
DataStreamMsg Message
Define the type of message used by the PAbstractSocketManager.
PMockSocket Socket
Define the socket of the backend used by the PAbstractSocketManager.
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 void msgResize(Message &msg, size_t sizeMsg)
Resize a message.
static Param client()
Create param for a client socket.
static Param server()
Create param for a server socket.
PMockSocketGenerator()
Default constructor of PMockBackend.
Example of a mock socket with vector of messages.
PGenericVecMock< DataStreamMsg > p_mock
Mock handler.
PMockSocket()
Default constructor of hte PMockSocket.
Param p_extraParam
Extra parameters of the socket.
PSocketParam p_socketParam
Parameters of the socket.
void close()
Close the PMockSocket.
PSendStatus::PSendStatus sendMsg(const Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
Specialisation to send a Message with the socket.
bool createServerSocket(const PSocketParam &socketParam, const Param &extraParam)
Initialise a server socket.
bool createClientSocket(const PSocketParam &socketParam, const Param &extraParam)
Initialise a client socket.
PRecvStatus::PRecvStatus recvMsg(Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
Recieved data with the socket.
bool isConnected() const
Say if the PMockSocket is connected.
void setIsMockRecord(bool isMockRecord)
Set the mock prefix (where to find/save it)
virtual ~PMockSocket()
Default destructor of hte PMockSocket.
void setMockPrefix(const std::string &mockPrefix)
Set the mock prefix (where to find/save it)
PRecvFlag
describe the receiving flag of the Socket
Definition PSocketFlag.h:35
PRecvStatus
describe the result of the recv
Definition PSocketFlag.h:43
@ NO_MESSAGE_RECEIVED
Definition PSocketFlag.h:45
PSendFlag
describe the sending flag of the Socket
Definition PSocketFlag.h:14
PSendStatus
describe the result of the send
Definition PSocketFlag.h:22
Set of parameters to be passed to create a socket with mock backend.
Parameters to create a socket.
Definition PSocketFlag.h:56