DHTRoutingTable.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "BNode.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. namespace aria2 {
  47. DHTRoutingTable::DHTRoutingTable(const SharedHandle<DHTNode>& localNode):
  48. _localNode(localNode),
  49. _numBucket(1),
  50. _logger(LogFactory::getInstance())
  51. {
  52. SharedHandle<DHTBucket> bucket(new DHTBucket(_localNode));
  53. _root = new BNode(bucket);
  54. }
  55. DHTRoutingTable::~DHTRoutingTable()
  56. {
  57. delete _root;
  58. }
  59. bool DHTRoutingTable::addNode(const SharedHandle<DHTNode>& node)
  60. {
  61. return addNode(node, false);
  62. }
  63. bool DHTRoutingTable::addGoodNode(const SharedHandle<DHTNode>& node)
  64. {
  65. return addNode(node, true);
  66. }
  67. bool DHTRoutingTable::addNode(const SharedHandle<DHTNode>& node, bool good)
  68. {
  69. _logger->debug("Trying to add node:%s", node->toString().c_str());
  70. if(_localNode == node) {
  71. _logger->debug("Adding node with the same ID with localnode is not"
  72. " allowed.");
  73. return false;
  74. }
  75. BNode* bnode = BNode::findBNodeFor(_root, node->getID());
  76. SharedHandle<DHTBucket> bucket = bnode->getBucket();
  77. while(1) {
  78. if(bucket->addNode(node)) {
  79. _logger->debug("Added DHTNode.");
  80. return true;
  81. } else if(bucket->splitAllowed()) {
  82. _logger->debug("Splitting bucket. Range:%s-%s",
  83. Util::toHex(bucket->getMinID(), DHT_ID_LENGTH).c_str(),
  84. Util::toHex(bucket->getMaxID(), DHT_ID_LENGTH).c_str());
  85. SharedHandle<DHTBucket> r = bucket->split();
  86. bnode->setBucket(SharedHandle<DHTBucket>());
  87. BNode* lbnode = new BNode(bucket);
  88. BNode* rbnode = new BNode(r);
  89. bnode->setLeft(lbnode);
  90. bnode->setRight(rbnode);
  91. ++_numBucket;
  92. if(r->isInRange(node)) {
  93. bucket = r;
  94. bnode = rbnode;
  95. } else {
  96. bnode = lbnode;
  97. }
  98. } else {
  99. if(good) {
  100. bucket->cacheNode(node);
  101. _logger->debug("Cached node=%s", node->toString().c_str());
  102. }
  103. return false;
  104. }
  105. }
  106. return false;
  107. }
  108. void DHTRoutingTable::getClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
  109. const unsigned char* key) const
  110. {
  111. BNode::findClosestKNodes(nodes, _root, key);
  112. }
  113. size_t DHTRoutingTable::countBucket() const
  114. {
  115. return _numBucket;
  116. }
  117. void DHTRoutingTable::showBuckets() const
  118. {/*
  119. for(std::deque<SharedHandle<DHTBucket> >::const_iterator itr = _buckets.begin(); itr != _buckets.end(); ++itr) {
  120. cerr << "prefix = " << (*itr)->getPrefixLength() << ", "
  121. << "nodes = " << (*itr)->countNode() << endl;
  122. }
  123. */
  124. }
  125. SharedHandle<DHTBucket> DHTRoutingTable::getBucketFor(const unsigned char* nodeID) const
  126. {
  127. return BNode::findBucketFor(_root, nodeID);
  128. }
  129. SharedHandle<DHTBucket> DHTRoutingTable::getBucketFor(const SharedHandle<DHTNode>& node) const
  130. {
  131. return getBucketFor(node->getID());
  132. }
  133. SharedHandle<DHTNode> DHTRoutingTable::getNode(const unsigned char* nodeID, const std::string& ipaddr, uint16_t port) const
  134. {
  135. SharedHandle<DHTBucket> bucket = getBucketFor(nodeID);
  136. return bucket->getNode(nodeID, ipaddr, port);
  137. }
  138. void DHTRoutingTable::dropNode(const SharedHandle<DHTNode>& node)
  139. {
  140. getBucketFor(node)->dropNode(node);
  141. }
  142. /*
  143. void DHTRoutingTable::moveBucketHead(const SharedHandle<DHTNode>& node)
  144. {
  145. getBucketFor(node)->moveToHead(node);
  146. }
  147. */
  148. void DHTRoutingTable::moveBucketTail(const SharedHandle<DHTNode>& node)
  149. {
  150. getBucketFor(node)->moveToTail(node);
  151. }
  152. void DHTRoutingTable::getBuckets(std::deque<SharedHandle<DHTBucket> >& buckets) const
  153. {
  154. BNode::enumerateBucket(buckets, _root);
  155. }
  156. void DHTRoutingTable::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)
  157. {
  158. _taskQueue = taskQueue;
  159. }
  160. void DHTRoutingTable::setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory)
  161. {
  162. _taskFactory = taskFactory;
  163. }
  164. } // namespace aria2