1 | /************************************************************************* |
2 | * libjson-rpc-cpp |
3 | ************************************************************************* |
4 | * @file linuxtcpsocketclient.h |
5 | * @date 17.10.2016 |
6 | * @author Alexandre Poirot <alexandre.poirot@legrand.fr> |
7 | * @license See attached LICENSE.txt |
8 | ************************************************************************/ |
9 | |
10 | #ifndef JSONRPC_CPP_LINUXTCPSOCKETCLIENT_H_ |
11 | #define JSONRPC_CPP_LINUXTCPSOCKETCLIENT_H_ |
12 | |
13 | #include <jsonrpccpp/client/connectors/tcpsocketclient.h> |
14 | |
15 | namespace jsonrpc { |
16 | /** |
17 | * This class is the Linux/UNIX implementation of TCPSocketClient. |
18 | * It uses the POSIX socket API to performs its job. |
19 | */ |
20 | class LinuxTcpSocketClient : public IClientConnector { |
21 | public: |
22 | /** |
23 | * @brief LinuxTcpSocketClient, constructor of the Linux/UNIX implementation of class TcpSocketClient |
24 | * @param hostToConnect The hostname or the ipv4 address on which the client should try to connect |
25 | * @param port The port on which the client should try to connect |
26 | */ |
27 | LinuxTcpSocketClient(const std::string &hostToConnect, const unsigned int &port); |
28 | /** |
29 | * @brief ~LinuxTcpSocketClient, the destructor of LinuxTcpSocketClient |
30 | */ |
31 | virtual ~LinuxTcpSocketClient(); |
32 | /** |
33 | * @brief The real implementation of TcpSocketClient::SendRPCMessage method. |
34 | * @param message The message to send |
35 | * @param result The result of the call returned by the server |
36 | * @throw JsonRpcException Thrown when an issue is encountered with socket manipulation (see message of exception for more information about what happened). |
37 | */ |
38 | virtual void SendRPCMessage(const std::string &message, std::string &result); |
39 | |
40 | protected: |
41 | std::string hostToConnect; /*!< The hostname or the ipv4 address on which the client should try to connect*/ |
42 | unsigned int port; /*!< The port on which the client should try to connect*/ |
43 | /** |
44 | * @brief Connects to the host and port provided by constructor parameters. |
45 | * |
46 | * This method detects if the hostToConnect attribute is either an IPv4 or a hostname. |
47 | * On first case it tries to connect to the ip. |
48 | * On second case it tries to resolve hostname to an ip and tries to connect to it if resolve was successful. |
49 | * |
50 | * @returns A file descriptor to the successfully connected socket |
51 | * @throw JsonRpcException Thrown when an issue is encountered while trying to connect (see message of exception for more information about what happened). |
52 | */ |
53 | int Connect(); |
54 | /** |
55 | * @brief Connects to provided ip and port. |
56 | * |
57 | * This method tries to connect to the provided ip and port. |
58 | * |
59 | * @param ip The ipv4 address to connect to |
60 | * @param port The port to connect to |
61 | * @returns A file descriptor to the successfully connected socket |
62 | * @throw JsonRpcException Thrown when an issue is encountered while trying to connect (see message of exception for more information about what happened). |
63 | */ |
64 | int Connect(const std::string &ip, const int &port); |
65 | /** |
66 | * @brief Check if provided ip is an ipv4 address. |
67 | * |
68 | * @param ip The ipv4 address to check |
69 | * @returns A boolean indicating if the provided ip is or is not an ipv4 address |
70 | */ |
71 | bool IsIpv4Address(const std::string &ip); |
72 | }; |
73 | |
74 | } /* namespace jsonrpc */ |
75 | #endif /* JSONRPC_CPP_LINUXTCPSOCKETCLIENT_H_ */ |
76 | |