i2pd/libi2pd/Event.h

54 lines
1.2 KiB
C
Raw Normal View History

2016-10-20 13:12:15 +00:00
#ifndef EVENT_H__
#define EVENT_H__
#include <map>
#include <string>
#include <memory>
2016-12-07 16:52:20 +00:00
#include <mutex>
#include <tuple>
2016-10-20 13:12:15 +00:00
#include <boost/asio.hpp>
typedef std::map<std::string, std::string> EventType;
namespace i2p
{
2016-11-01 14:26:40 +00:00
namespace event
{
class EventListener {
public:
virtual ~EventListener() {};
virtual void HandleEvent(const EventType & ev) = 0;
2016-12-07 16:52:20 +00:00
/** @brief handle collected event when pumped */
virtual void HandlePumpEvent(const EventType & ev, const uint64_t & val) = 0;
2016-11-01 14:26:40 +00:00
};
2016-10-20 13:12:15 +00:00
2016-11-01 14:26:40 +00:00
class EventCore
{
public:
void QueueEvent(const EventType & ev);
2016-12-07 16:52:20 +00:00
void CollectEvent(const std::string & type, const std::string & ident, uint64_t val);
2016-11-01 14:26:40 +00:00
void SetListener(EventListener * l);
2016-12-07 16:52:20 +00:00
void PumpCollected(EventListener * l);
2016-11-01 14:26:40 +00:00
private:
2016-12-07 16:52:20 +00:00
std::mutex m_collect_mutex;
struct CollectedEvent
{
std::string Key;
std::string Ident;
uint64_t Val;
};
std::map<std::string, CollectedEvent> m_collected;
2016-11-01 14:26:40 +00:00
EventListener * m_listener = nullptr;
};
2016-11-01 17:57:25 +00:00
#ifdef WITH_EVENTS
2016-11-01 14:26:40 +00:00
extern EventCore core;
2016-11-01 17:57:25 +00:00
#endif
2016-11-01 14:26:40 +00:00
}
2016-10-20 13:12:15 +00:00
}
2016-12-07 16:52:20 +00:00
void QueueIntEvent(const std::string & type, const std::string & ident, uint64_t val);
2016-10-20 13:12:15 +00:00
void EmitEvent(const EventType & ev);
#endif