| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 10 | #include "PGenericSocketManager.h" | ||
| 11 | |||
| 12 | ///Default constructor of PGenericSocketManager | ||
| 13 | /** @param mode : Mode of the Socket (no mock, mock, mock_record) | ||
| 14 | */ | ||
| 15 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 16 |
2/2✓ Branch 0 (3→4) taken 4 times.
✓ Branch 2 (4→5) taken 4 times.
|
4 | PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::PGenericSocketManager(PSocketMode::PSocketMode mode){ |
| 17 | 4 | initialisationPGenericSocketManager(mode); | |
| 18 | 4 | } | |
| 19 | |||
| 20 | ///Destructor of PGenericSocketManager | ||
| 21 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 22 | 4 | PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::~PGenericSocketManager(){ | |
| 23 | 4 | clear(); | |
| 24 | 4 | } | |
| 25 | |||
| 26 | ///Set if the current PGenericSocketManager is a mock | ||
| 27 | /** @param mode : Mode of the Socket (no mock, mock, mock_record) | ||
| 28 | */ | ||
| 29 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 30 | 1 | void PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::setMode(PSocketMode::PSocketMode mode){ | |
| 31 | 1 | p_mode = mode; | |
| 32 |
1/2✗ Branch 0 (8→3) not taken.
✓ Branch 1 (8→9) taken 1 times.
|
1 | 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 | 1 | } | |
| 36 | |||
| 37 | ///Get if the current PGenericSocketManager is a mock | ||
| 38 | /** @return Mode of the Socket (no mock, mock, mock_record) | ||
| 39 | */ | ||
| 40 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 41 | PSocketMode::PSocketMode PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::getMode() const{ | ||
| 42 | return p_mode; | ||
| 43 | } | ||
| 44 | |||
| 45 | ///Create a client socket | ||
| 46 | /** @param name : name (key) to get the socket | ||
| 47 | * @param socketParam : parameters of the socket (host, port, send/recv timeout) | ||
| 48 | * @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
| 49 | * @param mockPrefix : prefix to find and szve the mock | ||
| 50 | * @param mockParam : parameters to initialise the mock | ||
| 51 | * @return true if the socket has been created, false otherwise | ||
| 52 | */ | ||
| 53 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 54 | 2 | bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::addClientSocket(const _TSocketKey & name, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam) | |
| 55 | { | ||
| 56 |
2/5✓ Branch 0 (3→4) taken 2 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 2 times.
✗ Branch 4 (12→13) not taken.
✗ Branch 5 (12→14) not taken.
|
2 | PGenericSocket<_TBackend, _TMockBackend>* socket = new PGenericSocket<_TBackend, _TMockBackend>(p_mode); |
| 57 | 2 | bool b(socket != NULL); | |
| 58 |
1/2✓ Branch 0 (6→7) taken 2 times.
✗ Branch 1 (6→10) not taken.
|
2 | if(b){ |
| 59 | 2 | b &= socket->createClientSocket(p_backend, p_mockBackend, socketParam, param, mockPrefix, mockParam); | |
| 60 | //Add socket in any case to be able to destroy it in the destructor | ||
| 61 | 2 | p_mapSocket[name] = socket; //Check if there is no problem with failed socket creation which are destroyed afterward | |
| 62 | } | ||
| 63 | 2 | return b; | |
| 64 | } | ||
| 65 | |||
| 66 | ///Create a server socket | ||
| 67 | /** @param name : name (key) to get the socket | ||
| 68 | * @param socketParam : parameters of the socket (host, port, send/recv timeout) | ||
| 69 | * @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
| 70 | * @param mockPrefix : prefix to find and szve the mock | ||
| 71 | * @param mockParam : parameters to initialise the mock | ||
| 72 | * @return true if the socket has been created, false otherwise | ||
| 73 | */ | ||
| 74 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 75 | 2 | bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::addServerSocket(const _TSocketKey & name, const PSocketParam & socketParam, const typename _TBackend::Param & param, const std::string & mockPrefix, const typename _TMockBackend::Param & mockParam) | |
| 76 | { | ||
| 77 |
2/5✓ Branch 0 (3→4) taken 2 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 2 times.
✗ Branch 4 (12→13) not taken.
✗ Branch 5 (12→14) not taken.
|
2 | PGenericSocket<_TBackend, _TMockBackend>* socket = new PGenericSocket<_TBackend, _TMockBackend>(p_mode); |
| 78 | 2 | bool b(socket != NULL); | |
| 79 |
1/2✓ Branch 0 (6→7) taken 2 times.
✗ Branch 1 (6→10) not taken.
|
2 | if(b){ |
| 80 | 2 | b &= socket->createServerSocket(p_backend, p_mockBackend, socketParam, param, mockPrefix, mockParam); | |
| 81 | //Add socket in any case to be able to destroy it in the destructor | ||
| 82 | 2 | p_mapSocket[name] = socket; //Check if there is not problem with failed socket creation which are destroyed afterward | |
| 83 | } | ||
| 84 | 2 | return b; | |
| 85 | } | ||
| 86 | |||
| 87 | ///Remove the given socket | ||
| 88 | /** @param name : name (key) of the socket to be removed | ||
| 89 | */ | ||
| 90 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 91 | void PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::removeSocket(const _TSocketKey & name){ | ||
| 92 | typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.find(name)); | ||
| 93 | if(it != p_mapSocket.end()){ | ||
| 94 | delete it->second; | ||
| 95 | p_mapSocket.erase(it); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | ///Clear the map of socket | ||
| 100 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 101 | 4 | void PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::clear(){ | |
| 102 |
2/2✓ Branch 0 (11→3) taken 4 times.
✓ Branch 1 (11→12) taken 4 times.
|
8 | for(typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.begin()); it != p_mapSocket.end(); ++it){ |
| 103 |
1/1✓ Branch 0 (4→5) taken 4 times.
|
4 | it->second->close(); |
| 104 |
1/2✓ Branch 0 (6→7) taken 4 times.
✗ Branch 1 (6→8) not taken.
|
4 | delete it->second; |
| 105 | } | ||
| 106 | 4 | p_mapSocket.clear(); | |
| 107 | 4 | } | |
| 108 | |||
| 109 | ///Send message on the given socket | ||
| 110 | /** @param name : name of the socket to be used | ||
| 111 | * @param msg : message to be sent | ||
| 112 | * @param flag : flags to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
| 113 | * @return PSendStatus::PSendStatus | ||
| 114 | */ | ||
| 115 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 116 | 10 | PSendStatus::PSendStatus PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::sendMsg(const _TSocketKey & name, typename _TBackend::Message & msg, PSendFlag::PSendFlag flag){ | |
| 117 | 10 | PGenericSocket<_TBackend, _TMockBackend> * socket = getSocket(name); | |
| 118 |
1/2✓ Branch 0 (3→4) taken 10 times.
✗ Branch 1 (3→6) not taken.
|
10 | if(socket != NULL){ |
| 119 | 10 | return socket->sendMsg(msg, flag); | |
| 120 | }else{ | ||
| 121 | ✗ | return PSendStatus::BROKEN_SOCKET; | |
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | ///Receive message from the given socket | ||
| 126 | /** @param name : name of the socket to be used | ||
| 127 | * @param msg : message to be received | ||
| 128 | * @param flag : flags to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
| 129 | * @return PRecvStatus::PRecvStatus | ||
| 130 | */ | ||
| 131 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 132 | 10 | PRecvStatus::PRecvStatus PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::recvMsg(const _TSocketKey & name, typename _TBackend::Message & msg, PRecvFlag::PRecvFlag flag){ | |
| 133 | 10 | PGenericSocket<_TBackend, _TMockBackend> * socket = getSocket(name); | |
| 134 |
1/2✓ Branch 0 (3→4) taken 10 times.
✗ Branch 1 (3→6) not taken.
|
10 | if(socket != NULL){ |
| 135 | 10 | return socket->recvMsg(msg, flag); | |
| 136 | }else{ | ||
| 137 | ✗ | return PRecvStatus::BROKEN_SOCKET; | |
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | ///Get a socket by name (or key) | ||
| 142 | /** @param name : of the socket to be used | ||
| 143 | * @return pointer to the found socket, or NULL if the socket does not exist | ||
| 144 | */ | ||
| 145 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 146 | 42 | PGenericSocket<_TBackend, _TMockBackend>* PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::getSocket(const _TSocketKey & name) | |
| 147 | { | ||
| 148 |
1/1✓ Branch 0 (2→3) taken 42 times.
|
42 | typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::iterator it(p_mapSocket.find(name)); |
| 149 |
1/2✓ Branch 0 (5→6) taken 42 times.
✗ Branch 1 (5→8) not taken.
|
42 | if(it != p_mapSocket.end()){ |
| 150 | 42 | return it->second; | |
| 151 | }else{ | ||
| 152 | ✗ | return NULL; | |
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | ///Say if the socket exist with the given name | ||
| 157 | /** @param name : of the socket to be used | ||
| 158 | * @return true if the socket exists, false otherwise | ||
| 159 | */ | ||
| 160 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 161 | bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::isSocketExist(const _TSocketKey & name) const{ | ||
| 162 | typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::const_iterator it(p_mapSocket.find(name)); | ||
| 163 | return it != p_mapSocket.end(); | ||
| 164 | } | ||
| 165 | |||
| 166 | ///Say if the given socket is connected | ||
| 167 | /** @param name : name of the socket to be checked | ||
| 168 | * @return true if the socket exists and is connected, false otherwise | ||
| 169 | */ | ||
| 170 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 171 | 2 | bool PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::isConnected(const _TSocketKey & name) const{ | |
| 172 |
1/1✓ Branch 0 (2→3) taken 2 times.
|
2 | typename std::map<_TSocketKey, PGenericSocket<_TBackend, _TMockBackend>* >::const_iterator it(p_mapSocket.find(name)); |
| 173 |
1/2✓ Branch 0 (5→6) taken 2 times.
✗ Branch 1 (5→9) not taken.
|
2 | if(it != p_mapSocket.end()){ |
| 174 |
1/1✓ Branch 0 (7→8) taken 2 times.
|
2 | return it->second->isConnected(); |
| 175 | } | ||
| 176 | ✗ | return false; | |
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | ///Initialisation function of the class PGenericSocketManager | ||
| 181 | /** @param mode : Mode of the Socket (no mock, mock, mock_record) | ||
| 182 | */ | ||
| 183 | template<typename _TSocketKey, typename _TBackend, typename _TMockBackend> | ||
| 184 | 4 | void PGenericSocketManager<_TSocketKey, _TBackend, _TMockBackend>::initialisationPGenericSocketManager(PSocketMode::PSocketMode mode){ | |
| 185 | 4 | p_mode = mode; | |
| 186 | 4 | } | |
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | #endif | ||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 |