From: Thomas Markwalder Date: Fri, 22 Sep 2017 17:14:50 +0000 (-0400) Subject: [5332] Created config::CmdsImpl base class X-Git-Tag: trac5363_base~8^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=969956ddc18efd1bf4d3919308e552dfb1449dd2;p=thirdparty%2Fkea.git [5332] Created config::CmdsImpl base class Extracted command helper functions into new base class, so other hook libs may use it. --- diff --git a/src/hooks/dhcp/lease_cmds/lease_cmds.cc b/src/hooks/dhcp/lease_cmds/lease_cmds.cc index 18b2f88ec3..7c548e43ca 100644 --- a/src/hooks/dhcp/lease_cmds/lease_cmds.cc +++ b/src/hooks/dhcp/lease_cmds/lease_cmds.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -36,7 +37,7 @@ namespace isc { namespace lease_cmds { /// @brief Wrapper class around reservation command handlers. -class LeaseCmdsImpl { +class LeaseCmdsImpl : private CmdsImpl { public: /// @brief Parameters specified for reservation-get and reservation-del /// @@ -201,6 +202,7 @@ public: /// @throw BadValue if input arguments don't make sense. Parameters getParameters(bool v6, const ConstElementPtr& args); +#if 0 private: /// @brief Extracts the command name and arguments from a Callout handle /// @@ -252,6 +254,7 @@ private: /// @brief Stores the command arguments extracted by a call to extractCommand ConstElementPtr cmd_args_; +#endif }; int diff --git a/src/lib/config/Makefile.am b/src/lib/config/Makefile.am index c1487d59f7..4b36b78c0c 100644 --- a/src/lib/config/Makefile.am +++ b/src/lib/config/Makefile.am @@ -14,6 +14,7 @@ BUILT_SOURCES = config_messages.h config_messages.cc lib_LTLIBRARIES = libkea-cfgclient.la libkea_cfgclient_la_SOURCES = config_data.h config_data.cc +libkea_cfgclient_la_SOURCES += cmds_impl.h libkea_cfgclient_la_SOURCES += module_spec.h module_spec.cc libkea_cfgclient_la_SOURCES += base_command_mgr.cc base_command_mgr.h libkea_cfgclient_la_SOURCES += client_connection.cc client_connection.h diff --git a/src/lib/config/cmds_impl.h b/src/lib/config/cmds_impl.h new file mode 100644 index 0000000000..452465b99a --- /dev/null +++ b/src/lib/config/cmds_impl.h @@ -0,0 +1,78 @@ +// Copyright (C) 2017 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 +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +#ifndef CMDS_IMPL_H +#define CMDS_IMPL_H + +#include +#include +#include +#include +#include + +#include + +namespace isc { +namespace config { + +/// @brief Base class that command handler implementers may use for common tasks. +class CmdsImpl { +protected: + /// @brief Extracts the command name and arguments from a Callout handle + /// + /// @param handle Callout context handle expected to contain the JSON command + /// text + /// + /// @throw isc::BadValue if the text does not contain a properly formed command + void extractCommand(hooks::CalloutHandle& handle) { + try { + data::ConstElementPtr command; + handle.getArgument("command", command); + cmd_name_ = parseCommand(cmd_args_, command); + } catch (std::exception& ex) { + isc_throw(isc::BadValue, "JSON command text is invalid: " << ex.what()); + } + } + + /// @brief Set the callout argument "response" to indicate success + /// + /// @param handle Callout context handle in which to set the "response" argument + /// @param text string text to be used as the response description + void setSuccessResponse(hooks::CalloutHandle& handle, const std::string& text) { + data::ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, text); + setResponse (handle, response); + } + + /// @brief Set the callout argument "response" to indicate an error + /// + /// @param handle Callout context handle in which to set the "response" argument + /// @param text string text to be used as the response description + /// @param status numeric value to use as the response result, defaults to + /// CONTROL_RESULT_ERROR + void setErrorResponse(hooks::CalloutHandle& handle, const std::string& text, + int status=CONTROL_RESULT_ERROR) { + data::ConstElementPtr response = createAnswer(status, text); + setResponse (handle, response); + } + + /// @brief Set the callout argument "response" to the given response + /// + /// @param handle Callout context handle in which to set the "response" argument + /// @param response ElementPtr to a the result to use as the reponse + void setResponse(hooks::CalloutHandle& handle, data::ConstElementPtr& response) { + handle.setArgument ("response", response); + } + + /// @brief Stores the command name extracted by a call to extractCommand + std::string cmd_name_; + + /// @brief Stores the command arguments extracted by a call to extractCommand + data::ConstElementPtr cmd_args_; +}; + +} +} + +#endif // CMDS_IMPL_H