DHTRoutingTable.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "DHTRoutingTable.h"
  36. #include <cstring>
  37. #include "DHTNode.h"
  38. #include "DHTBucket.h"
  39. #include "DHTBucketTree.h"
  40. #include "DHTTaskQueue.h"
  41. #include "DHTTaskFactory.h"
  42. #include "DHTTask.h"
  43. #include "util.h"
  44. #include "LogFactory.h"
  45. #include "Logger.h"
  46. #include "fmt.h"
  47. namespace aria2 {
  48. DHTRoutingTable::DHTRoutingTable(const std::shared_ptr<DHTNode>& localNode)
  49. : localNode_(localNode),
  50. root_(make_unique<DHTBucketTreeNode>(
  51. std::make_shared<DHTBucket>(localNode_))),
  52. numBucket_(1),
  53. taskQueue_{nullptr},
  54. taskFactory_{nullptr}
  55. {
  56. }
  57. DHTRoutingTable::~DHTRoutingTable() = default;
  58. bool DHTRoutingTable::addNode(const std::shared_ptr<DHTNode>& node)
  59. {
  60. return addNode(node, false);
  61. }
  62. bool DHTRoutingTable::addGoodNode(const std::shared_ptr<DHTNode>& node)
  63. {
  64. return addNode(node, true);
  65. }
  66. bool DHTRoutingTable::addNode(const std::shared_ptr<DHTNode>& node, bool good)
  67. {
  68. A2_LOG_DEBUG(fmt("Trying to add node:%s", node->toString().c_str()));
  69. if (*localNode_ == *node) {
  70. A2_LOG_DEBUG("Adding node with the same ID with localnode is not allowed.");
  71. return false;
  72. }
  73. auto treeNode = dht::findTreeNodeFor(root_.get(), node->getID());
  74. while (1) {
  75. const std::shared_ptr<DHTBucket>& bucket = treeNode->getBucket();
  76. if (bucket->addNode(node)) {
  77. A2_LOG_DEBUG("Added DHTNode.");
  78. return true;
  79. }
  80. else if (bucket->splitAllowed()) {
  81. A2_LOG_DEBUG(fmt("Splitting bucket. Range:%s-%s",
  82. util::toHex(bucket->getMinID(), DHT_ID_LENGTH).c_str(),
  83. util::toHex(bucket->getMaxID(), DHT_ID_LENGTH).c_str()));
  84. treeNode->split();
  85. ++numBucket_;
  86. if (treeNode->getLeft()->isInRange(node->getID())) {
  87. treeNode = treeNode->getLeft();
  88. }
  89. else {
  90. treeNode = treeNode->getRight();
  91. }
  92. }
  93. else {
  94. if (good) {
  95. bucket->cacheNode(node);
  96. A2_LOG_DEBUG(fmt("Cached node=%s", node->toString().c_str()));
  97. }
  98. return false;
  99. }
  100. }
  101. return false;
  102. }
  103. void DHTRoutingTable::getClosestKNodes(
  104. std::vector<std::shared_ptr<DHTNode>>& nodes,
  105. const unsigned char* key) const
  106. {
  107. dht::findClosestKNodes(nodes, root_.get(), key);
  108. }
  109. int DHTRoutingTable::getNumBucket() const { return numBucket_; }
  110. void DHTRoutingTable::showBuckets() const
  111. {
  112. /*
  113. for(std::deque<std::shared_ptr<DHTBucket> >::const_iterator itr =
  114. buckets_.begin(); itr != buckets_.end(); ++itr) {
  115. cerr << "prefix = " << (*itr)->getPrefixLength() << ", "
  116. << "nodes = " << (*itr)->countNode() << endl;
  117. }
  118. */
  119. }
  120. std::shared_ptr<DHTBucket>
  121. DHTRoutingTable::getBucketFor(const unsigned char* nodeID) const
  122. {
  123. return dht::findBucketFor(root_.get(), nodeID);
  124. }
  125. std::shared_ptr<DHTBucket>
  126. DHTRoutingTable::getBucketFor(const std::shared_ptr<DHTNode>& node) const
  127. {
  128. return getBucketFor(node->getID());
  129. }
  130. std::shared_ptr<DHTNode> DHTRoutingTable::getNode(const unsigned char* nodeID,
  131. const std::string& ipaddr,
  132. uint16_t port) const
  133. {
  134. std::shared_ptr<DHTBucket> bucket = getBucketFor(nodeID);
  135. return bucket->getNode(nodeID, ipaddr, port);
  136. }
  137. void DHTRoutingTable::dropNode(const std::shared_ptr<DHTNode>& node)
  138. {
  139. getBucketFor(node)->dropNode(node);
  140. }
  141. /*
  142. void DHTRoutingTable::moveBucketHead(const std::shared_ptr<DHTNode>& node)
  143. {
  144. getBucketFor(node)->moveToHead(node);
  145. }
  146. */
  147. void DHTRoutingTable::moveBucketTail(const std::shared_ptr<DHTNode>& node)
  148. {
  149. getBucketFor(node)->moveToTail(node);
  150. }
  151. void DHTRoutingTable::getBuckets(
  152. std::vector<std::shared_ptr<DHTBucket>>& buckets) const
  153. {
  154. dht::enumerateBucket(buckets, root_.get());
  155. }
  156. void DHTRoutingTable::setTaskQueue(DHTTaskQueue* taskQueue)
  157. {
  158. taskQueue_ = taskQueue;
  159. }
  160. void DHTRoutingTable::setTaskFactory(DHTTaskFactory* taskFactory)
  161. {
  162. taskFactory_ = taskFactory;
  163. }
  164. } // namespace aria2