PostgreSQL.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef ZT_CONTROLLER_USE_LIBPQ
  19. #ifndef ZT_CONTROLLER_LIBPQ_HPP
  20. #define ZT_CONTROLLER_LIBPQ_HPP
  21. #include "DB.hpp"
  22. extern "C" {
  23. typedef struct pg_conn PGconn;
  24. }
  25. namespace ZeroTier
  26. {
  27. /**
  28. * A controller database driver that talks to PostgreSQL
  29. *
  30. * This is for use with ZeroTier Central. Others are free to build and use it
  31. * but be aware taht we might change it at any time.
  32. */
  33. class PostgreSQL : public DB
  34. {
  35. public:
  36. PostgreSQL(EmbeddedNetworkController *const nc, const Identity &myId, const char *path);
  37. virtual ~PostgreSQL();
  38. virtual bool waitForReady();
  39. virtual bool isReady();
  40. virtual void save(nlohmann::json *orig, nlohmann::json &record);
  41. virtual void eraseNetwork(const uint64_t networkId);
  42. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  43. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  44. protected:
  45. struct _PairHasher
  46. {
  47. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  48. };
  49. private:
  50. void initializeNetworks(PGconn *conn);
  51. void initializeMembers(PGconn *conn);
  52. void heartbeat();
  53. void membersDbWatcher();
  54. void networksDbWatcher();
  55. void commitThread();
  56. void onlineNotificationThread();
  57. std::string _connString;
  58. BlockingQueue<nlohmann::json *> _commitQueue;
  59. std::thread _heartbeatThread;
  60. std::thread _membersDbWatcher;
  61. std::thread _networksDbWatcher;
  62. std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];
  63. std::thread _onlineNotificationThread;
  64. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  65. mutable std::mutex _lastOnline_l;
  66. mutable std::mutex _readyLock;
  67. std::atomic<int> _ready, _connected, _run;
  68. mutable volatile bool _waitNoticePrinted;
  69. };
  70. }
  71. #endif // ZT_CONTROLLER_LIBPQ_HPP
  72. #endif // ZT_CONTROLLER_USE_LIBPQ