DBMirrorSet.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  38. // These are called by various DB instances when changes occur.
  39. virtual void onNetworkUpdate(const void* db, uint64_t networkId, const nlohmann::json& network);
  40. virtual void onNetworkMemberUpdate(const void* db, uint64_t networkId, uint64_t memberId, const nlohmann::json& member);
  41. virtual void onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId);
  42. AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
  43. inline void addDB(const std::shared_ptr<DB>& db)
  44. {
  45. db->addListener(this);
  46. std::unique_lock<std::shared_mutex> l(_dbs_l);
  47. _dbs.push_back(db);
  48. }
  49. private:
  50. DB::ChangeListener* const _listener;
  51. std::atomic_bool _running;
  52. std::thread _syncCheckerThread;
  53. std::vector<std::shared_ptr<DB> > _dbs;
  54. mutable std::shared_mutex _dbs_l;
  55. };
  56. } // namespace ZeroTier
  57. #endif