d_process.h 7.86 KB
Newer Older
1
// Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 7 8 9

#ifndef D_PROCESS_H
#define D_PROCESS_H

10
#include <asiolink/io_service.h>
11
#include <cc/data.h>
12
#include <process/d_cfg_mgr.h>
13

14 15 16 17 18
#include <boost/shared_ptr.hpp>

#include <exceptions/exceptions.h>

namespace isc {
19
namespace process {
20 21

/// @brief Exception thrown if the process encountered an operational error.
22
class DProcessBaseError : public isc::Exception {
23
public:
24
    DProcessBaseError(const char* file, size_t line, const char* what) :
25 26 27
        isc::Exception(file, line, what) { };
};

28 29 30 31
/// @brief String value for the shutdown command.
static const std::string SHUT_DOWN_COMMAND("shutdown");

/// @brief Returned by the process to indicate a command was successful.
32
static const int COMMAND_SUCCESS = 0;
33 34

/// @brief Returned by the process to indicates a command failed.
35 36
static const int COMMAND_ERROR = 1;

37 38
/// @brief Returned by the process to indicates a command is not valid.
static const int COMMAND_INVALID = 2;
39

40 41
/// @brief Application Process Interface
///
42 43 44
/// DProcessBase is an abstract class represents the primary "application"
/// level object in a "managed" asynchronous application. It provides a uniform
/// interface such that a managing layer can construct, initialize, and start
45
/// the application's event loop.  The event processing is centered around the
46 47 48 49 50 51
/// use of isc::asiolink::io_service. The io_service is shared between the
/// managing layer and the DProcessBase.  This allows management layer IO such
/// as directives to be sensed and handled, as well as processing IO activity
/// specific to the application.  In terms of management layer IO, there are
/// methods shutdown, configuration updates, and commands unique to the
/// application.
52
class DProcessBase {
53 54 55
public:
    /// @brief Constructor
    ///
56
    /// @param app_name is a text label for the process. Generally used
57
    /// in log statements, but otherwise arbitrary.
58 59
    /// @param io_service is the io_service used by the caller for
    /// asynchronous event handling.
60 61
    /// @param cfg_mgr the configuration manager instance that handles
    /// configuration parsing.
62
    ///
63
    /// @throw DProcessBaseError is io_service is NULL.
64
    DProcessBase(const char* app_name, asiolink::IOServicePtr io_service, 
65 66 67
                 DCfgMgrBasePtr cfg_mgr)
        : app_name_(app_name), io_service_(io_service), shut_down_flag_(false),
        cfg_mgr_(cfg_mgr) {
68
        if (!io_service_) {
69
            isc_throw (DProcessBaseError, "IO Service cannot be null");
70
        }
71 72 73 74

        if (!cfg_mgr_) {
            isc_throw (DProcessBaseError, "CfgMgr cannot be null");
        }
75 76 77
    };

    /// @brief May be used after instantiation to perform initialization unique
78 79 80
    /// to application. It must be invoked prior to invoking run. This would
    /// likely include the creation of additional IO sources and their
    /// integration into the io_service.
81
    /// @throw DProcessBaseError if the initialization fails.
82 83 84 85 86 87
    virtual void init() = 0;

    /// @brief Implements the process's event loop. In its simplest form it
    /// would an invocation io_service_->run().  This method should not exit
    /// until the process itself is exiting due to a request to shutdown or
    /// some anomaly is forcing an exit.
88
    /// @throw DProcessBaseError if an operational error is encountered.
89 90
    virtual void run() = 0;

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    /// @brief Initiates the process's shutdown process. 
    /// 
    /// This is last step in the shutdown event callback chain, that is 
    /// intended to notify the process it is to begin its shutdown process.
    ///
    /// @param args an Element set of shutdown arguments (if any) that are
    /// supported by the process derivation. 
    /// 
    /// @return an Element that contains the results of argument processing,
    /// consisting of an integer status value (0 means successful, 
    /// non-zero means failure), and a string explanation of the outcome. 
    ///  
    /// @throw DProcessBaseError if an operational error is encountered.
    virtual isc::data::ConstElementPtr 
        shutdown(isc::data::ConstElementPtr args) = 0;
106 107 108

    /// @brief Processes the given configuration.
    ///
109 110 111 112
    /// This method may be called multiple times during the process lifetime.
    /// Certainly once during process startup, and possibly later if the user
    /// alters configuration. This method must not throw, it should catch any
    /// processing errors and return a success or failure answer as described
113
    /// below.
114 115 116 117
    ///
    /// @param config_set a new configuration (JSON) for the process
    /// @return an Element that contains the results of configuration composed
    /// of an integer status value (0 means successful, non-zero means failure),
118
    /// and a string explanation of the outcome.
119
    virtual isc::data::ConstElementPtr configure(isc::data::ConstElementPtr
120
                                                 config_set) = 0;
121

122 123 124 125
    /// @brief Processes the given command.
    ///
    /// This method is called to execute any custom commands supported by the
    /// process. This method must not throw, it should catch any processing
126 127 128 129
    /// errors and return a success or failure answer as described below.
    ///
    /// @param command is a string label representing the command to execute.
    /// @param args is a set of arguments (if any) required for the given
130
    /// command.
131
    /// @return an Element that contains the results of command composed
132 133 134 135 136 137
    /// of an integer status value: 
    ///
    /// - COMMAND_SUCCESS indicates a command was successful.
    /// - COMMAND_ERROR indicates a valid command failed execute.
    /// - COMMAND_INVALID indicates a command is not valid.
    ///
138
    /// and a string explanation of the outcome.
139
    virtual isc::data::ConstElementPtr command(
140
            const std::string& command, isc::data::ConstElementPtr args) = 0;
141

142
    /// @brief Destructor
143
    virtual ~DProcessBase(){};
144

145 146
    /// @brief Checks if the process has been instructed to shut down.
    ///
147
    /// @return true if process shutdown flag is true.
148
    bool shouldShutdown() const {
149
        return (shut_down_flag_);
150 151
    }

152 153 154 155 156
    /// @brief Sets the process shut down flag to the given value.
    ///
    /// @param value is the new value to assign the flag.
    void setShutdownFlag(bool value) {
        shut_down_flag_ = value;
157 158
    }

159
    /// @brief Fetches the application name.
160
    ///
161
    /// @return application name string.
162 163
    const std::string getAppName() const {
        return (app_name_);
164 165
    }

166 167
    /// @brief Fetches the controller's IOService.
    ///
168
    /// @return a reference to the controller's IOService.
169
    asiolink::IOServicePtr& getIoService() {
170 171 172
        return (io_service_);
    }

173
    /// @brief Convenience method for stopping IOservice processing.
174
    /// Invoking this will cause the process to exit any blocking
175 176 177 178 179 180
    /// IOService method such as run().  No further IO events will be
    /// processed.
    void stopIOService() {
        io_service_->stop();
    }

181 182
    /// @brief Fetches the process's configuration manager.
    ///
183
    /// @return a reference to the configuration manager.
184 185 186 187
    DCfgMgrBasePtr& getCfgMgr() {
        return (cfg_mgr_);
    }

188
private:
189 190
    /// @brief Text label for the process. Generally used in log statements,
    /// but otherwise can be arbitrary.
191
    std::string app_name_;
192

193
    /// @brief The IOService to be used for asynchronous event handling.
194
    asiolink::IOServicePtr io_service_;
195 196

    /// @brief Boolean flag set when shutdown has been requested.
197
    bool shut_down_flag_;
198 199 200

    /// @brief  Pointer to the configuration manager.
    DCfgMgrBasePtr cfg_mgr_;
201 202
};

203 204
/// @brief Defines a shared pointer to DProcessBase.
typedef boost::shared_ptr<DProcessBase> DProcessBasePtr;
205

206
}; // namespace isc::process
207 208 209
}; // namespace isc

#endif