PhoenixSocket  1.0.0
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
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
15template<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
24
25 bool createClientSocket(const std::string & host, size_t port, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam);
26 bool createServerSocket(const std::string & host, size_t port, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam);
27
29
31
35 template<typename U>
36 bool sendData(const U & data, PSendFlag::PSendFlag flag){
37 size_t dataSize(data_size<U>(data));
38 bool b(true);
39
41 typename _TMockBackend::Message msg;
42 _TMockBackend::msgResize(msg, dataSize);
43 DataStreamIter iter = _TMockBackend::msgData(msg);
44 if(data_message_save<U>(iter, data)){ //Save the message
45 b &= _TMockBackend::send(p_mockSocket, msg, flag);
46 }else{
47 b = false;
48 }
49 }
50
51 // If we dont test if b is true, we will always send the message even if the mock backend is not connected
52 // I don't know if it is a good idea to do so
53 // 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
54 // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
55
56 if(p_mode != PSocketMode::MOCK && b){
57 typename _TBackend::Message msg;
58 _TBackend::msgResize(msg, dataSize);
59 DataStreamIter iter = _TBackend::msgData(msg);
60 if(data_message_save<U>(iter, data)){ //Save the message
61 b &= _TBackend::send(p_socket, msg, flag);
62 }else{
63 b = false;
64 }
65 }
66 return b;
67 }
68
69 bool sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag);
70
72
76 template<typename U>
77 bool recvData(U & data, PRecvFlag::PRecvFlag flag){
78 bool b(true);
79 if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
80 typename _TBackend::Message msg;
81 b &= _TBackend::recv(p_socket, msg, flag);
82 //If the message is empty we cannot initialise the given data, so, this is an error
83 b &= _TBackend::msgSize(msg) != 0lu;
84 if(b){
85 DataStreamIter iter = _TBackend::msgData(msg);
86 b &= data_message_load<U>(iter, data);
87 }
88 }else if(p_mode == PSocketMode::MOCK){ //Mock mode
89 typename _TMockBackend::Message msg;
90 b &= _TMockBackend::recv(p_mockSocket, msg, flag);
91 //If the message is empty we cannot initialise the given data, so, this is an error
92 b &= _TMockBackend::msgSize(msg) != 0lu;
93 if(b){
94 DataStreamIter iter = _TMockBackend::msgData(msg);
95 b &= data_message_load<U>(iter, data);
96 }
97 }else{ //Mock record mode
98 typename _TBackend::Message msg;
99 b &= _TBackend::recv(p_socket, msg, flag);
100 //If the message is empty we cannot initialise the given data, so, this is an error
101 b &= _TBackend::msgSize(msg) != 0lu;
102 if(b){
103 DataStreamIter iter = _TBackend::msgData(msg);
104 b &= data_message_load<U>(iter, data);
105 //Let's convert the message into the mock backend
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 }
117
118 bool recvMsg(typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag);
119
120 void close();
121 bool isConnected() const;
122
123 private:
125
128
130 typename _TBackend::Socket p_socket;
132 typename _TMockBackend::Socket p_mockSocket;
133};
134
135#include "PGenericSocket_impl.h"
136
137
138#endif
139
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.
void setMode(PSocketMode::PSocketMode mode)
Set the mode of the generic socket.
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