Leosac  0.8.0
Open Source Access Control
AsioModule.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 #include "BaseModule.hpp"
21 #include "tools/bs2.hpp"
23 #include <boost/asio/io_service.hpp>
24 #include <boost/asio/steady_timer.hpp>
25 
26 namespace Leosac
27 {
28 namespace Module
29 {
30 
40 class AsioModule : public BaseModule
41 {
42  public:
43  AsioModule(zmqpp::context &ctx, zmqpp::socket *pipe,
44  const boost::property_tree::ptree &cfg, CoreUtilsPtr utils);
45 
46  ~AsioModule();
47 
48  virtual void run() override final;
49 
57  template <typename Callable>
58  void post(Callable &&callable)
59  {
60  io_service_.post(std::forward<Callable>(callable));
61  }
62 
63  protected:
64  boost::asio::io_service io_service_;
65 
72  virtual void on_service_event(const service_event::Event &) = 0;
73 
74  private:
75  std::unique_ptr<boost::asio::io_service::work> work_;
76  bs2::connection service_event_listener_;
77 
86 
87  struct StopWatcher : public std::enable_shared_from_this<StopWatcher>
88  {
90  : self_(self)
91  , timer_(self.io_service_){};
92 
93  void schedule_wait();
94 
95  private:
97  boost::asio::steady_timer timer_;
98  void wait(const boost::system::error_code &ec);
99  };
100 
105  : public std::enable_shared_from_this<AsyncReactorPoller>
106  {
108  : self_(self)
109  , timer_(self.io_service_){};
110 
111  void schedule_wait();
112 
113  private:
115  boost::asio::steady_timer timer_;
116  void wait_handler(const boost::system::error_code &ec);
117  };
118 };
119 }
120 }
Leosac::Module::AsioModule::StopWatcher
Definition: AsioModule.hpp:87
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Module::AsioModule::StopWatcher::self_
AsioModule & self_
Definition: AsioModule.hpp:96
Leosac::Module::AsioModule::on_service_event
virtual void on_service_event(const service_event::Event &)=0
Function invoked when a service event is triggered.
Leosac::Module::AsioModule::run
virtual void run() override final
This is the main loop of the module.
Definition: AsioModule.cpp:38
Leosac::Module::AsioModule::StopWatcher::StopWatcher
StopWatcher(AsioModule &self)
Definition: AsioModule.hpp:89
Leosac::Module::AsioModule::StopWatcher::schedule_wait
void schedule_wait()
Definition: AsioModule.cpp:72
ServiceRegistry.hpp
Leosac::Module::AsioModule::service_event_listener_
bs2::connection service_event_listener_
Definition: AsioModule.hpp:76
BaseModule.hpp
Leosac::Module::AsioModule::AsyncReactorPoller::AsyncReactorPoller
AsyncReactorPoller(AsioModule &self)
Definition: AsioModule.hpp:107
Leosac::Module::AsioModule::StopWatcher::timer_
boost::asio::steady_timer timer_
Definition: AsioModule.hpp:97
bs2.hpp
Leosac::Module::AsioModule::post
void post(Callable &&callable)
Post some work onto the work queue of the module.
Definition: AsioModule.hpp:58
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::AsioModule::AsyncReactorPoller::schedule_wait
void schedule_wait()
Definition: AsioModule.cpp:79
Leosac::Module::AsioModule
This is a base class for boost::asio 'aware' module.
Definition: AsioModule.hpp:40
Leosac::Module::AsioModule::AsioModule
AsioModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: AsioModule.cpp:28
Leosac::Module::AsioModule::AsyncReactorPoller::timer_
boost::asio::steady_timer timer_
Definition: AsioModule.hpp:115
Leosac::Module::AsioModule::~AsioModule
~AsioModule()
Definition: AsioModule.cpp:34
Leosac::Module::AsioModule::StopWatcher::wait
void wait(const boost::system::error_code &ec)
Definition: AsioModule.cpp:59
Leosac::Module::AsioModule::AsyncReactorPoller::wait_handler
void wait_handler(const boost::system::error_code &ec)
Definition: AsioModule.cpp:86
Leosac::service_event::Event
Definition: ServiceRegistry.hpp:37
Leosac::Module::AsioModule::AsyncReactorPoller
Poll the zmq reactor from BaseModule.
Definition: AsioModule.hpp:104
Leosac::Module::AsioModule::AsyncReactorPoller::self_
AsioModule & self_
Definition: AsioModule.hpp:114
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Module::AsioModule::work_
std::unique_ptr< boost::asio::io_service::work > work_
Definition: AsioModule.hpp:75
Leosac::Module::AsioModule::install_async_handlers
void install_async_handlers()
Install handlers that periodically poll for activity on the ZMQ reactor from BaseModule.
Definition: AsioModule.cpp:50
Leosac::Module::AsioModule::io_service_
boost::asio::io_service io_service_
Definition: AsioModule.hpp:64