PhoenixZMQ  2.0.0
Library which integrates zeromq use in Phoenix
PGenericSocket< _TBackend, _TMockBackend > Class Template Reference

Abstract socket which has a mock mode to avoid heavy socket backend for unit tests. More...

#include <PGenericSocket.h>

Public Member Functions

void close ()
 Close the socket. More...
 
bool createClientSocket (const typename _TBackend::Param &param, const typename _TMockBackend::Param &mockParam)
 Create a client socket. More...
 
bool createServerSocket (const typename _TBackend::Param &param, const typename _TMockBackend::Param &mockParam)
 Create a server socket. More...
 
bool isConnected () const
 Say if the Socket is connected. More...
 
 PGenericSocket (PSocketMode::PSocketMode mode)
 Default constructor of PGenericSocket. More...
 
template<typename U >
bool recvData (U &data, PRecvFlag::PRecvFlag flag)
 Recieve message from the given socket. More...
 
bool recvMsg (typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag)
 Recieve message from the given socket. More...
 
template<typename U >
bool sendData (const U &data, PSendFlag::PSendFlag flag)
 Send message on the given socket. More...
 
bool sendMsg (typename _TBackend::Message &msg, PSendFlag::PSendFlag flag)
 Send message on the given socket. More...
 
virtual ~PGenericSocket ()
 Destructor of PGenericSocket. More...
 

Private Member Functions

void initialisationPGenericSocket (PSocketMode::PSocketMode mode)
 Initialisation function of the class PGenericSocket. More...
 

Private Attributes

_TMockBackend::Socket p_mockSocket
 Socket to be used with the mock backend. More...
 
PSocketMode::PSocketMode p_mode
 Mode of the Socket (no mock, mock, mock_record) More...
 
_TBackend::Socket p_socket
 Socket to be used with the classical backend. More...
 

Detailed Description

template<typename _TBackend, typename _TMockBackend>
class PGenericSocket< _TBackend, _TMockBackend >

Abstract socket which has a mock mode to avoid heavy socket backend for unit tests.

Definition at line 16 of file PGenericSocket.h.

Constructor & Destructor Documentation

◆ PGenericSocket()

template<typename _TBackend , typename _TMockBackend >
PGenericSocket< _TBackend, _TMockBackend >::PGenericSocket ( PSocketMode::PSocketMode  mode)

Default constructor of PGenericSocket.

Parameters
mode: Mode of the Socket (no mock, mock, mock_record)

Definition at line 16 of file PGenericSocket_impl.h.

16  {
18 }
void initialisationPGenericSocket(PSocketMode::PSocketMode mode)
Initialisation function of the class PGenericSocket.

◆ ~PGenericSocket()

template<typename _TBackend , typename _TMockBackend >
PGenericSocket< _TBackend, _TMockBackend >::~PGenericSocket
virtual

Destructor of PGenericSocket.

Definition at line 22 of file PGenericSocket_impl.h.

22  {
23  close();
24 }
void close()
Close the socket.

Member Function Documentation

◆ close()

template<typename _TBackend , typename _TMockBackend >
void PGenericSocket< _TBackend, _TMockBackend >::close

Close the socket.

Definition at line 120 of file PGenericSocket_impl.h.

120  {
121  if(p_mode == PSocketMode::MOCK){
122  _TMockBackend::close(p_mockSocket);
123  }else if(p_mode == PSocketMode::NO_MOCK){
124  _TBackend::close(p_socket);
125  }else{
126  _TMockBackend::close(p_mockSocket);
127  _TBackend::close(p_socket);
128  }
129 }
_TMockBackend::Socket p_mockSocket
Socket to be used with the mock backend.
_TBackend::Socket p_socket
Socket to be used with the classical backend.
PSocketMode::PSocketMode p_mode
Mode of the Socket (no mock, mock, mock_record)

References PSocketMode::MOCK, and PSocketMode::NO_MOCK.

◆ createClientSocket()

template<typename _TBackend , typename _TMockBackend >
bool PGenericSocket< _TBackend, _TMockBackend >::createClientSocket ( const typename _TBackend::Param &  param,
const typename _TMockBackend::Param &  mockParam 
)

Create a client socket.

Parameters
param: extra customisable parameters for the creation of the socket (depends on the backend)
mockParam: parameters of mock backend
Returns
true if the socket has been created, false otherwise

Definition at line 32 of file PGenericSocket_impl.h.

32  {
33  bool b(true);
35  b &= _TMockBackend::createClientSocket(p_mockSocket, mockParam);
36  }
38  b &= _TBackend::createClientSocket(p_socket, param);
39  }
40  return b;
41 }

References PSocketMode::MOCK, and PSocketMode::NO_MOCK.

Referenced by PGenericSocketManager< _TSocketKey, _TBackend, _TMockBackend >::addClientSocket().

+ Here is the caller graph for this function:

◆ createServerSocket()

template<typename _TBackend , typename _TMockBackend >
bool PGenericSocket< _TBackend, _TMockBackend >::createServerSocket ( const typename _TBackend::Param &  param,
const typename _TMockBackend::Param &  mockParam 
)

Create a server socket.

Parameters
param: extra customisable parameters for the creation of the socket (depends on the backend)
mockParam: parameters of mock backend
Returns
true if the socket has been created, false otherwise

Definition at line 49 of file PGenericSocket_impl.h.

49  {
50  bool b(true);
52  b &= _TMockBackend::createServerSocket(p_mockSocket, mockParam);
53  }
55  b &= _TBackend::createServerSocket(p_socket, param);
56  }
57  return b;
58 }

References PSocketMode::MOCK, and PSocketMode::NO_MOCK.

Referenced by PGenericSocketManager< _TSocketKey, _TBackend, _TMockBackend >::addServerSocket().

+ Here is the caller graph for this function:

◆ initialisationPGenericSocket()

template<typename _TBackend , typename _TMockBackend >
void PGenericSocket< _TBackend, _TMockBackend >::initialisationPGenericSocket ( PSocketMode::PSocketMode  mode)
private

Initialisation function of the class PGenericSocket.

Parameters
mode: Mode of the Socket (no mock, mock, mock_record)

Definition at line 150 of file PGenericSocket_impl.h.

150  {
151  p_mode = mode;
152 }

◆ isConnected()

template<typename _TBackend , typename _TMockBackend >
bool PGenericSocket< _TBackend, _TMockBackend >::isConnected

Say if the Socket is connected.

Returns
true if the current socket is connected (or both in record mode)

Definition at line 135 of file PGenericSocket_impl.h.

135  {
136  bool b(true);
138  b &= _TMockBackend::isConnected(p_mockSocket);
139  }
140  if(p_mode != PSocketMode::MOCK){
141  b &= _TBackend::isConnected(p_socket);
142  }
143  return b;
144 }

References PSocketMode::MOCK, and PSocketMode::NO_MOCK.

◆ recvData()

template<typename _TBackend , typename _TMockBackend >
template<typename U >
bool PGenericSocket< _TBackend, _TMockBackend >::recvData ( U &  data,
PRecvFlag::PRecvFlag  flag 
)
inline

Recieve message from the given socket.

Parameters
data: data 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 71 of file PGenericSocket.h.

71  {
72  bool b(true);
73  if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
74  typename _TBackend::Message msg;
75  b &= _TBackend::recv(p_socket, msg, flag);
76  //If the message is empty we cannot initialise the given data, so, this is an error
77  b &= _TBackend::msgSize(msg) != 0lu;
78  if(b){
79  DataStreamIter iter = _TBackend::msgData(msg);
80  b &= data_message_load<U>(iter, data);
81  }
82  }else if(p_mode == PSocketMode::MOCK){ //Mock mode
83  typename _TMockBackend::Message msg;
84  b &= _TMockBackend::recv(p_mockSocket, msg, flag);
85  //If the message is empty we cannot initialise the given data, so, this is an error
86  b &= _TMockBackend::msgSize(msg) != 0lu;
87  if(b){
88  DataStreamIter iter = _TMockBackend::msgData(msg);
89  b &= data_message_load<U>(iter, data);
90  }
91  }else{ //Mock record mode
92  typename _TBackend::Message msg;
93  b &= _TBackend::recv(p_socket, msg, flag);
94  //If the message is empty we cannot initialise the given data, so, this is an error
95  b &= _TBackend::msgSize(msg) != 0lu;
96  if(b){
97  DataStreamIter iter = _TBackend::msgData(msg);
98  b &= data_message_load<U>(iter, data);
99  //Let's convert the message into the mock backend
100  typename _TMockBackend::Message msgMock;
101  _TBackend::msgToMock(msgMock, msg);
102  b &= _TMockBackend::recv(p_mockSocket, msgMock, flag);
103  }
104  else {
105  typename _TMockBackend::Message empty_msg;
106  b &= _TMockBackend::recv(p_mockSocket, empty_msg, flag);
107  }
108  }
109  return b;
110  }
DataStreamType * DataStreamIter

References PSocketMode::MOCK, PSocketMode::NO_MOCK, PGenericSocket< _TBackend, _TMockBackend >::p_mockSocket, PGenericSocket< _TBackend, _TMockBackend >::p_mode, and PGenericSocket< _TBackend, _TMockBackend >::p_socket.

Referenced by PGenericSocketManager< _TSocketKey, _TBackend, _TMockBackend >::recvData().

+ Here is the caller graph for this function:

◆ recvMsg()

template<typename _TBackend , typename _TMockBackend >
bool PGenericSocket< _TBackend, _TMockBackend >::recvMsg ( typename _TBackend::Message &  msg,
PRecvFlag::PRecvFlag  flag 
)

Recieve message from the given socket.

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

Definition at line 93 of file PGenericSocket_impl.h.

93  {
94  bool b(true);
95  if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
96  b &= _TBackend::recv(p_socket, msg, flag);
97  }else if(p_mode == PSocketMode::MOCK){ //Mock mode
98  typename _TMockBackend::Message msgMock;
99  b &= _TMockBackend::recv(p_mockSocket, msgMock, flag);
100  if(b){
101  _TBackend::mockToMsg(msg, msgMock);
102  }
103  }else{ //Mock record mode
104  b &= _TBackend::recv(p_socket, msg, flag);
105  if(b){
106  typename _TMockBackend::Message msgMock;
107  _TBackend::msgToMock(msgMock, msg);
108  b &= _TMockBackend::recv(p_mockSocket, msgMock, flag);
109  }
110  else {
111  typename _TMockBackend::Message empty_msg;
112  b &= _TMockBackend::recv(p_mockSocket, empty_msg, flag);
113  }
114  }
115  return b;
116 }

References PSocketMode::MOCK, and PSocketMode::NO_MOCK.

Referenced by PGenericSocketManager< _TSocketKey, _TBackend, _TMockBackend >::recvMsg().

+ Here is the caller graph for this function:

◆ sendData()

template<typename _TBackend , typename _TMockBackend >
template<typename U >
bool PGenericSocket< _TBackend, _TMockBackend >::sendData ( const U &  data,
PSendFlag::PSendFlag  flag 
)
inline

Send message on the given socket.

Parameters
data: data 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 30 of file PGenericSocket.h.

30  {
31  size_t dataSize(data_size<U>(data));
32  bool b(true);
33 
35  typename _TMockBackend::Message msg;
36  _TMockBackend::msgResize(msg, dataSize);
37  DataStreamIter iter = _TMockBackend::msgData(msg);
38  if(data_message_save<U>(iter, data)){ //Save the message
39  b &= _TMockBackend::send(p_mockSocket, msg, flag);
40  }else{
41  b = false;
42  }
43  }
44 
45  // If we dont test if b is true, we will always send the message even if the mock backend is not connected
46  // I don't know if it is a good idea to do so
47  // If we do that, we might have somme issues if the user tries to end a message. The mock may give an error that will not be catch
48  // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
49 
50  if(p_mode != PSocketMode::MOCK && b){
51  typename _TBackend::Message msg;
52  _TBackend::msgResize(msg, dataSize);
53  DataStreamIter iter = _TBackend::msgData(msg);
54  if(data_message_save<U>(iter, data)){ //Save the message
55  b &= _TBackend::send(p_socket, msg, flag);
56  }else{
57  b = false;
58  }
59  }
60  return b;
61  }

References PSocketMode::MOCK, PSocketMode::NO_MOCK, PGenericSocket< _TBackend, _TMockBackend >::p_mockSocket, PGenericSocket< _TBackend, _TMockBackend >::p_mode, and PGenericSocket< _TBackend, _TMockBackend >::p_socket.

Referenced by PGenericSocketManager< _TSocketKey, _TBackend, _TMockBackend >::sendData().

+ Here is the caller graph for this function:

◆ sendMsg()

template<typename _TBackend , typename _TMockBackend >
bool PGenericSocket< _TBackend, _TMockBackend >::sendMsg ( typename _TBackend::Message &  msg,
PSendFlag::PSendFlag  flag 
)

Send message on the given socket.

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

Definition at line 66 of file PGenericSocket_impl.h.

66  {
67  bool b(true);
68  //This is normal to call the mock backend on a send, because it will check if the send message is the expected one
69  //by respected to the recorded mock
71  typename _TMockBackend::Message vecTmp;
72  _TBackend::msgToMock(vecTmp, msg);
73  b &= _TMockBackend::send(p_mockSocket, vecTmp, flag);
74  }
75 
76  // If we dont test if b is true, we will always send the message even if the mock backend is not connected
77  // I don't know if it is a good idea to do so
78  // If we do that, we might have somme issues if the user tries to end a message. The mock may give an error that will not be catch
79  // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
80 
81  if(p_mode != PSocketMode::MOCK && b){
82  b &= _TBackend::send(p_socket, msg, flag);
83  }
84  return b;
85 }

References PSocketMode::MOCK, and PSocketMode::NO_MOCK.

Referenced by PGenericSocketManager< _TSocketKey, _TBackend, _TMockBackend >::sendMsg().

+ Here is the caller graph for this function:

Member Data Documentation

◆ p_mockSocket

template<typename _TBackend , typename _TMockBackend >
_TMockBackend::Socket PGenericSocket< _TBackend, _TMockBackend >::p_mockSocket
private

Socket to be used with the mock backend.

Definition at line 126 of file PGenericSocket.h.

Referenced by PGenericSocket< _TBackend, _TMockBackend >::recvData(), and PGenericSocket< _TBackend, _TMockBackend >::sendData().

◆ p_mode

template<typename _TBackend , typename _TMockBackend >
PSocketMode::PSocketMode PGenericSocket< _TBackend, _TMockBackend >::p_mode
private

Mode of the Socket (no mock, mock, mock_record)

Definition at line 121 of file PGenericSocket.h.

Referenced by PGenericSocket< _TBackend, _TMockBackend >::recvData(), and PGenericSocket< _TBackend, _TMockBackend >::sendData().

◆ p_socket

template<typename _TBackend , typename _TMockBackend >
_TBackend::Socket PGenericSocket< _TBackend, _TMockBackend >::p_socket
private

Socket to be used with the classical backend.

Definition at line 124 of file PGenericSocket.h.

Referenced by PGenericSocket< _TBackend, _TMockBackend >::recvData(), and PGenericSocket< _TBackend, _TMockBackend >::sendData().


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