Leosac  0.7.0
OpenSourceAccessControl
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 =
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 }
A service object provided by the Websocket module.
Definition: Service.hpp:45
This is the header file for a generated source file, GitSHA1.cpp.
virtual void register_ws_handlers(Service &ws_service)=0
Called when websocket handler registration is possible.
virtual void unregister_ws_handlers(Service &ws_service)=0
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
std::unique_ptr< boost::asio::io_service::work > work_
void start_running()
Effectively starts an helper thread and run its io_service.
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
std::shared_ptr< WebSockAPI::Service > service_ptr_
bs2::connection register_event_listener(T &&callable)
Register a service-event listener.
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: log.hpp:34
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
void on_service_event(const service_event::Event &e)
This class provide a base implementation -designed to be extended by subclass- so that non-boost-asio...
BaseModuleSupportThread(const CoreUtilsPtr &core_utils, const ParameterT &param)
std::enable_if_t< is_shared_ptr_v< Out >, Out > assert_cast(const std::shared_ptr< In > &in)
Definition: AssertCast.hpp:58