PhoenixZMQ  2.0.0
Library which integrates zeromq use in Phoenix
PGenericSocket.h
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 #ifndef __PGENERIC_SOCKET_H__
8 #define __PGENERIC_SOCKET_H__
9 
10 #include "PSocketMode.h"
11 #include "PSocketFlag.h"
12 #include "phoenix_mock_socket.h"
13 
15 template<typename _TBackend, typename _TMockBackend>
17  public:
19  virtual ~PGenericSocket();
20 
21  bool createClientSocket(const typename _TBackend::Param & param, const typename _TMockBackend::Param & mockParam);
22  bool createServerSocket(const typename _TBackend::Param & param, const typename _TMockBackend::Param & mockParam);
23 
25 
29  template<typename U>
30  bool sendData(const U & data, PSendFlag::PSendFlag flag){
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  }
62 
63  bool sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag);
64 
66 
70  template<typename U>
71  bool recvData(U & data, PRecvFlag::PRecvFlag flag){
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  }
111 
112  bool recvMsg(typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag);
113 
114  void close();
115  bool isConnected() const;
116 
117  private:
119 
122 
124  typename _TBackend::Socket p_socket;
126  typename _TMockBackend::Socket p_mockSocket;
127 };
128 
129 #include "PGenericSocket_impl.h"
130 
131 
132 #endif
133 
Abstract socket which has a mock mode to avoid heavy socket backend for unit tests.
PGenericSocket(PSocketMode::PSocketMode mode)
Default constructor of PGenericSocket.
void close()
Close the socket.
_TMockBackend::Socket p_mockSocket
Socket to be used with the mock backend.
bool recvMsg(typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag)
Recieve message from the given socket.
bool recvData(U &data, PRecvFlag::PRecvFlag flag)
Recieve message from the given socket.
_TBackend::Socket p_socket
Socket to be used with the classical backend.
bool isConnected() const
Say if the Socket is connected.
virtual ~PGenericSocket()
Destructor of PGenericSocket.
bool sendData(const U &data, PSendFlag::PSendFlag flag)
Send message on the given socket.
PSocketMode::PSocketMode p_mode
Mode of the Socket (no mock, mock, mock_record)
bool sendMsg(typename _TBackend::Message &msg, PSendFlag::PSendFlag flag)
Send message on the given socket.
bool createClientSocket(const typename _TBackend::Param &param, const typename _TMockBackend::Param &mockParam)
Create a client socket.
void initialisationPGenericSocket(PSocketMode::PSocketMode mode)
Initialisation function of the class PGenericSocket.
bool createServerSocket(const typename _TBackend::Param &param, const typename _TMockBackend::Param &mockParam)
Create a server socket.
DataStreamType * DataStreamIter
PRecvFlag
describe the recieving flag of the Socket
Definition: PSocketFlag.h:20
PSendFlag
describe the sending flag of the Socket
Definition: PSocketFlag.h:12
PSocketMode
describe the mode of the Socket
Definition: PSocketMode.h:12