RedisListener.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifdef ZT_CONTROLLER_USE_LIBPQ
  2. #ifndef ZT_CONTROLLER_REDIS_LISTENER_HPP
  3. #define ZT_CONTROLLER_REDIS_LISTENER_HPP
  4. #include "DB.hpp"
  5. #include "NotificationListener.hpp"
  6. #include "Redis.hpp"
  7. #include <memory>
  8. #include <string>
  9. #include <sw/redis++/redis++.h>
  10. #include <thread>
  11. namespace ZeroTier {
  12. class RedisListener : public NotificationListener {
  13. public:
  14. RedisListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis);
  15. RedisListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster);
  16. virtual ~RedisListener();
  17. virtual void listen() = 0;
  18. virtual void onNotification(const std::string& payload) override
  19. {
  20. }
  21. void start()
  22. {
  23. _run = true;
  24. _listenThread = std::thread(&RedisListener::listen, this);
  25. }
  26. protected:
  27. std::string _controller_id;
  28. std::shared_ptr<sw::redis::Redis> _redis;
  29. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  30. bool _is_cluster = false;
  31. bool _run = false;
  32. private:
  33. std::thread _listenThread;
  34. };
  35. class RedisNetworkListener : public RedisListener {
  36. public:
  37. RedisNetworkListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis, DB* db);
  38. RedisNetworkListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster, DB* db);
  39. virtual ~RedisNetworkListener();
  40. virtual void listen() override;
  41. virtual void onNotification(const std::string& payload) override;
  42. private:
  43. DB* _db;
  44. };
  45. class RedisMemberListener : public RedisListener {
  46. public:
  47. RedisMemberListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis, DB* db);
  48. RedisMemberListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster, DB* db);
  49. virtual ~RedisMemberListener();
  50. virtual void listen() override;
  51. virtual void onNotification(const std::string& payload) override;
  52. private:
  53. DB* _db;
  54. };
  55. } // namespace ZeroTier
  56. #endif // ZT_CONTROLLER_REDIS_LISTENER_HPP
  57. #endif // ZT_CONTROLLER_USE_LIBPQ