Leosac  0.8.0
Open Source Access Control
kernel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "CoreUtils.hpp"
23 #include "MessageBus.hpp"
24 #include "RemoteControl.hpp"
25 #include "Scheduler.hpp"
29 #include "module_manager.hpp"
30 #include "tools/ToolsFwd.hpp"
32 #include "tools/db/db_fwd.hpp"
33 #include "tools/runtimeoptions.hpp"
36 #include <boost/property_tree/ptree.hpp>
37 #include <zmqpp/context.hpp>
38 
39 namespace Leosac
40 {
73 class Kernel
74 {
75  public:
85  explicit Kernel(const boost::property_tree::ptree &config,
86  bool strict_mode = false);
87 
91  Kernel(const Kernel &) = delete;
92 
96  Kernel &operator=(const Kernel &) = delete;
97 
101  Kernel &operator=(Kernel &&) = delete;
102 
106  Kernel(Kernel &&) = delete;
107 
111  ~Kernel();
112 
118  static boost::property_tree::ptree
120 
131  bool run();
132 
138  std::string script_directory() const;
139 
145  std::string factory_config_directory() const;
146 
150  const ModuleManager &module_manager() const;
151 
156 
160  zmqpp::context &zmqpp_context();
161 
166 
171 
177  bool save_config();
178 
183  void restart_later();
184 
188  const std::chrono::steady_clock::time_point start_time() const;
189 
193  DBPtr database();
194 
199 
200  private:
205  void module_manager_init();
206 
210  void handle_control_request();
211 
215  void factory_reset();
216 
220  void get_netconfig();
221 
227  void set_netconfig(zmqpp::message *msg);
228 
229  void extract_environ();
230 
231  void configure_database();
232 
233  void populate_default_db();
234 
238  void create_update_schema();
239 
240  void connect_to_db(const boost::property_tree::ptree &db_cfg_node);
241 
242  void configure_logger();
243 
248 
252  void register_core_services();
253 
259 
260  void shutdown();
261 
266  std::string config_file_path() const;
267 
269 
271 
275  zmqpp::context ctx_;
276 
281 
285  zmqpp::socket control_;
286 
290  zmqpp::socket bus_push_;
291 
295  zmqpp::reactor reactor_;
296 
301 
306 
312 
316  std::unique_ptr<NetworkConfig> network_config_;
317 
321  std::unique_ptr<RemoteControl> remote_controller_;
322 
323  enum class EnvironVar
324  {
326  SCRIPTS_DIR,
327  };
328 
329  std::map<EnvironVar, std::string> environ_;
330 
335 
339  bool autosave_;
340 
344  const std::chrono::steady_clock::time_point start_time_;
345 
350 
352 
354 
358  static Kernel *instance_;
360 };
361 }
Leosac::Kernel::reactor_
zmqpp::reactor reactor_
Watch for message on the control_ socket.
Definition: kernel.hpp:295
Leosac::Kernel::network_config_
std::unique_ptr< NetworkConfig > network_config_
Object that handle networking configuration.
Definition: kernel.hpp:316
Leosac::Kernel::factory_config_directory
std::string factory_config_directory() const
Return the path to factory config directory Uses environment variable if available,...
Definition: kernel.cpp:361
Leosac::Kernel::EnvironVar::FACTORY_CONFIG_DIR
@ FACTORY_CONFIG_DIR
Leosac::Kernel::bus_
MessageBus bus_
Application wide (inproc) message bus.
Definition: kernel.hpp:280
Leosac::Kernel::restart_later
void restart_later()
Set the running_ and want_restart flag so that leosac will restart in the next main loop iteration.
Definition: kernel.cpp:441
runtimeoptions.hpp
RuntimeOptions class declaration.
Leosac::Kernel::~Kernel
~Kernel()
Implemented in .cpp for unique_ptr to work.
Definition: kernel.cpp:117
Leosac::ConfigManager
That class helps manage the configuration for the application and its module.
Definition: ConfigManager.hpp:45
Leosac::Kernel::xmlnne_
Tools::XmlNodeNameEnforcer xmlnne_
Definition: kernel.hpp:351
Leosac::Kernel::set_netconfig
void set_netconfig(zmqpp::message *msg)
Handle SET_NETCONFIG command and update the configuration file directly.
Definition: kernel.cpp:307
Leosac::Kernel::module_manager_
ModuleManager module_manager_
Manages the different libraries (.so) we load, path to those libraries, modules instantiation.
Definition: kernel.hpp:311
Leosac::Kernel::autosave_
bool autosave_
Autosave configuration on shutdown.
Definition: kernel.hpp:339
Leosac::Kernel::run
bool run()
Main loop of the main thread.
Definition: kernel.cpp:151
ModuleManager
A second module manager that loads "ZMQ aware" module – modules that talks to the application through...
Definition: module_manager.hpp:54
Leosac::Kernel::connect_to_db
void connect_to_db(const boost::property_tree::ptree &db_cfg_node)
Definition: kernel.cpp:703
Leosac::Kernel::get_service_registry
friend ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Kernel::environ_
std::map< EnvironVar, std::string > environ_
Definition: kernel.hpp:329
Leosac::Kernel::service_registry_
ServiceRegistryUPtr service_registry_
Definition: kernel.hpp:353
Leosac::Kernel::database_
DBPtr database_
A pointer to the database used by Leosac, if any.
Definition: kernel.hpp:349
Scheduler.hpp
Leosac::Kernel::populate_default_db
void populate_default_db()
Definition: kernel.cpp:623
Leosac::Kernel::extract_environ
void extract_environ()
Definition: kernel.cpp:340
ToolsFwd.hpp
MessageBus.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Kernel::Kernel
Kernel(const boost::property_tree::ptree &config, bool strict_mode=false)
Construct a Kernel object.
Definition: kernel.cpp:66
Leosac::Kernel::configure_logger
void configure_logger()
Definition: kernel.cpp:368
RemoteControl.hpp
Leosac::Kernel::start_time
const std::chrono::steady_clock::time_point start_time() const
Return the time point at which Leosac started.
Definition: kernel.cpp:452
Leosac::Kernel::unregister_core_services
void unregister_core_services()
Unregister the service that were registered into register_core_services().
Definition: kernel.cpp:576
ServiceFwd.hpp
Leosac::Kernel::factory_reset
void factory_reset()
Reset Leosac configuration.
Definition: kernel.cpp:278
Leosac::Kernel::want_restart_
bool want_restart_
Should leosac restart ?
Definition: kernel.hpp:305
networkconfig.hpp
NetworkConfig class declaration.
Leosac::Kernel::get_netconfig
void get_netconfig()
Handle GET_NETCONFIG command.
Definition: kernel.cpp:295
Leosac::Kernel::ctx_
zmqpp::context ctx_
The application ZMQ context.
Definition: kernel.hpp:275
Leosac::Kernel::save_config
bool save_config()
Save the current configuration to its original file if autosave is enabled.
Definition: kernel.cpp:415
Leosac::ServiceRegistry
A class that manages services.
Definition: ServiceRegistry.hpp:110
Leosac::Kernel::operator=
Kernel & operator=(const Kernel &)=delete
Disable assignment operator.
Leosac::ServiceRegistryUPtr
std::unique_ptr< ServiceRegistry > ServiceRegistryUPtr
Definition: ServiceFwd.hpp:27
Leosac::Kernel::shutdown
void shutdown()
Definition: kernel.cpp:457
Leosac::Kernel::send_sighup_
bool send_sighup_
Should we broadcast "SIGHUP" in the next main loop iteration ?
Definition: kernel.hpp:334
Leosac::Kernel::is_running_
bool is_running_
Controls core main loop.
Definition: kernel.hpp:300
Leosac::Kernel::zmqpp_context
zmqpp::context & zmqpp_context()
Returns a reference to the zmqpp context created for the application.
Definition: kernel.cpp:431
Leosac::Kernel::module_manager_init
void module_manager_init()
Init the module manager by feeding it paths to library file, loading module, etc.
Definition: kernel.cpp:186
MessageBus
Implements a message bus (running in its own thread) that use 2 sockets.
Definition: MessageBus.hpp:29
Leosac::Kernel::configure_signal_handler
void configure_signal_handler()
Setup signal handling for the process.
Definition: kernel.cpp:734
Leosac::Kernel::make_config
static boost::property_tree::ptree make_config(const Leosac::Tools::RuntimeOptions &opt)
Build a property tree from a runtime object object.
Definition: kernel.cpp:128
Leosac::Kernel::instance_
static Kernel * instance_
A global pointer to the Kernel instance.
Definition: kernel.hpp:358
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
Leosac::Kernel::utils_
CoreUtilsPtr utils_
Definition: kernel.hpp:268
module_manager.hpp
Leosac::Kernel::config_manager_
ConfigManager config_manager_
Definition: kernel.hpp:270
Leosac::Kernel::script_directory
std::string script_directory() const
Return the path to the scripts directory.
Definition: kernel.cpp:354
Leosac::Kernel::create_update_schema
void create_update_schema()
Create and/or update the database schema.
Definition: kernel.cpp:756
CoreUtils.hpp
db_fwd.hpp
Leosac::Kernel::handle_control_request
void handle_control_request()
A request has arrived on the control_ socket.
Definition: kernel.cpp:235
Leosac::Kernel::remote_controller_
std::unique_ptr< RemoteControl > remote_controller_
Object that expose leosac to the world.
Definition: kernel.hpp:321
Leosac::Kernel::register_core_services
void register_core_services()
Register some important services to the service registry.
Definition: kernel.cpp:528
Leosac::Kernel::config_file_path
std::string config_file_path() const
Return the path to the kernel configuration file.
Definition: kernel.cpp:523
Leosac::Tools::XmlNodeNameEnforcer
This class is a simple wrapper that throws a ConfigException message formated to report the user that...
Definition: XmlNodeNameEnforcer.hpp:36
Leosac::Kernel::control_
zmqpp::socket control_
A REP socket to send request to the kernel.
Definition: kernel.hpp:285
Leosac::Tools::RuntimeOptions
Holds informations about runtime options, such as "is this a verbose run" or path to configurations f...
Definition: runtimeoptions.hpp:42
Leosac::Kernel::service_registry
ServiceRegistry & service_registry()
Retrieve a reference to the service registry.
Definition: kernel.cpp:617
Leosac::Kernel::configure_database
void configure_database()
Definition: kernel.cpp:476
ConfigChecker.hpp
Leosac::Kernel::core_utils
CoreUtilsPtr core_utils()
Returns a (smart) pointer to the core utils: some thread-safe utilities.
Definition: kernel.cpp:447
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
ConfigManager.hpp
Leosac::Kernel::module_manager
const ModuleManager & module_manager() const
Returns a reference to the module manager object (const version).
Definition: kernel.cpp:405
XmlNodeNameEnforcer.hpp
Leosac::Kernel::EnvironVar::SCRIPTS_DIR
@ SCRIPTS_DIR
Leosac::Kernel::EnvironVar
EnvironVar
Definition: kernel.hpp:323
Leosac::Kernel::config_manager
ConfigManager & config_manager()
Retrieve a reference to the configuration manager object.
Definition: kernel.cpp:436
Leosac::Kernel::start_time_
const std::chrono::steady_clock::time_point start_time_
Time-point when Leosac started to run.
Definition: kernel.hpp:344
Leosac::Kernel::bus_push_
zmqpp::socket bus_push_
A PUSH socket to write on the bus.
Definition: kernel.hpp:290
Leosac::Kernel::database
DBPtr database()
Retrieve a pointer to the database, if any.
Definition: kernel.cpp:518