dodo  0.0.1
A C++ library to create containerized Linux services
application.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file application.hpp
20  * Defines the dodo::common::Config class.
21  */
22 
23 #ifndef common_application_hpp
24 #define common_application_hpp
25 
26 #include <string>
27 #include "common/logger.hpp"
28 
29 namespace dodo::common {
30 
31  /**
32  * @brief Red tape wrapper class for applications, from command line to services. The Application class
33  *
34  * - initializes and closes the dodo library.
35  * - installs signal handlers, flag when the Application is requested to stop.
36  * - determining the HostType.
37  * - exposing and managing the configuration file
38  * - providing a logging mechanism
39  *
40  * @code
41  * using namespace dodo::common;
42  *
43  * class MyApp : public common::Application {
44  * public:
45  * MyApp( const StartParameters &param ) : common::Application( param ) {}
46  * virtual int run() { cout << "Hello world!" << endl; return 0; }
47  * };
48 
49  * int main( int argc, char* argv[], char** envp ) {
50  * try {
51  * MyApp app( { "myapp", "myapp.cnf", argc, argv, envp } );
52  * return app.run();
53  * }
54  * catch ( const std::exception &e ) {
55  * cerr << e.what() << endl;
56  * return 1;
57  * }
58  * }
59  * @endcode
60  */
61  class Application {
62  public:
63 
64  /**
65  * The type of host the Application is running on.
66  */
67  enum class HostType {
68  BareMetal, /**< Bare metal deployment. */
69  Docker, /**< Docker container. */
70  VirtualBox, /**< VirtualBox. */
71  VMWare, /**< VMWare. */
72  KVM, /**< RedHat KVM. */
73  GenericVM, /**< An indeterminate virtual machine. */
74  };
75 
76  /**
77  * Start parameters for the Application.
78  */
79  struct StartParameters {
80  std::string config; /**< Configuration file. */
81  int argc; /**< Argument count. */
82  char** argv; /**< Argument array. */
83  char** envp; /**< Environment variables. */
84  };
85 
86  /**
87  * Construct an Application instance.
88  * @param param The Application start parameters.
89  * @throw dodo::common::Exception when the name is empty, the config file is not empty and invalid.
90  */
91  Application( const StartParameters &param );
92 
93  /**
94  * Destructor
95  */
96  virtual ~Application();
97 
98 
99  /**
100  * Return the HostType the Application is running on.
101  * @return The HostType.
102  */
103  HostType getHostType() const { return hosttype_; }
104 
105  /**
106  * Return true when the main thread got a SIGTERM or SIGQUIT.
107  * @return true when the main thread got a SIGTERM or SIGQUIT.
108  */
109  bool hasStopRequest() const { return has_stop_request_; }
110 
111  /**
112  * Convert a HostType to a human readable string.
113  * @param ht The HostType to convert.
114  * @return The string.
115  */
116  static std::string getHostTypeAsString( HostType ht );
117 
118 
119 
120  /**
121  * Override.
122  * @return the return code as returned to the OS (exit code of the process).
123  */
124  virtual int run() { return 0; }
125  private:
126 
127  /**
128  * Detect the HostType of the host the Application is running on.
129  * @return The HostType;
130  */
132 
133  /**
134  * Signal handler called by the OS.
135  * Depends on application_ to be valid.
136  * @param signal the signal received.
137  */
138  static void signal_handler( int signal );
139 
140  /**
141  * Install the signal handlers.
142  */
143  void installSignalHandlers();
144 
145  /**
146  * Called by signal_handler. Note that this call can be made at any time interrupting the current thread,
147  * but no need for synchronization setting has_stop_request_ to true, other threads only read it.
148  * @param signal The received signal.
149  */
150  void onSignal( int signal );
151 
152  /**
153  * Singleton Application object.
154  */
156 
157  /**
158  * The configuration file name
159  */
160  std::string config_file_;
161 
162  /**
163  * The HostType, detected once in the Application constructor.
164  */
166 
167  /**
168  * True when the Application main pid got a signal to stop (SIGTERM).
169  */
171 
172  /**
173  * The Logger.
174  */
176 
177  };
178 
179 }
180 
181 #endif
dodo::common::Application::StartParameters::envp
char ** envp
Environment variables.
Definition: application.hpp:83
dodo::common::Application::HostType::VirtualBox
@ VirtualBox
VirtualBox.
dodo::common::Application::hasStopRequest
bool hasStopRequest() const
Return true when the main thread got a SIGTERM or SIGQUIT.
Definition: application.hpp:109
dodo::common::Logger
A Logger interface.
Definition: logger.hpp:40
dodo::common::Application::StartParameters
Start parameters for the Application.
Definition: application.hpp:79
dodo::common::Application::detectHostType
HostType detectHostType()
Detect the HostType of the host the Application is running on.
Definition: application.cpp:85
dodo::common::Application::hosttype_
HostType hosttype_
The HostType, detected once in the Application constructor.
Definition: application.hpp:165
dodo::common::Application::getHostType
HostType getHostType() const
Return the HostType the Application is running on.
Definition: application.hpp:103
dodo::common::Application::signal_handler
static void signal_handler(int signal)
Signal handler called by the OS.
Definition: application.cpp:62
dodo::common::Application::HostType::VMWare
@ VMWare
VMWare.
dodo::common::Application::application_
static Application * application_
Singleton Application object.
Definition: application.hpp:155
dodo::common::Application::HostType::GenericVM
@ GenericVM
An indeterminate virtual machine.
dodo::common::Application::HostType::Docker
@ Docker
Docker container.
dodo::common::Application::onSignal
void onSignal(int signal)
Called by signal_handler.
Definition: application.cpp:72
dodo::common::Application::logger_
Logger * logger_
The Logger.
Definition: application.hpp:175
dodo::common::Application::HostType::BareMetal
@ BareMetal
Bare metal deployment.
dodo::common::Application::getHostTypeAsString
static std::string getHostTypeAsString(HostType ht)
Convert a HostType to a human readable string.
Definition: application.cpp:50
dodo::common::Application::StartParameters::argv
char ** argv
Argument array.
Definition: application.hpp:82
dodo::common::Application::~Application
virtual ~Application()
Destructor.
Definition: application.cpp:44
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::Application::config_file_
std::string config_file_
The configuration file name.
Definition: application.hpp:160
dodo::common::Application
Red tape wrapper class for applications, from command line to services.
Definition: application.hpp:61
dodo::common::Application::has_stop_request_
bool has_stop_request_
True when the Application main pid got a signal to stop (SIGTERM).
Definition: application.hpp:170
logger.hpp
dodo::common::Application::Application
Application(const StartParameters &param)
Construct an Application instance.
Definition: application.cpp:33
dodo::common::Application::run
virtual int run()
Override.
Definition: application.hpp:124
dodo::common::Application::StartParameters::argc
int argc
Argument count.
Definition: application.hpp:81
dodo::common::Application::HostType::KVM
@ KVM
RedHat KVM.
dodo::common::Application::installSignalHandlers
void installSignalHandlers()
Install the signal handlers.
Definition: application.cpp:66
dodo::common::Application::StartParameters::config
std::string config
Configuration file.
Definition: application.hpp:80
dodo::common::Application::HostType
HostType
The type of host the Application is running on.
Definition: application.hpp:67