123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- // Copyright (C) 2009-2015,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 COMMAND_INTERPRETER_H
- #define COMMAND_INTERPRETER_H
- #include <cc/data.h>
- #include <string>
- /// @file command_interpreter.h
- ///
- /// This file contains several functions and constants that are used for
- /// handling commands and responses sent over control channel. The design
- /// is described here: http://kea.isc.org/wiki/StatsDesign, but also
- /// in @ref ctrlSocket section in the Developer's Guide.
- namespace isc {
- namespace config {
- /// @brief String used for commands ("command")
- extern const char *CONTROL_COMMAND;
- /// @brief String used for result, i.e. integer status ("result")
- extern const char *CONTROL_RESULT;
- /// @brief String used for storing textual description ("text")
- extern const char *CONTROL_TEXT;
- /// @brief String used for arguments map ("arguments")
- extern const char *CONTROL_ARGUMENTS;
- /// @brief Status code indicating a successful operation
- const int CONTROL_RESULT_SUCCESS = 0;
- /// @brief Status code indicating a general failure
- const int CONTROL_RESULT_ERROR = 1;
- /// @brief Status code indicating that the specified command is not supported.
- const int CONTROL_RESULT_COMMAND_UNSUPPORTED = 2;
- /// @brief A standard control channel exception that is thrown if a function
- /// is there is a problem with one of the messages
- class CtrlChannelError : public isc::Exception {
- public:
- CtrlChannelError(const char* file, size_t line, const char* what) :
- isc::Exception(file, line, what) {}
- };
- /// @brief Creates a standard config/command level success answer message
- /// (i.e. of the form { "result": 0 }
- /// @return Standard command/config success answer message
- isc::data::ConstElementPtr createAnswer();
- /// @brief Creates a standard config/command level answer message
- /// (i.e. of the form { "result": 1, "text": "Invalid command received" }
- ///
- /// @param status_code The return code (0 for success)
- /// @param status_text A string to put into the "text" argument
- /// @return Standard command/config answer message
- isc::data::ConstElementPtr createAnswer(const int status_code,
- const std::string& status_text);
- /// @brief Creates a standard config/command level answer message
- /// (i.e. of the form { "result": status_code, "arguments": arg }
- ///
- /// @param status_code The return code (0 for success)
- /// @param arg The optional argument for the answer. This can be of
- /// any Element type. May be NULL.
- /// @return Standard command/config answer message
- isc::data::ConstElementPtr createAnswer(const int status_code,
- const isc::data::ConstElementPtr& arg);
- /// @brief Creates a standard config/command level answer message
- ///
- /// @param status_code The return code (0 for success)
- /// @param status textual representation of the status (used mostly for errors)
- /// @param arg The optional argument for the answer. This can be of
- /// any Element type. May be NULL.
- /// @return Standard command/config answer message
- isc::data::ConstElementPtr createAnswer(const int status_code,
- const std::string& status,
- const isc::data::ConstElementPtr& arg);
- /// @brief Parses a standard config/command level answer message.
- ///
- /// @param status_code This value will be set to the return code contained in
- /// the message
- /// @param msg The message to parse
- /// @return The optional argument in the message.
- isc::data::ConstElementPtr parseAnswer(int &status_code,
- const isc::data::ConstElementPtr& msg);
- /// @brief Converts answer to printable text
- ///
- /// @param msg answer to be parsed
- /// @return printable string
- std::string answerToText(const isc::data::ConstElementPtr& msg);
- /// @brief Creates a standard config/command command message with no
- /// argument (of the form { "command": "my_command" })
- ///
- /// @param command The command string
- /// @return The created message
- isc::data::ConstElementPtr createCommand(const std::string& command);
- /// @brief Creates a standard config/command command message with the
- /// given argument (of the form { "command": "my_command", "arguments": arg }
- ///
- /// @param command The command string
- /// @param arg The optional argument for the command. This can be of
- /// any Element type. May be NULL.
- /// @return The created message
- isc::data::ConstElementPtr createCommand(const std::string& command,
- isc::data::ConstElementPtr arg);
- /// @brief Parses the given command into a string containing the actual
- /// command and an ElementPtr containing the optional argument.
- ///
- /// @throw Raises a CtrlChannelError if this is not a well-formed command
- ///
- /// @param arg This value will be set to the ElementPtr pointing to
- /// the argument, or to an empty Map (ElementPtr) if there was none.
- /// @param command The command message containing the command (as made
- /// by createCommand()
- /// @return The command name
- std::string parseCommand(isc::data::ConstElementPtr& arg,
- isc::data::ConstElementPtr command);
- /// @brief Combines lists of commands carried in two responses.
- ///
- /// This method is used to combine list of commands returned by the
- /// two command managers.
- ///
- /// If the same command appears in two responses only a single
- /// instance is returned in the combined response.
- ///
- /// @param response1 First command response.
- /// @param response2 Second command response.
- ///
- /// @return Pointer to the 'list-commands' response holding combined
- /// list of commands.
- isc::data::ConstElementPtr
- combineCommandsLists(const isc::data::ConstElementPtr& response1,
- const isc::data::ConstElementPtr& response2);
- }; // end of namespace isc::config
- }; // end of namespace isc
- #endif // COMMAND_INTERPRETER_H
|