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)
  7. : pqxx::notification_receiver(c, channel)
  8. , _psql(p)
  9. {
  10. fprintf(stderr, "initialize MemberNotificationReceiver\n");
  11. }
  12. void MemberNotificationReceiver::operator() (const std::string &payload, int packend_pid) {
  13. fprintf(stderr, "Member Notification received: %s\n", payload.c_str());
  14. Metrics::pgsql_mem_notification++;
  15. json tmp(json::parse(payload));
  16. json &ov = tmp["old_val"];
  17. json &nv = tmp["new_val"];
  18. json oldConfig, newConfig;
  19. if (ov.is_object()) oldConfig = ov;
  20. if (nv.is_object()) newConfig = nv;
  21. if (oldConfig.is_object() || newConfig.is_object()) {
  22. _psql->_memberChanged(oldConfig,newConfig,_psql->ready());
  23. fprintf(stderr, "payload sent\n");
  24. }
  25. }
  26. NetworkNotificationReceiver::NetworkNotificationReceiver(DB *p, pqxx::connection &c, const std::string &channel)
  27. : pqxx::notification_receiver(c, channel)
  28. , _psql(p)
  29. {
  30. fprintf(stderr, "initialize NetworkNotificationReceiver\n");
  31. }
  32. void NetworkNotificationReceiver::operator() (const std::string &payload, int packend_pid) {
  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()) oldConfig = ov;
  40. if (nv.is_object()) newConfig = nv;
  41. if (oldConfig.is_object() || newConfig.is_object()) {
  42. _psql->_networkChanged(oldConfig,newConfig,_psql->ready());
  43. fprintf(stderr, "payload sent\n");
  44. }
  45. }
  46. #endif