Kea 2.7.0
cmd_http_listener.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2024 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
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 // Instruct the HTTP listener to actually open socket, install
71 // callback and start listening.
72 http_listener_->start();
73
74 // Create the thread pool with immediate start.
75 thread_pool_.reset(new IoServiceThreadPool(thread_io_service_, thread_pool_size_));
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 if (thread_pool_) {
85 // Stop the thread pool.
86 thread_pool_->stop();
87 }
88
89 if (http_listener_) {
90 // Stop the listener.
91 http_listener_->stop();
92 }
93
94 if (thread_io_service_) {
95 thread_io_service_->stopAndPoll();
96 thread_io_service_->stop();
97 }
98
99 // Get rid of the thread pool.
100 thread_pool_.reset();
101
102 // Get rid of the listener.
103 http_listener_.reset();
104
105 // Ditch the IOService.
106 thread_io_service_.reset();
107
108 isc_throw(Unexpected, "CmdHttpListener::run failed: " << ex.what());
109 }
110}
111
112void
114 if (thread_pool_) {
115 thread_pool_->checkPausePermissions();
116 }
117}
118
119void
121 if (thread_pool_) {
122 thread_pool_->pause();
123 }
124}
125
126void
128 if (thread_pool_) {
129 thread_pool_->run();
130 }
131}
132
133void
135 // Nothing to do.
136 if (!thread_io_service_) {
137 return;
138 }
139
141 .arg(address_)
142 .arg(port_);
143
144 // Stop the thread pool.
145 thread_pool_->stop();
146
147 // Stop the listener.
148 http_listener_->stop();
149
150 thread_io_service_->stopAndPoll();
151 thread_io_service_->stop();
152
153 // Get rid of the thread pool.
154 thread_pool_.reset();
155
156 // Get rid of the listener.
157 http_listener_.reset();
158
159 // Ditch the IOService.
160 thread_io_service_.reset();
161
163 .arg(address_)
164 .arg(port_);
165}
166
167bool
169 if (thread_pool_) {
170 return (thread_pool_->isRunning());
171 }
172
173 return (false);
174}
175
176bool
178 if (thread_pool_) {
179 return (thread_pool_->isStopped());
180 }
181
182 return (true);
183}
184
185bool
187 if (thread_pool_) {
188 return (thread_pool_->isPaused());
189 }
190
191 return (false);
192}
193
194} // namespace config
195} // 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
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
#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.
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