Kea 2.2.0
cmd_http_listener.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2022 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#include <config.h>
10#include <asiolink/io_error.h>
11#include <asiolink/io_service.h>
12#include <cmd_http_listener.h>
14#include <config_log.h>
15#include <config/timeouts.h>
17
18#include <boost/pointer_cast.hpp>
19
20using namespace isc::asiolink;
21using namespace isc::config;
22using namespace isc::data;
23using namespace isc::http;
24using namespace isc::util;
25
26namespace isc {
27namespace config {
28
29CmdHttpListener::CmdHttpListener(const IOAddress& address, const uint16_t port,
30 const uint16_t thread_pool_size /* = 1 */,
31 TlsContextPtr context /* = () */)
32 : address_(address), port_(port), thread_io_service_(), http_listener_(),
33 thread_pool_size_(thread_pool_size), thread_pool_(),
34 tls_context_(context) {
35}
36
38 stop();
39}
40
41void
43 // We must be in multi-threading mode.
44 if (!MultiThreadingMgr::instance().getMode()) {
45 isc_throw(InvalidOperation, "CmdHttpListener cannot be started"
46 " when multi-threading is disabled");
47 }
48
49 // Punt if we're already started.
50 if (!isStopped()) {
51 isc_throw(InvalidOperation, "CmdHttpListener already started!");
52 }
53
54 try {
55 // Create a new IOService.
56 thread_io_service_.reset(new IOService());
57
58 // Create the response creator factory first. It will be used to
59 // generate response creators. Each response creator will be
60 // used to generate the answer to specific request.
62
63 // Create the HTTP listener. It will open up a TCP socket and be
64 // prepared to accept incoming connections.
65 http_listener_.reset(new HttpListener(*thread_io_service_, address_,
66 port_, tls_context_, rcf,
69
70 // Create the thread pool with immediate start.
71 thread_pool_.reset(new HttpThreadPool(thread_io_service_, thread_pool_size_));
72
73 // Instruct the HTTP listener to actually open socket, install
74 // callback and start listening.
75 http_listener_->start();
76
77 // OK, seems like we're good to go.
79 .arg(thread_pool_size_)
80 .arg(address_)
81 .arg(port_)
82 .arg(tls_context_ ? "true" : "false");
83 } catch (const std::exception& ex) {
84 isc_throw(Unexpected, "CmdHttpListener::run failed:" << ex.what());
85 }
86}
87
88void
90 if (thread_pool_) {
91 thread_pool_->checkPausePermissions();
92 }
93}
94
95void
97 if (thread_pool_) {
98 thread_pool_->pause();
99 }
100}
101
102void
104 if (thread_pool_) {
105 thread_pool_->run();
106 }
107}
108
109void
111 // Nothing to do.
112 if (!thread_io_service_) {
113 return;
114 }
115
117 .arg(address_)
118 .arg(port_);
119
120 // Stop the thread pool.
121 thread_pool_->stop();
122
123 // Get rid of the listener.
124 http_listener_.reset();
125
126 // Ditch the IOService.
127 thread_io_service_.reset();
128
130 .arg(address_)
131 .arg(port_);
132}
133
134bool
136 if (thread_pool_) {
137 return (thread_pool_->isRunning());
138 }
139
140 return (false);
141}
142
143bool
145 if (thread_pool_) {
146 return (thread_pool_->isStopped());
147 }
148
149 return (true);
150}
151
152bool
154 if (thread_pool_) {
155 return (thread_pool_->isPaused());
156 }
157
158 return (false);
159}
160
161} // namespace isc::config
162} // namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a function is called in a prohibited way.
A generic exception that is thrown when an unexpected error condition occurs.
void checkPermissions()
Check if the current thread can perform thread pool state transition.
void start()
Starts running the listener's thread pool.
virtual ~CmdHttpListener()
Destructor.
bool isPaused()
Indicates if the thread pool is paused.
CmdHttpListener(const asiolink::IOAddress &address, const uint16_t port, const uint16_t thread_pool_size=1, asiolink::TlsContextPtr context=asiolink::TlsContextPtr())
Constructor.
void pause()
Pauses the listener's thread pool.
void resume()
Resumes running the listener's thread pool.
bool isRunning()
Indicates if the thread pool is running.
void stop()
Stops the listener's thread pool.
bool isStopped()
Indicates if the thread pool is stopped.
HTTP response creator factory for an API listener.
HTTP listener.
Definition: listener.h:52
Implements a pausable pool of IOService driven threads.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
constexpr long TIMEOUT_AGENT_IDLE_CONNECTION_TIMEOUT
Timeout for the idle connection to be closed.
Definition: timeouts.h:24
const isc::log::MessageID COMMAND_HTTP_LISTENER_STARTED
const isc::log::MessageID COMMAND_HTTP_LISTENER_STOPPED
const isc::log::MessageID COMMAND_HTTP_LISTENER_STOPPING
const int DBG_COMMAND
Definition: config_log.h:24
isc::log::Logger command_logger("commands")
Command processing Logger.
Definition: config_log.h:21
constexpr long TIMEOUT_AGENT_RECEIVE_COMMAND
Timeout for the Control Agent to receive command over the RESTful interface.
Definition: timeouts.h:21
boost::shared_ptr< HttpResponseCreatorFactory > HttpResponseCreatorFactoryPtr
Pointer to the HttpResponseCreatorFactory.
Definition: edns.h:19
Defines the logger used by the top-level component of kea-lfc.
Idle connection timeout.
Definition: listener.h:67
HTTP request timeout value.
Definition: listener.h:56