DHTAbstractNodeLookupTask.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = newEntries.begin();
  66. i != newEntries.end(); ++i) {
  67. if(memcmp(_localNode->getID(), (*i)->_node->getID(), DHT_ID_LENGTH) != 0) {
  68. _entries.push_front(*i);
  69. ++count;
  70. }
  71. }
  72. _logger->debug("%u node lookup entries added.", count);
  73. std::stable_sort(_entries.begin(), _entries.end(), DHTIDCloser(_targetID));
  74. _entries.erase(std::unique(_entries.begin(), _entries.end()), _entries.end());
  75. _logger->debug("%u node lookup entries are unique.", _entries.size());
  76. if(_entries.size() > DHTBucket::K) {
  77. _entries.erase(_entries.begin()+DHTBucket::K, _entries.end());
  78. }
  79. if(needsAdditionalOutgoingMessage()) {
  80. sendMessage();
  81. }
  82. if(_inFlightMessage == 0) {
  83. _logger->debug("Finished node_lookup for node ID %s",
  84. Util::toHex(_targetID, DHT_ID_LENGTH).c_str());
  85. onFinish();
  86. updateBucket();
  87. _finished = true;
  88. }
  89. }
  90. void DHTAbstractNodeLookupTask::onTimeout(const SharedHandle<DHTNode>& node)
  91. {
  92. _logger->debug("node lookup message timeout for node ID=%s",
  93. Util::toHex(node->getID(), DHT_ID_LENGTH).c_str());
  94. --_inFlightMessage;
  95. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i = _entries.begin(); i != _entries.end(); ++i) {
  96. if((*i)->_node == node) {
  97. _entries.erase(i);
  98. break;
  99. }
  100. }
  101. if(needsAdditionalOutgoingMessage()) {
  102. sendMessage();
  103. }
  104. if(_inFlightMessage == 0) {
  105. _logger->debug("Finished node_lookup for node ID %s",
  106. Util::toHex(_targetID, DHT_ID_LENGTH).c_str());
  107. onFinish();
  108. updateBucket();
  109. _finished = true;
  110. }
  111. }
  112. void DHTAbstractNodeLookupTask::sendMessage()
  113. {
  114. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i = _entries.begin(); i != _entries.end() && _inFlightMessage < ALPHA; ++i) {
  115. if((*i)->_used == false) {
  116. ++_inFlightMessage;
  117. (*i)->_used = true;
  118. SharedHandle<DHTMessage> m = createMessage((*i)->_node);
  119. WeakHandle<DHTMessageCallbackListener> listener(this);
  120. SharedHandle<DHTMessageCallback> callback(new DHTMessageCallbackImpl(listener));
  121. _dispatcher->addMessageToQueue(m, callback);
  122. }
  123. }
  124. }
  125. void DHTAbstractNodeLookupTask::updateBucket()
  126. {
  127. // TODO we have to something here?
  128. }
  129. void DHTAbstractNodeLookupTask::startup()
  130. {
  131. std::deque<SharedHandle<DHTNode> > nodes;
  132. _routingTable->getClosestKNodes(nodes, _targetID);
  133. _entries.clear();
  134. toEntries(_entries, nodes);
  135. if(_entries.empty()) {
  136. _finished = true;
  137. } else {
  138. // TODO use RTT here
  139. _inFlightMessage = 0;
  140. sendMessage();
  141. if(_inFlightMessage == 0) {
  142. _logger->debug("No message was sent in this lookup stage. Finished.");
  143. _finished = true;
  144. }
  145. }
  146. }
  147. void DHTAbstractNodeLookupTask::toEntries
  148. (std::deque<SharedHandle<DHTNodeLookupEntry> >& entries,
  149. const std::deque<SharedHandle<DHTNode> >& nodes) const
  150. {
  151. for(std::deque<SharedHandle<DHTNode> >::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
  152. SharedHandle<DHTNodeLookupEntry> e(new DHTNodeLookupEntry(*i));
  153. entries.push_back(e);
  154. }
  155. }
  156. } // namespace aria2