DBMirrorSet.hpp 2.3 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 <vector>
  17. #include <memory>
  18. #include <shared_mutex>
  19. #include <set>
  20. #include <thread>
  21. namespace ZeroTier {
  22. class DBMirrorSet : public DB::ChangeListener
  23. {
  24. public:
  25. DBMirrorSet(DB::ChangeListener *listener);
  26. virtual ~DBMirrorSet();
  27. bool hasNetwork(const uint64_t networkId) const;
  28. bool get(const uint64_t networkId,nlohmann::json &network);
  29. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  30. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,DB::NetworkSummaryInfo &info);
  31. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  32. void networks(std::set<uint64_t> &networks);
  33. bool waitForReady();
  34. bool isReady();
  35. bool save(nlohmann::json &record,bool notifyListeners);
  36. void eraseNetwork(const uint64_t networkId);
  37. void eraseMember(const uint64_t networkId,const uint64_t memberId);
  38. void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  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