d_process.h 8.3 KB

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