1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* JsonRpc-Cpp - JSON-RPC implementation.
* Copyright (C) 2008-2011 Sebastien Vincent <sebastien.vincent@cppextrem.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file jsonrpc_httpclient.h
* \brief JSON-RPC HTTP client.
* \author Brian Panneton
*/
#ifndef JSONRPC_HTTPCLIENT_H
#define JSONRPC_HTTPCLIENT_H
#include <iostream>
#include <curl/curl.h>
#include "jsonrpc_client.h"
#include "system.h"
namespace Json
{
namespace Rpc
{
/**
*\class HttpClient
* \brief JSON-RPC Http client.
*/
class HttpClient : public Client
{
public:
/**
* \brief Constructor without address or port.
* \note Make sure you set them yourself.
*/
HttpClient();
/**
* \brief Destructor.
*/
virtual ~HttpClient();
/**
* \brief Constructor.
* \param address address URL
* \note Make sure you either set the port or include it
* in the string. If you do both, the one set by ChangePort()
* will override.
*/
HttpClient(const std::string& address);
/**
* \brief Constructor.
* \param address remote network address
* \param port remote local port
* \note Port will override whatever is set in the address string.
*/
HttpClient(const std::string& address, uint16_t port);
/**
* \brief Provide the option to change the address.
* \param address string containing the address (include the port here)
* \note ChangePort() will override the port added in address.
*/
void ChangeAddress(const std::string& address);
/**
* \brief Provide the option to change the port, this will override the
* port set in ChangeAddress.
* \param port number
*/
void ChangePort(uint16_t port);
/**
* \brief Receive last string received from server.
*
* Strings received from server are placed in a linked list (FIFO)
* which Recv pops from the front. 'data' will be null if there
* are not strings left.
* \param data if data is received it will put in this reference
* \return length of string
*/
ssize_t Recv(std::string& data);
/**
* \brief Receives next string from server, will wait for data.
* \param data data received is put in this reference
* \return length of string
*/
ssize_t WaitRecv(std::string& data);
/**
* \brief Receives next string from server, will wait for data for max
* timeout seconds.
* \param data data received is put in this reference
* \param timeout timeout for receive operation
* \return length of string
*/
ssize_t WaitRecv(std::string& data, unsigned int timeout);
/**
* \brief Sets the size of the receive string list.
*
* If the list exceeds this size, a strings will be popped off of the
* front until it is of correct size; Default size: -1 == unlimited.
* \param size number of strings to keep in the list
*/
void SetRecvListSize(int size);
/**
* \brief Gets the size of the receive string list.
* \return size of the receive string list (-1 == unlimited)
*/
int GetRecvListSize();
/**
* \brief Send data.
* \param data data to send
* \return 0 if successful or libcurl error code if error
*/
ssize_t Send(const std::string& data);
/**
* \brief Connect to the remote machine.
* \return true if success, false otherwise
* \note This method is not needed in HTTP and always returns false.
*/
bool Connect();
/**
* \brief Close socket.
* \note This method is useless in HTTP and does nothing.
*/
void Close();
/**
* \brief Get socket descriptor.
* \return socket descriptor.
* \note This method is useless in HTTP and always returns -1.
*/
int GetSocket() const;
private:
/**
* \brief Receive data from the network.
*
* This is a static function that is the callback when receiving data
* from the network. It calls Recv providing it with 'this'.
* \param buffer Non-null terminated data that it received back
* \param size size_t count of how many nmemb make up buffer
* \param nmemb size_t multiply by size to get full size of buffer
* \param f pointer to user data (in our case it is out class instance)
* \return the size of the data received
*/
static size_t StaticRecv(void *buffer, size_t size, size_t nmemb,
void*f);
/**
* \brief Stores the last received json string from the server
* This allows the last json string that the server sent
* to the static callback to be stored within the class.
* The next json string from the server will overwrite this.
* \param data the string represention of the received json
* \note Any server errors will also be stored here.
*/
void StoreRecv(const std::string& data);
/**
* \brief Initializes the class instance
*/
void Initialize();
/**
* \brief CURL handle.
*/
CURL *m_curlHandle;
/**
* \brief List of CURL headers.
*/
struct curl_slist *m_headers;
/**
* \brief List of received JSON-RPC response.
*/
std::list<std::string> m_lastReceivedList;
/**
* \brief Size of the m_lastReceivedList list.
*/
int m_lastReceivedListSize;
/**
* \brief Mutex to protected m_lastReceivedList.
*/
system_util::Mutex m_mutex;
};
} /* namespace Rpc */
} /* namespace Json */
#endif /* JSONRPC_HTTPCLIENT_H */
|