Kea  1.9.9-git
d_controller.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2021 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>
12 #include <cc/data.h>
13 #include <exceptions/exceptions.h>
14 #include <log/logger_support.h>
15 #include <process/daemon.h>
16 #include <process/d_log.h>
17 #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 
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 
153  virtual int launch(int argc, char* argv[], const bool test_mode);
154 
167  new_config);
168 
182  new_config);
183 
218 
222  std::string getAppName() const {
223  return (app_name_);
224  }
225 
229  std::string getBinName() const {
230  return (bin_name_);
231  }
232 
242  versionGetHandler(const std::string& command,
244 
254  buildReportHandler(const std::string& command,
256 
266  configGetHandler(const std::string& command,
268 
283  configWriteHandler(const std::string& command,
285 
295  configTestHandler(const std::string& command,
297 
307  configReloadHandler(const std::string& command,
309 
319  configSetHandler(const std::string& command,
321 
330  shutdownHandler(const std::string& command,
332 
342  serverTagGetHandler(const std::string& command,
344 
355  statusGetHandler(const std::string& command,
357 
358 protected:
370  virtual bool customOption(int option, char *optarg);
371 
380  virtual DProcessBase* createProcess() = 0;
381 
387  virtual const std::string getUsageText() const {
388  return ("");
389  }
390 
397  virtual const std::string getCustomOpts() const {
398  return ("");
399  }
400 
406  void checkConfigOnly();
407 
423  virtual void processSignal(int signum);
424 
428  bool isVerbose() const {
429  return (verbose_);
430  }
431 
435  void setVerbose(bool value) {
436  verbose_ = value;
437  }
438 
442  bool isCheckOnly() const {
443  return (check_only_);
444  }
445 
451  void setCheckOnly(bool value) {
452  check_only_ = value;
453  }
454 
459  return (io_service_);
460  }
461 
466  static DControllerBasePtr& getController() {
467  return (controller_);
468  }
469 
476  static void setController(const DControllerBasePtr& controller);
477 
492  void parseArgs(int argc, char* argv[]);
493 
494 
510  virtual isc::data::ConstElementPtr parseFile(const std::string& file_name);
511 
527  virtual isc::data::ConstElementPtr parseText(const std::string& input) {
528  static_cast<void>(input); // just tu shut up the unused parameter warning
530  return (elements);
531  }
532 
541  void initProcess();
542 
551  // @throw throws DControllerBaseError or indirectly DProcessBaseError
552  void runProcess();
553 
573 
579  void initSignalHandling();
580 
585  return (process_);
586  }
587 
592  void usage(const std::string& text);
593 
601  virtual std::string getVersionAddendum() { return (""); }
602 
611 
612 private:
616  std::string app_name_;
617 
621  std::string bin_name_;
622 
624  bool verbose_;
625 
628  bool check_only_;
629 
634  DProcessBasePtr process_;
635 
637  isc::asiolink::IOServicePtr io_service_;
638 
640  isc::asiolink::IOSignalSetPtr io_signal_set_;
641 
643  static DControllerBasePtr controller_;
644 
645 // DControllerTest is named a friend class to facilitate unit testing while
646 // leaving the intended member scopes intact.
647 friend class DControllerTest;
648 };
649 
650 } // namespace isc::process
651 } // namespace isc
652 
653 #endif
virtual isc::data::ConstElementPtr parseFile(const std::string &file_name)
Parse a given file into Elements.
Definition: d_controller.cc:54
DControllerBaseError(const char *file, size_t line, const char *what)
Definition: d_controller.h:72
boost::shared_ptr< DControllerBase > DControllerBasePtr
Definition: d_controller.h:78
virtual isc::data::ConstElementPtr parseText(const std::string &input)
Parse text into Elements.
Definition: d_controller.h:527
std::string getVersion(bool extended)
returns Kea version on stdout and exit.
isc::data::ConstElementPtr statusGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for status-get command
isc::data::ConstElementPtr configTestHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-test command
isc::data::ConstElementPtr serverTagGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for server-tag-get command
virtual isc::data::ConstElementPtr configFromFile()
Reconfigures the process from a configuration file.
void usage(const std::string &text)
Prints the program usage text to std error.
asiolink::IOServicePtr & getIOService()
Getter for fetching the controller's IOService.
Definition: d_controller.h:458
isc::data::ConstElementPtr configReloadHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-reload command
Exception thrown when the application process fails.
Definition: d_controller.h:55
virtual void processSignal(int signum)
Application-level signal processing method.
void parseArgs(int argc, char *argv[])
Processes the command line arguments.
virtual ~DControllerBase()
Destructor.
DProcessBasePtr getProcess()
Fetches the current process.
Definition: d_controller.h:584
void runProcess()
Invokes the application process's event loop,(DBaseProcess::run).
virtual bool customOption(int option, char *optarg)
Virtual method that provides derivations the opportunity to support additional command line options...
isc::data::ConstElementPtr shutdownHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for 'shutdown' command
boost::shared_ptr< DProcessBase > DProcessBasePtr
Defines a shared pointer to DProcessBase.
Definition: d_process.h:215
Exception used to convey version info upwards.
Definition: d_controller.h:41
virtual std::string getVersionAddendum()
Fetches text containing additional version specifics.
Definition: d_controller.h:601
isc::data::ConstElementPtr shutdownProcess(isc::data::ConstElementPtr args)
Initiates shutdown procedure.
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:397
Exception thrown when the controller encounters an operational error.
Definition: d_controller.h:70
virtual isc::data::ConstElementPtr checkConfig(isc::data::ConstElementPtr new_config)
Instance method invoked by the configuration event handler and which processes the actual configurati...
void setVerbose(bool value)
Method for enabling or disabling verbose logging.
Definition: d_controller.h:435
static DControllerBasePtr & getController()
Static getter which returns the singleton instance.
Definition: d_controller.h:466
bool isCheckOnly() const
Supplies whether or not check only mode is enabled.
Definition: d_controller.h:442
std::string handleOtherObjects(isc::data::ConstElementPtr args)
Deals with other (i.e.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::data::ConstElementPtr configWriteHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-write command
void checkConfigOnly()
Check the configuration.
virtual const std::string getUsageText() const
Virtual method which can be used to contribute derivation specific usage text.
Definition: d_controller.h:387
InvalidUsage(const char *file, size_t line, const char *what)
Definition: d_controller.h:32
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
virtual isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr new_config)
Instance method invoked by the configuration event handler and which processes the actual configurati...
ProcessRunError(const char *file, size_t line, const char *what)
Definition: d_controller.h:65
virtual int 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:60
Exception thrown when the command line is invalid.
Definition: d_controller.h:30
This is a base class for exceptions thrown from the DNS library module.
static void setController(const DControllerBasePtr &controller)
Static setter which sets the singleton instance.
Definition: d_controller.cc:42
Defines the logger used by the top-level component of kea-dhcp-ddns.
LaunchError(const char *file, size_t line, const char *what)
Definition: d_controller.h:50
bool isVerbose() const
Supplies whether or not verbose logging is enabled.
Definition: d_controller.h:428
isc::data::ConstElementPtr configGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-get command
Logging initialization functions.
void initProcess()
Instantiates the application process and then initializes it.
isc::data::ConstElementPtr configSetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-set command
Exception thrown when the controller launch fails.
Definition: d_controller.h:48
ProcessInitError(const char *file, size_t line, const char *what)
Definition: d_controller.h:57
void setCheckOnly(bool value)
Method for enabling or disabling check only mode.
Definition: d_controller.h:451
std::string getAppName() const
Fetches the name of the application under control.
Definition: d_controller.h:222
Base class for all services.
Definition: daemon.h:48
std::string getBinName() const
Fetches the name of the application executable.
Definition: d_controller.h:229
DControllerBase(const char *app_name, const char *bin_name)
Constructor.
Definition: d_controller.cc:34
VersionMessage(const char *file, size_t line, const char *what)
Definition: d_controller.h:43
Application Process Interface.
Definition: d_process.h:81
isc::data::ConstElementPtr buildReportHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for 'build-report' command
Exception thrown when the application process encounters an operation in its event loop (i...
Definition: d_controller.h:63
void initSignalHandling()
Initializes signal handling.
Application Controller.
Definition: d_controller.h:104
virtual DProcessBase * createProcess()=0
Abstract method that is responsible for instantiating the application process object.
isc::data::ConstElementPtr versionGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for version-get command