DBMirrorSet.hpp 2.1 KB

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