DHTRoutingTable.cc 5.6 KB

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