PhoenixSocket  4.0.1
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
PGenericSocket_impl.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_IMPL__
8#define __PGENERIC_SOCKET_H_IMPL__
9
10#include "PGenericSocket.h"
11
13
15template<typename _TBackend, typename _TMockBackend>
19
21template<typename _TBackend, typename _TMockBackend>
25
27
34template<typename _TBackend, typename _TMockBackend>
35bool PGenericSocket<_TBackend, _TMockBackend>::createClientSocket(_TBackend & backend, _TMockBackend & mockBackend, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam){
36 bool b(true);
37 b &= mockBackend.createClientSocket(p_mockSocket, socketParam, mockParam);
38 b &= backend.createClientSocket(p_socket, socketParam, param);
39 p_mockSocket.setMockPrefix(mockPrefix);
41 return b;
42}
43
45
52template<typename _TBackend, typename _TMockBackend>
53bool PGenericSocket<_TBackend, _TMockBackend>::createServerSocket(_TBackend & backend, _TMockBackend & mockBackend, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam){
54 bool b(true);
55 b &= mockBackend.createServerSocket(p_mockSocket, socketParam, mockParam);
56 b &= backend.createServerSocket(p_socket, socketParam, param);
57 p_mockSocket.setMockPrefix(mockPrefix);
59 return b;
60}
61
63
65template<typename _TBackend, typename _TMockBackend>
70
72
76template<typename _TBackend, typename _TMockBackend>
79 //This is normal to call the mock backend on a send, because it will check if the send message is the expected one
80 //by respected to the recorded mock
82 typename _TMockBackend::Message msgMock;
83 _TBackend::msgToMock(msgMock, msg);
84 sendStatus = p_mockSocket.sendMsg(msgMock, flag);
85 }
86
87 // If we dont test if b is true, we will always send the message even if the mock backend is not connected
88 // I don't know if it is a good idea to do so
89 // 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
90 // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
91
92 if(p_mode != PSocketMode::MOCK && sendStatus == PSendStatus::OK){
93 sendStatus = p_socket.sendMsg(msg, flag);
94 }
95 return sendStatus;
96}
97
99
103template<typename _TBackend, typename _TMockBackend>
106 if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
107 recvStatus = p_socket.recvMsg(msg, flag);
108 }else if(p_mode == PSocketMode::MOCK){ //Mock mode
109 typename _TMockBackend::Message msgMock;
110 recvStatus = p_mockSocket.recvMsg(msgMock, flag);
111 _TBackend::mockToMsg(msg, msgMock);
112 }else{ //Mock record mode
113 recvStatus = p_socket.recvMsg(msg, flag);
114 if(recvStatus == PRecvStatus::OK){
115 typename _TMockBackend::Message msgMock;
116 _TBackend::msgToMock(msgMock, msg);
117 recvStatus = p_mockSocket.recvMsg(msgMock, flag);
118 }else{
119 typename _TMockBackend::Message emptyMsg;
120 recvStatus = p_mockSocket.recvMsg(emptyMsg, flag);
121 }
122 }
123 return recvStatus;
124}
125
127template<typename _TBackend, typename _TMockBackend>
132
134
136template<typename _TBackend, typename _TMockBackend>
138 bool b(true);
140 b &= p_mockSocket.isConnected();
141 }
143 b &= p_socket.isConnected();
144 }
145 return b;
146}
147
152template<typename _TBackend, typename _TMockBackend>
153bool PGenericSocket<_TBackend, _TMockBackend>::waitUntilConnection(size_t refreshTimer, size_t nbRetry) const {
154 size_t counter(0);
155 bool b(false);
156 while(!b && counter < nbRetry) {
157 b = isConnected();
158 //FIXME : use the clock backend instead of usleep
159 usleep(refreshTimer);
160 ++counter;
161 }
162 return b;
163}
164
166
168template<typename _TBackend, typename _TMockBackend>
172
173
174#endif
175
176
177
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.
PSendStatus::PSendStatus sendMsg(typename _TBackend::Message &msg, PSendFlag::PSendFlag flag)
Send message on the given socket.
_TBackend::Socket p_socket
Socket to be used with the classical backend.
bool isConnected() const
Say if the Socket is connected.
PRecvStatus::PRecvStatus recvMsg(typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag)
Receive message from the given socket.
virtual ~PGenericSocket()
Destructor of PGenericSocket.
bool waitUntilConnection(uint64_t refreshTimer, size_t nbRetry) const
Wait until the socket is connected.
bool createClientSocket(_TBackend &backend, _TMockBackend &mockBackend, const PSocketParam &socketParam, const typename _TBackend::Param &param, const std::string &mockPrefix, const typename _TMockBackend::Param &mockParam)
Create a client socket.
bool createServerSocket(_TBackend &backend, _TMockBackend &mockBackend, const PSocketParam &socketParam, const typename _TBackend::Param &param, const std::string &mockPrefix, const typename _TMockBackend::Param &mockParam)
Create a server socket.
PSocketMode::PSocketMode p_mode
Mode of the Socket (no mock, mock, mock_record)
void initialisationPGenericSocket(PSocketMode::PSocketMode mode)
Initialisation function of the class PGenericSocket.
void setMode(PSocketMode::PSocketMode mode)
Set the mode of the generic socket.
PRecvFlag
describe the receiving flag of the Socket
Definition PSocketFlag.h:41
PRecvStatus
describe the result of the recv
Definition PSocketFlag.h:52
PSendFlag
describe the sending flag of the Socket
Definition PSocketFlag.h:14
PSendStatus
describe the result of the send
Definition PSocketFlag.h:25
PSocketMode
describe the mode of the Socket
Definition PSocketMode.h:12
Parameters to create a socket.
Definition PSocketFlag.h:68