Leosac  0.8.0
Open Source Access Control
BaseModule.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 "LeosacFwd.hpp"
25 #include "tools/ThreadUtils.hpp"
26 #include "tools/log.hpp"
27 #include <boost/property_tree/ptree.hpp>
28 #include <zmqpp/zmqpp.hpp>
29 
30 extern "C" {
31 const char *get_module_name();
32 }
33 
34 namespace Leosac
35 {
36 namespace Module
37 {
44 template <typename UserModule>
45 bool start_module_helper(zmqpp::socket *pipe, boost::property_tree::ptree cfg,
46  zmqpp::context &zmq_ctx, CoreUtilsPtr utils)
47 {
48  try
49  {
51  UserModule module(zmq_ctx, pipe, cfg, utils);
52  INFO("Module " << get_module_name()
53  << " is now initialized. Thread id = " << Leosac::gettid());
54  pipe->send(zmqpp::signal::ok);
55 
56  module.run();
57  }
58  catch (const std::exception &e)
59  {
60  ERROR("An exception occurred in module code. Leosac will terminate."
61  << std::endl
62  << "Message: " << e.what() << std::endl
63  << "Module: " << get_module_name());
64  std::terminate();
65  }
66  INFO("Module " << get_module_name() << " has now terminated.");
67  return true;
68 }
69 
111 {
112  public:
116  BaseModule(zmqpp::context &ctx, zmqpp::socket *pipe,
117  const boost::property_tree::ptree &cfg, CoreUtilsPtr utils);
118 
119  virtual ~BaseModule() = default;
120 
127  virtual void run();
128 
129  protected:
136  virtual void handle_pipe();
137 
141  virtual void handle_control();
142 
155  virtual void dump_additional_config(zmqpp::message *out) const;
156 
163  void dump_config(ConfigManager::ConfigFormat fmt, zmqpp::message *out_msg) const;
164 
170  void config_check(const std::string &obj_name, Leosac::Hardware::DeviceClass type);
171 
177  void config_check(const std::string &obj_name);
178 
183  zmqpp::context &ctx_;
184 
188  zmqpp::socket &pipe_;
189 
193  boost::property_tree::ptree config_;
194 
199 
204 
208  zmqpp::socket control_;
209 
214  zmqpp::reactor reactor_;
215 
216  std::string name_;
217 };
218 }
219 }
Leosac::Module::BaseModule::BaseModule
BaseModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Constructor of BaseModule.
Definition: BaseModule.cpp:32
Leosac::gettid
uint64_t gettid()
Return the Linux thread ID.
Definition: ThreadUtils.cpp:32
Leosac::Module::BaseModule::pipe_
zmqpp::socket & pipe_
A reference to the pair socket that link back to the module manager.
Definition: BaseModule.hpp:188
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Hardware::DeviceClass
DeviceClass
An enumeration describing the class of the device.
Definition: HardwareFwd.hpp:42
Leosac::Module::BaseModule::config_check
void config_check(const std::string &obj_name, Leosac::Hardware::DeviceClass type)
An helper that checks configuration the existence of some objects.
Definition: BaseModule.cpp:143
ERROR
@ ERROR
Definition: log.hpp:32
Leosac::Module::start_module_helper
bool start_module_helper(zmqpp::socket *pipe, boost::property_tree::ptree cfg, zmqpp::context &zmq_ctx, CoreUtilsPtr utils)
This function template is a helper wrt writing the start_module function for every module.
Definition: BaseModule.hpp:45
LeosacFwd.hpp
INFO
@ INFO
Definition: log.hpp:34
Leosac::ConfigManager::ConfigFormat
ConfigFormat
This enum is used internally, when core request module configuration.
Definition: ConfigManager.hpp:118
Leosac::set_thread_name
void set_thread_name(const std::string &name)
Set the name of the current thread.
Definition: ThreadUtils.cpp:37
ThreadUtils.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::BaseModule::config_
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
Definition: BaseModule.hpp:193
Leosac::Module::BaseModule::handle_pipe
virtual void handle_pipe()
The base class register the pipe_ socket to its reactor_ so that this function is called when the pip...
Definition: BaseModule.cpp:56
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::BaseModule::handle_control
virtual void handle_control()
Handle called when a message on the module's control socket arrives.
Definition: BaseModule.cpp:101
Leosac::Module::BaseModule::utils_
CoreUtilsPtr utils_
Pointer to the core utils, which gives access to scheduler and others.
Definition: BaseModule.hpp:198
Leosac::Module::BaseModule::is_running_
bool is_running_
Boolean indicating whether the main loop should run or not.
Definition: BaseModule.hpp:203
Leosac::Module::BaseModule::dump_additional_config
virtual void dump_additional_config(zmqpp::message *out) const
Dump additional configuration (for example module specific config file).
Definition: BaseModule.cpp:127
get_module_name
const char * get_module_name()
Definition: init.cpp:26
Leosac::Module::BaseModule::~BaseModule
virtual ~BaseModule()=default
Leosac::Module::BaseModule::run
virtual void run()
This is the main loop of the module.
Definition: BaseModule.cpp:48
Leosac::Colorize::detail::format
std::string format(const std::string &escape_code, const T &in)
Return a string containing the escape code, a string representation of T and the clear escape string.
Definition: Colorize.hpp:49
log.hpp
Leosac::Module::BaseModule::control_
zmqpp::socket control_
Control REP socket.
Definition: BaseModule.hpp:208
ConfigChecker.hpp
Leosac::Module::BaseModule::name_
std::string name_
Definition: BaseModule.hpp:216
Leosac::Module::BaseModule::ctx_
zmqpp::context & ctx_
A reference to the ZeroMQ context in case you need it to create additional socket.
Definition: BaseModule.hpp:183
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
ConfigManager.hpp
Leosac::Module::BaseModule::dump_config
void dump_config(ConfigManager::ConfigFormat fmt, zmqpp::message *out_msg) const
Fills a message with the module's configuration information.
Definition: BaseModule.cpp:76