DHTAbstractNodeLookupTask.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #ifndef D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H
  36. #define D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H
  37. #include "DHTAbstractTask.h"
  38. #include <cstring>
  39. #include <algorithm>
  40. #include <deque>
  41. #include <vector>
  42. #include "DHTConstants.h"
  43. #include "DHTNodeLookupEntry.h"
  44. #include "DHTRoutingTable.h"
  45. #include "DHTMessageDispatcher.h"
  46. #include "DHTMessageFactory.h"
  47. #include "DHTMessage.h"
  48. #include "DHTNode.h"
  49. #include "DHTBucket.h"
  50. #include "LogFactory.h"
  51. #include "Logger.h"
  52. #include "util.h"
  53. #include "DHTIDCloser.h"
  54. #include "a2functional.h"
  55. #include "fmt.h"
  56. namespace aria2 {
  57. class DHTNode;
  58. class DHTMessage;
  59. template <class ResponseMessage>
  60. class DHTAbstractNodeLookupTask : public DHTAbstractTask {
  61. private:
  62. unsigned char targetID_[DHT_ID_LENGTH];
  63. std::deque<std::unique_ptr<DHTNodeLookupEntry>> entries_;
  64. size_t inFlightMessage_;
  65. template <typename Container>
  66. void toEntries(Container& entries,
  67. const std::vector<std::shared_ptr<DHTNode>>& nodes) const
  68. {
  69. for (auto& node : nodes) {
  70. entries.push_back(make_unique<DHTNodeLookupEntry>(node));
  71. }
  72. }
  73. void sendMessage()
  74. {
  75. for (auto i = std::begin(entries_), eoi = std::end(entries_);
  76. i != eoi && inFlightMessage_ < ALPHA; ++i) {
  77. if ((*i)->used == false) {
  78. ++inFlightMessage_;
  79. (*i)->used = true;
  80. getMessageDispatcher()->addMessageToQueue(createMessage((*i)->node),
  81. createCallback());
  82. }
  83. }
  84. }
  85. void sendMessageAndCheckFinish()
  86. {
  87. if (needsAdditionalOutgoingMessage()) {
  88. sendMessage();
  89. }
  90. if (inFlightMessage_ == 0) {
  91. A2_LOG_DEBUG(fmt("Finished node_lookup for node ID %s",
  92. util::toHex(targetID_, DHT_ID_LENGTH).c_str()));
  93. onFinish();
  94. updateBucket();
  95. setFinished(true);
  96. }
  97. else {
  98. A2_LOG_DEBUG(fmt("%lu in flight message for node ID %s",
  99. static_cast<unsigned long>(inFlightMessage_),
  100. util::toHex(targetID_, DHT_ID_LENGTH).c_str()));
  101. }
  102. }
  103. void updateBucket() {}
  104. protected:
  105. const unsigned char* getTargetID() const { return targetID_; }
  106. const std::deque<std::unique_ptr<DHTNodeLookupEntry>>& getEntries() const
  107. {
  108. return entries_;
  109. }
  110. virtual void getNodesFromMessage(std::vector<std::shared_ptr<DHTNode>>& nodes,
  111. const ResponseMessage* message) = 0;
  112. virtual void onReceivedInternal(const ResponseMessage* message) {}
  113. virtual bool needsAdditionalOutgoingMessage() { return true; }
  114. virtual void onFinish() {}
  115. virtual std::unique_ptr<DHTMessage>
  116. createMessage(const std::shared_ptr<DHTNode>& remoteNode) = 0;
  117. virtual std::unique_ptr<DHTMessageCallback> createCallback() = 0;
  118. public:
  119. DHTAbstractNodeLookupTask(const unsigned char* targetID) : inFlightMessage_(0)
  120. {
  121. memcpy(targetID_, targetID, DHT_ID_LENGTH);
  122. }
  123. static const size_t ALPHA = 3;
  124. virtual void startup() CXX11_OVERRIDE
  125. {
  126. std::vector<std::shared_ptr<DHTNode>> nodes;
  127. getRoutingTable()->getClosestKNodes(nodes, targetID_);
  128. entries_.clear();
  129. toEntries(entries_, nodes);
  130. if (entries_.empty()) {
  131. setFinished(true);
  132. }
  133. else {
  134. // TODO use RTT here
  135. inFlightMessage_ = 0;
  136. sendMessage();
  137. if (inFlightMessage_ == 0) {
  138. A2_LOG_DEBUG("No message was sent in this lookup stage. Finished.");
  139. setFinished(true);
  140. }
  141. }
  142. }
  143. void onReceived(const ResponseMessage* message)
  144. {
  145. --inFlightMessage_;
  146. // Replace old Node ID with new Node ID.
  147. for (auto& entry : entries_) {
  148. if (entry->node->getIPAddress() ==
  149. message->getRemoteNode()->getIPAddress() &&
  150. entry->node->getPort() == message->getRemoteNode()->getPort()) {
  151. entry->node = message->getRemoteNode();
  152. }
  153. }
  154. onReceivedInternal(message);
  155. std::vector<std::shared_ptr<DHTNode>> nodes;
  156. getNodesFromMessage(nodes, message);
  157. std::vector<std::unique_ptr<DHTNodeLookupEntry>> newEntries;
  158. toEntries(newEntries, nodes);
  159. size_t count = 0;
  160. for (auto& ne : newEntries) {
  161. if (memcmp(getLocalNode()->getID(), ne->node->getID(), DHT_ID_LENGTH) !=
  162. 0) {
  163. A2_LOG_DEBUG(fmt("Received nodes: id=%s, ip=%s",
  164. util::toHex(ne->node->getID(), DHT_ID_LENGTH).c_str(),
  165. ne->node->getIPAddress().c_str()));
  166. entries_.push_front(std::move(ne));
  167. ++count;
  168. }
  169. }
  170. A2_LOG_DEBUG(fmt("%lu node lookup entries added.",
  171. static_cast<unsigned long>(count)));
  172. std::stable_sort(std::begin(entries_), std::end(entries_),
  173. DHTIDCloser(targetID_));
  174. entries_.erase(
  175. std::unique(std::begin(entries_), std::end(entries_),
  176. DerefEqualTo<std::unique_ptr<DHTNodeLookupEntry>>{}),
  177. std::end(entries_));
  178. A2_LOG_DEBUG(fmt("%lu node lookup entries are unique.",
  179. static_cast<unsigned long>(entries_.size())));
  180. if (entries_.size() > DHTBucket::K) {
  181. entries_.erase(std::begin(entries_) + DHTBucket::K, std::end(entries_));
  182. }
  183. sendMessageAndCheckFinish();
  184. }
  185. void onTimeout(const std::shared_ptr<DHTNode>& node)
  186. {
  187. A2_LOG_DEBUG(fmt("node lookup message timeout for node ID=%s",
  188. util::toHex(node->getID(), DHT_ID_LENGTH).c_str()));
  189. --inFlightMessage_;
  190. for (auto i = std::begin(entries_), eoi = std::end(entries_); i != eoi;
  191. ++i) {
  192. if (*(*i)->node == *node) {
  193. entries_.erase(i);
  194. break;
  195. }
  196. }
  197. sendMessageAndCheckFinish();
  198. }
  199. };
  200. } // namespace aria2
  201. #endif // D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H