d_process.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <asiolink/asiolink.h>
  17. #include <cc/data.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <exceptions/exceptions.h>
  20. typedef boost::shared_ptr<isc::asiolink::IOService> IOServicePtr;
  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 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, intialize, 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. /// the managing layer and the DProcessBase. This allows management layer IO
  37. /// such as directives to be sensed and handled, as well as processing IO
  38. /// activity specific to the application. In terms of management layer IO,
  39. /// there are methods shutdown, configuration updates, and commands unique
  40. /// to the application.
  41. class DProcessBase {
  42. public:
  43. /// @brief Constructor
  44. ///
  45. /// @param name 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. ///
  50. /// @throw DProcessBaseError is io_service is NULL.
  51. DProcessBase(const char* name, IOServicePtr io_service) : name_(name),
  52. io_service_(io_service), shut_down_flag_(false) {
  53. if (!io_service_) {
  54. isc_throw (DProcessBaseError, "IO Service cannot be null");
  55. }
  56. };
  57. /// @brief May be used after instantiation to perform initialization unique
  58. /// to application. It must be invoked prior to invoking run. This would
  59. /// likely include the creation of additional IO sources and their
  60. /// integration into the io_service.
  61. virtual void init() = 0;
  62. /// @brief Implements the process's event loop. In its simplest form it
  63. /// would an invocation io_service_->run(). This method should not exit
  64. /// until the process itself is exiting due to a request to shutdown or
  65. /// some anomaly is forcing an exit.
  66. /// @return returns EXIT_SUCCESS upon a successful, normal termination,
  67. /// and EXIT_FAILURE to indicate an abnormal termination.
  68. virtual int run() = 0;
  69. /// @brief Implements the process's shutdown processing. When invoked, it
  70. /// should ensure that the process gracefully exits the run method.
  71. virtual int shutdown() = 0;
  72. /// @brief Processes the given configuration.
  73. ///
  74. /// This method may be called multiple times during the process lifetime.
  75. /// Certainly once during process startup, and possibly later if the user
  76. /// alters configuration. This method must not throw, it should catch any
  77. /// processing errors and return a success or failure answer as described
  78. /// below.
  79. ///
  80. /// @param config_set a new configuration (JSON) for the process
  81. /// @return an Element that contains the results of configuration composed
  82. /// of an integer status value (0 means successful, non-zero means failure),
  83. /// and a string explanation of the outcome.
  84. virtual isc::data::ConstElementPtr configure(isc::data::ConstElementPtr
  85. config_set) = 0;
  86. /// @brief Processes the given command.
  87. ///
  88. /// This method is called to execute any custom commands supported by the
  89. /// process. This method must not throw, it should catch any processing
  90. /// errors and return a success or failure answer as described below.
  91. ///
  92. /// @param command is a string label representing the command to execute.
  93. /// @param args is a set of arguments (if any) required for the given
  94. /// command.
  95. /// @return an Element that contains the results of command composed
  96. /// of an integer status value (0 means successful, non-zero means failure),
  97. /// and a string explanation of the outcome.
  98. virtual isc::data::ConstElementPtr command(
  99. const std::string& command, isc::data::ConstElementPtr args) = 0;
  100. /// @brief Destructor
  101. virtual ~DProcessBase(){};
  102. bool shouldShutdown() {
  103. return (shut_down_flag_);
  104. }
  105. void setShutdownFlag(bool value) {
  106. shut_down_flag_ = value;
  107. }
  108. const std::string& getName() const {
  109. return (name_);
  110. }
  111. IOServicePtr& getIoService() {
  112. return (io_service_);
  113. }
  114. private:
  115. /// @brief Text label for the process. Generally used in log statements,
  116. /// but otherwise can be arbitrary.
  117. std::string name_;
  118. /// @brief The IOService to be used for asynchronous event handling.
  119. IOServicePtr io_service_;
  120. /// @brief Boolean flag set when shutdown has been requested.
  121. bool shut_down_flag_;
  122. };
  123. /// @brief Defines a shared pointer to DProcessBase.
  124. typedef boost::shared_ptr<DProcessBase> DProcessBasePtr;
  125. }; // namespace isc::d2
  126. }; // namespace isc
  127. #endif