RedisListener.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 bool onNotification(const std::string& payload) override
  19. {
  20. return true;
  21. }
  22. void start()
  23. {
  24. _run = true;
  25. _listenThread = std::thread(&RedisListener::listen, this);
  26. }
  27. protected:
  28. std::string _controller_id;
  29. std::shared_ptr<sw::redis::Redis> _redis;
  30. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  31. bool _is_cluster = false;
  32. bool _run = false;
  33. private:
  34. std::thread _listenThread;
  35. };
  36. class RedisNetworkListener : public RedisListener {
  37. public:
  38. RedisNetworkListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis, DB* db);
  39. RedisNetworkListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster, DB* db);
  40. virtual ~RedisNetworkListener();
  41. virtual void listen() override;
  42. virtual bool onNotification(const std::string& payload) override;
  43. private:
  44. DB* _db;
  45. };
  46. class RedisMemberListener : public RedisListener {
  47. public:
  48. RedisMemberListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis, DB* db);
  49. RedisMemberListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster, DB* db);
  50. virtual ~RedisMemberListener();
  51. virtual void listen() override;
  52. virtual bool onNotification(const std::string& payload) override;
  53. private:
  54. DB* _db;
  55. };
  56. } // namespace ZeroTier
  57. #endif // ZT_CONTROLLER_REDIS_LISTENER_HPP
  58. #endif // ZT_CONTROLLER_USE_LIBPQ