| 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_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 | |||
| 16 | ///@brief Abstract socket which has a mock mode to avoid heavy socket backend for unit tests | ||
| 17 | template<typename _TBackend, typename _TMockBackend> | ||
| 18 | class PGenericSocket{ | ||
| 19 | public: | ||
| 20 | PGenericSocket(PSocketMode::PSocketMode mode); | ||
| 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 | |||
| 26 | void setMode(PSocketMode::PSocketMode mode); | ||
| 27 | |||
| 28 | ///Send message on the given socket | ||
| 29 | /** @param data : data to be sent | ||
| 30 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
| 31 | * @return PSendStatus::PSendStatus | ||
| 32 | */ | ||
| 33 | template<typename U> | ||
| 34 | 10 | PSendStatus::PSendStatus sendData(const U & data, PSendFlag::PSendFlag flag){ | |
| 35 | 10 | PSendStatus::PSendStatus sendStatus = PSendStatus::OK; | |
| 36 |
1/2✓ Branch 0 (2→3) taken 10 times.
✗ Branch 1 (2→5) not taken.
|
10 | if(p_mode != PSocketMode::NO_MOCK){ |
| 37 | 10 | 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 |
2/4✓ Branch 0 (5→6) taken 10 times.
✗ Branch 1 (5→9) not taken.
✓ Branch 2 (6→7) taken 10 times.
✗ Branch 3 (6→9) not taken.
|
10 | if(p_mode != PSocketMode::MOCK && sendStatus == PSendStatus::OK){ |
| 45 | 10 | sendStatus = p_socket.sendData(data, flag); | |
| 46 | } | ||
| 47 | 10 | return sendStatus; | |
| 48 | } | ||
| 49 | PSendStatus::PSendStatus sendMsg(typename _TBackend::Message & msg, PSendFlag::PSendFlag flag); | ||
| 50 | |||
| 51 | ///Receive message from the given socket | ||
| 52 | /** @param data : data to be received | ||
| 53 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
| 54 | * @return PRecvStatus::PRecvStatus | ||
| 55 | */ | ||
| 56 | template<typename U> | ||
| 57 | 10 | PRecvStatus::PRecvStatus recvData(U & data, PRecvFlag::PRecvFlag flag){ | |
| 58 | 10 | PRecvStatus::PRecvStatus recvStatus = PRecvStatus::OK; | |
| 59 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 10 times.
|
10 | if(p_mode == PSocketMode::NO_MOCK){ //Normal mode |
| 60 | ✗ | recvStatus = p_socket.recvData(data, flag); | |
| 61 |
1/2✓ Branch 0 (5→6) taken 10 times.
✗ Branch 1 (5→8) not taken.
|
10 | }else if(p_mode == PSocketMode::MOCK){ //Mock mode |
| 62 | 10 | 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 | 10 | 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: | ||
| 78 | void initialisationPGenericSocket(PSocketMode::PSocketMode mode); | ||
| 79 | |||
| 80 | ///Mode of the Socket (no mock, mock, mock_record) | ||
| 81 | PSocketMode::PSocketMode p_mode; | ||
| 82 | |||
| 83 | ///Socket to be used with the classical backend | ||
| 84 | typename _TBackend::Socket p_socket; | ||
| 85 | ///Socket to be used with the mock backend | ||
| 86 | typename _TMockBackend::Socket p_mockSocket; | ||
| 87 | }; | ||
| 88 | |||
| 89 | #include "PGenericSocket_impl.h" | ||
| 90 | |||
| 91 | |||
| 92 | #endif | ||
| 93 | |||
| 94 |