Leosac  0.8.0
Open Source Access Control
WSHelperThread.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2017 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 "core/CoreUtils.hpp"
26 #include "tools/AssertCast.hpp"
27 #include "tools/bs2.hpp"
29 #include <boost/asio.hpp>
30 
31 
32 namespace Leosac
33 {
34 namespace Module
35 {
36 namespace WebSockAPI
37 {
60 template <typename ParameterT>
62 {
63  public:
64  explicit BaseModuleSupportThread(const CoreUtilsPtr &core_utils,
65  const ParameterT &param)
66  : core_utils_(core_utils)
67  , parameters_(param)
68  {
69  }
70 
72  {
73  work_ = nullptr;
74  try
75  {
76  if (thread_)
77  thread_->join();
78  }
79  catch (const std::exception &e)
80  {
81  ERROR("Failed to join WebSockAPI::BaseModuleSupportThread");
82  }
83  }
84 
93  {
94  thread_ = std::make_unique<std::thread>([this]() { run_io_service(); });
95  }
96 
97  void set_parameter(const ParameterT &p)
98  {
99  std::lock_guard<decltype(mutex_)> lg(mutex_);
100  parameters_ = p;
101  }
102 
103  virtual void unregister_ws_handlers(Service &ws_service) = 0;
104 
105  private:
107  {
110  [this](const service_event::Event &e) { on_service_event(e); });
111 
112  // If we already have a WS Service object, register ws handler asap.
113  if (ws_service)
114  {
115  service_ptr_ = ws_service;
116  io_.post([this, ws_service]() {
117  std::lock_guard<decltype(mutex_)> lg(mutex_);
118  register_ws_handlers(*ws_service);
119  });
120  }
121 
122  work_ = std::make_unique<boost::asio::io_service::work>(io_);
123  io_.run();
124  }
125 
127  {
128  // We can be called on any thread.
129  if (e.type() == service_event::REGISTERED)
130  {
131  auto &service_registered_event =
132  assert_cast<const service_event::ServiceRegistered &>(e);
133  if (service_registered_event.interface_type() ==
134  boost::typeindex::type_id<WebSockAPI::Service>())
135  {
136  // request registration of handler from our helper thread.
137  io_.post([this]() {
138  auto ws_service =
140  ASSERT_LOG(ws_service, "WSService has disappeared");
141  service_ptr_ = ws_service;
142  {
143  std::lock_guard<decltype(mutex_)> lg(mutex_);
144  register_ws_handlers(*ws_service);
145  }
146  });
147  }
148  }
149  }
150 
157  virtual void register_ws_handlers(Service &ws_service) = 0;
158 
159  protected:
161  bs2::scoped_connection service_event_listener_;
162  std::unique_ptr<std::thread> thread_;
163  boost::asio::io_service io_;
164  std::unique_ptr<boost::asio::io_service::work> work_;
165  ParameterT parameters_;
166  std::mutex mutex_;
167 
168  // Keep a ref to the WS service so it doesn't
169  // disapear. This effectively keep the WebSockAPI module
170  // alive.
171  // Todo: Maybe improve the whole module/service infrastructure.
172  std::shared_ptr<WebSockAPI::Service> service_ptr_;
173 };
174 }
175 }
176 }
Leosac::Module::WebSockAPI::BaseModuleSupportThread::io_
boost::asio::io_service io_
Definition: WSHelperThread.hpp:163
Leosac::Module::WebSockAPI::BaseModuleSupportThread::set_parameter
void set_parameter(const ParameterT &p)
Definition: WSHelperThread.hpp:97
Leosac::Module::WebSockAPI::BaseModuleSupportThread::core_utils_
CoreUtilsPtr core_utils_
Definition: WSHelperThread.hpp:160
Leosac::Module::WebSockAPI::BaseModuleSupportThread::work_
std::unique_ptr< boost::asio::io_service::work > work_
Definition: WSHelperThread.hpp:164
Leosac::service_event::Event::type
EventType type() const
Definition: ServiceRegistry.hpp:45
Leosac::Module::WebSockAPI::BaseModuleSupportThread::mutex_
std::mutex mutex_
Definition: WSHelperThread.hpp:166
ERROR
@ ERROR
Definition: log.hpp:32
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
ServiceRegistry.hpp
Leosac::Module::WebSockAPI::BaseModuleSupportThread::start_running
void start_running()
Effectively starts an helper thread and run its io_service.
Definition: WSHelperThread.hpp:92
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Module::WebSockAPI::BaseModuleSupportThread
This class provide a base implementation -designed to be extended by subclass- so that non-boost-asio...
Definition: WSHelperThread.hpp:61
Leosac::Module::WebSockAPI::BaseModuleSupportThread::BaseModuleSupportThread
BaseModuleSupportThread(const CoreUtilsPtr &core_utils, const ParameterT &param)
Definition: WSHelperThread.hpp:64
bs2.hpp
Leosac::Module::WebSockAPI::BaseModuleSupportThread::service_ptr_
std::shared_ptr< WebSockAPI::Service > service_ptr_
Definition: WSHelperThread.hpp:172
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::Module::WebSockAPI::Service
A service object provided by the Websocket module.
Definition: Service.hpp:45
Leosac::Module::WebSockAPI::BaseModuleSupportThread::~BaseModuleSupportThread
virtual ~BaseModuleSupportThread()
Definition: WSHelperThread.hpp:71
Leosac::Module::WebSockAPI::BaseModuleSupportThread::on_service_event
void on_service_event(const service_event::Event &e)
Definition: WSHelperThread.hpp:126
Leosac::Module::WebSockAPI::BaseModuleSupportThread::register_ws_handlers
virtual void register_ws_handlers(Service &ws_service)=0
Called when websocket handler registration is possible.
Service.hpp
WebSockFwd.hpp
Leosac::Module::WebSockAPI::BaseModuleSupportThread::unregister_ws_handlers
virtual void unregister_ws_handlers(Service &ws_service)=0
Leosac::ServiceRegistry::register_event_listener
bs2::connection register_event_listener(T &&callable)
Register a service-event listener.
Definition: ServiceRegistry.hpp:349
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
CoreUtils.hpp
Leosac::service_event::Event
Definition: ServiceRegistry.hpp:37
Leosac::service_event::REGISTERED
@ REGISTERED
Definition: ServiceRegistry.hpp:34
Leosac::Module::WebSockAPI::BaseModuleSupportThread::run_io_service
void run_io_service()
Definition: WSHelperThread.hpp:106
Leosac::Module::WebSockAPI::BaseModuleSupportThread::service_event_listener_
bs2::scoped_connection service_event_listener_
Definition: WSHelperThread.hpp:161
Leosac::Module::WebSockAPI::BaseModuleSupportThread::thread_
std::unique_ptr< std::thread > thread_
Definition: WSHelperThread.hpp:162
Leosac::Module::WebSockAPI::BaseModuleSupportThread::parameters_
ParameterT parameters_
Definition: WSHelperThread.hpp:165
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
AssertCast.hpp