PostgreSQL.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifdef ZT_CONTROLLER_USE_LIBPQ
  2. #include "PostgreSQL.hpp"
  3. #include <nlohmann/json.hpp>
  4. using namespace nlohmann;
  5. using namespace ZeroTier;
  6. MemberNotificationReceiver::MemberNotificationReceiver(DB* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  7. {
  8. fprintf(stderr, "initialize MemberNotificationReceiver\n");
  9. }
  10. void MemberNotificationReceiver::operator()(const std::string& payload, int packend_pid)
  11. {
  12. fprintf(stderr, "Member Notification received: %s\n", payload.c_str());
  13. Metrics::pgsql_mem_notification++;
  14. json tmp(json::parse(payload));
  15. json& ov = tmp["old_val"];
  16. json& nv = tmp["new_val"];
  17. json oldConfig, newConfig;
  18. if (ov.is_object())
  19. oldConfig = ov;
  20. if (nv.is_object())
  21. newConfig = nv;
  22. if (oldConfig.is_object() || newConfig.is_object()) {
  23. _psql->_memberChanged(oldConfig, newConfig, _psql->isReady());
  24. fprintf(stderr, "payload sent\n");
  25. }
  26. }
  27. NetworkNotificationReceiver::NetworkNotificationReceiver(DB* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  28. {
  29. fprintf(stderr, "initialize NetworkNotificationReceiver\n");
  30. }
  31. void NetworkNotificationReceiver::operator()(const std::string& payload, int packend_pid)
  32. {
  33. fprintf(stderr, "Network Notification received: %s\n", payload.c_str());
  34. Metrics::pgsql_net_notification++;
  35. json tmp(json::parse(payload));
  36. json& ov = tmp["old_val"];
  37. json& nv = tmp["new_val"];
  38. json oldConfig, newConfig;
  39. if (ov.is_object())
  40. oldConfig = ov;
  41. if (nv.is_object())
  42. newConfig = nv;
  43. if (oldConfig.is_object() || newConfig.is_object()) {
  44. _psql->_networkChanged(oldConfig, newConfig, _psql->isReady());
  45. fprintf(stderr, "payload sent\n");
  46. }
  47. }
  48. #endif