d_process.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (C) 2013, 2015 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 D_PROCESS_H
  7. #define D_PROCESS_H
  8. #include <asiolink/io_service.h>
  9. #include <cc/data.h>
  10. #include <d2/d_cfg_mgr.h>
  11. #include <boost/shared_ptr.hpp>
  12. #include <exceptions/exceptions.h>
  13. namespace isc {
  14. namespace d2 {
  15. /// @brief Exception thrown if the process encountered an operational error.
  16. class DProcessBaseError : public isc::Exception {
  17. public:
  18. DProcessBaseError(const char* file, size_t line, const char* what) :
  19. isc::Exception(file, line, what) { };
  20. };
  21. /// @brief String value for the shutdown command.
  22. static const std::string SHUT_DOWN_COMMAND("shutdown");
  23. /// @brief Returned by the process to indicate a command was successful.
  24. static const int COMMAND_SUCCESS = 0;
  25. /// @brief Returned by the process to indicates a command failed.
  26. static const int COMMAND_ERROR = 1;
  27. /// @brief Returned by the process to indicates a command is not valid.
  28. static const int COMMAND_INVALID = 2;
  29. /// @brief Application Process Interface
  30. ///
  31. /// DProcessBase is an abstract class represents the primary "application"
  32. /// level object in a "managed" asynchronous application. It provides a uniform
  33. /// interface such that a managing layer can construct, initialize, and start
  34. /// the application's event loop. The event processing is centered around the
  35. /// use of isc::asiolink::io_service. The io_service is shared between the
  36. /// managing layer and the DProcessBase. This allows management layer IO such
  37. /// as directives to be sensed and handled, as well as processing IO activity
  38. /// specific to the application. In terms of management layer IO, there are
  39. /// methods shutdown, configuration updates, and commands unique to the
  40. /// application.
  41. class DProcessBase {
  42. public:
  43. /// @brief Constructor
  44. ///
  45. /// @param app_name is a text label for the process. Generally used
  46. /// in log statements, but otherwise arbitrary.
  47. /// @param io_service is the io_service used by the caller for
  48. /// asynchronous event handling.
  49. /// @param cfg_mgr the configuration manager instance that handles
  50. /// configuration parsing.
  51. ///
  52. /// @throw DProcessBaseError is io_service is NULL.
  53. DProcessBase(const char* app_name, asiolink::IOServicePtr io_service,
  54. DCfgMgrBasePtr cfg_mgr)
  55. : app_name_(app_name), io_service_(io_service), shut_down_flag_(false),
  56. cfg_mgr_(cfg_mgr) {
  57. if (!io_service_) {
  58. isc_throw (DProcessBaseError, "IO Service cannot be null");
  59. }
  60. if (!cfg_mgr_) {
  61. isc_throw (DProcessBaseError, "CfgMgr cannot be null");
  62. }
  63. };
  64. /// @brief May be used after instantiation to perform initialization unique
  65. /// to application. It must be invoked prior to invoking run. This would
  66. /// likely include the creation of additional IO sources and their
  67. /// integration into the io_service.
  68. /// @throw DProcessBaseError if the initialization fails.
  69. virtual void init() = 0;
  70. /// @brief Implements the process's event loop. In its simplest form it
  71. /// would an invocation io_service_->run(). This method should not exit
  72. /// until the process itself is exiting due to a request to shutdown or
  73. /// some anomaly is forcing an exit.
  74. /// @throw DProcessBaseError if an operational error is encountered.
  75. virtual void run() = 0;
  76. /// @brief Initiates the process's shutdown process.
  77. ///
  78. /// This is last step in the shutdown event callback chain, that is
  79. /// intended to notify the process it is to begin its shutdown process.
  80. ///
  81. /// @param args an Element set of shutdown arguments (if any) that are
  82. /// supported by the process derivation.
  83. ///
  84. /// @return an Element that contains the results of argument processing,
  85. /// consisting of an integer status value (0 means successful,
  86. /// non-zero means failure), and a string explanation of the outcome.
  87. ///
  88. /// @throw DProcessBaseError if an operational error is encountered.
  89. virtual isc::data::ConstElementPtr
  90. shutdown(isc::data::ConstElementPtr args) = 0;
  91. /// @brief Processes the given configuration.
  92. ///
  93. /// This method may be called multiple times during the process lifetime.
  94. /// Certainly once during process startup, and possibly later if the user
  95. /// alters configuration. This method must not throw, it should catch any
  96. /// processing errors and return a success or failure answer as described
  97. /// below.
  98. ///
  99. /// @param config_set a new configuration (JSON) for the process
  100. /// @return an Element that contains the results of configuration composed
  101. /// of an integer status value (0 means successful, non-zero means failure),
  102. /// and a string explanation of the outcome.
  103. virtual isc::data::ConstElementPtr configure(isc::data::ConstElementPtr
  104. config_set) = 0;
  105. /// @brief Processes the given command.
  106. ///
  107. /// This method is called to execute any custom commands supported by the
  108. /// process. This method must not throw, it should catch any processing
  109. /// errors and return a success or failure answer as described below.
  110. ///
  111. /// @param command is a string label representing the command to execute.
  112. /// @param args is a set of arguments (if any) required for the given
  113. /// command.
  114. /// @return an Element that contains the results of command composed
  115. /// of an integer status value:
  116. ///
  117. /// - COMMAND_SUCCESS indicates a command was successful.
  118. /// - COMMAND_ERROR indicates a valid command failed execute.
  119. /// - COMMAND_INVALID indicates a command is not valid.
  120. ///
  121. /// and a string explanation of the outcome.
  122. virtual isc::data::ConstElementPtr command(
  123. const std::string& command, isc::data::ConstElementPtr args) = 0;
  124. /// @brief Destructor
  125. virtual ~DProcessBase(){};
  126. /// @brief Checks if the process has been instructed to shut down.
  127. ///
  128. /// @return true if process shutdown flag is true.
  129. bool shouldShutdown() const {
  130. return (shut_down_flag_);
  131. }
  132. /// @brief Sets the process shut down flag to the given value.
  133. ///
  134. /// @param value is the new value to assign the flag.
  135. void setShutdownFlag(bool value) {
  136. shut_down_flag_ = value;
  137. }
  138. /// @brief Fetches the application name.
  139. ///
  140. /// @return application name string.
  141. const std::string getAppName() const {
  142. return (app_name_);
  143. }
  144. /// @brief Fetches the controller's IOService.
  145. ///
  146. /// @return a reference to the controller's IOService.
  147. asiolink::IOServicePtr& getIoService() {
  148. return (io_service_);
  149. }
  150. /// @brief Convenience method for stopping IOservice processing.
  151. /// Invoking this will cause the process to exit any blocking
  152. /// IOService method such as run(). No further IO events will be
  153. /// processed.
  154. void stopIOService() {
  155. io_service_->stop();
  156. }
  157. /// @brief Fetches the process's configuration manager.
  158. ///
  159. /// @return a reference to the configuration manager.
  160. DCfgMgrBasePtr& getCfgMgr() {
  161. return (cfg_mgr_);
  162. }
  163. private:
  164. /// @brief Text label for the process. Generally used in log statements,
  165. /// but otherwise can be arbitrary.
  166. std::string app_name_;
  167. /// @brief The IOService to be used for asynchronous event handling.
  168. asiolink::IOServicePtr io_service_;
  169. /// @brief Boolean flag set when shutdown has been requested.
  170. bool shut_down_flag_;
  171. /// @brief Pointer to the configuration manager.
  172. DCfgMgrBasePtr cfg_mgr_;
  173. };
  174. /// @brief Defines a shared pointer to DProcessBase.
  175. typedef boost::shared_ptr<DProcessBase> DProcessBasePtr;
  176. }; // namespace isc::d2
  177. }; // namespace isc
  178. #endif