Kea 2.5.8
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
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 // 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_->restart();
96 try {
97 thread_io_service_->poll();
98 } catch (...) {
99 }
100 }
101
102 // Get rid of the thread pool.
103 thread_pool_.reset();
104
105 // Get rid of the listener.
106 http_listener_.reset();
107
108 // Ditch the IOService.
109 thread_io_service_.reset();
110
111 isc_throw(Unexpected, "CmdHttpListener::run failed: " << ex.what());
112 }
113}
114
115void
117 if (thread_pool_) {
118 thread_pool_->checkPausePermissions();
119 }
120}
121
122void
124 if (thread_pool_) {
125 thread_pool_->pause();
126 }
127}
128
129void
131 if (thread_pool_) {
132 thread_pool_->run();
133 }
134}
135
136void
138 // Nothing to do.
139 if (!thread_io_service_) {
140 return;
141 }
142
144 .arg(address_)
145 .arg(port_);
146
147 // Stop the thread pool.
148 thread_pool_->stop();
149
150 // Stop the listener.
151 http_listener_->stop();
152
153 thread_io_service_->restart();
154 try {
155 thread_io_service_->poll();
156 } catch (...) {
157 }
158
159 // Get rid of the thread pool.
160 thread_pool_.reset();
161
162 // Get rid of the listener.
163 http_listener_.reset();
164
165 // Ditch the IOService.
166 thread_io_service_.reset();
167
169 .arg(address_)
170 .arg(port_);
171}
172
173bool
175 if (thread_pool_) {
176 return (thread_pool_->isRunning());
177 }
178
179 return (false);
180}
181
182bool
184 if (thread_pool_) {
185 return (thread_pool_->isStopped());
186 }
187
188 return (true);
189}
190
191bool
193 if (thread_pool_) {
194 return (thread_pool_->isPaused());
195 }
196
197 return (false);
198}
199
200} // namespace config
201} // 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