i2pd/libi2pd/Event.cpp

62 lines
1.2 KiB
C++
Raw Normal View History

2016-10-20 16:12:15 +03:00
#include "Event.h"
#include "Log.h"
namespace i2p
{
2016-11-01 17:26:40 +03:00
namespace event
{
2016-11-01 20:57:25 +03:00
#ifdef WITH_EVENTS
2016-11-01 17:26:40 +03:00
EventCore core;
2016-11-01 20:57:25 +03:00
#endif
2016-10-20 16:12:15 +03:00
2016-11-01 17:26:40 +03:00
void EventCore::SetListener(EventListener * l)
{
m_listener = l;
LogPrint(eLogInfo, "Event: listener set");
}
2016-10-20 16:12:15 +03:00
2016-11-01 17:26:40 +03:00
void EventCore::QueueEvent(const EventType & ev)
{
2016-12-07 19:52:20 +03:00
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;
}
2018-01-06 06:48:51 +03:00
2016-12-07 19:52:20 +03:00
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();
2016-11-01 17:26:40 +03:00
}
}
2016-10-20 16:12:15 +03:00
}
2016-12-07 19:52:20 +03:00
void QueueIntEvent(const std::string & type, const std::string & ident, uint64_t val)
2016-10-20 16:12:15 +03:00
{
2016-11-01 17:26:40 +03:00
#ifdef WITH_EVENTS
2016-12-07 19:52:20 +03:00
i2p::event::core.CollectEvent(type, ident, val);
#endif
}
void EmitEvent(const EventType & e)
{
#if WITH_EVENTS
2016-11-01 17:26:40 +03:00
i2p::event::core.QueueEvent(e);
#endif
2016-10-20 16:12:15 +03:00
}