DBMirrorSet.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c)2019 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. #ifndef ZT_DBMIRRORSET_HPP
  14. #define ZT_DBMIRRORSET_HPP
  15. #include "DB.hpp"
  16. #include <memory>
  17. #include <set>
  18. #include <shared_mutex>
  19. #include <thread>
  20. #include <vector>
  21. namespace ZeroTier {
  22. class DBMirrorSet : public DB::ChangeListener {
  23. public:
  24. DBMirrorSet(DB::ChangeListener* listener);
  25. virtual ~DBMirrorSet();
  26. bool hasNetwork(const uint64_t networkId) const;
  27. bool get(const uint64_t networkId, nlohmann::json& network);
  28. bool get(const uint64_t networkId, nlohmann::json& network, const uint64_t memberId, nlohmann::json& member);
  29. bool get(const uint64_t networkId, nlohmann::json& network, const uint64_t memberId, nlohmann::json& member, DB::NetworkSummaryInfo& info);
  30. bool get(const uint64_t networkId, nlohmann::json& network, std::vector<nlohmann::json>& members);
  31. void networks(std::set<uint64_t>& networks);
  32. bool waitForReady();
  33. bool isReady();
  34. bool save(nlohmann::json& record, bool notifyListeners);
  35. void eraseNetwork(const uint64_t networkId);
  36. void eraseMember(const uint64_t networkId, const uint64_t memberId);
  37. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  38. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch);
  39. // These are called by various DB instances when changes occur.
  40. virtual void onNetworkUpdate(const void* db, uint64_t networkId, const nlohmann::json& network);
  41. virtual void onNetworkMemberUpdate(const void* db, uint64_t networkId, uint64_t memberId, const nlohmann::json& member);
  42. virtual void onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId);
  43. AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
  44. inline void addDB(const std::shared_ptr<DB>& db)
  45. {
  46. db->addListener(this);
  47. std::unique_lock<std::shared_mutex> l(_dbs_l);
  48. _dbs.push_back(db);
  49. }
  50. private:
  51. DB::ChangeListener* const _listener;
  52. std::atomic_bool _running;
  53. std::thread _syncCheckerThread;
  54. std::vector<std::shared_ptr<DB> > _dbs;
  55. mutable std::shared_mutex _dbs_l;
  56. };
  57. } // namespace ZeroTier
  58. #endif