BNode.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "DHTBucket.h"
  37. #include "DHTNode.h"
  38. #include <functional>
  39. #include <algorithm>
  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. SharedHandle<DHTBucket> BNode::getBucket() const
  52. {
  53. return _bucket;
  54. }
  55. BNode* BNode::getLeft() const
  56. {
  57. return _left;
  58. }
  59. BNode* BNode::getRight() const
  60. {
  61. return _right;
  62. }
  63. BNode* BNode::getUp() const
  64. {
  65. return _up;
  66. }
  67. void BNode::setLeft(BNode* left)
  68. {
  69. _left = left;
  70. _left->_up = this;
  71. }
  72. void BNode::setRight(BNode* right)
  73. {
  74. _right = right;
  75. _right->_up = this;
  76. }
  77. void BNode::setUp(BNode* up)
  78. {
  79. _up = up;
  80. }
  81. void BNode::setBucket(const SharedHandle<DHTBucket>& bucket)
  82. {
  83. _bucket = bucket;
  84. }
  85. bool BNode::isInRange(const unsigned char* key) const
  86. {
  87. if(_bucket.isNull()) {
  88. return _left->isInRange(key) || _right->isInRange(key);
  89. } else {
  90. return _bucket->isInRange(key);
  91. }
  92. }
  93. BNode* BNode::findBNodeFor(BNode* b, const unsigned char* key)
  94. {
  95. if(!b->isInRange(key)) {
  96. return 0;
  97. }
  98. while(1) {
  99. if(!b->getBucket().isNull()) {
  100. return b;
  101. }
  102. // we assume key fits in either left or right bucket range.
  103. if(b->getLeft()->isInRange(key)) {
  104. b = b->getLeft();
  105. } else {
  106. b = b->getRight();
  107. }
  108. }
  109. // for properly configured BNode tree, here is unreachable.
  110. return 0;
  111. }
  112. SharedHandle<DHTBucket> BNode::findBucketFor(BNode* b, const unsigned char* key)
  113. {
  114. BNode* bnode = findBNodeFor(b, key);
  115. if(bnode) {
  116. return bnode->getBucket();
  117. } else {
  118. return SharedHandle<DHTBucket>();
  119. }
  120. }
  121. void BNode::findClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
  122. BNode* b, const unsigned char* key)
  123. {
  124. BNode* bnode = findBNodeFor(b, key);
  125. if(!bnode) {
  126. return;
  127. }
  128. {
  129. SharedHandle<DHTBucket> bucket = bnode->getBucket();
  130. bucket->getGoodNodes(nodes);
  131. }
  132. if(nodes.size() >= DHTBucket::K) {
  133. return;
  134. }
  135. std::deque<const BNode*> visited;
  136. visited.push_back(bnode);
  137. BNode* up = bnode->getUp();
  138. if(!up) {
  139. return;
  140. }
  141. bool leftFirst = false;
  142. if(up->getLeft() == bnode) {
  143. leftFirst = true;
  144. }
  145. bnode = up;
  146. std::const_mem_fun_t<BNode*, BNode> firstfunc = leftFirst?std::mem_fun(&BNode::getLeft):std::mem_fun(&BNode::getRight);
  147. std::const_mem_fun_t<BNode*, BNode> secondfunc = leftFirst?std::mem_fun(&BNode::getRight):std::mem_fun(&BNode::getLeft);
  148. while(nodes.size() < DHTBucket::K) {
  149. if(!bnode->getLeft() && !bnode->getRight()) {
  150. bnode = bnode->getUp();
  151. } else {
  152. if(std::find(visited.begin(), visited.end(), firstfunc(bnode)) == visited.end()) {
  153. bnode = firstfunc(bnode);
  154. } else if(std::find(visited.begin(), visited.end(), secondfunc(bnode)) == visited.end()) {
  155. bnode = secondfunc(bnode);
  156. } else {
  157. bnode = bnode->getUp();
  158. }
  159. }
  160. visited.push_back(bnode);
  161. if(!bnode) {
  162. break;
  163. }
  164. {
  165. SharedHandle<DHTBucket> bucket = bnode->getBucket();
  166. if(!bucket.isNull()) {
  167. std::deque<SharedHandle<DHTNode> > goodNodes;
  168. bucket->getGoodNodes(goodNodes);
  169. size_t r = DHTBucket::K-nodes.size();
  170. if(goodNodes.size() <= r) {
  171. nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
  172. } else {
  173. nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.begin()+r);
  174. }
  175. }
  176. }
  177. }
  178. return;
  179. }
  180. void BNode::enumerateBucket(std::deque<SharedHandle<DHTBucket> >& buckets,
  181. const BNode* b)
  182. {
  183. std::deque<const BNode*> visited;
  184. visited.push_back(b);
  185. while(1) {
  186. if(!b) {
  187. break;
  188. }
  189. if(!b->getBucket().isNull()) {
  190. buckets.push_back(b->getBucket());
  191. b = b->getUp();
  192. } else if(std::find(visited.begin(), visited.end(), b->getLeft()) == visited.end()) {
  193. b = b->getLeft();
  194. visited.push_back(b);
  195. } else if(std::find(visited.begin(), visited.end(), b->getRight()) == visited.end()) {
  196. b = b->getRight();
  197. visited.push_back(b);
  198. } else {
  199. b = b->getUp();
  200. }
  201. }
  202. return;
  203. }
  204. } // namespace aria2