PostgreSQL.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c)2025 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifdef ZT_CONTROLLER_USE_LIBPQ
  14. #ifndef ZT_CONTROLLER_POSTGRESQL_HPP
  15. #define ZT_CONTROLLER_POSTGRESQL_HPP
  16. #include "ConnectionPool.hpp"
  17. #include "DB.hpp"
  18. #include <memory>
  19. #include <pqxx/pqxx>
  20. namespace ZeroTier {
  21. extern "C" {
  22. typedef struct pg_conn PGconn;
  23. }
  24. class PostgresConnection : public Connection {
  25. public:
  26. virtual ~PostgresConnection()
  27. {
  28. }
  29. std::shared_ptr<pqxx::connection> c;
  30. int a;
  31. };
  32. class PostgresConnFactory : public ConnectionFactory {
  33. public:
  34. PostgresConnFactory(std::string& connString) : m_connString(connString)
  35. {
  36. }
  37. virtual std::shared_ptr<Connection> create()
  38. {
  39. Metrics::conn_counter++;
  40. auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
  41. c->c = std::make_shared<pqxx::connection>(m_connString);
  42. return std::static_pointer_cast<Connection>(c);
  43. }
  44. private:
  45. std::string m_connString;
  46. };
  47. class MemberNotificationReceiver : public pqxx::notification_receiver {
  48. public:
  49. MemberNotificationReceiver(DB* p, pqxx::connection& c, const std::string& channel);
  50. virtual ~MemberNotificationReceiver()
  51. {
  52. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  53. }
  54. virtual void operator()(const std::string& payload, int backendPid);
  55. private:
  56. DB* _psql;
  57. };
  58. class NetworkNotificationReceiver : public pqxx::notification_receiver {
  59. public:
  60. NetworkNotificationReceiver(DB* p, pqxx::connection& c, const std::string& channel);
  61. virtual ~NetworkNotificationReceiver()
  62. {
  63. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  64. };
  65. virtual void operator()(const std::string& payload, int packend_pid);
  66. private:
  67. DB* _psql;
  68. };
  69. struct NodeOnlineRecord {
  70. uint64_t lastSeen;
  71. InetAddress physicalAddress;
  72. std::string osArch;
  73. };
  74. } // namespace ZeroTier
  75. #endif // ZT_CONTROLLER_POSTGRESQL_HPP
  76. #endif // ZT_CONTROLLER_USE_LIBPQ