mirror of https://github.com/PurpleI2P/i2pd.git
R4SAS
5 years ago
7 changed files with 1 additions and 117 deletions
@ -1,42 +0,0 @@
@@ -1,42 +0,0 @@
|
||||
#include "Event.h" |
||||
#include "Log.h" |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace event |
||||
{ |
||||
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(); |
||||
} |
||||
} |
||||
} |
@ -1,46 +0,0 @@
@@ -1,46 +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; |
||||
}; |
||||
} |
||||
} |
||||
#endif |
Loading…
Reference in new issue