Kea  1.9.9-git
cmd_http_listener.cc
Go to the documentation of this file.
1 // Copyright (C) 2021 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  : address_(address), port_(port), thread_io_service_(), http_listener_(),
32  thread_pool_size_(thread_pool_size), thread_pool_() {
33 }
34 
36  stop();
37 }
38 
39 void
41  // We must be in multi-threading mode.
42  if (!MultiThreadingMgr::instance().getMode()) {
43  isc_throw(InvalidOperation, "CmdHttpListener cannot be started"
44  " when multi-threading is disabled");
45  }
46 
47  // Punt if we're already started.
48  if (!isStopped()) {
49  isc_throw(InvalidOperation, "CmdHttpListener already started!");
50  }
51 
52  try {
53  // Create a new IOService.
54  thread_io_service_.reset(new IOService());
55 
56  // Create the response creator factory first. It will be used to
57  // generate response creators. Each response creator will be
58  // used to generate the answer to specific request.
60 
61  // Create the HTTP listener. It will open up a TCP socket and be
62  // prepared to accept incoming connections.
63  TlsContextPtr tls_context;
64  http_listener_.reset(new HttpListener(*thread_io_service_, address_, port_, tls_context, rcf,
67 
68  // Create the thread pool with immediate start.
69  thread_pool_.reset(new HttpThreadPool(thread_io_service_, thread_pool_size_));
70 
71  // Instruct the HTTP listener to actually open socket, install
72  // callback and start listening.
73  http_listener_->start();
74 
75  // OK, seems like we're good to go.
77  .arg(thread_pool_size_)
78  .arg(address_)
79  .arg(port_);
80  } catch (const std::exception& ex) {
81  isc_throw(Unexpected, "CmdHttpListener::run failed:" << ex.what());
82  }
83 }
84 
85 void
87  if (thread_pool_) {
88  thread_pool_->pause();
89  }
90 }
91 
92 void
94  if (thread_pool_) {
95  thread_pool_->run();
96  }
97 }
98 
99 void
101  // Nothing to do.
102  if (!thread_io_service_) {
103  return;
104  }
105 
107  .arg(address_)
108  .arg(port_);
109 
110  // Stop the thread pool.
111  thread_pool_->stop();
112 
113  // Get rid of the listener.
114  http_listener_.reset();
115 
116  // Ditch the IOService.
117  thread_io_service_.reset();
118 
120  .arg(address_)
121  .arg(port_);
122 }
123 
124 bool
126  if (thread_pool_) {
127  return (thread_pool_->isRunning());
128  }
129 
130  return (false);
131 }
132 
133 bool
135  if (thread_pool_) {
136  return (thread_pool_->isStopped());
137  }
138 
139  return (true);
140 }
141 
142 bool
144  if (thread_pool_) {
145  return (thread_pool_->isPaused());
146  }
147 
148  return (false);
149 }
150 
151 } // namespace isc::config
152 } // 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's thread pool.
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
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
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
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's thread pool.
void resume()
Resumes running the listener's thread pool.
void pause()
Pauses the listener's thread pool.
HTTP request timeout value.
Definition: listener.h:56