DHTAbstractNodeLookupTask.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "DHTAbstractNodeLookupTask.h"
  36. #include "DHTRoutingTable.h"
  37. #include "DHTMessageDispatcher.h"
  38. #include "DHTMessageFactory.h"
  39. #include "DHTMessage.h"
  40. #include "DHTNode.h"
  41. #include "DHTNodeLookupEntry.h"
  42. #include "DHTMessageCallbackImpl.h"
  43. #include "DHTBucket.h"
  44. #include "LogFactory.h"
  45. #include "Logger.h"
  46. #include "util.h"
  47. #include "DHTIDCloser.h"
  48. #include <cstring>
  49. #include <algorithm>
  50. namespace aria2 {
  51. DHTAbstractNodeLookupTask::DHTAbstractNodeLookupTask(const unsigned char* targetID):
  52. _inFlightMessage(0)
  53. {
  54. memcpy(_targetID, targetID, DHT_ID_LENGTH);
  55. }
  56. void DHTAbstractNodeLookupTask::onReceived(const SharedHandle<DHTMessage>& message)
  57. {
  58. --_inFlightMessage;
  59. onReceivedInternal(message);
  60. std::deque<SharedHandle<DHTNode> > nodes;
  61. getNodesFromMessage(nodes, message);
  62. std::deque<SharedHandle<DHTNodeLookupEntry> > newEntries;
  63. toEntries(newEntries, nodes);
  64. size_t count = 0;
  65. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::const_iterator i =
  66. newEntries.begin(); i != newEntries.end(); ++i) {
  67. if(memcmp(_localNode->getID(), (*i)->_node->getID(), DHT_ID_LENGTH) != 0) {
  68. _entries.push_front(*i);
  69. ++count;
  70. _logger->debug("Received nodes: id=%s, ip=%s",
  71. util::toHex((*i)->_node->getID(), DHT_ID_LENGTH).c_str(),
  72. (*i)->_node->getIPAddress().c_str());
  73. }
  74. }
  75. _logger->debug("%u node lookup entries added.", count);
  76. std::stable_sort(_entries.begin(), _entries.end(), DHTIDCloser(_targetID));
  77. _entries.erase(std::unique(_entries.begin(), _entries.end()), _entries.end());
  78. _logger->debug("%u node lookup entries are unique.", _entries.size());
  79. if(_entries.size() > DHTBucket::K) {
  80. _entries.erase(_entries.begin()+DHTBucket::K, _entries.end());
  81. }
  82. sendMessageAndCheckFinish();
  83. }
  84. void DHTAbstractNodeLookupTask::onTimeout(const SharedHandle<DHTNode>& node)
  85. {
  86. _logger->debug("node lookup message timeout for node ID=%s",
  87. util::toHex(node->getID(), DHT_ID_LENGTH).c_str());
  88. --_inFlightMessage;
  89. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i = _entries.begin(); i != _entries.end(); ++i) {
  90. if((*i)->_node == node) {
  91. _entries.erase(i);
  92. break;
  93. }
  94. }
  95. sendMessageAndCheckFinish();
  96. }
  97. void DHTAbstractNodeLookupTask::sendMessageAndCheckFinish()
  98. {
  99. if(needsAdditionalOutgoingMessage()) {
  100. sendMessage();
  101. }
  102. if(_inFlightMessage == 0) {
  103. _logger->debug("Finished node_lookup for node ID %s",
  104. util::toHex(_targetID, DHT_ID_LENGTH).c_str());
  105. onFinish();
  106. updateBucket();
  107. _finished = true;
  108. } else {
  109. _logger->debug("%d in flight message for node ID %s",
  110. _inFlightMessage,
  111. util::toHex(_targetID, DHT_ID_LENGTH).c_str());
  112. }
  113. }
  114. void DHTAbstractNodeLookupTask::sendMessage()
  115. {
  116. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i = _entries.begin(); i != _entries.end() && _inFlightMessage < ALPHA; ++i) {
  117. if((*i)->_used == false) {
  118. ++_inFlightMessage;
  119. (*i)->_used = true;
  120. SharedHandle<DHTMessage> m = createMessage((*i)->_node);
  121. WeakHandle<DHTMessageCallbackListener> listener(this);
  122. SharedHandle<DHTMessageCallback> callback(new DHTMessageCallbackImpl(listener));
  123. _dispatcher->addMessageToQueue(m, callback);
  124. }
  125. }
  126. }
  127. void DHTAbstractNodeLookupTask::updateBucket()
  128. {
  129. // TODO we have to something here?
  130. }
  131. void DHTAbstractNodeLookupTask::startup()
  132. {
  133. std::deque<SharedHandle<DHTNode> > nodes;
  134. _routingTable->getClosestKNodes(nodes, _targetID);
  135. _entries.clear();
  136. toEntries(_entries, nodes);
  137. if(_entries.empty()) {
  138. _finished = true;
  139. } else {
  140. // TODO use RTT here
  141. _inFlightMessage = 0;
  142. sendMessage();
  143. if(_inFlightMessage == 0) {
  144. _logger->debug("No message was sent in this lookup stage. Finished.");
  145. _finished = true;
  146. }
  147. }
  148. }
  149. void DHTAbstractNodeLookupTask::toEntries
  150. (std::deque<SharedHandle<DHTNodeLookupEntry> >& entries,
  151. const std::deque<SharedHandle<DHTNode> >& nodes) const
  152. {
  153. for(std::deque<SharedHandle<DHTNode> >::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
  154. SharedHandle<DHTNodeLookupEntry> e(new DHTNodeLookupEntry(*i));
  155. entries.push_back(e);
  156. }
  157. }
  158. } // namespace aria2