DBMirrorSet.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_DBMIRRORSET_HPP
  27. #define ZT_DBMIRRORSET_HPP
  28. #include "DB.hpp"
  29. #include <vector>
  30. #include <memory>
  31. #include <mutex>
  32. #include <set>
  33. #include <thread>
  34. namespace ZeroTier {
  35. class DBMirrorSet : public DB::ChangeListener
  36. {
  37. public:
  38. DBMirrorSet(DB::ChangeListener *listener);
  39. virtual ~DBMirrorSet();
  40. bool hasNetwork(const uint64_t networkId) const;
  41. bool get(const uint64_t networkId,nlohmann::json &network);
  42. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  43. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,DB::NetworkSummaryInfo &info);
  44. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  45. void networks(std::set<uint64_t> &networks);
  46. bool waitForReady();
  47. bool isReady();
  48. bool save(nlohmann::json &record,bool notifyListeners);
  49. void eraseNetwork(const uint64_t networkId);
  50. void eraseMember(const uint64_t networkId,const uint64_t memberId);
  51. void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  52. // These are called by various DB instances when changes occur.
  53. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network);
  54. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member);
  55. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId);
  56. inline void addDB(const std::shared_ptr<DB> &db)
  57. {
  58. db->addListener(this);
  59. std::lock_guard<std::mutex> l(_dbs_l);
  60. _dbs.push_back(db);
  61. }
  62. private:
  63. DB::ChangeListener *const _listener;
  64. std::atomic_bool _running;
  65. std::thread _syncCheckerThread;
  66. std::vector< std::shared_ptr< DB > > _dbs;
  67. mutable std::mutex _dbs_l;
  68. };
  69. } // namespace ZeroTier
  70. #endif