PostgreSQL.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "DB.hpp"
  17. #include "ConnectionPool.hpp"
  18. #include <pqxx/pqxx>
  19. #include <memory>
  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. std::shared_ptr<pqxx::connection> c;
  29. int a;
  30. };
  31. class PostgresConnFactory : public ConnectionFactory {
  32. public:
  33. PostgresConnFactory(std::string &connString)
  34. : m_connString(connString)
  35. {
  36. }
  37. virtual std::shared_ptr<Connection> create() {
  38. Metrics::conn_counter++;
  39. auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
  40. c->c = std::make_shared<pqxx::connection>(m_connString);
  41. return std::static_pointer_cast<Connection>(c);
  42. }
  43. private:
  44. std::string m_connString;
  45. };
  46. class MemberNotificationReceiver : public pqxx::notification_receiver {
  47. public:
  48. MemberNotificationReceiver(DB *p, pqxx::connection &c, const std::string &channel);
  49. virtual ~MemberNotificationReceiver() {
  50. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  51. }
  52. virtual void operator() (const std::string &payload, int backendPid);
  53. private:
  54. DB *_psql;
  55. };
  56. class NetworkNotificationReceiver : public pqxx::notification_receiver {
  57. public:
  58. NetworkNotificationReceiver(DB *p, pqxx::connection &c, const std::string &channel);
  59. virtual ~NetworkNotificationReceiver() {
  60. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  61. };
  62. virtual void operator() (const std::string &payload, int packend_pid);
  63. private:
  64. DB *_psql;
  65. };
  66. } // namespace ZeroTier
  67. #endif // ZT_CONTROLLER_POSTGRESQL_HPP
  68. #endif // ZT_CONTROLLER_USE_LIBPQ