From: Francis Dupont Date: Fri, 28 Dec 2018 02:34:35 +0000 (+0100) Subject: [30-implement-control-socket-for-ddns-2] Added commands X-Git-Tag: 208-move-logging-from-global-objects-to-global-params_base~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=964141036cd7a7d806bc7d749b3b4c851e84fdb1;p=thirdparty%2Fkea.git [30-implement-control-socket-for-ddns-2] Added commands --- diff --git a/src/bin/d2/d2_controller.cc b/src/bin/d2/d2_controller.cc index 93fbd63c65..ae2ea2eed9 100644 --- a/src/bin/d2/d2_controller.cc +++ b/src/bin/d2/d2_controller.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -6,12 +6,14 @@ #include +#include #include #include #include #include +using namespace isc::config; using namespace isc::process; namespace isc { @@ -46,6 +48,53 @@ D2Controller::D2Controller() : DControllerBase(d2_app_name_, d2_bin_name_) { } +void +D2Controller::registerCommands() { + // CommandMgr uses IO service to run asynchronous socket operations. + CommandMgr::instance().setIOService(getIOService()); + + // These are the commands always supported by the D2 server. + // Please keep the list in alphabetic order. + CommandMgr::instance().registerCommand("build-report", + boost::bind(&D2Controller::buildReportHandler, this, _1, _2)); + + CommandMgr::instance().registerCommand("config-get", + boost::bind(&D2Controller::configGetHandler, this, _1, _2)); + + CommandMgr::instance().registerCommand("config-test", + boost::bind(&D2Controller::configTestHandler, this, _1, _2)); + + CommandMgr::instance().registerCommand("config-write", + boost::bind(&D2Controller::configWriteHandler, this, _1, _2)); + + CommandMgr::instance().registerCommand("shutdown", + boost::bind(&D2Controller::shutdownHandler, this, _1, _2)); + + CommandMgr::instance().registerCommand("version-get", + boost::bind(&D2Controller::versionGetHandler, this, _1, _2)); +} + +void +D2Controller::deregisterCommands() { + try { + // Close the command socket (if it exists). + CommandMgr::instance().closeCommandSocket(); + + // Deregister any registered commands (please keep in alphabetic order) + CommandMgr::instance().deregisterCommand("build-report"); + CommandMgr::instance().deregisterCommand("config-get"); + CommandMgr::instance().deregisterCommand("config-test"); + CommandMgr::instance().deregisterCommand("config-write"); + CommandMgr::instance().deregisterCommand("shutdown"); + CommandMgr::instance().deregisterCommand("version-get"); + + } catch (...) { + // What to do? Simply ignore... + } +} + + + isc::data::ConstElementPtr D2Controller::parseFile(const std::string& file_name) { isc::data::ConstElementPtr elements; @@ -61,6 +110,7 @@ D2Controller::parseFile(const std::string& file_name) { } D2Controller::~D2Controller() { + deregisterCommands(); } std::string diff --git a/src/bin/d2/d2_controller.h b/src/bin/d2/d2_controller.h index 0c0fcb3e00..0f2f8d2fd2 100644 --- a/src/bin/d2/d2_controller.h +++ b/src/bin/d2/d2_controller.h @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -42,6 +42,12 @@ public: /// by convention this should match the executable name. static const char* d2_bin_name_; + /// @brief Register commands. + void registerCommands(); + + /// @brief Deregister commands. + void deregisterCommands(); + protected: /// @brief Returns version info specific to D2 virtual std::string getVersionAddendum(); diff --git a/src/bin/d2/d2_process.cc b/src/bin/d2/d2_process.cc index 64a23d3152..6817704219 100644 --- a/src/bin/d2/d2_process.cc +++ b/src/bin/d2/d2_process.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include using namespace isc::process; @@ -45,9 +46,12 @@ D2Process::init() { void D2Process::run() { LOG_INFO(d2_logger, DHCP_DDNS_STARTED).arg(VERSION); - // Loop forever until we are allowed to shutdown. - while (!canShutdown()) { - try { + try { + // Now logging was initialized so commands can be registered. + boost::dynamic_pointer_cast(D2Controller::instance())->registerCommands(); + + // Loop forever until we are allowed to shutdown. + while (!canShutdown()) { // Check on the state of the request queue. Take any // actions necessary regarding it. checkQueueStatus(); @@ -69,17 +73,20 @@ D2Process::run() { isc_throw(DProcessBaseError, "Primary IO service stopped unexpectedly"); } - } catch (const std::exception& ex) { - LOG_FATAL(d2_logger, DHCP_DDNS_FAILED).arg(ex.what()); - isc_throw (DProcessBaseError, - "Process run method failed: " << ex.what()); } + } catch (const std::exception& ex) { + LOG_FATAL(d2_logger, DHCP_DDNS_FAILED).arg(ex.what()); + boost::dynamic_pointer_cast(D2Controller::instance())->deregisterCommands(); + isc_throw (DProcessBaseError, + "Process run method failed: " << ex.what()); } // @todo - if queue isn't empty, we may need to persist its contents // this might be the place to do it, once there is a persistence mgr. // This may also be better in checkQueueStatus. + boost::dynamic_pointer_cast(D2Controller::instance())->deregisterCommands(); + LOG_DEBUG(d2_logger, isc::log::DBGLVL_START_SHUT, DHCP_DDNS_RUN_EXIT); };