DHTBucketTree.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 "DHTBucketTree.h"
  36. #include <cstring>
  37. #include <algorithm>
  38. #include "DHTBucket.h"
  39. #include "DHTNode.h"
  40. #include "a2functional.h"
  41. namespace aria2 {
  42. DHTBucketTreeNode::DHTBucketTreeNode(std::unique_ptr<DHTBucketTreeNode> left,
  43. std::unique_ptr<DHTBucketTreeNode> right)
  44. : parent_(nullptr), left_(std::move(left)), right_(std::move(right))
  45. {
  46. resetRelation();
  47. }
  48. DHTBucketTreeNode::DHTBucketTreeNode(std::shared_ptr<DHTBucket> bucket)
  49. : parent_(nullptr), bucket_(std::move(bucket))
  50. {
  51. memcpy(minId_, bucket_->getMinID(), DHT_ID_LENGTH);
  52. memcpy(maxId_, bucket_->getMaxID(), DHT_ID_LENGTH);
  53. }
  54. DHTBucketTreeNode::~DHTBucketTreeNode() = default;
  55. void DHTBucketTreeNode::resetRelation()
  56. {
  57. left_->setParent(this);
  58. right_->setParent(this);
  59. memcpy(minId_, left_->getMinId(), DHT_ID_LENGTH);
  60. memcpy(maxId_, right_->getMaxId(), DHT_ID_LENGTH);
  61. }
  62. DHTBucketTreeNode* DHTBucketTreeNode::dig(const unsigned char* key)
  63. {
  64. if (leaf()) {
  65. return nullptr;
  66. }
  67. if (left_->isInRange(key)) {
  68. return left_.get();
  69. }
  70. else {
  71. return right_.get();
  72. }
  73. }
  74. bool DHTBucketTreeNode::isInRange(const unsigned char* key) const
  75. {
  76. return !std::lexicographical_compare(&key[0], &key[DHT_ID_LENGTH], &minId_[0],
  77. &minId_[DHT_ID_LENGTH]) &&
  78. !std::lexicographical_compare(&maxId_[0], &maxId_[DHT_ID_LENGTH],
  79. &key[0], &key[DHT_ID_LENGTH]);
  80. }
  81. void DHTBucketTreeNode::split()
  82. {
  83. left_ = make_unique<DHTBucketTreeNode>(bucket_->split());
  84. right_ = make_unique<DHTBucketTreeNode>(bucket_);
  85. bucket_.reset();
  86. resetRelation();
  87. }
  88. namespace dht {
  89. DHTBucketTreeNode* findTreeNodeFor(DHTBucketTreeNode* root,
  90. const unsigned char* key)
  91. {
  92. if (root->leaf()) {
  93. return root;
  94. }
  95. else {
  96. return findTreeNodeFor(root->dig(key), key);
  97. }
  98. }
  99. std::shared_ptr<DHTBucket> findBucketFor(DHTBucketTreeNode* root,
  100. const unsigned char* key)
  101. {
  102. DHTBucketTreeNode* leaf = findTreeNodeFor(root, key);
  103. return leaf->getBucket();
  104. }
  105. namespace {
  106. void collectNodes(std::vector<std::shared_ptr<DHTNode>>& nodes,
  107. const std::shared_ptr<DHTBucket>& bucket)
  108. {
  109. std::vector<std::shared_ptr<DHTNode>> goodNodes;
  110. bucket->getGoodNodes(goodNodes);
  111. nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
  112. }
  113. } // namespace
  114. namespace {
  115. void collectDownwardLeftFirst(std::vector<std::shared_ptr<DHTNode>>& nodes,
  116. DHTBucketTreeNode* tnode)
  117. {
  118. if (tnode->leaf()) {
  119. collectNodes(nodes, tnode->getBucket());
  120. }
  121. else {
  122. collectDownwardLeftFirst(nodes, tnode->getLeft());
  123. if (nodes.size() < DHTBucket::K) {
  124. collectDownwardLeftFirst(nodes, tnode->getRight());
  125. }
  126. }
  127. }
  128. } // namespace
  129. namespace {
  130. void collectDownwardRightFirst(std::vector<std::shared_ptr<DHTNode>>& nodes,
  131. DHTBucketTreeNode* tnode)
  132. {
  133. if (tnode->leaf()) {
  134. collectNodes(nodes, tnode->getBucket());
  135. }
  136. else {
  137. collectDownwardRightFirst(nodes, tnode->getRight());
  138. if (nodes.size() < DHTBucket::K) {
  139. collectDownwardRightFirst(nodes, tnode->getLeft());
  140. }
  141. }
  142. }
  143. } // namespace
  144. namespace {
  145. void collectUpward(std::vector<std::shared_ptr<DHTNode>>& nodes,
  146. DHTBucketTreeNode* from)
  147. {
  148. while (1) {
  149. DHTBucketTreeNode* parent = from->getParent();
  150. if (!parent) {
  151. break;
  152. }
  153. if (parent->getLeft() == from) {
  154. collectNodes(nodes, parent->getRight()->getBucket());
  155. }
  156. else {
  157. collectNodes(nodes, parent->getLeft()->getBucket());
  158. }
  159. from = parent;
  160. if (DHTBucket::K <= nodes.size()) {
  161. break;
  162. }
  163. }
  164. }
  165. } // namespace
  166. void findClosestKNodes(std::vector<std::shared_ptr<DHTNode>>& nodes,
  167. DHTBucketTreeNode* root, const unsigned char* key)
  168. {
  169. size_t nodesSize = nodes.size();
  170. if (DHTBucket::K <= nodesSize) {
  171. return;
  172. }
  173. DHTBucketTreeNode* leaf = findTreeNodeFor(root, key);
  174. if (leaf == root) {
  175. collectNodes(nodes, leaf->getBucket());
  176. }
  177. else {
  178. DHTBucketTreeNode* parent = leaf->getParent();
  179. if (parent->getLeft() == leaf) {
  180. collectDownwardLeftFirst(nodes, parent);
  181. }
  182. else {
  183. collectDownwardRightFirst(nodes, parent);
  184. }
  185. if (nodes.size() < DHTBucket::K) {
  186. collectUpward(nodes, parent);
  187. }
  188. }
  189. if (DHTBucket::K < nodes.size()) {
  190. nodes.erase(nodes.begin() + DHTBucket::K, nodes.end());
  191. }
  192. }
  193. void enumerateBucket(std::vector<std::shared_ptr<DHTBucket>>& buckets,
  194. DHTBucketTreeNode* root)
  195. {
  196. if (root->leaf()) {
  197. buckets.push_back(root->getBucket());
  198. }
  199. else {
  200. enumerateBucket(buckets, root->getLeft());
  201. enumerateBucket(buckets, root->getRight());
  202. }
  203. }
  204. } // namespace dht
  205. } // namespace aria2