PhoenixSocket  4.0.1
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
PMockBackendFile.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 "PMockBackendFile.h"
11
13
19bool phoenix_createMockBackend(PMockSocketFile & mock, const PSocketParam & socketParam, const std::string & prefix, const PMockParamFile & 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 PMockSocketFile::createClientSocket(const PSocketParam & socketParam, const PMockSocketFile::Param & extraParam){
41 p_socketParam = socketParam;
42 p_extraParam = extraParam;
43 return true;
44}
45
47
50bool PMockSocketFile::createServerSocket(const PSocketParam & socketParam, const PMockSocketFile::Param & extraParam){
51 return createClientSocket(socketParam, extraParam);
52}
53
55
57void PMockSocketFile::setMockPrefix(const std::string & mockPrefix){
58 std::stringstream socketFileName;
59 socketFileName << mockPrefix << p_socketParam.hostname << "_" << p_socketParam.port << ".pmockbackend";
60 p_mock.setFileName(socketFileName.str());
61}
62
64
66void PMockSocketFile::setIsMockRecord(bool isMockRecord){
67 p_mock.setIsRecord(isMockRecord);
68}
69
71
76 if(p_mock.getIsRecord()){
77 p_mock.append(msg);
78 }else{
79 check_stream_assert(p_mock.checkCurrentValue("PMockSocketFile::sendMsg", msg));
80 }
81 return PSendStatus::OK;
82}
83
85
90 if(p_mock.getIsRecord()){
91 p_mock.append(msg);
92 return PRecvStatus::OK;
93 }else{
94 p_mock.getCurrentValue(msg);
95 if(msg.size() == 0lu){ //If the message is empty, it is not received
97 }else{
98 return PRecvStatus::OK;
99 }
100 }
101}
102
104
107 return true;
108}
109
112 p_mock.close();
113}
114
117
120
122
128
130
135
137
143 return socket.createClientSocket(socketParam, param);
144}
145
147
153 return createClientSocket(socket, socketParam, param);
154}
155
157
161 msg.resize(sizeMsg);
162}
163
165
169 return msg.size();
170}
171
173
176const DataStreamIter PMockBackendFile::msgData(const PMockBackendFile::Message & msg){
177 return (const DataStreamIter)msg.data();
178}
179
181
185 return msg.data();
186}
187
189
192void PMockBackendFile::msgToMock(DataStreamMsg & mockMsg, const PMockBackendFile::Message & msg){
193 size_t dataSize(PMockBackendFile::msgSize(msg));
194 mockMsg.resize(dataSize);
195 memcpy(mockMsg.data(), PMockBackendFile::msgData(msg), dataSize);
196}
197
199
202void PMockBackendFile::mockToMsg(PMockBackendFile::Message & msg, DataStreamMsg & mockMsg){
203 size_t dataSize(mockMsg.size());
204 PMockBackendFile::msgResize(msg, dataSize);
205 memcpy(PMockBackendFile::msgData(msg), mockMsg.data(), dataSize);
206}
207
208
bool phoenix_createMockBackend(PMockSocketFile &mock, const PSocketParam &socketParam, const std::string &prefix, const PMockParamFile &extraParam)
Create a mock backend file.
PMockParamFile Param
Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketM...
static void msgToMock(DataStreamMsg &mockMsg, const Message &msg)
Copy current backend message data into mock message.
static void msgResize(Message &msg, size_t sizeMsg)
Resize a message.
DataStreamMsg Message
Define the type of message used by the PAbstractSocketManager.
bool createServerSocket(Socket &socket, const PSocketParam &socketParam, const Param &param)
Create a client socket.
static const DataStreamIter msgData(const Message &msg)
Get the data of a message.
virtual ~PMockBackendFile()
Destructor of PMockBackendFile.
static Param client()
Create param for a client socket.
PMockSocketFile Socket
Define the socket of the backend used by the PAbstractSocketManager.
bool createClientSocket(Socket &socket, const PSocketParam &socketParam, const Param &param)
Create a client socket.
static size_t msgSize(const Message &msg)
Get the size of a message.
static void mockToMsg(Message &msg, DataStreamMsg &mockMsg)
Copy mock message data into current backend message.
PMockBackendFile()
Default constructor of PMockBackendFile.
static Param server()
Create param for a server socket.
Example of a mock socket with vector of messages.
PRecvStatus::PRecvStatus recvMsg(Message &msg, PRecvFlag::PRecvFlag flag)
Recieved data with the socket.
void setMockPrefix(const std::string &mockPrefix)
Set the mock prefix (where to find/save it)
Param p_extraParam
Extra parameters of the socket.
bool createClientSocket(const PSocketParam &socketParam, const Param &extraParam)
Initialise a client socket.
PMockParamFile Param
Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketM...
void setIsMockRecord(bool isMockRecord)
Set the mock prefix (where to find/save it)
PSocketParam p_socketParam
Parameters of the socket.
bool isConnected() const
Say if the PMockSocketFile is connected.
PMockSocketFile()
Default constructor of hte PMockSocketFile.
bool createServerSocket(const PSocketParam &socketParam, const Param &extraParam)
Initialise a server socket.
void close()
Close the PMockSocketFile.
PSendStatus::PSendStatus sendMsg(const Message &msg, PSendFlag::PSendFlag flag)
Specialisation to send a Message with the socket.
PGenericFileMock< DataStreamMsg > p_mock
Mock handler.
virtual ~PMockSocketFile()
Default destructor of hte PMockSocketFile.
DataStreamMsg Message
Define the type of message used by the PAbstractSocketManager.
PRecvFlag
describe the receiving flag of the Socket
Definition PSocketFlag.h:41
PRecvStatus
describe the result of the recv
Definition PSocketFlag.h:52
@ NO_MESSAGE_RECEIVED
Definition PSocketFlag.h:54
PSendFlag
describe the sending flag of the Socket
Definition PSocketFlag.h:14
PSendStatus
describe the result of the send
Definition PSocketFlag.h:25
Set of parameters to be passed to create a socket with mock backend.
Parameters to create a socket.
Definition PSocketFlag.h:68