Leosac  0.8.0
Open Source Access Control
Leosac::Kernel Class Reference

Core of Leosac. More...

#include <kernel.hpp>

+ Collaboration diagram for Leosac::Kernel:

Public Member Functions

 Kernel (const boost::property_tree::ptree &config, bool strict_mode=false)
 Construct a Kernel object. More...
 
 Kernel (const Kernel &)=delete
 Disable copy constructor as it makes no sense to copy this. More...
 
Kerneloperator= (const Kernel &)=delete
 Disable assignment operator. More...
 
Kerneloperator= (Kernel &&)=delete
 Disable move-assignment. More...
 
 Kernel (Kernel &&)=delete
 Disable move-construction. More...
 
 ~Kernel ()
 Implemented in .cpp for unique_ptr to work. More...
 
bool run ()
 Main loop of the main thread. More...
 
std::string script_directory () const
 Return the path to the scripts directory. More...
 
std::string factory_config_directory () const
 Return the path to factory config directory Uses environment variable if available, otherwise defaults to hardcoded path relative to cwd. More...
 
const ModuleManagermodule_manager () const
 Returns a reference to the module manager object (const version). More...
 
ModuleManagermodule_manager ()
 Returns a reference to the module manager object. More...
 
zmqpp::context & zmqpp_context ()
 Returns a reference to the zmqpp context created for the application. More...
 
CoreUtilsPtr core_utils ()
 Returns a (smart) pointer to the core utils: some thread-safe utilities. More...
 
ConfigManagerconfig_manager ()
 Retrieve a reference to the configuration manager object. More...
 
bool save_config ()
 Save the current configuration to its original file if autosave is enabled. More...
 
void restart_later ()
 Set the running_ and want_restart flag so that leosac will restart in the next main loop iteration. More...
 
const std::chrono::steady_clock::time_point start_time () const
 Return the time point at which Leosac started. More...
 
DBPtr database ()
 Retrieve a pointer to the database, if any. More...
 
ServiceRegistryservice_registry ()
 Retrieve a reference to the service registry. More...
 

Static Public Member Functions

static boost::property_tree::ptree make_config (const Leosac::Tools::RuntimeOptions &opt)
 Build a property tree from a runtime object object. More...
 

Private Types

enum  EnvironVar { EnvironVar::FACTORY_CONFIG_DIR, EnvironVar::SCRIPTS_DIR }
 

Private Member Functions

void module_manager_init ()
 Init the module manager by feeding it paths to library file, loading module, etc. More...
 
void handle_control_request ()
 A request has arrived on the control_ socket. More...
 
void factory_reset ()
 Reset Leosac configuration. More...
 
void get_netconfig ()
 Handle GET_NETCONFIG command. More...
 
void set_netconfig (zmqpp::message *msg)
 Handle SET_NETCONFIG command and update the configuration file directly. More...
 
void extract_environ ()
 
void configure_database ()
 
void populate_default_db ()
 
void create_update_schema ()
 Create and/or update the database schema. More...
 
void connect_to_db (const boost::property_tree::ptree &db_cfg_node)
 
void configure_logger ()
 
void configure_signal_handler ()
 Setup signal handling for the process. More...
 
void register_core_services ()
 Register some important services to the service registry. More...
 
void unregister_core_services ()
 Unregister the service that were registered into register_core_services(). More...
 
void shutdown ()
 
std::string config_file_path () const
 Return the path to the kernel configuration file. More...
 

Private Attributes

CoreUtilsPtr utils_
 
ConfigManager config_manager_
 
zmqpp::context ctx_
 The application ZMQ context. More...
 
MessageBus bus_
 Application wide (inproc) message bus. More...
 
zmqpp::socket control_
 A REP socket to send request to the kernel. More...
 
zmqpp::socket bus_push_
 A PUSH socket to write on the bus. More...
 
zmqpp::reactor reactor_
 Watch for message on the control_ socket. More...
 
bool is_running_
 Controls core main loop. More...
 
bool want_restart_
 Should leosac restart ? More...
 
ModuleManager module_manager_
 Manages the different libraries (.so) we load, path to those libraries, modules instantiation. More...
 
std::unique_ptr< NetworkConfignetwork_config_
 Object that handle networking configuration. More...
 
std::unique_ptr< RemoteControlremote_controller_
 Object that expose leosac to the world. More...
 
std::map< EnvironVar, std::string > environ_
 
bool send_sighup_
 Should we broadcast "SIGHUP" in the next main loop iteration ? More...
 
bool autosave_
 Autosave configuration on shutdown. More...
 
const std::chrono::steady_clock::time_point start_time_
 Time-point when Leosac started to run. More...
 
DBPtr database_
 A pointer to the database used by Leosac, if any. More...
 
Tools::XmlNodeNameEnforcer xmlnne_
 
ServiceRegistryUPtr service_registry_
 

Static Private Attributes

static Kernelinstance_ = nullptr
 A global pointer to the Kernel instance. More...
 

Friends

ServiceRegistryget_service_registry ()
 A function to retrieve the ServiceRegistry from pretty much anywhere. More...
 

Detailed Description

Core of Leosac.

Handles module management and loading. The kernel binds a REP socket at "inproc://leosac-kernel".

Supported commands

Leosac kernel support a few commands. Those commands can be sent by modules.

Command (Frame 1) Frame 2 Frame 3 Description
RESTART Restart Leosac.

This will destroy the kernel object, unload all module, and restart. RESET | | | Reset Leosac configuration to factory default. This will also restart. GET_NETCONFIG | | | Send the network config. SET_NETCONFIG | Serialized ptree | | Write the new network config to file SCRIPTS_DIR | | | Ask the path to scripts directory FACTORY_CONFIG_DIR | | | Ask the path to factory config directory

Notifications

Leosac core occasionally broadcast notification for other subsystem. The message's topic is "KERNEL" for all notification coming from Leosac's core.

  • SYSTEM_READY message when all modules are initialized.
  • SIGHUP to tell module to reload their configuration (if they support hot-reload). SIGHUP is sent upon receiving the operating system level SIGHUP signal.

Definition at line 73 of file kernel.hpp.

Member Enumeration Documentation

◆ EnvironVar

enum Leosac::Kernel::EnvironVar
strongprivate
Enumerator
FACTORY_CONFIG_DIR 
SCRIPTS_DIR 

Definition at line 323 of file kernel.hpp.

Constructor & Destructor Documentation

◆ Kernel() [1/3]

Kernel::Kernel ( const boost::property_tree::ptree &  config,
bool  strict_mode = false 
)
explicit

Construct a Kernel object.

Only one Kernel object may be alive at any given time. This is because a global reference to the instance is kept.

Parameters
configinitial configuration tree
strict_modeare we running in strict mode ? (This is wrt configuration validation)
Note
You can use Kernel::make_config() to build a configuration tree.

Definition at line 66 of file kernel.cpp.

◆ Kernel() [2/3]

Leosac::Kernel::Kernel ( const Kernel )
delete

Disable copy constructor as it makes no sense to copy this.

◆ Kernel() [3/3]

Leosac::Kernel::Kernel ( Kernel &&  )
delete

Disable move-construction.

◆ ~Kernel()

Kernel::~Kernel ( )

Implemented in .cpp for unique_ptr to work.

Definition at line 117 of file kernel.cpp.

Member Function Documentation

◆ config_file_path()

std::string Kernel::config_file_path ( ) const
private

Return the path to the kernel configuration file.

This is a simple helper function to throw ConfigException more easily.

Definition at line 523 of file kernel.cpp.

◆ config_manager()

ConfigManager & Kernel::config_manager ( )

Retrieve a reference to the configuration manager object.

Definition at line 436 of file kernel.cpp.

◆ configure_database()

void Kernel::configure_database ( )
private

Definition at line 476 of file kernel.cpp.

◆ configure_logger()

void Kernel::configure_logger ( )
private

Definition at line 368 of file kernel.cpp.

◆ configure_signal_handler()

void Kernel::configure_signal_handler ( )
private

Setup signal handling for the process.

Definition at line 734 of file kernel.cpp.

◆ connect_to_db()

void Kernel::connect_to_db ( const boost::property_tree::ptree &  db_cfg_node)
private

Definition at line 703 of file kernel.cpp.

◆ core_utils()

CoreUtilsPtr Kernel::core_utils ( )

Returns a (smart) pointer to the core utils: some thread-safe utilities.

Definition at line 447 of file kernel.cpp.

◆ create_update_schema()

void Kernel::create_update_schema ( )
private

Create and/or update the database schema.

Definition at line 756 of file kernel.cpp.

◆ database()

DBPtr Kernel::database ( )

Retrieve a pointer to the database, if any.

Definition at line 518 of file kernel.cpp.

◆ extract_environ()

void Kernel::extract_environ ( )
private

Definition at line 340 of file kernel.cpp.

◆ factory_config_directory()

std::string Kernel::factory_config_directory ( ) const

Return the path to factory config directory Uses environment variable if available, otherwise defaults to hardcoded path relative to cwd.

Definition at line 361 of file kernel.cpp.

◆ factory_reset()

void Kernel::factory_reset ( )
private

Reset Leosac configuration.

Definition at line 278 of file kernel.cpp.

◆ get_netconfig()

void Kernel::get_netconfig ( )
private

Handle GET_NETCONFIG command.

Definition at line 295 of file kernel.cpp.

◆ handle_control_request()

void Kernel::handle_control_request ( )
private

A request has arrived on the control_ socket.

Definition at line 235 of file kernel.cpp.

◆ make_config()

boost::property_tree::ptree Kernel::make_config ( const Leosac::Tools::RuntimeOptions opt)
static

Build a property tree from a runtime object object.

It assume the kernel-config (-k) switch points to an XML config file. If we support more config source type (json) we should move this code.

Definition at line 128 of file kernel.cpp.

◆ module_manager() [1/2]

ModuleManager& Leosac::Kernel::module_manager ( )

Returns a reference to the module manager object.

◆ module_manager() [2/2]

ModuleManager & Kernel::module_manager ( ) const

Returns a reference to the module manager object (const version).

Definition at line 405 of file kernel.cpp.

◆ module_manager_init()

void Kernel::module_manager_init ( )
private

Init the module manager by feeding it paths to library file, loading module, etc.

Definition at line 186 of file kernel.cpp.

◆ operator=() [1/2]

Kernel& Leosac::Kernel::operator= ( const Kernel )
delete

Disable assignment operator.

◆ operator=() [2/2]

Kernel& Leosac::Kernel::operator= ( Kernel &&  )
delete

Disable move-assignment.

◆ populate_default_db()

void Kernel::populate_default_db ( )
private

Definition at line 623 of file kernel.cpp.

◆ register_core_services()

void Kernel::register_core_services ( )
private

Register some important services to the service registry.

Definition at line 528 of file kernel.cpp.

◆ restart_later()

void Kernel::restart_later ( )

Set the running_ and want_restart flag so that leosac will restart in the next main loop iteration.

Definition at line 441 of file kernel.cpp.

◆ run()

bool Kernel::run ( )

Main loop of the main thread.

What it does:

  1. It starts the communication BUS for devices's abstraction and general module to use.
  2. It starts by loading modules requested in configuration
  3. Wait for shutdown / restart order (linux signals).
    Returns
    If it returns true that means the application should restart instead of stopping.

Definition at line 151 of file kernel.cpp.

◆ save_config()

bool Kernel::save_config ( )

Save the current configuration to its original file if autosave is enabled.

This means that configuration change made when Leosac was running will be persisted.

Definition at line 415 of file kernel.cpp.

◆ script_directory()

std::string Kernel::script_directory ( ) const

Return the path to the scripts directory.

Uses environment variable if available, otherwise defaults to hardcoded path relative to cwd.

Definition at line 354 of file kernel.cpp.

◆ service_registry()

ServiceRegistry & Kernel::service_registry ( )

Retrieve a reference to the service registry.

Definition at line 617 of file kernel.cpp.

◆ set_netconfig()

void Kernel::set_netconfig ( zmqpp::message *  msg)
private

Handle SET_NETCONFIG command and update the configuration file directly.

The configuration update will take effect on the next restart.

Parameters
msgZMQ message that holds config

Definition at line 307 of file kernel.cpp.

◆ shutdown()

void Kernel::shutdown ( )
private

Definition at line 457 of file kernel.cpp.

◆ start_time()

const std::chrono::steady_clock::time_point Kernel::start_time ( ) const

Return the time point at which Leosac started.

Definition at line 452 of file kernel.cpp.

◆ unregister_core_services()

void Kernel::unregister_core_services ( )
private

Unregister the service that were registered into register_core_services().

Definition at line 576 of file kernel.cpp.

◆ zmqpp_context()

zmqpp::context & Kernel::zmqpp_context ( )

Returns a reference to the zmqpp context created for the application.

Definition at line 431 of file kernel.cpp.

Friends And Related Function Documentation

◆ get_service_registry

ServiceRegistry& get_service_registry ( )
friend

A function to retrieve the ServiceRegistry from pretty much anywhere.

The only requirement is that a Kernel object is currently alive.

Definition at line 25 of file GetServiceRegistry.cpp.

Member Data Documentation

◆ autosave_

bool Leosac::Kernel::autosave_
private

Autosave configuration on shutdown.

Definition at line 339 of file kernel.hpp.

◆ bus_

MessageBus Leosac::Kernel::bus_
private

Application wide (inproc) message bus.

Definition at line 280 of file kernel.hpp.

◆ bus_push_

zmqpp::socket Leosac::Kernel::bus_push_
private

A PUSH socket to write on the bus.

Definition at line 290 of file kernel.hpp.

◆ config_manager_

ConfigManager Leosac::Kernel::config_manager_
private

Definition at line 270 of file kernel.hpp.

◆ control_

zmqpp::socket Leosac::Kernel::control_
private

A REP socket to send request to the kernel.

Definition at line 285 of file kernel.hpp.

◆ ctx_

zmqpp::context Leosac::Kernel::ctx_
private

The application ZMQ context.

Definition at line 275 of file kernel.hpp.

◆ database_

DBPtr Leosac::Kernel::database_
private

A pointer to the database used by Leosac, if any.

Definition at line 349 of file kernel.hpp.

◆ environ_

std::map<EnvironVar, std::string> Leosac::Kernel::environ_
private

Definition at line 329 of file kernel.hpp.

◆ instance_

Kernel * Kernel::instance_ = nullptr
staticprivate

A global pointer to the Kernel instance.

Definition at line 358 of file kernel.hpp.

◆ is_running_

bool Leosac::Kernel::is_running_
private

Controls core main loop.

Definition at line 300 of file kernel.hpp.

◆ module_manager_

ModuleManager Leosac::Kernel::module_manager_
private

Manages the different libraries (.so) we load, path to those libraries, modules instantiation.

Definition at line 311 of file kernel.hpp.

◆ network_config_

std::unique_ptr<NetworkConfig> Leosac::Kernel::network_config_
private

Object that handle networking configuration.

Definition at line 316 of file kernel.hpp.

◆ reactor_

zmqpp::reactor Leosac::Kernel::reactor_
private

Watch for message on the control_ socket.

Definition at line 295 of file kernel.hpp.

◆ remote_controller_

std::unique_ptr<RemoteControl> Leosac::Kernel::remote_controller_
private

Object that expose leosac to the world.

Definition at line 321 of file kernel.hpp.

◆ send_sighup_

bool Leosac::Kernel::send_sighup_
private

Should we broadcast "SIGHUP" in the next main loop iteration ?

Definition at line 334 of file kernel.hpp.

◆ service_registry_

ServiceRegistryUPtr Leosac::Kernel::service_registry_
private

Definition at line 353 of file kernel.hpp.

◆ start_time_

const std::chrono::steady_clock::time_point Leosac::Kernel::start_time_
private

Time-point when Leosac started to run.

Definition at line 344 of file kernel.hpp.

◆ utils_

CoreUtilsPtr Leosac::Kernel::utils_
private

Definition at line 268 of file kernel.hpp.

◆ want_restart_

bool Leosac::Kernel::want_restart_
private

Should leosac restart ?

Definition at line 305 of file kernel.hpp.

◆ xmlnne_

Tools::XmlNodeNameEnforcer Leosac::Kernel::xmlnne_
private

Definition at line 351 of file kernel.hpp.


The documentation for this class was generated from the following files: