mirror of https://github.com/PurpleI2P/i2pd.git
R4SAS
5 years ago
committed by
GitHub
20 changed files with 33 additions and 1133 deletions
@ -1,61 +0,0 @@
@@ -1,61 +0,0 @@
|
||||
#include "Event.h" |
||||
#include "Log.h" |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace event |
||||
{ |
||||
#ifdef WITH_EVENTS |
||||
EventCore core; |
||||
#endif |
||||
|
||||
void EventCore::SetListener(EventListener * l) |
||||
{ |
||||
m_listener = l; |
||||
LogPrint(eLogInfo, "Event: listener set"); |
||||
} |
||||
|
||||
void EventCore::QueueEvent(const EventType & ev) |
||||
{ |
||||
if(m_listener) m_listener->HandleEvent(ev); |
||||
} |
||||
|
||||
void EventCore::CollectEvent(const std::string & type, const std::string & ident, uint64_t val) |
||||
{ |
||||
std::unique_lock<std::mutex> lock(m_collect_mutex); |
||||
std::string key = type + "." + ident; |
||||
if (m_collected.find(key) == m_collected.end()) |
||||
{ |
||||
m_collected[key] = {type, key, 0}; |
||||
} |
||||
m_collected[key].Val += val; |
||||
} |
||||
|
||||
void EventCore::PumpCollected(EventListener * listener) |
||||
{ |
||||
std::unique_lock<std::mutex> lock(m_collect_mutex); |
||||
if(listener) |
||||
{ |
||||
for(const auto & ev : m_collected) { |
||||
listener->HandlePumpEvent({{"type", ev.second.Key}, {"ident", ev.second.Ident}}, ev.second.Val); |
||||
} |
||||
} |
||||
m_collected.clear(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void QueueIntEvent(const std::string & type, const std::string & ident, uint64_t val) |
||||
{ |
||||
#ifdef WITH_EVENTS |
||||
i2p::event::core.CollectEvent(type, ident, val); |
||||
#endif |
||||
} |
||||
|
||||
void EmitEvent(const EventType & e) |
||||
{ |
||||
#if WITH_EVENTS |
||||
i2p::event::core.QueueEvent(e); |
||||
#endif |
||||
} |
||||
|
@ -1,53 +0,0 @@
@@ -1,53 +0,0 @@
|
||||
#ifndef EVENT_H__ |
||||
#define EVENT_H__ |
||||
#include <map> |
||||
#include <string> |
||||
#include <memory> |
||||
#include <mutex> |
||||
#include <tuple> |
||||
|
||||
#include <boost/asio.hpp> |
||||
|
||||
typedef std::map<std::string, std::string> EventType; |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace event |
||||
{ |
||||
class EventListener { |
||||
public: |
||||
virtual ~EventListener() {}; |
||||
virtual void HandleEvent(const EventType & ev) = 0; |
||||
/** @brief handle collected event when pumped */ |
||||
virtual void HandlePumpEvent(const EventType & ev, const uint64_t & val) = 0; |
||||
}; |
||||
|
||||
class EventCore |
||||
{ |
||||
public: |
||||
void QueueEvent(const EventType & ev); |
||||
void CollectEvent(const std::string & type, const std::string & ident, uint64_t val); |
||||
void SetListener(EventListener * l); |
||||
void PumpCollected(EventListener * l); |
||||
|
||||
private: |
||||
std::mutex m_collect_mutex; |
||||
struct CollectedEvent |
||||
{ |
||||
std::string Key; |
||||
std::string Ident; |
||||
uint64_t Val; |
||||
}; |
||||
std::map<std::string, CollectedEvent> m_collected; |
||||
EventListener * m_listener = nullptr; |
||||
}; |
||||
#ifdef WITH_EVENTS |
||||
extern EventCore core; |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
void QueueIntEvent(const std::string & type, const std::string & ident, uint64_t val); |
||||
void EmitEvent(const EventType & ev); |
||||
|
||||
#endif |
@ -1,195 +0,0 @@
@@ -1,195 +0,0 @@
|
||||
#ifdef WITH_EVENTS |
||||
#include "Websocket.h" |
||||
#include "Log.h" |
||||
|
||||
#include <set> |
||||
#include <functional> |
||||
|
||||
#include <websocketpp/config/asio_no_tls.hpp> |
||||
#include <websocketpp/server.hpp> |
||||
#include <boost/property_tree/ini_parser.hpp> |
||||
#define GCC47_BOOST149 ((BOOST_VERSION == 104900) && (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) |
||||
#if !GCC47_BOOST149 |
||||
#include <boost/property_tree/json_parser.hpp> |
||||
#endif |
||||
|
||||
#include <stdexcept> |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace event |
||||
{ |
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> ServerImpl; |
||||
typedef websocketpp::connection_hdl ServerConn; |
||||
|
||||
class WebsocketServerImpl : public EventListener |
||||
{ |
||||
private: |
||||
typedef ServerImpl::message_ptr MessagePtr; |
||||
public: |
||||
|
||||
WebsocketServerImpl(const std::string & addr, int port) : |
||||
m_run(false), |
||||
m_ws_thread(nullptr), |
||||
m_ev_thread(nullptr), |
||||
m_WebsocketTicker(m_Service) |
||||
{ |
||||
m_server.init_asio(); |
||||
m_server.set_open_handler(std::bind(&WebsocketServerImpl::ConnOpened, this, std::placeholders::_1)); |
||||
m_server.set_close_handler(std::bind(&WebsocketServerImpl::ConnClosed, this, std::placeholders::_1)); |
||||
m_server.set_message_handler(std::bind(&WebsocketServerImpl::OnConnMessage, this, std::placeholders::_1, std::placeholders::_2)); |
||||
|
||||
m_server.listen(boost::asio::ip::address::from_string(addr), port); |
||||
} |
||||
|
||||
~WebsocketServerImpl() |
||||
{ |
||||
} |
||||
|
||||
void Start() { |
||||
m_run = true; |
||||
m_server.start_accept(); |
||||
m_ws_thread = new std::thread([&] () { |
||||
while(m_run) { |
||||
try { |
||||
m_server.run(); |
||||
} catch (std::exception & e ) { |
||||
LogPrint(eLogError, "Websocket server: ", e.what()); |
||||
} |
||||
} |
||||
}); |
||||
m_ev_thread = new std::thread([&] () { |
||||
while(m_run) { |
||||
try { |
||||
m_Service.run(); |
||||
break; |
||||
} catch (std::exception & e ) { |
||||
LogPrint(eLogError, "Websocket service: ", e.what()); |
||||
} |
||||
} |
||||
}); |
||||
ScheduleTick(); |
||||
} |
||||
|
||||
void Stop() { |
||||
m_run = false; |
||||
m_Service.stop(); |
||||
m_server.stop(); |
||||
|
||||
if(m_ev_thread) { |
||||
m_ev_thread->join(); |
||||
delete m_ev_thread; |
||||
} |
||||
m_ev_thread = nullptr; |
||||
|
||||
if(m_ws_thread) { |
||||
m_ws_thread->join(); |
||||
delete m_ws_thread; |
||||
} |
||||
m_ws_thread = nullptr; |
||||
} |
||||
|
||||
void ConnOpened(ServerConn c) |
||||
{ |
||||
std::lock_guard<std::mutex> lock(m_connsMutex); |
||||
m_conns.insert(c); |
||||
} |
||||
|
||||
void ConnClosed(ServerConn c) |
||||
{ |
||||
std::lock_guard<std::mutex> lock(m_connsMutex); |
||||
m_conns.erase(c); |
||||
} |
||||
|
||||
void OnConnMessage(ServerConn conn, ServerImpl::message_ptr msg) |
||||
{ |
||||
(void) conn; |
||||
(void) msg; |
||||
} |
||||
|
||||
void HandleTick(const boost::system::error_code & ec) |
||||
{ |
||||
|
||||
if(ec != boost::asio::error::operation_aborted) |
||||
LogPrint(eLogError, "Websocket ticker: ", ec.message()); |
||||
// pump collected events to us
|
||||
i2p::event::core.PumpCollected(this); |
||||
ScheduleTick(); |
||||
} |
||||
|
||||
void ScheduleTick() |
||||
{ |
||||
LogPrint(eLogDebug, "Websocket schedule tick"); |
||||
boost::posix_time::seconds dlt(1); |
||||
m_WebsocketTicker.expires_from_now(dlt); |
||||
m_WebsocketTicker.async_wait(std::bind(&WebsocketServerImpl::HandleTick, this, std::placeholders::_1)); |
||||
} |
||||
|
||||
/** @brief called from m_ev_thread */ |
||||
void HandlePumpEvent(const EventType & ev, const uint64_t & val) |
||||
{ |
||||
EventType e; |
||||
for (const auto & i : ev) |
||||
e[i.first] = i.second; |
||||
|
||||
e["number"] = std::to_string(val); |
||||
HandleEvent(e); |
||||
} |
||||
|
||||
/** @brief called from m_ws_thread */ |
||||
void HandleEvent(const EventType & ev) |
||||
{ |
||||
std::lock_guard<std::mutex> lock(m_connsMutex); |
||||
boost::property_tree::ptree event; |
||||
for (const auto & item : ev) { |
||||
event.put(item.first, item.second); |
||||
} |
||||
std::ostringstream ss; |
||||
write_json(ss, event); |
||||
std::string s = ss.str(); |
||||
|
||||
ConnList::iterator it; |
||||
for (it = m_conns.begin(); it != m_conns.end(); ++it) { |
||||
ServerImpl::connection_ptr con = m_server.get_con_from_hdl(*it); |
||||
con->send(s); |
||||
} |
||||
} |
||||
|
||||
private: |
||||
typedef std::set<ServerConn, std::owner_less<ServerConn> > ConnList; |
||||
bool m_run; |
||||
std::thread * m_ws_thread; |
||||
std::thread * m_ev_thread; |
||||
std::mutex m_connsMutex; |
||||
ConnList m_conns; |
||||
ServerImpl m_server; |
||||
boost::asio::io_service m_Service; |
||||
boost::asio::deadline_timer m_WebsocketTicker; |
||||
}; |
||||
|
||||
|
||||
WebsocketServer::WebsocketServer(const std::string & addr, int port) : m_impl(new WebsocketServerImpl(addr, port)) {} |
||||
WebsocketServer::~WebsocketServer() |
||||
{ |
||||
delete m_impl; |
||||
} |
||||
|
||||
|
||||
void WebsocketServer::Start() |
||||
{ |
||||
m_impl->Start(); |
||||
} |
||||
|
||||
void WebsocketServer::Stop() |
||||
{ |
||||
m_impl->Stop(); |
||||
} |
||||
|
||||
EventListener * WebsocketServer::ToListener() |
||||
{ |
||||
return m_impl; |
||||
} |
||||
} |
||||
} |
||||
#endif |
@ -1,28 +0,0 @@
@@ -1,28 +0,0 @@
|
||||
#ifndef WEBSOCKET_H__ |
||||
#define WEBSOCKET_H__ |
||||
#include "Event.h" |
||||
namespace i2p |
||||
{ |
||||
namespace event |
||||
{ |
||||
|
||||
class WebsocketServerImpl; |
||||
|
||||
class WebsocketServer |
||||
{ |
||||
public: |
||||
WebsocketServer(const std::string & addr, int port); |
||||
~WebsocketServer(); |
||||
|
||||
void Start(); |
||||
void Stop(); |
||||
|
||||
EventListener * ToListener(); |
||||
|
||||
private: |
||||
WebsocketServerImpl * m_impl; |
||||
}; |
||||
|
||||
} |
||||
} |
||||
#endif |
Loading…
Reference in new issue