base_command_mgr.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2017 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. #ifndef BASE_COMMAND_MGR_H
  7. #define BASE_COMMAND_MGR_H
  8. #include <cc/data.h>
  9. #include <exceptions/exceptions.h>
  10. #include <boost/function.hpp>
  11. #include <map>
  12. #include <string>
  13. namespace isc {
  14. namespace config {
  15. /// @brief Exception indicating that the handler specified is not valid
  16. class InvalidCommandHandler : public Exception {
  17. public:
  18. InvalidCommandHandler(const char* file, size_t line, const char* what) :
  19. isc::Exception(file, line, what) { };
  20. };
  21. /// @brief Exception indicating that the command name is not valid
  22. class InvalidCommandName : public Exception {
  23. public:
  24. InvalidCommandName(const char* file, size_t line, const char* what) :
  25. isc::Exception(file, line, what) { };
  26. };
  27. /// @brief Commands Manager, responsible for processing external commands.
  28. ///
  29. /// Commands Manager is a generic interface for handling external commands.
  30. /// Commands are received over control sockets. Derivations of this class
  31. /// provide implementations of the control socket layers, e.g. unix domain
  32. /// sockets, TCP sockets etc. This base class merely provides methods to manage
  33. /// command handling functions, i.e. register commands, deregister commands.
  34. /// It also includes a @ref BaseCommandMgr::processCommand method which
  35. /// uses the command as an input and invokes appropriate handlers.
  36. ///
  37. /// The commands and responses are formatted using JSON.
  38. /// See http://kea.isc.org/wiki/StatsDesign for details.
  39. ///
  40. /// Below is an example of the command using JSON format:
  41. /// @code
  42. /// {
  43. /// "command": "statistic-get",
  44. /// "arguments": {
  45. /// "name": "received-packets"
  46. /// }
  47. /// }
  48. /// @endcode
  49. ///
  50. /// And the response is:
  51. ///
  52. /// @code
  53. /// {
  54. /// "result": 0,
  55. /// "observations": {
  56. /// "received-packets": [ [ 1234, "2015-04-15 12:34:45.123" ] ]
  57. /// }
  58. /// }
  59. /// @endcode
  60. ///
  61. /// BaseCommandsMgr does not implement the commands (except one,
  62. /// "list-commands") itself, but rather provides an interface
  63. /// (see @ref registerCommand, @ref deregisterCommand, @ref processCommand)
  64. /// for other components to use it.
  65. class BaseCommandMgr {
  66. public:
  67. /// @brief Defines command handler type
  68. ///
  69. /// Command handlers are expected to use this format.
  70. ///
  71. /// @param name name of the commands
  72. /// @param params parameters specific to the command
  73. /// @return response (created with createAnswer())
  74. typedef boost::function<isc::data::ConstElementPtr (const std::string& name,
  75. const isc::data::ConstElementPtr& params)> CommandHandler;
  76. /// @brief Constructor.
  77. ///
  78. /// Registers "list-commands" command.
  79. BaseCommandMgr();
  80. /// @brief Destructor.
  81. virtual ~BaseCommandMgr() { };
  82. /// @brief Triggers command processing.
  83. ///
  84. /// This method processes specified command. The command is specified using
  85. /// a single Element. See @ref BaseCommandMgr for description of its syntax.
  86. ///
  87. /// @param cmd Pointer to the data element representing command in JSON
  88. /// format.
  89. isc::data::ConstElementPtr
  90. processCommand(const isc::data::ConstElementPtr& cmd);
  91. /// @brief Registers specified command handler for a given command
  92. ///
  93. /// @param cmd Name of the command to be handled.
  94. /// @param handler Pointer to the method that will handle the command.
  95. void registerCommand(const std::string& cmd, CommandHandler handler);
  96. /// @brief Deregisters specified command handler.
  97. ///
  98. /// @param cmd Name of the command that's no longer handled.
  99. void deregisterCommand(const std::string& cmd);
  100. /// @brief Auxiliary method that removes all installed commands.
  101. ///
  102. /// The only unwipeable method is list-commands, which is internally
  103. /// handled at all times.
  104. void deregisterAll();
  105. protected:
  106. /// @brief Handles the command having a given name and arguments.
  107. ///
  108. /// This method can be overriden in the derived classes to provide
  109. /// custom logic for processing commands. For example, the
  110. /// @ref HookedCommandMgr extends this method to delegate commmands
  111. /// processing to a hook library.
  112. ///
  113. /// @param cmd_name Command name.
  114. /// @param params Command arguments.
  115. ///
  116. /// @return Pointer to the const data element representing response
  117. /// to a command.
  118. virtual isc::data::ConstElementPtr
  119. handleCommand(const std::string& cmd_name,
  120. const isc::data::ConstElementPtr& params);
  121. /// @brief Type of the container for command handlers.
  122. typedef std::map<std::string, CommandHandler> HandlerContainer;
  123. /// @brief Container for command handlers.
  124. HandlerContainer handlers_;
  125. private:
  126. /// @brief 'list-commands' command handler.
  127. ///
  128. /// This method implements command 'list-commands'. It returns a list of all
  129. /// currently supported commands.
  130. ///
  131. /// @param name Name of the command (should always be 'list-commands').
  132. /// @param params Additional parameters (ignored).
  133. ///
  134. /// @return Pointer to the structure that includes all currently supported
  135. /// commands.
  136. isc::data::ConstElementPtr
  137. listCommandsHandler(const std::string& name,
  138. const isc::data::ConstElementPtr& params);
  139. };
  140. } // end of namespace isc::config
  141. } // end of namespace isc
  142. #endif