DBMirrorSet.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c)2013-2020 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: 2025-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 <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. inline void addDB(const std::shared_ptr<DB> &db)
  44. {
  45. db->addListener(this);
  46. std::lock_guard<std::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::mutex _dbs_l;
  55. };
  56. } // namespace ZeroTier
  57. #endif