BNode.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "BNode.h"
  36. #include <functional>
  37. #include <algorithm>
  38. #include "DHTBucket.h"
  39. #include "DHTNode.h"
  40. namespace aria2 {
  41. BNode::BNode(const SharedHandle<DHTBucket>& bucket):
  42. bucket_(bucket),
  43. up_(0),
  44. left_(0),
  45. right_(0) {}
  46. BNode::~BNode()
  47. {
  48. delete left_;
  49. delete right_;
  50. }
  51. void BNode::setLeft(BNode* left)
  52. {
  53. left_ = left;
  54. left_->up_ = this;
  55. }
  56. void BNode::setRight(BNode* right)
  57. {
  58. right_ = right;
  59. right_->up_ = this;
  60. }
  61. void BNode::setUp(BNode* up)
  62. {
  63. up_ = up;
  64. }
  65. void BNode::setBucket(const SharedHandle<DHTBucket>& bucket)
  66. {
  67. bucket_ = bucket;
  68. }
  69. bool BNode::isInRange(const unsigned char* key) const
  70. {
  71. if(bucket_.isNull()) {
  72. return left_->isInRange(key) || right_->isInRange(key);
  73. } else {
  74. return bucket_->isInRange(key);
  75. }
  76. }
  77. BNode* BNode::findBNodeFor(BNode* b, const unsigned char* key)
  78. {
  79. if(!b->isInRange(key)) {
  80. return 0;
  81. }
  82. while(1) {
  83. if(!b->getBucket().isNull()) {
  84. return b;
  85. }
  86. // we assume key fits in either left or right bucket range.
  87. if(b->getLeft()->isInRange(key)) {
  88. b = b->getLeft();
  89. } else {
  90. b = b->getRight();
  91. }
  92. }
  93. // for properly configured BNode tree, here is unreachable.
  94. return 0;
  95. }
  96. SharedHandle<DHTBucket> BNode::findBucketFor(BNode* b, const unsigned char* key)
  97. {
  98. BNode* bnode = findBNodeFor(b, key);
  99. if(bnode) {
  100. return bnode->getBucket();
  101. } else {
  102. return SharedHandle<DHTBucket>();
  103. }
  104. }
  105. void BNode::findClosestKNodes(std::vector<SharedHandle<DHTNode> >& nodes,
  106. BNode* b, const unsigned char* key)
  107. {
  108. BNode* bnode = findBNodeFor(b, key);
  109. if(!bnode) {
  110. return;
  111. }
  112. {
  113. SharedHandle<DHTBucket> bucket = bnode->getBucket();
  114. bucket->getGoodNodes(nodes);
  115. }
  116. if(nodes.size() >= DHTBucket::K) {
  117. return;
  118. }
  119. std::vector<const BNode*> visited;
  120. visited.push_back(bnode);
  121. BNode* up = bnode->getUp();
  122. if(!up) {
  123. return;
  124. }
  125. bool leftFirst = false;
  126. if(up->getLeft() == bnode) {
  127. leftFirst = true;
  128. }
  129. bnode = up;
  130. std::const_mem_fun_t<BNode*, BNode> firstfunc = leftFirst?std::mem_fun(&BNode::getLeft):std::mem_fun(&BNode::getRight);
  131. std::const_mem_fun_t<BNode*, BNode> secondfunc = leftFirst?std::mem_fun(&BNode::getRight):std::mem_fun(&BNode::getLeft);
  132. while(nodes.size() < DHTBucket::K) {
  133. if(!bnode->getLeft() && !bnode->getRight()) {
  134. bnode = bnode->getUp();
  135. } else {
  136. if(std::find(visited.begin(), visited.end(), firstfunc(bnode)) == visited.end()) {
  137. bnode = firstfunc(bnode);
  138. } else if(std::find(visited.begin(), visited.end(), secondfunc(bnode)) == visited.end()) {
  139. bnode = secondfunc(bnode);
  140. } else {
  141. bnode = bnode->getUp();
  142. }
  143. }
  144. if(!bnode) {
  145. break;
  146. }
  147. visited.push_back(bnode);
  148. {
  149. SharedHandle<DHTBucket> bucket = bnode->getBucket();
  150. if(!bucket.isNull()) {
  151. std::vector<SharedHandle<DHTNode> > goodNodes;
  152. bucket->getGoodNodes(goodNodes);
  153. size_t r = DHTBucket::K-nodes.size();
  154. if(goodNodes.size() <= r) {
  155. nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
  156. } else {
  157. nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.begin()+r);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. void BNode::enumerateBucket(std::vector<SharedHandle<DHTBucket> >& buckets,
  164. const BNode* b)
  165. {
  166. std::vector<const BNode*> visited;
  167. visited.push_back(b);
  168. while(1) {
  169. if(!b) {
  170. break;
  171. }
  172. if(!b->getBucket().isNull()) {
  173. buckets.push_back(b->getBucket());
  174. b = b->getUp();
  175. } else if(std::find(visited.begin(), visited.end(), b->getLeft()) == visited.end()) {
  176. b = b->getLeft();
  177. visited.push_back(b);
  178. } else if(std::find(visited.begin(), visited.end(), b->getRight()) == visited.end()) {
  179. b = b->getRight();
  180. visited.push_back(b);
  181. } else {
  182. b = b->getUp();
  183. }
  184. }
  185. return;
  186. }
  187. } // namespace aria2