PhoenixSocket  4.0.1
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
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.
 
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.
 
bool isConnected () const
 Say if the Socket is connected.
 
 PGenericSocket (PSocketMode::PSocketMode mode)
 Default constructor of PGenericSocket.
 
template<typename U>
PRecvStatus::PRecvStatus recvData (U &data, PRecvFlag::PRecvFlag flag)
 Receive message from the given socket.
 
PRecvStatus::PRecvStatus recvMsg (typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag)
 Receive message from the given socket.
 
template<typename U>
PSendStatus::PSendStatus sendData (const U &data, PSendFlag::PSendFlag flag)
 Send message on the given socket.
 
PSendStatus::PSendStatus sendMsg (typename _TBackend::Message &msg, PSendFlag::PSendFlag flag)
 Send message on the given socket.
 
void setMode (PSocketMode::PSocketMode mode)
 Set the mode of the generic socket.
 
bool waitUntilConnection (uint64_t refreshTimer, size_t nbRetry) const
 Wait until the socket is connected.
 
virtual ~PGenericSocket ()
 Destructor of PGenericSocket.
 

Private Member Functions

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

Private Attributes

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

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 18 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}
Abstract socket which has a mock mode to avoid heavy socket backend for unit tests.
void initialisationPGenericSocket(PSocketMode::PSocketMode mode)
Initialisation function of the class PGenericSocket.

References initialisationPGenericSocket().

+ Here is the call graph for this function:

◆ ~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.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ close()

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

Close the socket.

Definition at line 128 of file PGenericSocket_impl.h.

128 {
129 p_mockSocket.close();
130 p_socket.close();
131}
_TMockBackend::Socket p_mockSocket
Socket to be used with the mock backend.
_TBackend::Socket p_socket
Socket to be used with the classical backend.

References p_mockSocket, and p_socket.

Referenced by ~PGenericSocket().

+ Here is the caller graph for this function:

◆ createClientSocket()

template<typename _TBackend, typename _TMockBackend>
bool 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 )

Create a client socket.

Parameters
backend: instanciated backend of the socket
mockBackend: instanciated backend of the mock socket
socketParam: parameters of the socket (host, port, send/recv timeout)
param: extra parameters of the backend
mockPrefix: prefix where to find/save the mock
mockParam: extra parameters of the mock

Definition at line 35 of file PGenericSocket_impl.h.

35 {
36 bool b(true);
39 p_mockSocket.setMockPrefix(mockPrefix);
41 return b;
42}
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.
PSocketMode::PSocketMode p_mode
Mode of the Socket (no mock, mock, mock_record)

References PSocketMode::MOCK_RECORD, p_mockSocket, p_mode, and p_socket.

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 ( _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.

Parameters
backend: instanciated backend of the socket
mockBackend: instanciated backend of the mock socket
socketParam: parameters of the socket (host, port, send/recv timeout)
param: extra parameters of the backend
mockPrefix: prefix where to find/save the mock
mockParam: extra parameters of the mock

Definition at line 53 of file PGenericSocket_impl.h.

53 {
54 bool b(true);
57 p_mockSocket.setMockPrefix(mockPrefix);
59 return b;
60}
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.

References PSocketMode::MOCK_RECORD, p_mockSocket, p_mode, and p_socket.

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 169 of file PGenericSocket_impl.h.

169 {
170 p_mode = mode;
171}

References p_mode.

Referenced by PGenericSocket().

+ Here is the caller graph for this function:

◆ isConnected()

template<typename _TBackend, typename _TMockBackend>
bool PGenericSocket< _TBackend, _TMockBackend >::isConnected ( ) const

Say if the Socket is connected.

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

Definition at line 137 of file PGenericSocket_impl.h.

137 {
138 bool b(true);
140 b &= p_mockSocket.isConnected();
141 }
143 b &= p_socket.isConnected();
144 }
145 return b;
146}

References PSocketMode::MOCK, PSocketMode::NO_MOCK, p_mockSocket, p_mode, and p_socket.

Referenced by waitUntilConnection().

+ Here is the caller graph for this function:

◆ recvData()

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

Receive message from the given socket.

Parameters
data: data to be received
flag: flag to be used to send the message (BLOCK, NON_BLOCK, etc)
Returns
PRecvStatus::PRecvStatus

Definition at line 57 of file PGenericSocket.h.

57 {
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 }

References PSocketMode::MOCK, PSocketMode::NO_MOCK, PRecvStatus::OK, p_mockSocket, p_mode, and p_socket.

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

+ Here is the caller graph for this function:

◆ recvMsg()

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

Receive message from the given socket.

Parameters
msg: message to be received
flag: flags to be used to send the message (BLOCK, NON_BLOCK, etc)
Returns
PRecvStatus::PRecvStatus

Definition at line 104 of file PGenericSocket_impl.h.

104 {
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
112 }else{ //Mock record mode
113 recvStatus = p_socket.recvMsg(msg, flag);
118 }else{
121 }
122 }
123 return recvStatus;
124}

References PSocketMode::MOCK, PSocketMode::NO_MOCK, PRecvStatus::OK, p_mockSocket, p_mode, and p_socket.

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

+ Here is the caller graph for this function:

◆ sendData()

template<typename _TBackend, typename _TMockBackend>
template<typename U>
PSendStatus::PSendStatus 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
PSendStatus::PSendStatus

Definition at line 34 of file PGenericSocket.h.

34 {
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
45 sendStatus = p_socket.sendData(data, flag);
46 }
47 return sendStatus;
48 }

References PSocketMode::MOCK, PSocketMode::NO_MOCK, PSendStatus::OK, p_mockSocket, p_mode, and p_socket.

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

+ Here is the caller graph for this function:

◆ sendMsg()

template<typename _TBackend, typename _TMockBackend>
PSendStatus::PSendStatus 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
PSendStatus::PSendStatus

Definition at line 77 of file PGenericSocket_impl.h.

77 {
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
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
93 sendStatus = p_socket.sendMsg(msg, flag);
94 }
95 return sendStatus;
96}

References PSocketMode::MOCK, PSocketMode::NO_MOCK, PSendStatus::OK, p_mockSocket, p_mode, and p_socket.

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

+ Here is the caller graph for this function:

◆ setMode()

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

Set the mode of the generic socket.

Parameters
mode: mode of the PGenericSocket (NO_MOCK, MOCK, MOCK_RECORD)

Definition at line 66 of file PGenericSocket_impl.h.

66 {
67 p_mode = mode;
68 p_mockSocket.setIsMockRecord(mode == PSocketMode::MOCK_RECORD);
69}

References PSocketMode::MOCK_RECORD, p_mockSocket, and p_mode.

◆ waitUntilConnection()

template<typename _TBackend, typename _TMockBackend>
bool PGenericSocket< _TBackend, _TMockBackend >::waitUntilConnection ( uint64_t refreshTimer,
size_t nbRetry ) const

Wait until the socket is connected.

Parameters
refreshTimer: time in µs between two checks
nbRetry: number of retries before giving up
Returns
true if the socket is connected, false otherwise

Definition at line 153 of file PGenericSocket_impl.h.

153 {
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
160 ++counter;
161 }
162 return b;
163}
bool isConnected() const
Say if the Socket is connected.

References isConnected().

+ Here is the call 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 86 of file PGenericSocket.h.

Referenced by close(), createClientSocket(), createServerSocket(), isConnected(), recvData(), recvMsg(), sendData(), sendMsg(), and setMode().

◆ 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 81 of file PGenericSocket.h.

Referenced by createClientSocket(), createServerSocket(), initialisationPGenericSocket(), isConnected(), recvData(), recvMsg(), sendData(), sendMsg(), and setMode().

◆ 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 84 of file PGenericSocket.h.

Referenced by close(), createClientSocket(), createServerSocket(), isConnected(), recvData(), recvMsg(), sendData(), and sendMsg().


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