PubSubListener.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifdef ZT_CONTROLLER_USE_LIBPQ
  2. #ifndef ZT_CONTROLLER_PUBSUBLISTENER_HPP
  3. #define ZT_CONTROLLER_PUBSUBLISTENER_HPP
  4. #include "NotificationListener.hpp"
  5. #include "rustybits.h"
  6. #include <memory>
  7. #include <string>
  8. #include <thread>
  9. namespace ZeroTier {
  10. class DB;
  11. struct PubSubConfig {
  12. const char* controller_id;
  13. uint64_t listen_timeout;
  14. };
  15. /**
  16. * Base class for GCP PubSub listeners
  17. */
  18. class PubSubListener : public NotificationListener {
  19. public:
  20. virtual ~PubSubListener()
  21. {
  22. }
  23. virtual void onNotification(const std::string& payload) = 0;
  24. };
  25. /**
  26. * Listener for network notifications via GCP PubSub
  27. */
  28. class PubSubNetworkListener : public PubSubListener {
  29. public:
  30. PubSubNetworkListener(std::string controller_id, uint64_t listen_timeout, DB* db);
  31. virtual ~PubSubNetworkListener();
  32. virtual void onNotification(const std::string& payload) override;
  33. private:
  34. void listenThread();
  35. void changeHandlerThread();
  36. bool _run = false;
  37. std::string _controller_id;
  38. DB* _db;
  39. const rustybits::NetworkListener* _listener;
  40. std::thread _listenThread;
  41. std::thread _changeHandlerThread;
  42. };
  43. /**
  44. * Listener for member notifications via GCP PubSub
  45. */
  46. class PubSubMemberListener : public PubSubListener {
  47. public:
  48. PubSubMemberListener(std::string controller_id, uint64_t listen_timeout, DB* db);
  49. virtual ~PubSubMemberListener();
  50. virtual void onNotification(const std::string& payload) override;
  51. private:
  52. void listenThread();
  53. void changeHandlerThread();
  54. bool _run = false;
  55. std::string _controller_id;
  56. DB* _db;
  57. const rustybits::MemberListener* _listener;
  58. std::thread _listenThread;
  59. std::thread _changeHandlerThread;
  60. };
  61. } // namespace ZeroTier
  62. #endif // ZT_CONTROLLER_PUBSUBLISTENER_HPP
  63. #endif // ZT_CONTROLLER_USE_LIBPQ