dodo  0.0.1
A C++ library to create containerized Linux services
dodo::common::Application Class Reference

Red tape wrapper class for applications, from command line to services. More...

#include <application.hpp>

Collaboration diagram for dodo::common::Application:

Data Structures

struct  StartParameters
 Start parameters for the Application. More...
 

Public Types

enum  HostType {
  HostType::BareMetal, HostType::Docker, HostType::VirtualBox, HostType::VMWare,
  HostType::KVM, HostType::GenericVM
}
 The type of host the Application is running on. More...
 

Public Member Functions

 Application (const StartParameters &param)
 Construct an Application instance. More...
 
virtual ~Application ()
 Destructor. More...
 
HostType getHostType () const
 Return the HostType the Application is running on. More...
 
bool hasStopRequest () const
 Return true when the main thread got a SIGTERM or SIGQUIT. More...
 
virtual int run ()
 Override. More...
 

Static Public Member Functions

static std::string getHostTypeAsString (HostType ht)
 Convert a HostType to a human readable string. More...
 

Private Member Functions

HostType detectHostType ()
 Detect the HostType of the host the Application is running on. More...
 
void installSignalHandlers ()
 Install the signal handlers. More...
 
void onSignal (int signal)
 Called by signal_handler. More...
 

Static Private Member Functions

static void signal_handler (int signal)
 Signal handler called by the OS. More...
 

Private Attributes

std::string config_file_
 The configuration file name. More...
 
bool has_stop_request_
 True when the Application main pid got a signal to stop (SIGTERM). More...
 
HostType hosttype_
 The HostType, detected once in the Application constructor. More...
 
Loggerlogger_
 The Logger. More...
 

Static Private Attributes

static Applicationapplication_ = nullptr
 Singleton Application object. More...
 

Detailed Description

Red tape wrapper class for applications, from command line to services.

The Application class

  • initializes and closes the dodo library.
  • installs signal handlers, flag when the Application is requested to stop.
  • determining the HostType.
  • exposing and managing the configuration file
  • providing a logging mechanism
using namespace dodo::common;
class MyApp : public common::Application {
public:
MyApp( const StartParameters &param ) : common::Application( param ) {}
virtual int run() { cout << "Hello world!" << endl; return 0; }
};
int main( int argc, char* argv[], char** envp ) {
try {
MyApp app( { "myapp", "myapp.cnf", argc, argv, envp } );
return app.run();
}
catch ( const std::exception &e ) {
cerr << e.what() << endl;
return 1;
}
}

Definition at line 61 of file application.hpp.

Member Enumeration Documentation

◆ HostType

The type of host the Application is running on.

Enumerator
BareMetal 

Bare metal deployment.

Docker 

Docker container.

VirtualBox 

VirtualBox.

VMWare 

VMWare.

KVM 

RedHat KVM.

GenericVM 

An indeterminate virtual machine.

Definition at line 67 of file application.hpp.

Constructor & Destructor Documentation

◆ Application()

dodo::common::Application::Application ( const StartParameters param)

◆ ~Application()

dodo::common::Application::~Application ( )
virtual

Destructor.

Definition at line 44 of file application.cpp.

References dodo::closeLibrary(), dodo::common::Config::config_, and dodo::common::Logger::logger_.

Here is the call graph for this function:

Member Function Documentation

◆ detectHostType()

Application::HostType dodo::common::Application::detectHostType ( )
private

Detect the HostType of the host the Application is running on.

Returns
The HostType;

Definition at line 85 of file application.cpp.

References BareMetal, Docker, dodo::common::fileReadAccess(), dodo::common::fileReadStrings(), GenericVM, KVM, VirtualBox, and VMWare.

Referenced by Application().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getHostType()

HostType dodo::common::Application::getHostType ( ) const
inline

Return the HostType the Application is running on.

Returns
The HostType.

Definition at line 103 of file application.hpp.

References hosttype_.

◆ getHostTypeAsString()

std::string dodo::common::Application::getHostTypeAsString ( HostType  ht)
static

Convert a HostType to a human readable string.

Parameters
htThe HostType to convert.
Returns
The string.

Definition at line 50 of file application.cpp.

References BareMetal, Docker, GenericVM, KVM, VirtualBox, and VMWare.

Referenced by Application().

Here is the caller graph for this function:

◆ hasStopRequest()

bool dodo::common::Application::hasStopRequest ( ) const
inline

Return true when the main thread got a SIGTERM or SIGQUIT.

Returns
true when the main thread got a SIGTERM or SIGQUIT.

Definition at line 109 of file application.hpp.

References has_stop_request_.

◆ installSignalHandlers()

void dodo::common::Application::installSignalHandlers ( )
private

Install the signal handlers.

Definition at line 66 of file application.cpp.

References signal_handler().

Referenced by Application().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onSignal()

void dodo::common::Application::onSignal ( int  signal)
private

Called by signal_handler.

Note that this call can be made at any time interrupting the current thread, but no need for synchronization setting has_stop_request_ to true, other threads only read it.

Parameters
signalThe received signal.

Definition at line 72 of file application.cpp.

References has_stop_request_, and log_Warning.

Referenced by signal_handler().

Here is the caller graph for this function:

◆ run()

virtual int dodo::common::Application::run ( )
inlinevirtual

Override.

Returns
the return code as returned to the OS (exit code of the process).

Definition at line 124 of file application.hpp.

◆ signal_handler()

void dodo::common::Application::signal_handler ( int  signal)
staticprivate

Signal handler called by the OS.

Depends on application_ to be valid.

Parameters
signalthe signal received.

Definition at line 62 of file application.cpp.

References application_, and onSignal().

Referenced by installSignalHandlers().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ application_

Application * dodo::common::Application::application_ = nullptr
staticprivate

Singleton Application object.

Definition at line 155 of file application.hpp.

Referenced by Application(), and signal_handler().

◆ config_file_

std::string dodo::common::Application::config_file_
private

The configuration file name.

Definition at line 160 of file application.hpp.

◆ has_stop_request_

bool dodo::common::Application::has_stop_request_
private

True when the Application main pid got a signal to stop (SIGTERM).

Definition at line 170 of file application.hpp.

Referenced by Application(), hasStopRequest(), and onSignal().

◆ hosttype_

HostType dodo::common::Application::hosttype_
private

The HostType, detected once in the Application constructor.

Definition at line 165 of file application.hpp.

Referenced by Application(), and getHostType().

◆ logger_

Logger* dodo::common::Application::logger_
private

The Logger.

Definition at line 175 of file application.hpp.

Referenced by Application().


The documentation for this class was generated from the following files:
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::Application
Red tape wrapper class for applications, from command line to services.
Definition: application.hpp:61
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