DBMirrorSet.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "DBMirrorSet.hpp"
  27. namespace ZeroTier {
  28. DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener) :
  29. _listener(listener)
  30. {
  31. }
  32. DBMirrorSet::~DBMirrorSet()
  33. {
  34. }
  35. bool DBMirrorSet::hasNetwork(const uint64_t networkId) const
  36. {
  37. std::lock_guard<std::mutex> l(_dbs_l);
  38. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  39. if ((*d)->hasNetwork(networkId))
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network)
  45. {
  46. std::lock_guard<std::mutex> l(_dbs_l);
  47. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  48. if ((*d)->get(networkId,network)) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member)
  55. {
  56. std::lock_guard<std::mutex> l(_dbs_l);
  57. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  58. if ((*d)->get(networkId,network,memberId,member))
  59. return true;
  60. }
  61. return false;
  62. }
  63. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,DB::NetworkSummaryInfo &info)
  64. {
  65. std::lock_guard<std::mutex> l(_dbs_l);
  66. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  67. if ((*d)->get(networkId,network,memberId,member,info))
  68. return true;
  69. }
  70. return false;
  71. }
  72. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members)
  73. {
  74. std::lock_guard<std::mutex> l(_dbs_l);
  75. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  76. if ((*d)->get(networkId,network,members))
  77. return true;
  78. }
  79. return false;
  80. }
  81. void DBMirrorSet::networks(std::set<uint64_t> &networks)
  82. {
  83. std::lock_guard<std::mutex> l(_dbs_l);
  84. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  85. (*d)->networks(networks);
  86. }
  87. }
  88. bool DBMirrorSet::waitForReady()
  89. {
  90. bool r = false;
  91. std::lock_guard<std::mutex> l(_dbs_l);
  92. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  93. r |= (*d)->waitForReady();
  94. }
  95. return r;
  96. }
  97. bool DBMirrorSet::isReady()
  98. {
  99. std::lock_guard<std::mutex> l(_dbs_l);
  100. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  101. if (!(*d)->isReady())
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool DBMirrorSet::save(nlohmann::json &record,bool notifyListeners)
  107. {
  108. std::vector< std::shared_ptr<DB> > dbs;
  109. {
  110. std::lock_guard<std::mutex> l(_dbs_l);
  111. dbs = _dbs;
  112. }
  113. if (notifyListeners) {
  114. for(auto d=dbs.begin();d!=dbs.end();++d) {
  115. if ((*d)->save(record,notifyListeners))
  116. return true;
  117. }
  118. return false;
  119. } else {
  120. bool modified = false;
  121. for(auto d=dbs.begin();d!=dbs.end();++d) {
  122. modified |= (*d)->save(record,notifyListeners);
  123. }
  124. return modified;
  125. }
  126. }
  127. void DBMirrorSet::eraseNetwork(const uint64_t networkId)
  128. {
  129. std::lock_guard<std::mutex> l(_dbs_l);
  130. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  131. (*d)->eraseNetwork(networkId);
  132. }
  133. }
  134. void DBMirrorSet::eraseMember(const uint64_t networkId,const uint64_t memberId)
  135. {
  136. std::lock_guard<std::mutex> l(_dbs_l);
  137. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  138. (*d)->eraseMember(networkId,memberId);
  139. }
  140. }
  141. void DBMirrorSet::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  142. {
  143. std::lock_guard<std::mutex> l(_dbs_l);
  144. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  145. (*d)->nodeIsOnline(networkId,memberId,physicalAddress);
  146. }
  147. }
  148. void DBMirrorSet::onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network)
  149. {
  150. nlohmann::json record(network);
  151. std::lock_guard<std::mutex> l(_dbs_l);
  152. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  153. if (d->get() != db) {
  154. (*d)->save(record,false);
  155. }
  156. }
  157. _listener->onNetworkUpdate(this,networkId,network);
  158. }
  159. void DBMirrorSet::onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member)
  160. {
  161. nlohmann::json record(member);
  162. std::lock_guard<std::mutex> l(_dbs_l);
  163. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  164. if (d->get() != db) {
  165. (*d)->save(record,false);
  166. }
  167. }
  168. _listener->onNetworkMemberUpdate(this,networkId,memberId,member);
  169. }
  170. void DBMirrorSet::onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId)
  171. {
  172. _listener->onNetworkMemberDeauthorize(this,networkId,memberId);
  173. }
  174. } // namespace ZeroTier