Leosac  0.7.0
OpenSourceAccessControl
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 }
boost::asio::steady_timer timer_
Definition: AsioModule.hpp:97
This is the header file for a generated source file, GitSHA1.cpp.
virtual void on_service_event(const service_event::Event &)=0
Function invoked when a service event is triggered.
boost::asio::io_service io_service_
Definition: AsioModule.hpp:64
bs2::connection service_event_listener_
Definition: AsioModule.hpp:76
void post(Callable &&callable)
Post some work onto the work queue of the module.
Definition: AsioModule.hpp:58
void install_async_handlers()
Install handlers that periodically poll for activity on the ZMQ reactor from BaseModule.
Definition: AsioModule.cpp:50
Base class for module implementation.
Definition: BaseModule.hpp:110
Poll the zmq reactor from BaseModule.
Definition: AsioModule.hpp:104
AsioModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: AsioModule.cpp:28
std::unique_ptr< boost::asio::io_service::work > work_
Definition: AsioModule.hpp:75
This is a base class for boost::asio &#39;aware&#39; module.
Definition: AsioModule.hpp:40
virtual void run() override final
This is the main loop of the module.
Definition: AsioModule.cpp:38
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
void wait(const boost::system::error_code &ec)
Definition: AsioModule.cpp:59