PostgreSQL.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <pqxx/pqxx>
  23. extern "C" {
  24. typedef struct pg_conn PGconn;
  25. }
  26. namespace ZeroTier
  27. {
  28. class _MemberNotificationReceiver;
  29. class _NetworkNotificationReceiver;
  30. /**
  31. * A controller database driver that talks to PostgreSQL
  32. *
  33. * This is for use with ZeroTier Central. Others are free to build and use it
  34. * but be aware taht we might change it at any time.
  35. */
  36. class PostgreSQL : public DB
  37. {
  38. public:
  39. PostgreSQL(EmbeddedNetworkController *const nc, const Identity &myId, const char *path);
  40. virtual ~PostgreSQL();
  41. virtual bool waitForReady();
  42. virtual bool isReady();
  43. virtual void save(nlohmann::json *orig, nlohmann::json &record);
  44. virtual void eraseNetwork(const uint64_t networkId);
  45. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  46. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  47. protected:
  48. struct _PairHasher
  49. {
  50. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  51. };
  52. private:
  53. void initializeNetworks(pqxx::connection &conn);
  54. void initializeMembers(pqxx::connection &conn);
  55. void heartbeat();
  56. void membersDbWatcher();
  57. void networksDbWatcher();
  58. void commitThread();
  59. void onlineNotificationThread();
  60. std::string _connString;
  61. BlockingQueue<nlohmann::json *> _commitQueue;
  62. std::thread _heartbeatThread;
  63. std::thread _membersDbWatcher;
  64. std::thread _networksDbWatcher;
  65. std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];
  66. std::thread _onlineNotificationThread;
  67. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  68. mutable std::mutex _lastOnline_l;
  69. mutable std::mutex _readyLock;
  70. std::atomic<int> _ready, _connected, _run;
  71. mutable volatile bool _waitNoticePrinted;
  72. friend class _MemberNotificationReceiver;
  73. friend class _NetworkNotificationReceiver;
  74. };
  75. }
  76. #endif // ZT_CONTROLLER_LIBPQ_HPP
  77. #endif // ZT_CONTROLLER_USE_LIBPQ