PhoenixSocket  4.0.1
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 <unistd.h>
11
12#include "PSocketMode.h"
13#include "PSocketFlag.h"
14#include "phoenix_mock_socket.h"
15
17template<typename _TBackend, typename _TMockBackend>
19 public:
21 virtual ~PGenericSocket();
22
23 bool createClientSocket(_TBackend & backend, _TMockBackend & mockBackend, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam);
24 bool createServerSocket(_TBackend & backend, _TMockBackend & mockBackend, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam);
25
27
29
33 template<typename U>
37 sendStatus = p_mockSocket.sendData(data, flag);
38 }
39 // If we dont test if sendStatus is OK, we will always send the message even if the mock backend is not connected
40 // I don't know if it is a good idea to do so
41 // 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
42 // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
43
44 if(p_mode != PSocketMode::MOCK && sendStatus == PSendStatus::OK){
45 sendStatus = p_socket.sendData(data, flag);
46 }
47 return sendStatus;
48 }
49 PSendStatus::PSendStatus sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag);
50
52
56 template<typename U>
59 if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
60 recvStatus = p_socket.recvData(data, flag);
61 }else if(p_mode == PSocketMode::MOCK){ //Mock mode
62 recvStatus = p_mockSocket.recvData(data, flag);
63 }else{ //Mock record mode
64 recvStatus = p_socket.recvData(data, flag);
65 //If no message was recieved, we record it anyway
66 //Maybe we do not want to erase the status of the real socket with the mock backend, so no recvStatus = p_mockSocket.recvData(data, flag);
67 p_mockSocket.recvData(data, flag);
68 }
69 return recvStatus;
70 }
71
72 PRecvStatus::PRecvStatus recvMsg(typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag);
73
74 void close();
75 bool isConnected() const;
76 bool waitUntilConnection(uint64_t refreshTimer, size_t nbRetry) const;
77 private:
79
82
84 typename _TBackend::Socket p_socket;
86 typename _TMockBackend::Socket p_mockSocket;
87};
88
89#include "PGenericSocket_impl.h"
90
91
92#endif
93
PGenericSocket(PSocketMode::PSocketMode mode)
Default constructor of PGenericSocket.
PSendStatus::PSendStatus sendData(const U &data, PSendFlag::PSendFlag flag)
Send message on the given socket.
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)
PRecvStatus::PRecvStatus recvData(U &data, PRecvFlag::PRecvFlag flag)
Receive message from the given socket.
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