Kea  1.9.9-git
client.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef HTTP_CLIENT_H
8 #define HTTP_CLIENT_H
9 
10 #include <asiolink/io_service.h>
11 #include <asiolink/tls_socket.h>
12 #include <exceptions/exceptions.h>
13 #include <http/url.h>
14 #include <http/request.h>
15 #include <http/response.h>
16 #include <http/http_thread_pool.h>
17 #include <boost/shared_ptr.hpp>
18 #include <functional>
19 #include <string>
20 #include <thread>
21 #include <vector>
22 
23 namespace isc {
24 namespace http {
25 
27 class HttpClientError : public Exception {
28 public:
29  HttpClientError(const char* file, size_t line, const char* what) :
30  isc::Exception(file, line, what) { };
31 };
32 
33 class HttpClientImpl;
34 
87 class HttpClient {
88 public:
90  struct RequestTimeout {
94  explicit RequestTimeout(long value)
95  : value_(value) {
96  }
97  long value_;
98  };
99 
101  typedef std::function<void(const boost::system::error_code&,
102  const HttpResponsePtr&,
103  const std::string&)> RequestHandler;
104 
115  typedef std::function<bool(const boost::system::error_code&, const int)> ConnectHandler;
116 
128  typedef std::function<bool(const boost::system::error_code&, const int)> HandshakeHandler;
129 
133  typedef std::function<void(const int)> CloseHandler;
134 
148  explicit HttpClient(asiolink::IOService& io_service, size_t thread_pool_size = 0,
149  bool defer_thread_start = false);
150 
152  ~HttpClient();
153 
238  void asyncSendRequest(const Url& url,
239  const asiolink::TlsContextPtr& tls_context,
240  const HttpRequestPtr& request,
241  const HttpResponsePtr& response,
242  const RequestHandler& request_callback,
243  const RequestTimeout& request_timeout =
244  RequestTimeout(10000),
245  const ConnectHandler& connect_callback =
246  ConnectHandler(),
247  const HandshakeHandler& handshake_callback =
249  const CloseHandler& close_callback =
250  CloseHandler());
251 
253  void start();
254 
259  void pause();
260 
265  void resume();
266 
272  void stop();
273 
286  void closeIfOutOfBand(int socket_fd);
287 
294 
298  uint16_t getThreadPoolSize() const;
299 
303  uint16_t getThreadCount() const;
304 
309  bool isRunning();
310 
315  bool isStopped();
316 
321  bool isPaused();
322 
323 private:
324 
326  boost::shared_ptr<HttpClientImpl> impl_;
327 };
328 
330 typedef boost::shared_ptr<HttpClient> HttpClientPtr;
331 
332 } // end of namespace isc::http
333 } // end of namespace isc
334 
335 #endif
void stop()
Halts client-side IO activity.
Definition: client.cc:1977
HttpClientError(const char *file, size_t line, const char *what)
Definition: client.h:29
std::function< bool(const boost::system::error_code &, const int)> ConnectHandler
Optional handler invoked when client connects to the server.
Definition: client.h:115
uint16_t getThreadPoolSize() const
Fetches the maximum size of the thread pool.
Definition: client.cc:1987
void asyncSendRequest(const Url &url, const asiolink::TlsContextPtr &tls_context, const HttpRequestPtr &request, const HttpResponsePtr &response, const RequestHandler &request_callback, const RequestTimeout &request_timeout=RequestTimeout(10000), const ConnectHandler &connect_callback=ConnectHandler(), const HandshakeHandler &handshake_callback=HandshakeHandler(), const CloseHandler &close_callback=CloseHandler())
Queues new asynchronous HTTP request for a given URL.
Definition: client.cc:1921
long value_
Timeout value specified.
Definition: client.h:97
const asiolink::IOServicePtr getThreadIOService() const
Fetches a pointer to the internal IOService used to drive the thread-pool in multi-threaded mode...
Definition: client.cc:1982
std::function< void(const boost::system::error_code &, const HttpResponsePtr &, const std::string &)> RequestHandler
Callback type used in call to HttpClient::asyncSendRequest.
Definition: client.h:103
HttpClient(asiolink::IOService &io_service, size_t thread_pool_size=0, bool defer_thread_start=false)
Constructor.
Definition: client.cc:1903
HTTP request/response timeout value.
Definition: client.h:90
RequestTimeout(long value)
Constructor.
Definition: client.h:94
std::function< void(const int)> CloseHandler
Optional handler invoked when client closes the connection to the server.
Definition: client.h:133
bool isStopped()
Indicates if the thread pool is stopped.
Definition: client.cc:2002
void resume()
Resumes running the client's thread pool.
Definition: client.cc:1972
Represents an URL.
Definition: url.h:20
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
void start()
Starts running the client's thread pool, if multi-threaded.
Definition: client.cc:1962
~HttpClient()
Destructor.
Definition: client.cc:1917
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
HTTP client class.
Definition: client.h:87
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::function< bool(const boost::system::error_code &, const int)> HandshakeHandler
Optional handler invoked when client performs the TLS handshake with the server.
Definition: client.h:128
void pause()
Pauses the client's thread pool.
Definition: client.cc:1967
bool isPaused()
Indicates if the thread pool is paused.
Definition: client.cc:2007
bool isRunning()
Indicates if the thread pool is running.
Definition: client.cc:1997
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:27
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
Definition: client.cc:1992
void closeIfOutOfBand(int socket_fd)
Closes a connection if it has an out-of-band socket event.
Definition: client.cc:1957
A generic error raised by the HttpClient class.
Definition: client.h:27
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:330