PhoenixSocket  1.0.0
Library which integrates socket unix use in Phoenix
Loading...
Searching...
No Matches
PGenericSocketManager_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_MANAGER_H_IMPL__
8#define __PGENERIC_SOCKET_MANAGER_H_IMPL__
9
11
13
15template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
19
21template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
25
27
29template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
31 p_mode = mode;
32 for(typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.begin()); it != p_mapSocket.end(); ++it){
33 it->second->setMode(mode);
34 }
35}
36
38
40template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
44
46
54template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
55bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::addClientSocket(const _TSocketKey & name, const std::string & host, size_t port, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam)
56{
58 bool b(socket != NULL);
59 if(b){
60 b &= socket->createClientSocket(host, port, param, mockPrefix, mockParam);
61 if(b){
62 p_mapSocket[name] = socket;
63 }
64 }
65 return b;
66}
67
69
77template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
78bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::addServerSocket(const _TSocketKey & name, const std::string & host, size_t port, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam)
79{
81 bool b(socket != NULL);
82 if(b){
83 b &= socket->createServerSocket(host, port, param, mockPrefix, mockParam);
84 if(b){
85 p_mapSocket[name] = socket;
86 }
87 }
88 return b;
89}
90
92
94template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
96 typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.find(name));
97 if(it != p_mapSocket.end()){
98 delete it->second;
99 p_mapSocket.erase(it);
100 }
101}
102
104template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
106 for(typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.begin()); it != p_mapSocket.end(); ++it){
107 it->second->close();
108 delete it->second;
109 }
110 p_mapSocket.clear();
111}
112
114
119template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
120bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::sendMsg(const _TSocketKey & name, typename _TBackend::Message & msg, PSendFlag::PSendFlag flag){
122 if(socket != NULL){
123 return socket->sendMsg(msg, flag);
124 }else{
125 return false;
126 }
127}
128
130
135template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
136bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::recvMsg(const _TSocketKey & name, typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag){
138 if(socket != NULL){
139 return socket->recvMsg(msg, flag);
140 }else{
141 return false;
142 }
143}
144
146
149template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
151{
152 typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.find(name));
153 if(it != p_mapSocket.end()){
154 return it->second;
155 }else{
156 return NULL;
157 }
158}
159
161
164template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
166 typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::const_iterator it(p_mapSocket.find(name));
167 return it != p_mapSocket.end();
168}
169
171
174template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
176 typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::const_iterator it(p_mapSocket.find(name));
177 if(it != p_mapSocket.end()){
178 return it->second->isConnected();
179 }
180 return false;
181}
182
183
185
187template<typename _TSocketKey, typename _TBackend, typename _TMockBackend>
191
192
193
194
195
196#endif
197
198
199
bool isSocketExist(const _TSocketKey &name) const
Say if the socket exist with the given name.
bool isConnected(const _TSocketKey &name) const
Say if the given socket is connected.
std::map< _TSocketKey, PGenericSocket< _TBackend, _TMockBackend > * > p_mapSocket
Map of the zmq sockets to be used by the manager.
bool sendMsg(const _TSocketKey &name, typename _TBackend::Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
Send message on the given socket.
PGenericSocket< _TBackend, _TMockBackend > * getSocket(const _TSocketKey &name)
Get a socket by name (or key)
PSocketMode::PSocketMode p_mode
Mode of the Socket (no mock, mock, mock_record)
bool addServerSocket(const _TSocketKey &name, 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.
void removeSocket(const _TSocketKey &name)
Remove the given socket.
PGenericSocketManager(PSocketMode::PSocketMode mode=PSocketMode::NO_MOCK)
Default constructor of PGenericSocketManager.
void setMode(PSocketMode::PSocketMode mode)
Set if the current PGenericSocketManager is a mock.
virtual ~PGenericSocketManager()
Destructor of PGenericSocketManager.
PSocketMode::PSocketMode getMode() const
Get if the current PGenericSocketManager is a mock.
bool recvMsg(const _TSocketKey &name, typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
Recieve message from the given socket.
void clear()
Clear the map of socket.
void initialisationPGenericSocketManager(PSocketMode::PSocketMode mode)
Initialisation function of the class PGenericSocketManager.
bool addClientSocket(const _TSocketKey &name, 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.
Abstract socket which has a mock mode to avoid heavy socket backend for unit tests.
bool recvMsg(typename _TBackend::Message &msg, PRecvFlag::PRecvFlag flag)
Recieve message from the given socket.
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.
bool createServerSocket(const typename _TBackend::Param &param, const typename _TMockBackend::Param &mockParam)
Create a server 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