PhoenixSocket  1.0.0
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 (const std::string &host, size_t port, const typename _TBackend::Param &param, const std::string &mockPrefix, const typename _TMockBackend::Param &mockParam)
 Create a client socket.
 
bool createClientSocket (const typename _TBackend::Param &param, const typename _TMockBackend::Param &mockParam)
 Create a client socket.
 
bool createServerSocket (const std::string &host, size_t port, const typename _TBackend::Param &param, const std::string &mockPrefix, const typename _TMockBackend::Param &mockParam)
 Create a server socket.
 
bool createServerSocket (const typename _TBackend::Param &param, 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>
bool recvData (U &data, PRecvFlag::PRecvFlag flag)
 Recieve message from the given socket.
 
bool recvMsg (typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag)
 Recieve message from the given socket.
 
template<typename U>
bool sendData (const U &data, PSendFlag::PSendFlag flag)
 Send message on the given socket.
 
bool 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.
 
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 16 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 165 of file PGenericSocket_impl.h.

165 {
168}
_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() [1/2]

template<typename _TBackend, typename _TMockBackend>
bool PGenericSocket< _TBackend, _TMockBackend >::createClientSocket ( const std::string & host,
size_t port,
const typename _TBackend::Param & param,
const std::string & mockPrefix,
const typename _TMockBackend::Param & mockParam )

Create a client socket.

Parameters
host: connection host
port: connection port
param: extra parameters of the backend
mockPrefix: prefix where to find/save the mock
mockParam: extra parameters of the mock

Definition at line 68 of file PGenericSocket_impl.h.

68 {
69 bool b(true);
74
75 return b;
76}
PSocketMode::PSocketMode p_mode
Mode of the Socket (no mock, mock, mock_record)

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

◆ createClientSocket() [2/2]

template<typename _TBackend, typename _TMockBackend>
bool PGenericSocket< _TBackend, _TMockBackend >::createClientSocket ( const typename _TBackend::Param & param,
const typename _TMockBackend::Param & mockParam )

Create a client socket.

Parameters
param: extra customisable parameters for the creation of the socket (depends on the backend)
mockParam: parameters of mock backend
Returns
true if the socket has been created, false otherwise

Definition at line 32 of file PGenericSocket_impl.h.

32 {
33 bool b(true);
36 }
39 }
40 return b;
41}

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

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

+ Here is the caller graph for this function:

◆ createServerSocket() [1/2]

template<typename _TBackend, typename _TMockBackend>
bool PGenericSocket< _TBackend, _TMockBackend >::createServerSocket ( const std::string & host,
size_t port,
const typename _TBackend::Param & param,
const std::string & mockPrefix,
const typename _TMockBackend::Param & mockParam )

Create a server socket.

Parameters
host: connection host
port: connection port
param: extra parameters of the backend
mockPrefix: prefix where to find/save the mock
mockParam: extra parameters of the mock

Definition at line 86 of file PGenericSocket_impl.h.

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

◆ createServerSocket() [2/2]

template<typename _TBackend, typename _TMockBackend>
bool PGenericSocket< _TBackend, _TMockBackend >::createServerSocket ( const typename _TBackend::Param & param,
const typename _TMockBackend::Param & mockParam )

Create a server socket.

Parameters
param: extra customisable parameters for the creation of the socket (depends on the backend)
mockParam: parameters of mock backend
Returns
true if the socket has been created, false otherwise

Definition at line 49 of file PGenericSocket_impl.h.

49 {
50 bool b(true);
53 }
56 }
57 return b;
58}

References PSocketMode::MOCK, PSocketMode::NO_MOCK, 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 189 of file PGenericSocket_impl.h.

189 {
190 p_mode = mode;
191}

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

174 {
175 bool b(true);
178 }
181 }
182 return b;
183}

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

◆ recvData()

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

Recieve message from the given socket.

Parameters
data: data to be recieved
flag: flag to be used to send the message (BLOCK, NON_BLOCK, etc)
Returns
true on success, false otherwise

Definition at line 77 of file PGenericSocket.h.

77 {
78 bool b(true);
79 if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
80 typename _TBackend::Message msg;
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){
87 }
88 }else if(p_mode == PSocketMode::MOCK){ //Mock mode
91 //If the message is empty we cannot initialise the given data, so, this is an error
93 if(b){
96 }
97 }else{ //Mock record mode
98 typename _TBackend::Message msg;
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){
105 //Let's convert the message into the mock backend
109 }
110 else {
113 }
114 }
115 return b;
116 }

References PSocketMode::MOCK, PSocketMode::NO_MOCK, 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>
bool PGenericSocket< _TBackend, _TMockBackend >::recvMsg ( typename _TBackend::Message & msg,
PRecvFlag::PRecvFlag flag )

Recieve message from the given socket.

Parameters
msg: message to be recieved
flag: flags to be used to send the message (BLOCK, NON_BLOCK, etc)
Returns
true on success, false otherwise

Definition at line 138 of file PGenericSocket_impl.h.

138 {
139 bool b(true);
140 if(p_mode == PSocketMode::NO_MOCK){ //Normal mode
142 }else if(p_mode == PSocketMode::MOCK){ //Mock mode
145 if(b){
147 }
148 }else{ //Mock record mode
150 if(b){
154 }
155 else {
158 }
159 }
160 return b;
161}

References PSocketMode::MOCK, PSocketMode::NO_MOCK, 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>
bool 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
true on success, false otherwise

Definition at line 36 of file PGenericSocket.h.

36 {
37 size_t dataSize(data_size<U>(data));
38 bool b(true);
39
44 if(data_message_save<U>(iter, data)){ //Save the message
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;
60 if(data_message_save<U>(iter, data)){ //Save the message
62 }else{
63 b = false;
64 }
65 }
66 return b;
67 }

References PSocketMode::MOCK, PSocketMode::NO_MOCK, 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>
bool 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
true on success, false otherwise

Definition at line 111 of file PGenericSocket_impl.h.

111 {
112 bool b(true);
113 //This is normal to call the mock backend on a send, because it will check if the send message is the expected one
114 //by respected to the recorded mock
119 }
120
121 // If we dont test if b is true, we will always send the message even if the mock backend is not connected
122 // I don't know if it is a good idea to do so
123 // 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
124 // Because the boolean value is not checked. So the real backend could have sent corretly the message and the mock not....
125
126 if(p_mode != PSocketMode::MOCK && b){
128 }
129 return b;
130}

References PSocketMode::MOCK, PSocketMode::NO_MOCK, 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 100 of file PGenericSocket_impl.h.

References PSocketMode::MOCK_RECORD, p_mockSocket, and p_mode.

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

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

◆ p_mode

template<typename _TBackend, typename _TMockBackend>
PSocketMode::PSocketMode PGenericSocket< _TBackend, _TMockBackend >::p_mode
private

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

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


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