Leosac  0.7.0
OpenSourceAccessControl
AsioModule.cpp
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 "AsioModule.hpp"
22 
23 namespace Leosac
24 {
25 namespace Module
26 {
27 
28 AsioModule::AsioModule(zmqpp::context &ctx, zmqpp::socket *pipe,
29  const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
30  : BaseModule(ctx, pipe, cfg, utils)
31 {
32 }
33 
35 {
36 }
37 
39 {
41  [this](const service_event::Event &e) { on_service_event(e); });
42 
43  work_ = std::make_unique<boost::asio::io_service::work>(io_service_);
45  io_service_.run();
46 
47  service_event_listener_.disconnect();
48 }
49 
51 {
52  auto stop_watcher(std::make_shared<StopWatcher>(*this));
53  stop_watcher->schedule_wait();
54 
55  auto reactor_poller(std::make_shared<AsyncReactorPoller>(*this));
56  reactor_poller->schedule_wait();
57 }
58 
59 void AsioModule::StopWatcher::wait(const boost::system::error_code &ec)
60 {
61  if (ec)
62  {
63  WARN("AsioModule StopWatcher timer has errored: " << ec.message());
64  ASSERT_LOG(0, "Failed.");
65  }
66  if (!self_.is_running_)
67  self_.work_.reset();
68  else
69  schedule_wait();
70 }
71 
73 {
74  timer_.expires_from_now(std::chrono::milliseconds(1000));
75  timer_.async_wait(std::bind(&AsioModule::StopWatcher::wait, shared_from_this(),
76  std::placeholders::_1));
77 }
78 
80 {
81  timer_.expires_from_now(std::chrono::milliseconds(25));
82  timer_.async_wait(std::bind(&AsioModule::AsyncReactorPoller::wait_handler,
83  shared_from_this(), std::placeholders::_1));
84 }
85 
87  const boost::system::error_code &ec)
88 {
89  ASSERT_LOG(ec == 0, "Error while processing wait_handler: " << ec.message());
90  self_.reactor_.poll(0);
91  if (self_.is_running_)
92  schedule_wait();
93 }
94 }
95 }
This is the header file for a generated source file, GitSHA1.cpp.
void wait_handler(const boost::system::error_code &ec)
Definition: AsioModule.cpp:86
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
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
bs2::connection service_event_listener_
Definition: AsioModule.hpp:76
Definition: log.hpp:35
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
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
AsioModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: AsioModule.cpp:28
bs2::connection register_event_listener(T &&callable)
Register a service-event listener.
std::unique_ptr< boost::asio::io_service::work > work_
Definition: AsioModule.hpp:75
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