command_interpreter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (C) 2009-2015,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 COMMAND_INTERPRETER_H
  7. #define COMMAND_INTERPRETER_H
  8. #include <cc/data.h>
  9. #include <string>
  10. /// @file command_interpreter.h
  11. ///
  12. /// This file contains several functions and constants that are used for
  13. /// handling commands and responses sent over control channel. The design
  14. /// is described here: http://kea.isc.org/wiki/StatsDesign, but also
  15. /// in @ref ctrlSocket section in the Developer's Guide.
  16. namespace isc {
  17. namespace config {
  18. /// @brief String used for commands ("command")
  19. extern const char *CONTROL_COMMAND;
  20. /// @brief String used for result, i.e. integer status ("result")
  21. extern const char *CONTROL_RESULT;
  22. /// @brief String used for storing textual description ("text")
  23. extern const char *CONTROL_TEXT;
  24. /// @brief String used for arguments map ("arguments")
  25. extern const char *CONTROL_ARGUMENTS;
  26. /// @brief Status code indicating a successful operation
  27. const int CONTROL_RESULT_SUCCESS = 0;
  28. /// @brief Status code indicating a general failure
  29. const int CONTROL_RESULT_ERROR = 1;
  30. /// @brief Status code indicating that the specified command is not supported.
  31. const int CONTROL_RESULT_COMMAND_UNSUPPORTED = 2;
  32. /// @brief A standard control channel exception that is thrown if a function
  33. /// is there is a problem with one of the messages
  34. class CtrlChannelError : public isc::Exception {
  35. public:
  36. CtrlChannelError(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what) {}
  38. };
  39. /// @brief Creates a standard config/command level success answer message
  40. /// (i.e. of the form { "result": 0 }
  41. /// @return Standard command/config success answer message
  42. isc::data::ConstElementPtr createAnswer();
  43. /// @brief Creates a standard config/command level answer message
  44. /// (i.e. of the form { "result": 1, "text": "Invalid command received" }
  45. ///
  46. /// @param status_code The return code (0 for success)
  47. /// @param status_text A string to put into the "text" argument
  48. /// @return Standard command/config answer message
  49. isc::data::ConstElementPtr createAnswer(const int status_code,
  50. const std::string& status_text);
  51. /// @brief Creates a standard config/command level answer message
  52. /// (i.e. of the form { "result": status_code, "arguments": arg }
  53. ///
  54. /// @param status_code The return code (0 for success)
  55. /// @param arg The optional argument for the answer. This can be of
  56. /// any Element type. May be NULL.
  57. /// @return Standard command/config answer message
  58. isc::data::ConstElementPtr createAnswer(const int status_code,
  59. const isc::data::ConstElementPtr& arg);
  60. /// @brief Creates a standard config/command level answer message
  61. ///
  62. /// @param status_code The return code (0 for success)
  63. /// @param status textual representation of the status (used mostly for errors)
  64. /// @param arg The optional argument for the answer. This can be of
  65. /// any Element type. May be NULL.
  66. /// @return Standard command/config answer message
  67. isc::data::ConstElementPtr createAnswer(const int status_code,
  68. const std::string& status,
  69. const isc::data::ConstElementPtr& arg);
  70. /// @brief Parses a standard config/command level answer message.
  71. ///
  72. /// @param status_code This value will be set to the return code contained in
  73. /// the message
  74. /// @param msg The message to parse
  75. /// @return The optional argument in the message.
  76. isc::data::ConstElementPtr parseAnswer(int &status_code,
  77. const isc::data::ConstElementPtr& msg);
  78. /// @brief Converts answer to printable text
  79. ///
  80. /// @param msg answer to be parsed
  81. /// @return printable string
  82. std::string answerToText(const isc::data::ConstElementPtr& msg);
  83. /// @brief Creates a standard config/command command message with no
  84. /// argument (of the form { "command": "my_command" })
  85. ///
  86. /// @param command The command string
  87. /// @return The created message
  88. isc::data::ConstElementPtr createCommand(const std::string& command);
  89. /// @brief Creates a standard config/command command message with the
  90. /// given argument (of the form { "command": "my_command", "arguments": arg }
  91. ///
  92. /// @param command The command string
  93. /// @param arg The optional argument for the command. This can be of
  94. /// any Element type. May be NULL.
  95. /// @return The created message
  96. isc::data::ConstElementPtr createCommand(const std::string& command,
  97. isc::data::ConstElementPtr arg);
  98. /// @brief Parses the given command into a string containing the actual
  99. /// command and an ElementPtr containing the optional argument.
  100. ///
  101. /// @throw Raises a CtrlChannelError if this is not a well-formed command
  102. ///
  103. /// @param arg This value will be set to the ElementPtr pointing to
  104. /// the argument, or to an empty Map (ElementPtr) if there was none.
  105. /// @param command The command message containing the command (as made
  106. /// by createCommand()
  107. /// @return The command name
  108. std::string parseCommand(isc::data::ConstElementPtr& arg,
  109. isc::data::ConstElementPtr command);
  110. /// @brief Combines lists of commands carried in two responses.
  111. ///
  112. /// This method is used to combine list of commands returned by the
  113. /// two command managers.
  114. ///
  115. /// If the same command appears in two responses only a single
  116. /// instance is returned in the combined response.
  117. ///
  118. /// @param response1 First command response.
  119. /// @param response2 Second command response.
  120. ///
  121. /// @return Pointer to the 'list-commands' response holding combined
  122. /// list of commands.
  123. isc::data::ConstElementPtr
  124. combineCommandsLists(const isc::data::ConstElementPtr& response1,
  125. const isc::data::ConstElementPtr& response2);
  126. }; // end of namespace isc::config
  127. }; // end of namespace isc
  128. #endif // COMMAND_INTERPRETER_H