Kea  1.5.0
d_controller.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2018 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 
7 #ifndef D_CONTROLLER_H
8 #define D_CONTROLLER_H
9 
10 #include <asiolink/io_service.h>
11 #include <cc/data.h>
12 #include <exceptions/exceptions.h>
13 #include <log/logger_support.h>
14 #include <process/daemon.h>
15 #include <process/d_log.h>
16 #include <process/d_process.h>
18 
19 #include <boost/shared_ptr.hpp>
20 #include <boost/noncopyable.hpp>
21 
22 #include <string>
23 #include <set>
24 
25 namespace isc {
26 namespace process {
27 
30 class InvalidUsage : public isc::Exception {
31 public:
32  InvalidUsage(const char* file, size_t line, const char* what) :
33  isc::Exception(file, line, what) { };
34 };
35 
42 public:
43  VersionMessage(const char* file, size_t line, const char* what) :
44  isc::Exception(file, line, what) { };
45 };
46 
48 class LaunchError: public isc::Exception {
49 public:
50  LaunchError (const char* file, size_t line, const char* what) :
51  isc::Exception(file, line, what) { };
52 };
53 
56 public:
57  ProcessInitError (const char* file, size_t line, const char* what) :
58  isc::Exception(file, line, what) { };
59 };
60 
64 public:
65  ProcessRunError (const char* file, size_t line, const char* what) :
66  isc::Exception(file, line, what) { };
67 };
68 
71 public:
72  DControllerBaseError (const char* file, size_t line, const char* what) :
73  isc::Exception(file, line, what) { };
74 };
75 
76 
78 class DControllerBase;
79 typedef boost::shared_ptr<DControllerBase> DControllerBasePtr;
80 
104 class DControllerBase : public Daemon {
105 public:
111  DControllerBase(const char* app_name, const char* bin_name);
112 
114  virtual ~DControllerBase();
115 
118  std::string getVersion(bool extended);
119 
151  virtual void launch(int argc, char* argv[], const bool test_mode);
152 
165  new_config);
166 
180  new_config);
181 
220 
224  std::string getAppName() const {
225  return (app_name_);
226  }
227 
231  std::string getBinName() const {
232  return (bin_name_);
233  }
234 
244  versionGetHandler(const std::string& command,
246 
256  buildReportHandler(const std::string& command,
258 
268  configGetHandler(const std::string& command,
270 
285  configWriteHandler(const std::string& command,
287 
297  configTestHandler(const std::string& command,
299 
308  shutdownHandler(const std::string& command,
310 
311 protected:
323  virtual bool customOption(int option, char *optarg);
324 
333  virtual DProcessBase* createProcess() = 0;
334 
340  virtual const std::string getUsageText() const {
341  return ("");
342  }
343 
350  virtual const std::string getCustomOpts() const {
351  return ("");
352  }
353 
359  void checkConfigOnly();
360 
377  virtual void processSignal(int signum);
378 
382  bool isVerbose() const {
383  return (verbose_);
384  }
385 
389  void setVerbose(bool value) {
390  verbose_ = value;
391  }
392 
396  bool isCheckOnly() const {
397  return (check_only_);
398  }
399 
405  void setCheckOnly(bool value) {
406  check_only_ = value;
407  }
408 
413  return (io_service_);
414  }
415 
421  return (controller_);
422  }
423 
430  static void setController(const DControllerBasePtr& controller);
431 
446  void parseArgs(int argc, char* argv[]);
447 
448 
464  virtual isc::data::ConstElementPtr parseFile(const std::string& file_name);
465 
481  virtual isc::data::ConstElementPtr parseText(const std::string& input) {
482  static_cast<void>(input); // just tu shut up the unused parameter warning
484  return (elements);
485  }
486 
495  void initProcess();
496 
505  // @throw throws DControllerBaseError or indirectly DProcessBaseError
506  void runProcess();
507 
527 
534  void initSignalHandling();
535 
548  bool osSignalHandler(int signum);
549 
560  void ioSignalHandler(IOSignalId sequence_id);
561 
566  return (process_);
567  }
568 
573  void usage(const std::string& text);
574 
582  virtual std::string getVersionAddendum() { return (""); }
583 
584 private:
588  std::string app_name_;
589 
593  std::string bin_name_;
594 
596  bool verbose_;
597 
600  bool check_only_;
601 
606  DProcessBasePtr process_;
607 
609  asiolink::IOServicePtr io_service_;
610 
612  IOSignalQueuePtr io_signal_queue_;
613 
615  static DControllerBasePtr controller_;
616 
617 // DControllerTest is named a friend class to facilitate unit testing while
618 // leaving the intended member scopes intact.
619 friend class DControllerTest;
620 };
621 
622 }; // namespace isc::process
623 }; // namespace isc
624 
625 #endif
isc::process::DControllerBase::isCheckOnly
bool isCheckOnly() const
Supplies whether or not check only mode is enabled.
Definition: d_controller.h:396
isc::process::ProcessRunError::ProcessRunError
ProcessRunError(const char *file, size_t line, const char *what)
Definition: d_controller.h:65
isc::process::DControllerBase::checkConfig
virtual isc::data::ConstElementPtr checkConfig(isc::data::ConstElementPtr new_config)
Instance method invoked by the configuration event handler and which processes the actual configurati...
Definition: d_controller.cc:411
isc::process::LaunchError::LaunchError
LaunchError(const char *file, size_t line, const char *what)
Definition: d_controller.h:50
isc::process::DControllerBase::parseArgs
void parseArgs(int argc, char *argv[])
Processes the command line arguments.
Definition: d_controller.cc:211
isc::process::ProcessRunError
Exception thrown when the application process encounters an operation in its event loop (i....
Definition: d_controller.h:63
d_process.h
io_service_signal.h
isc::process::DControllerBase
Application Controller.
Definition: d_controller.h:104
isc::process::DControllerBase::launch
virtual void launch(int argc, char *argv[], const bool test_mode)
Acts as the primary entry point into the controller execution and provides the outermost application ...
Definition: d_controller.cc:57
isc::process::DControllerBaseError
Exception thrown when the controller encounters an operational error.
Definition: d_controller.h:70
isc::process::DControllerBase::setCheckOnly
void setCheckOnly(bool value)
Method for enabling or disabling check only mode.
Definition: d_controller.h:405
isc::process::DControllerBase::isVerbose
bool isVerbose() const
Supplies whether or not verbose logging is enabled.
Definition: d_controller.h:382
isc::process::DControllerBase::customOption
virtual bool customOption(int option, char *optarg)
Virtual method that provides derivations the opportunity to support additional command line options.
Definition: d_controller.cc:292
isc::process::IOSignalQueuePtr
boost::shared_ptr< IOSignalQueue > IOSignalQueuePtr
Defines a pointer to an IOSignalQueue.
Definition: io_service_signal.h:253
isc::process::InvalidUsage::InvalidUsage
InvalidUsage(const char *file, size_t line, const char *what)
Definition: d_controller.h:32
isc::process::DControllerBase::getVersionAddendum
virtual std::string getVersionAddendum()
Fetches text containing additional version specifics.
Definition: d_controller.h:582
isc::process::DControllerBase::DControllerBase
DControllerBase(const char *app_name, const char *bin_name)
Constructor.
Definition: d_controller.cc:31
isc::process::DControllerBase::getBinName
std::string getBinName() const
Fetches the name of the application executable.
Definition: d_controller.h:231
isc::process::Daemon
Base class for all services.
Definition: daemon.h:49
isc::process::DControllerBase::~DControllerBase
virtual ~DControllerBase()
Destructor.
Definition: d_controller.cc:674
isc::process::DControllerBase::parseFile
virtual isc::data::ConstElementPtr parseFile(const std::string &file_name)
Parse a given file into Elements.
Definition: d_controller.cc:51
isc::process::DControllerBase::setController
static void setController(const DControllerBasePtr &controller)
Static setter which sets the singleton instance.
Definition: d_controller.cc:39
io_service.h
isc::process::DControllerBase::getProcess
DProcessBasePtr getProcess()
Fetches the current process.
Definition: d_controller.h:565
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::process::DControllerBase::configTestHandler
isc::data::ConstElementPtr configTestHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-test command
Definition: d_controller.cc:505
isc::process::LaunchError
Exception thrown when the controller launch fails.
Definition: d_controller.h:48
isc::process::ProcessInitError
Exception thrown when the application process fails.
Definition: d_controller.h:55
isc::process::DControllerBase::usage
void usage(const std::string &text)
Prints the program usage text to std error.
Definition: d_controller.cc:652
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::process::IOSignalId
uint64_t IOSignalId
Defines a unique identifier type for IOSignal.
Definition: io_service_signal.h:28
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::process::DControllerBase::shutdownHandler
isc::data::ConstElementPtr shutdownHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for 'shutdown' command
Definition: d_controller.cc:561
isc::process::ProcessInitError::ProcessInitError
ProcessInitError(const char *file, size_t line, const char *what)
Definition: d_controller.h:57
isc::process::DControllerBase::versionGetHandler
isc::data::ConstElementPtr versionGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for version-get command
Definition: d_controller.cc:544
isc::process::DControllerBasePtr
boost::shared_ptr< DControllerBase > DControllerBasePtr
Definition: d_controller.h:78
isc::process::DControllerBase::updateConfig
virtual isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr new_config)
Instance method invoked by the configuration event handler and which processes the actual configurati...
Definition: d_controller.cc:405
isc::process::DControllerBase::configFromFile
virtual isc::data::ConstElementPtr configFromFile()
Reconfigures the process from a configuration file.
Definition: d_controller.cc:322
isc::process::DControllerBase::getUsageText
virtual const std::string getUsageText() const
Virtual method which can be used to contribute derivation specific usage text.
Definition: d_controller.h:340
isc::process::DProcessBase
Application Process Interface.
Definition: d_process.h:67
isc::process::DControllerBase::getCustomOpts
virtual const std::string getCustomOpts() const
Virtual method which returns a string containing the option letters for any custom command line optio...
Definition: d_controller.h:350
isc::process::DControllerBase::parseText
virtual isc::data::ConstElementPtr parseText(const std::string &input)
Parse text into Elements.
Definition: d_controller.h:481
isc::process::DControllerBase::getController
static DControllerBasePtr & getController()
Static getter which returns the singleton instance.
Definition: d_controller.h:420
isc::process::VersionMessage
Exception used to convey version info upwards.
Definition: d_controller.h:41
isc::process::DControllerBase::processSignal
virtual void processSignal(int signum)
Application-level signal processing method.
Definition: d_controller.cc:619
isc::process::DControllerBase::configWriteHandler
isc::data::ConstElementPtr configWriteHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-write command
Definition: d_controller.cc:424
isc::process::DControllerBase::osSignalHandler
bool osSignalHandler(int signum)
Handler for processing OS-level signals.
Definition: d_controller.cc:597
isc::process::DProcessBasePtr
boost::shared_ptr< DProcessBase > DProcessBasePtr
Defines a shared pointer to DProcessBase.
Definition: d_process.h:201
isc::process::DControllerBase::getVersion
std::string getVersion(bool extended)
returns Kea version on stdout and exit.
Definition: d_controller.cc:681
data.h
exceptions.h
isc::process::DControllerBase::configGetHandler
isc::data::ConstElementPtr configGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-get command
Definition: d_controller.cc:416
isc::process::DControllerBase::initProcess
void initProcess()
Instantiates the application process and then initializes it.
Definition: d_controller.cc:299
isc::process::DControllerBase::initSignalHandling
void initSignalHandling()
Initializes signal handling.
Definition: d_controller.cc:582
isc::process::DControllerBase::getAppName
std::string getAppName() const
Fetches the name of the application under control.
Definition: d_controller.h:224
isc::process::DControllerBase::createProcess
virtual DProcessBase * createProcess()=0
Abstract method that is responsible for instantiating the application process object.
isc::process::DControllerBase::DControllerTest
friend class DControllerTest
Definition: d_controller.h:619
d_log.h
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::process::InvalidUsage
Exception thrown when the command line is invalid.
Definition: d_controller.h:30
isc::process::DControllerBase::checkConfigOnly
void checkConfigOnly()
Check the configuration.
Definition: d_controller.cc:157
isc::process::DControllerBase::shutdownProcess
isc::data::ConstElementPtr shutdownProcess(isc::data::ConstElementPtr args)
Initiates shutdown procedure.
Definition: d_controller.cc:570
isc::process::DControllerBaseError::DControllerBaseError
DControllerBaseError(const char *file, size_t line, const char *what)
Definition: d_controller.h:72
isc::process::DControllerBase::buildReportHandler
isc::data::ConstElementPtr buildReportHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for 'build-report' command
Definition: d_controller.cc:556
isc::process::VersionMessage::VersionMessage
VersionMessage(const char *file, size_t line, const char *what)
Definition: d_controller.h:43
isc::process::DControllerBase::ioSignalHandler
void ioSignalHandler(IOSignalId sequence_id)
Handler for processing IOSignals.
Definition: d_controller.cc:606
isc::process::DControllerBase::runProcess
void runProcess()
Invokes the application process's event loop,(DBaseProcess::run).
Definition: d_controller.cc:390
logger_support.h
Logging initialization functions.
daemon.h
isc::process::DControllerBase::setVerbose
void setVerbose(bool value)
Method for enabling or disabling verbose logging.
Definition: d_controller.h:389
isc::process::DControllerBase::getIOService
asiolink::IOServicePtr & getIOService()
Getter for fetching the controller's IOService.
Definition: d_controller.h:412