Kea 2.2.0
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>
13#include <http/url.h>
14#include <http/request.h>
15#include <http/response.h>
17#include <boost/shared_ptr.hpp>
18#include <functional>
19#include <string>
20#include <thread>
21#include <vector>
22
23namespace isc {
24namespace http {
25
27class HttpClientError : public Exception {
28public:
29 HttpClientError(const char* file, size_t line, const char* what) :
30 isc::Exception(file, line, what) { };
31};
32
33class HttpClientImpl;
34
88public:
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 =
247 const HandshakeHandler& handshake_callback =
249 const CloseHandler& close_callback =
250 CloseHandler());
251
257 void checkPermissions();
258
260 void start();
261
266 void pause();
267
272 void resume();
273
279 void stop();
280
293 void closeIfOutOfBand(int socket_fd);
294
301
305 uint16_t getThreadPoolSize() const;
306
310 uint16_t getThreadCount() const;
311
316 bool isRunning();
317
322 bool isStopped();
323
328 bool isPaused();
329
330private:
331
333 boost::shared_ptr<HttpClientImpl> impl_;
334};
335
337typedef boost::shared_ptr<HttpClient> HttpClientPtr;
338
339} // end of namespace isc::http
340} // end of namespace isc
341
342#endif
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic error raised by the HttpClient class.
Definition: client.h:27
HttpClientError(const char *file, size_t line, const char *what)
Definition: client.h:29
HTTP client class.
Definition: client.h:87
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
Definition: client.cc:2043
bool isRunning()
Indicates if the thread pool is running.
Definition: client.cc:2048
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
void stop()
Halts client-side IO activity.
Definition: client.cc:2028
bool isPaused()
Indicates if the thread pool is paused.
Definition: client.cc:2058
void pause()
Pauses the client's thread pool.
Definition: client.cc:2018
std::function< void(const int)> CloseHandler
Optional handler invoked when client closes the connection to the server.
Definition: client.h:133
HttpClient(asiolink::IOService &io_service, size_t thread_pool_size=0, bool defer_thread_start=false)
Constructor.
Definition: client.cc:1949
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:2033
void start()
Starts running the client's thread pool, if multi-threaded.
Definition: client.cc:2008
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:2038
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 closeIfOutOfBand(int socket_fd)
Closes a connection if it has an out-of-band socket event.
Definition: client.cc:2003
~HttpClient()
Destructor.
Definition: client.cc:1963
void resume()
Resumes running the client's thread pool.
Definition: client.cc:2023
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:1967
bool isStopped()
Indicates if the thread pool is stopped.
Definition: client.cc:2053
void checkPermissions()
Check if the current thread can perform thread pool state transition.
Definition: client.cc:2013
Represents an URL.
Definition: url.h:20
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:337
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:27
Defines the logger used by the top-level component of kea-lfc.
HTTP request/response timeout value.
Definition: client.h:90
RequestTimeout(long value)
Constructor.
Definition: client.h:94
long value_
Timeout value specified.
Definition: client.h:97