DHTRoutingTable.cc 5.1 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 "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. _numBucket(1),
  49. _logger(LogFactory::getInstance())
  50. {
  51. SharedHandle<DHTBucket> bucket(new DHTBucket(_localNode));
  52. _root = new BNode(bucket);
  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(SharedHandle<DHTBucket>());
  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) {
  94. bucket->cacheNode(node);
  95. _logger->debug("Cached node=%s", node->toString().c_str());
  96. }
  97. return false;
  98. }
  99. }
  100. return false;
  101. }
  102. void DHTRoutingTable::getClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
  103. const unsigned char* key) const
  104. {
  105. BNode::findClosestKNodes(nodes, _root, key);
  106. }
  107. size_t DHTRoutingTable::countBucket() const
  108. {
  109. return _numBucket;
  110. }
  111. void DHTRoutingTable::showBuckets() const
  112. {/*
  113. for(std::deque<SharedHandle<DHTBucket> >::const_iterator itr = _buckets.begin(); itr != _buckets.end(); ++itr) {
  114. cerr << "prefix = " << (*itr)->getPrefixLength() << ", "
  115. << "nodes = " << (*itr)->countNode() << endl;
  116. }
  117. */
  118. }
  119. SharedHandle<DHTBucket> DHTRoutingTable::getBucketFor(const unsigned char* nodeID) const
  120. {
  121. return BNode::findBucketFor(_root, nodeID);
  122. }
  123. SharedHandle<DHTBucket> DHTRoutingTable::getBucketFor(const SharedHandle<DHTNode>& node) const
  124. {
  125. return getBucketFor(node->getID());
  126. }
  127. SharedHandle<DHTNode> DHTRoutingTable::getNode(const unsigned char* nodeID, const std::string& ipaddr, uint16_t port) const
  128. {
  129. SharedHandle<DHTBucket> bucket = getBucketFor(nodeID);
  130. return bucket->getNode(nodeID, ipaddr, port);
  131. }
  132. void DHTRoutingTable::dropNode(const SharedHandle<DHTNode>& node)
  133. {
  134. getBucketFor(node)->dropNode(node);
  135. }
  136. /*
  137. void DHTRoutingTable::moveBucketHead(const SharedHandle<DHTNode>& node)
  138. {
  139. getBucketFor(node)->moveToHead(node);
  140. }
  141. */
  142. void DHTRoutingTable::moveBucketTail(const SharedHandle<DHTNode>& node)
  143. {
  144. getBucketFor(node)->moveToTail(node);
  145. }
  146. void DHTRoutingTable::getBuckets(std::deque<SharedHandle<DHTBucket> >& buckets) const
  147. {
  148. BNode::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