DBMirrorSet.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 (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 (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 (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 (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::lock_guard<std::mutex> l(_dbs_l);
  109. if (notifyListeners) {
  110. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  111. if ((*d)->save(record,notifyListeners))
  112. return true;
  113. }
  114. return false;
  115. } else {
  116. bool modified = false;
  117. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  118. modified |= (*d)->save(record,notifyListeners);
  119. }
  120. return modified;
  121. }
  122. }
  123. void DBMirrorSet::eraseNetwork(const uint64_t networkId)
  124. {
  125. std::lock_guard<std::mutex> l(_dbs_l);
  126. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  127. (*d)->eraseNetwork(networkId);
  128. }
  129. }
  130. void DBMirrorSet::eraseMember(const uint64_t networkId,const uint64_t memberId)
  131. {
  132. std::lock_guard<std::mutex> l(_dbs_l);
  133. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  134. (*d)->eraseMember(networkId,memberId);
  135. }
  136. }
  137. void DBMirrorSet::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  138. {
  139. std::lock_guard<std::mutex> l(_dbs_l);
  140. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  141. (*d)->nodeIsOnline(networkId,memberId,physicalAddress);
  142. }
  143. }
  144. void DBMirrorSet::onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network)
  145. {
  146. nlohmann::json record(network);
  147. std::lock_guard<std::mutex> l(_dbs_l);
  148. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  149. if (d->get() != db) {
  150. (*d)->save(record,false);
  151. }
  152. }
  153. _listener->onNetworkUpdate(this,networkId,network);
  154. }
  155. void DBMirrorSet::onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member)
  156. {
  157. nlohmann::json record(member);
  158. std::lock_guard<std::mutex> l(_dbs_l);
  159. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  160. if (d->get() != db) {
  161. (*d)->save(record,false);
  162. }
  163. }
  164. _listener->onNetworkMemberUpdate(this,networkId,memberId,member);
  165. }
  166. void DBMirrorSet::onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId)
  167. {
  168. _listener->onNetworkMemberDeauthorize(this,networkId,memberId);
  169. }
  170. } // namespace ZeroTier