Event.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef EVENT_H__
  2. #define EVENT_H__
  3. #include <map>
  4. #include <string>
  5. #include <memory>
  6. #include <mutex>
  7. #include <tuple>
  8. #include <boost/asio.hpp>
  9. typedef std::map<std::string, std::string> EventType;
  10. namespace i2p
  11. {
  12. namespace event
  13. {
  14. class EventListener {
  15. public:
  16. virtual ~EventListener() {};
  17. virtual void HandleEvent(const EventType & ev) = 0;
  18. /** @brief handle collected event when pumped */
  19. virtual void HandlePumpEvent(const EventType & ev, const uint64_t & val) = 0;
  20. };
  21. class EventCore
  22. {
  23. public:
  24. void QueueEvent(const EventType & ev);
  25. void CollectEvent(const std::string & type, const std::string & ident, uint64_t val);
  26. void SetListener(EventListener * l);
  27. void PumpCollected(EventListener * l);
  28. private:
  29. std::mutex m_collect_mutex;
  30. struct CollectedEvent
  31. {
  32. std::string Key;
  33. std::string Ident;
  34. uint64_t Val;
  35. };
  36. std::map<std::string, CollectedEvent> m_collected;
  37. EventListener * m_listener = nullptr;
  38. };
  39. #ifdef WITH_EVENTS
  40. extern EventCore core;
  41. #endif
  42. }
  43. }
  44. void QueueIntEvent(const std::string & type, const std::string & ident, uint64_t val);
  45. void EmitEvent(const EventType & ev);
  46. #endif