PhoenixSocket  4.0.1
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
PMockSocket Class Reference

Example of a mock socket with vector of messages. More...

#include <PMockBackend.h>

+ Collaboration diagram for PMockSocket:

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.
 

Public Member Functions

void close ()
 Close the PMockSocket.
 
bool createClientSocket (const PSocketParam &socketParam, const Param &extraParam)
 Initialise a client socket.
 
bool createServerSocket (const PSocketParam &socketParam, const Param &extraParam)
 Initialise a server socket.
 
bool isConnected () const
 Say if the PMockSocket is connected.
 
 PMockSocket ()
 Default constructor of hte PMockSocket.
 
template<typename T>
PRecvStatus::PRecvStatus recvData (T &data, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
 Recieved data with the socket.
 
PRecvStatus::PRecvStatus recvMsg (Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
 Recieved data with the socket.
 
template<typename T>
PSendStatus::PSendStatus sendData (const T &data, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
 Send data with the socket.
 
PSendStatus::PSendStatus sendMsg (const Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
 Specialisation to send a Message with the socket.
 
void setIsMockRecord (bool isMockRecord)
 Set the mock prefix (where to find/save it)
 
void setMockPrefix (const std::string &mockPrefix)
 Set the mock prefix (where to find/save it)
 
virtual ~PMockSocket ()
 Default destructor of hte PMockSocket.
 

Private Attributes

Param p_extraParam
 Extra parameters of the socket.
 
PGenericVecMock< DataStreamMsg > p_mock
 Mock handler.
 
PSocketParam p_socketParam
 Parameters of the socket.
 

Detailed Description

Example of a mock socket with vector of messages.

Definition at line 17 of file PMockBackend.h.

Member Typedef Documentation

◆ Message

typedef DataStreamMsg PMockSocket::Message

Define the type of message used by the PAbstractSocketManager.

Definition at line 20 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 22 of file PMockBackend.h.

Constructor & Destructor Documentation

◆ PMockSocket()

PMockSocket::PMockSocket ( )

Default constructor of hte PMockSocket.

Definition at line 27 of file PMockBackend.cpp.

27 {
28
29}

◆ ~PMockSocket()

PMockSocket::~PMockSocket ( )
virtual

Default destructor of hte PMockSocket.

Definition at line 32 of file PMockBackend.cpp.

32 {
33
34}

Member Function Documentation

◆ close()

void PMockSocket::close ( )

Close the PMockSocket.

Definition at line 112 of file PMockBackend.cpp.

112 {
113 p_mock.close();
114}
PGenericVecMock< DataStreamMsg > p_mock
Mock handler.

References p_mock.

◆ createClientSocket()

bool PMockSocket::createClientSocket ( const PSocketParam & socketParam,
const Param & extraParam )

Initialise a client socket.

Parameters
socketParam: parameters to be use to initialise the socket (hostname, port, etc)
extraParam: extra customisable parameters for the creation of the socket (depends on the backend)

Definition at line 40 of file PMockBackend.cpp.

40 {
41 p_socketParam = socketParam;
42 p_extraParam = extraParam;
43
44 return true;
45}
Param p_extraParam
Extra parameters of the socket.
PSocketParam p_socketParam
Parameters of the socket.

References p_extraParam, and p_socketParam.

Referenced by PMockBackend::createClientSocket(), createServerSocket(), and phoenix_createMockBackend().

+ Here is the caller graph for this function:

◆ createServerSocket()

bool PMockSocket::createServerSocket ( const PSocketParam & socketParam,
const Param & extraParam )

Initialise a server socket.

Parameters
socketParam: parameters to be use to initialise the socket (hostname, port, etc)
extraParam: extra customisable parameters for the creation of the socket (depends on the backend)

Definition at line 51 of file PMockBackend.cpp.

51 {
52 return createClientSocket(socketParam, extraParam);
53}
bool createClientSocket(const PSocketParam &socketParam, const Param &extraParam)
Initialise a client socket.

References createClientSocket().

+ Here is the call graph for this function:

◆ isConnected()

bool PMockSocket::isConnected ( ) const

Say if the PMockSocket is connected.

Returns
true if the PMockSocket is connected, false otherwise

Definition at line 107 of file PMockBackend.cpp.

107 {
108 return true;
109}

◆ recvData()

template<typename T>
PRecvStatus::PRecvStatus PMockSocket::recvData ( T & data,
PRecvFlag::PRecvFlag flag = PRecvFlag::BLOCK )

Recieved data with the socket.

Parameters
[out]data: data to be recieved with the socket
flag: recieving flag (BLOCK, NON_BLOCK)
Returns
status of the recv

Definition at line 61 of file PMockBackend_impl.h.

61 {
64 if(p_mock.getIsRecord()){
65 if(socket_serialize_message(msg, data)){
66 return recvMsg(msg, flag);
67 }else{
69 }
70 }
71 else{
72 recvStatus = recvMsg(msg, flag);
73 //If the message is empty we cannot initialise the given data, so, this is an error
74 if(msg.size() != 0lu){
75 DataStreamIter iter = (DataStreamIter)msg.data();
76 if(!data_message_load<T>(iter, data)){
78 }
79 }else{
81 }
82 }
83 return recvStatus;
84}
bool socket_serialize_message(PMockSocket::Message &msg, const T &data)
Serialize a message from data.
DataStreamMsg Message
Define the type of message used by the PAbstractSocketManager.
PRecvStatus::PRecvStatus recvMsg(Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
Recieved data with the socket.
PRecvStatus
describe the result of the recv
Definition PSocketFlag.h:52
@ CANNOT_DESERIALIZE_DATA
Definition PSocketFlag.h:60
@ NO_MESSAGE_RECEIVED
Definition PSocketFlag.h:54

References PRecvStatus::CANNOT_DESERIALIZE_DATA, PRecvStatus::NO_MESSAGE_RECEIVED, PRecvStatus::OK, p_mock, recvMsg(), and socket_serialize_message().

+ Here is the call graph for this function:

◆ recvMsg()

Recieved data with the socket.

Parameters
[out]msg: Message to be recieved with the socket
flag: recieving flag (BLOCK, NON_BLOCK)
Returns
status of the recv

Definition at line 90 of file PMockBackend.cpp.

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

References PRecvStatus::NO_MESSAGE_RECEIVED, PRecvStatus::OK, and p_mock.

Referenced by recvData().

+ Here is the caller graph for this function:

◆ sendData()

template<typename T>
PSendStatus::PSendStatus PMockSocket::sendData ( const T & data,
PSendFlag::PSendFlag flag = PSendFlag::BLOCK )

Send data with the socket.

Parameters
data: data to be sent with the socket
flag: sending flag (BLOCK, NON_BLOCK)
Returns
status of the send

Definition at line 32 of file PMockBackend_impl.h.

32 {
33 if(p_mock.getIsRecord()){
35 if(socket_serialize_message(msg, data)){
36 return sendMsg(msg, flag);
37 }else{
39 }
40 }else{
41 T referenceData;
42 PMockSocket::Message referenceMsg;
43 p_mock.getCurrentValue(referenceMsg); //Let's get the reference message
44 if(referenceMsg.size() != 0lu){
45 DataStreamIter iter = (DataStreamIter)referenceMsg.data();
46 check_stream_assert(data_message_load<T>(iter, referenceData));
47 std::stringstream index;
48 index << p_mock.getCurrentIndex();
49 check_stream_assert(phoenix_check_stream("mock '"+p_mock.getFileName()+"', [mock index = "+index.str()+"]", data, referenceData));
50 }
51 return PSendStatus::OK;
52 }
53}
PSendStatus::PSendStatus sendMsg(const Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
Specialisation to send a Message with the socket.
@ CANNOT_SERIALIZE_DATA
Definition PSocketFlag.h:32

References PSendStatus::CANNOT_SERIALIZE_DATA, PSendStatus::OK, p_mock, sendMsg(), and socket_serialize_message().

+ Here is the call graph for this function:

◆ sendMsg()

PSendStatus::PSendStatus PMockSocket::sendMsg ( const Message & msg,
PSendFlag::PSendFlag flag = PSendFlag::BLOCK )

Specialisation to send a Message with the socket.

Parameters
msg: Message to be sent
flag: sending flag (BLOCK, NON_BLOCK)
Returns
status of the send

Definition at line 76 of file PMockBackend.cpp.

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

References PSendStatus::OK, and p_mock.

Referenced by sendData().

+ Here is the caller graph for this function:

◆ setIsMockRecord()

void PMockSocket::setIsMockRecord ( bool isMockRecord)

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

Parameters
isMockRecord: true if the mock has to be recorded

Definition at line 67 of file PMockBackend.cpp.

67 {
68 p_mock.setIsRecord(isMockRecord);
69}

References p_mock.

Referenced by phoenix_createMockBackend().

+ Here is the caller graph for this function:

◆ setMockPrefix()

void PMockSocket::setMockPrefix ( const std::string & mockPrefix)

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

Parameters
mockPrefix: prefix of the mock to find or write it

Definition at line 58 of file PMockBackend.cpp.

58 {
59 std::stringstream socketFileName;
60 socketFileName << mockPrefix << p_socketParam.hostname << "_" << p_socketParam.port << ".pmockbackend";
61 p_mock.setFileName(socketFileName.str());
62}

References p_mock, and p_socketParam.

Referenced by phoenix_createMockBackend().

+ Here is the caller graph for this function:

Member Data Documentation

◆ p_extraParam

Param PMockSocket::p_extraParam
private

Extra parameters of the socket.

Definition at line 49 of file PMockBackend.h.

Referenced by createClientSocket().

◆ p_mock

PGenericVecMock<DataStreamMsg> PMockSocket::p_mock
private

Mock handler.

Definition at line 45 of file PMockBackend.h.

Referenced by close(), recvData(), recvMsg(), sendData(), sendMsg(), setIsMockRecord(), and setMockPrefix().

◆ p_socketParam

PSocketParam PMockSocket::p_socketParam
private

Parameters of the socket.

Definition at line 47 of file PMockBackend.h.

Referenced by createClientSocket(), and setMockPrefix().


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