Leosac  0.8.0
Open Source Access Control
WebSockAPI.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 "WebSockAPI.hpp"
21 #include "WSServer.hpp"
22 #include "core/CoreAPI.hpp"
23 #include "core/CoreUtils.hpp"
25 #include <boost/filesystem.hpp>
26 #include <zmqpp/proxy.hpp>
27 
28 using namespace Leosac;
29 using namespace Leosac::Module;
30 using namespace Leosac::Module::WebSockAPI;
31 
32 WebSockAPIModule::WebSockAPIModule(zmqpp::context &ctx, zmqpp::socket *pipe,
33  const boost::property_tree::ptree &cfg,
34  CoreUtilsPtr utils)
35  : BaseModule(ctx, pipe, cfg, utils)
36 {
37  port_ = cfg.get<uint16_t>("module_config.port", 8976);
38  interface_ = cfg.get<std::string>("module_config.interface", "127.0.0.1");
39 
40  auto endpoint_colorized = Colorize::green(
42  INFO(Colorize::green("WEBSOCKET_API") << " module binding to "
43  << endpoint_colorized);
44 }
45 
47 {
48  wssrv_ = std::make_unique<WSServer>(*this, core_utils()->database());
49  std::thread thread(std::bind(&WSServer::run, wssrv_.get(), interface_, port_));
50 
51  while (is_running_)
52  {
53  reactor_.poll();
54  }
55  INFO("Websocket module registered shutdown. Will now stop websocket server.");
56  wssrv_->start_shutdown();
57  thread.join();
58 }
59 
61 {
62  return utils_;
63 }
WebSockAPI.hpp
Leosac::Module::WebSockAPI::WebSockAPIModule::WebSockAPIModule
WebSockAPIModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: WebSockAPI.cpp:32
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Module::WebSockAPI::WebSockAPIModule::run
virtual void run() override
This is the main loop of the module.
Definition: WebSockAPI.cpp:46
Leosac::Module::WebSockAPI::WSServer::run
void run(const std::string &interface, uint16_t port)
Definition: WSServer.cpp:223
INFO
@ INFO
Definition: log.hpp:34
Leosac::Module
All modules that provides features to Leosac shall be in this namespace.
Leosac::Colorize::underline
std::string underline(const T &in)
Definition: Colorize.hpp:64
Leosac::Module::WebSockAPI::WebSockAPIModule::core_utils
CoreUtilsPtr core_utils()
This module explicitly expose CoreUtils to other object in the module.
Definition: WebSockAPI.cpp:60
Leosac::Module::WebSockAPI::WebSockAPIModule::port_
uint16_t port_
Port to bind the websocket endpoint.
Definition: WebSockAPI.hpp:66
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::BaseModule::utils_
CoreUtilsPtr utils_
Pointer to the core utils, which gives access to scheduler and others.
Definition: BaseModule.hpp:198
Leosac::Colorize::green
std::string green(const T &in)
Definition: Colorize.hpp:82
CoreAPI.hpp
Leosac::Module::BaseModule::is_running_
bool is_running_
Boolean indicating whether the main loop should run or not.
Definition: BaseModule.hpp:203
XmlPropertyTree.hpp
WSServer.hpp
Leosac::Colorize::detail::format
std::string format(const std::string &escape_code, const T &in)
Return a string containing the escape code, a string representation of T and the clear escape string.
Definition: Colorize.hpp:49
CoreUtils.hpp
Leosac::Module::WebSockAPI::WebSockAPIModule::interface_
std::string interface_
IP address of the interface to listen on.
Definition: WebSockAPI.hpp:71
Leosac::Module::WebSockAPI::WebSockAPIModule::wssrv_
std::unique_ptr< WSServer > wssrv_
Our websocket server object.
Definition: WebSockAPI.hpp:76
Leosac::Module::WebSockAPI
Definition: ActionActionParam.hpp:28
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35