mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
62 lines
1.2 KiB
8 years ago
|
#include "Event.h"
|
||
|
#include "Log.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
8 years ago
|
namespace event
|
||
|
{
|
||
8 years ago
|
#ifdef WITH_EVENTS
|
||
8 years ago
|
EventCore core;
|
||
8 years ago
|
#endif
|
||
8 years ago
|
|
||
8 years ago
|
void EventCore::SetListener(EventListener * l)
|
||
|
{
|
||
|
m_listener = l;
|
||
|
LogPrint(eLogInfo, "Event: listener set");
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
void EventCore::QueueEvent(const EventType & ev)
|
||
|
{
|
||
8 years ago
|
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();
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
void QueueIntEvent(const std::string & type, const std::string & ident, uint64_t val)
|
||
8 years ago
|
{
|
||
8 years ago
|
#ifdef WITH_EVENTS
|
||
8 years ago
|
i2p::event::core.CollectEvent(type, ident, val);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void EmitEvent(const EventType & e)
|
||
|
{
|
||
|
#if WITH_EVENTS
|
||
8 years ago
|
i2p::event::core.QueueEvent(e);
|
||
|
#endif
|
||
8 years ago
|
}
|
||
|
|