DHTRoutingTable.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "DHTNode.h"
  37. #include "DHTBucket.h"
  38. #include "BNode.h"
  39. #include "DHTTaskQueue.h"
  40. #include "DHTTaskFactory.h"
  41. #include "DHTTask.h"
  42. #include "Util.h"
  43. #include "LogFactory.h"
  44. #include "Logger.h"
  45. namespace aria2 {
  46. DHTRoutingTable::DHTRoutingTable(const SharedHandle<DHTNode>& localNode):
  47. _localNode(localNode),
  48. _root(new BNode(new DHTBucket(localNode))),
  49. _numBucket(1),
  50. _taskQueue(0),
  51. _taskFactory(0),
  52. _logger(LogFactory::getInstance())
  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. _logger->debug("Trying to add node:%s", node->toString().c_str());
  69. BNode* bnode = BNode::findBNodeFor(_root, node->getID());
  70. SharedHandle<DHTBucket> bucket = bnode->getBucket();
  71. while(1) {
  72. if(bucket->addNode(node)) {
  73. _logger->debug("Added DHTNode.");
  74. return true;
  75. } else if(bucket->splitAllowed()) {
  76. _logger->debug("Splitting bucket. Range:%s-%s",
  77. Util::toHex(bucket->getMinID(), DHT_ID_LENGTH).c_str(),
  78. Util::toHex(bucket->getMaxID(), DHT_ID_LENGTH).c_str());
  79. SharedHandle<DHTBucket> r = bucket->split();
  80. bnode->setBucket(0);
  81. BNode* lbnode = new BNode(bucket);
  82. BNode* rbnode = new BNode(r);
  83. bnode->setLeft(lbnode);
  84. bnode->setRight(rbnode);
  85. ++_numBucket;
  86. if(r->isInRange(node)) {
  87. bucket = r;
  88. bnode = rbnode;
  89. } else {
  90. bnode = lbnode;
  91. }
  92. } else {
  93. if(good && bucket->containsQuestionableNode()) {
  94. _logger->debug("Issuing ReplaceNodeTask: new node=%s", node->toString().c_str());
  95. _taskQueue->addImmediateTask(_taskFactory->createReplaceNodeTask(bucket, node));
  96. }
  97. return false;
  98. }
  99. }
  100. return false;
  101. }
  102. std::deque<SharedHandle<DHTNode> > DHTRoutingTable::getClosestKNodes(const unsigned char* key) const
  103. {
  104. return BNode::findClosestKNodes(_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 BNode::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. std::deque<SharedHandle<DHTBucket> > DHTRoutingTable::getBuckets() const
  146. {
  147. return BNode::enumerateBucket(_root);
  148. }
  149. void DHTRoutingTable::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)
  150. {
  151. _taskQueue = taskQueue;
  152. }
  153. void DHTRoutingTable::setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory)
  154. {
  155. _taskFactory = taskFactory;
  156. }
  157. } // namespace aria2