DBMirrorSet.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. namespace ZeroTier {
  34. class DBMirrorSet : public DB::ChangeListener
  35. {
  36. public:
  37. DBMirrorSet(DB::ChangeListener *listener);
  38. virtual ~DBMirrorSet();
  39. bool hasNetwork(const uint64_t networkId) const;
  40. bool get(const uint64_t networkId,nlohmann::json &network);
  41. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  42. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,DB::NetworkSummaryInfo &info);
  43. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  44. void networks(std::set<uint64_t> &networks);
  45. bool waitForReady();
  46. bool isReady();
  47. bool save(nlohmann::json &record,bool notifyListeners);
  48. void eraseNetwork(const uint64_t networkId);
  49. void eraseMember(const uint64_t networkId,const uint64_t memberId);
  50. void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  51. // These are called by various DB instances when changes occur.
  52. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network);
  53. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member);
  54. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId);
  55. inline void addDB(const std::shared_ptr<DB> &db)
  56. {
  57. db->addListener(this);
  58. std::lock_guard<std::mutex> l(_dbs_l);
  59. _dbs.push_back(db);
  60. }
  61. private:
  62. DB::ChangeListener *const _listener;
  63. std::vector< std::shared_ptr< DB > > _dbs;
  64. mutable std::mutex _dbs_l;
  65. };
  66. } // namespace ZeroTier
  67. #endif