Kea 2.7.4
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 if (MultiThreadingMgr::instance().isTestMode()) {
44 return;
45 }
46 // We must be in multi-threading mode.
47 if (!MultiThreadingMgr::instance().getMode()) {
48 isc_throw(InvalidOperation, "CmdHttpListener cannot be started"
49 " when multi-threading is disabled");
50 }
51
52 // Punt if we're already started.
53 if (!isStopped()) {
54 isc_throw(InvalidOperation, "CmdHttpListener already started!");
55 }
56
57 try {
58 // Create a new IOService.
59 thread_io_service_.reset(new IOService());
60
61 // Create the response creator factory first. It will be used to
62 // generate response creators. Each response creator will be
63 // used to generate the answer to specific request.
65
66 // Create the HTTP listener. It will open up a TCP socket and be
67 // prepared to accept incoming connections.
68 http_listener_.reset(new HttpListener(thread_io_service_, address_,
69 port_, tls_context_, rcf,
72
73 // Instruct the HTTP listener to actually open socket, install
74 // callback and start listening.
75 http_listener_->start();
76
77 // Create the thread pool with immediate start.
78 thread_pool_.reset(new IoServiceThreadPool(thread_io_service_, thread_pool_size_));
79
80 // OK, seems like we're good to go.
82 .arg(thread_pool_size_)
83 .arg(address_)
84 .arg(port_)
85 .arg(tls_context_ ? "true" : "false");
86 } catch (const std::exception& ex) {
87 if (thread_pool_) {
88 // Stop the thread pool.
89 thread_pool_->stop();
90 }
91
92 if (http_listener_) {
93 // Stop the listener.
94 http_listener_->stop();
95 }
96
97 if (thread_io_service_) {
98 thread_io_service_->stopAndPoll();
99 thread_io_service_->stop();
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_->stopAndPoll();
154 thread_io_service_->stop();
155
156 // Get rid of the thread pool.
157 thread_pool_.reset();
158
159 // Get rid of the listener.
160 http_listener_.reset();
161
162 // Ditch the IOService.
163 thread_io_service_.reset();
164
166 .arg(address_)
167 .arg(port_);
168}
169
170bool
172 if (thread_pool_) {
173 return (thread_pool_->isRunning());
174 }
175
176 return (false);
177}
178
179bool
181 if (thread_pool_) {
182 return (thread_pool_->isStopped());
183 }
184
185 return (true);
186}
187
188bool
190 if (thread_pool_) {
191 return (thread_pool_->isPaused());
192 }
193
194 return (false);
195}
196
197} // namespace config
198} // 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