Kea  2.1.7-git
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>
9 #include <asiolink/io_address.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 
20 using namespace isc::asiolink;
21 using namespace isc::config;
22 using namespace isc::data;
23 using namespace isc::http;
24 using namespace isc::util;
25 
26 namespace isc {
27 namespace config {
28 
29 CmdHttpListener::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 
41 void
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 
88 void
90  if (thread_pool_) {
91  thread_pool_->checkPausePermissions();
92  }
93 }
94 
95 void
97  if (thread_pool_) {
98  thread_pool_->pause();
99  }
100 }
101 
102 void
104  if (thread_pool_) {
105  thread_pool_->run();
106  }
107 }
108 
109 void
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 
134 bool
136  if (thread_pool_) {
137  return (thread_pool_->isRunning());
138  }
139 
140  return (false);
141 }
142 
143 bool
145  if (thread_pool_) {
146  return (thread_pool_->isStopped());
147  }
148 
149  return (true);
150 }
151 
152 bool
154  if (thread_pool_) {
155  return (thread_pool_->isPaused());
156  }
157 
158  return (false);
159 }
160 
161 } // namespace isc::config
162 } // namespace isc
boost::shared_ptr< HttpResponseCreatorFactory > HttpResponseCreatorFactoryPtr
Pointer to the HttpResponseCreatorFactory.
bool isStopped()
Indicates if the thread pool is stopped.
virtual ~CmdHttpListener()
Destructor.
HTTP response creator factory for an API listener.
const isc::log::MessageID COMMAND_HTTP_LISTENER_STARTED
const isc::log::MessageID COMMAND_HTTP_LISTENER_STOPPED
const int DBG_COMMAND
Definition: config_log.h:24
void stop()
Stops the listener&#39;s thread pool.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
HTTP listener.
Definition: listener.h:52
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: edns.h:19
isc::log::Logger command_logger("commands")
Command processing Logger.
Definition: config_log.h:21
Idle connection timeout.
Definition: listener.h:67
A generic exception that is thrown when an unexpected error condition occurs.
bool isPaused()
Indicates if the thread pool is paused.
const isc::log::MessageID COMMAND_HTTP_LISTENER_STOPPING
constexpr long TIMEOUT_AGENT_RECEIVE_COMMAND
Timeout for the Control Agent to receive command over the RESTful interface.
Definition: timeouts.h:21
Defines the logger used by the top-level component of kea-lfc.
bool isRunning()
Indicates if the thread pool is running.
A generic exception that is thrown if a function is called in a prohibited way.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
void checkPermissions()
Check if the current thread can perform thread pool state transition.
Implements a pausable pool of IOService driven threads.
constexpr long TIMEOUT_AGENT_IDLE_CONNECTION_TIMEOUT
Timeout for the idle connection to be closed.
Definition: timeouts.h:24
void start()
Starts running the listener&#39;s thread pool.
void resume()
Resumes running the listener&#39;s thread pool.
void pause()
Pauses the listener&#39;s thread pool.
HTTP request timeout value.
Definition: listener.h:56