DHTRoutingTable.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 SharedHandle<DHTNode>& localNode)
  49. : localNode_(localNode),
  50. root_(new DHTBucketTreeNode
  51. (SharedHandle<DHTBucket>(new DHTBucket(localNode_)))),
  52. numBucket_(1)
  53. {}
  54. DHTRoutingTable::~DHTRoutingTable()
  55. {
  56. delete root_;
  57. }
  58. bool DHTRoutingTable::addNode(const SharedHandle<DHTNode>& node)
  59. {
  60. return addNode(node, false);
  61. }
  62. bool DHTRoutingTable::addGoodNode(const SharedHandle<DHTNode>& node)
  63. {
  64. return addNode(node, true);
  65. }
  66. bool DHTRoutingTable::addNode(const SharedHandle<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. DHTBucketTreeNode* treeNode = dht::findTreeNodeFor(root_, node->getID());
  74. while(1) {
  75. const SharedHandle<DHTBucket>& bucket = treeNode->getBucket();
  76. if(bucket->addNode(node)) {
  77. A2_LOG_DEBUG("Added DHTNode.");
  78. return true;
  79. } else if(bucket->splitAllowed()) {
  80. A2_LOG_DEBUG(fmt("Splitting bucket. Range:%s-%s",
  81. util::toHex(bucket->getMinID(), DHT_ID_LENGTH).c_str(),
  82. util::toHex(bucket->getMaxID(), DHT_ID_LENGTH).c_str()));
  83. treeNode->split();
  84. ++numBucket_;
  85. if(treeNode->getLeft()->isInRange(node->getID())) {
  86. treeNode = treeNode->getLeft();
  87. } else {
  88. treeNode = treeNode->getRight();
  89. }
  90. } else {
  91. if(good) {
  92. bucket->cacheNode(node);
  93. A2_LOG_DEBUG(fmt("Cached node=%s", node->toString().c_str()));
  94. }
  95. return false;
  96. }
  97. }
  98. return false;
  99. }
  100. void DHTRoutingTable::getClosestKNodes
  101. (std::vector<SharedHandle<DHTNode> >& nodes,
  102. const unsigned char* key) const
  103. {
  104. dht::findClosestKNodes(nodes, root_, key);
  105. }
  106. size_t DHTRoutingTable::countBucket() const
  107. {
  108. return numBucket_;
  109. }
  110. void DHTRoutingTable::showBuckets() const
  111. {/*
  112. for(std::deque<SharedHandle<DHTBucket> >::const_iterator itr = buckets_.begin(); itr != buckets_.end(); ++itr) {
  113. cerr << "prefix = " << (*itr)->getPrefixLength() << ", "
  114. << "nodes = " << (*itr)->countNode() << endl;
  115. }
  116. */
  117. }
  118. SharedHandle<DHTBucket> DHTRoutingTable::getBucketFor(const unsigned char* nodeID) const
  119. {
  120. return dht::findBucketFor(root_, nodeID);
  121. }
  122. SharedHandle<DHTBucket> DHTRoutingTable::getBucketFor(const SharedHandle<DHTNode>& node) const
  123. {
  124. return getBucketFor(node->getID());
  125. }
  126. SharedHandle<DHTNode> DHTRoutingTable::getNode(const unsigned char* nodeID, const std::string& ipaddr, uint16_t port) const
  127. {
  128. SharedHandle<DHTBucket> bucket = getBucketFor(nodeID);
  129. return bucket->getNode(nodeID, ipaddr, port);
  130. }
  131. void DHTRoutingTable::dropNode(const SharedHandle<DHTNode>& node)
  132. {
  133. getBucketFor(node)->dropNode(node);
  134. }
  135. /*
  136. void DHTRoutingTable::moveBucketHead(const SharedHandle<DHTNode>& node)
  137. {
  138. getBucketFor(node)->moveToHead(node);
  139. }
  140. */
  141. void DHTRoutingTable::moveBucketTail(const SharedHandle<DHTNode>& node)
  142. {
  143. getBucketFor(node)->moveToTail(node);
  144. }
  145. void DHTRoutingTable::getBuckets
  146. (std::vector<SharedHandle<DHTBucket> >& buckets) const
  147. {
  148. dht::enumerateBucket(buckets, root_);
  149. }
  150. void DHTRoutingTable::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)
  151. {
  152. taskQueue_ = taskQueue;
  153. }
  154. void DHTRoutingTable::setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory)
  155. {
  156. taskFactory_ = taskFactory;
  157. }
  158. } // namespace aria2