Leosac  0.8.0
Open Source Access Control
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 == boost::system::errc::success, "Error while processing wait_handler: " << ec.message());
90  self_.reactor_.poll(0);
91  if (self_.is_running_)
92  schedule_wait();
93 }
94 }
95 }
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Module::AsioModule::StopWatcher::self_
AsioModule & self_
Definition: AsioModule.hpp:96
WARN
@ WARN
Definition: log.hpp:33
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
AsioModule.hpp
Leosac::Module::AsioModule::StopWatcher::schedule_wait
void schedule_wait()
Definition: AsioModule.cpp:72
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Module::AsioModule::service_event_listener_
bs2::connection service_event_listener_
Definition: AsioModule.hpp:76
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::Module::AsioModule::AsyncReactorPoller::schedule_wait
void schedule_wait()
Definition: AsioModule.cpp:79
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::BaseModule::is_running_
bool is_running_
Boolean indicating whether the main loop should run or not.
Definition: BaseModule.hpp:203
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::ServiceRegistry::register_event_listener
bs2::connection register_event_listener(T &&callable)
Register a service-event listener.
Definition: ServiceRegistry.hpp:349
Leosac::service_event::Event
Definition: ServiceRegistry.hpp:37
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