]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[30-implement-control-socket-for-ddns-2] Added commands
authorFrancis Dupont <fdupont@isc.org>
Fri, 28 Dec 2018 02:34:35 +0000 (03:34 +0100)
committerFrancis Dupont <fdupont@isc.org>
Thu, 3 Jan 2019 09:05:03 +0000 (04:05 -0500)
src/bin/d2/d2_controller.cc
src/bin/d2/d2_controller.h
src/bin/d2/d2_process.cc

index 93fbd63c65da2a1a25ba93e668c0930c0993dac4..ae2ea2eed9c1ddcfd839c7ecc8230271ec923c75 100644 (file)
@@ -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 <config.h>
 
+#include <config/command_mgr.h>
 #include <d2/d2_controller.h>
 #include <d2/d2_process.h>
 #include <d2/parser_context.h>
 
 #include <stdlib.h>
 
+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
index 0c0fcb3e00a9af7fd3d00d630db08092da5b2d3b..0f2f8d2fd2d5332a4f6a5513c9d75b6d29708447 100644 (file)
@@ -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();
index 64a23d31521f7efc62fa43e16d57b227571cfd76..68177042199635eb56835f6c2e72914084640911 100644 (file)
@@ -9,6 +9,7 @@
 #include <cc/command_interpreter.h>
 #include <d2/d2_log.h>
 #include <d2/d2_cfg_mgr.h>
+#include <d2/d2_controller.h>
 #include <d2/d2_process.h>
 
 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>(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>(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>(D2Controller::instance())->deregisterCommands();
+
     LOG_DEBUG(d2_logger, isc::log::DBGLVL_START_SHUT, DHCP_DDNS_RUN_EXIT);
 
 };