DHTMessageTracker.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "DHTMessageTracker.h"
  36. #include <utility>
  37. #include "DHTMessage.h"
  38. #include "DHTMessageCallback.h"
  39. #include "DHTMessageTrackerEntry.h"
  40. #include "DHTNode.h"
  41. #include "DHTRoutingTable.h"
  42. #include "DHTMessageFactory.h"
  43. #include "util.h"
  44. #include "LogFactory.h"
  45. #include "Logger.h"
  46. #include "DlAbortEx.h"
  47. #include "DHTConstants.h"
  48. #include "fmt.h"
  49. namespace aria2 {
  50. DHTMessageTracker::DHTMessageTracker()
  51. : routingTable_{nullptr}, factory_{nullptr}
  52. {
  53. }
  54. void DHTMessageTracker::addMessage(DHTMessage* message,
  55. std::chrono::seconds timeout,
  56. std::unique_ptr<DHTMessageCallback> callback)
  57. {
  58. entries_.push_back(make_unique<DHTMessageTrackerEntry>(
  59. message->getRemoteNode(), message->getTransactionID(),
  60. message->getMessageType(), std::move(timeout), std::move(callback)));
  61. }
  62. std::pair<std::unique_ptr<DHTResponseMessage>,
  63. std::unique_ptr<DHTMessageCallback>>
  64. DHTMessageTracker::messageArrived(const Dict* dict, const std::string& ipaddr,
  65. uint16_t port)
  66. {
  67. const String* tid = downcast<String>(dict->get(DHTMessage::T));
  68. if (!tid) {
  69. throw DL_ABORT_EX(
  70. fmt("Malformed DHT message. From:%s:%u", ipaddr.c_str(), port));
  71. }
  72. A2_LOG_DEBUG(fmt("Searching tracker entry for TransactionID=%s, Remote=%s:%u",
  73. util::toHex(tid->s()).c_str(), ipaddr.c_str(), port));
  74. for (auto i = std::begin(entries_), eoi = std::end(entries_); i != eoi; ++i) {
  75. if ((*i)->match(tid->s(), ipaddr, port)) {
  76. auto entry = std::move(*i);
  77. entries_.erase(i);
  78. A2_LOG_DEBUG("Tracker entry found.");
  79. auto& targetNode = entry->getTargetNode();
  80. try {
  81. auto message = factory_->createResponseMessage(
  82. entry->getMessageType(), dict, targetNode->getIPAddress(),
  83. targetNode->getPort());
  84. auto rtt = std::chrono::duration_cast<std::chrono::milliseconds>(
  85. entry->getElapsed());
  86. A2_LOG_DEBUG(
  87. fmt("RTT is %" PRId64 "", static_cast<int64_t>(rtt.count())));
  88. message->getRemoteNode()->updateRTT(rtt);
  89. if (*targetNode != *message->getRemoteNode()) {
  90. // Node ID has changed. Drop previous node ID from
  91. // DHTRoutingTable
  92. A2_LOG_DEBUG(
  93. fmt("Node ID has changed: old:%s, new:%s",
  94. util::toHex(targetNode->getID(), DHT_ID_LENGTH).c_str(),
  95. util::toHex(message->getRemoteNode()->getID(), DHT_ID_LENGTH)
  96. .c_str()));
  97. routingTable_->dropNode(targetNode);
  98. }
  99. return std::make_pair(std::move(message), entry->popCallback());
  100. }
  101. catch (RecoverableException& e) {
  102. handleTimeoutEntry(entry.get());
  103. throw;
  104. }
  105. }
  106. }
  107. A2_LOG_DEBUG("Tracker entry not found.");
  108. return std::pair<std::unique_ptr<DHTResponseMessage>,
  109. std::unique_ptr<DHTMessageCallback>>{};
  110. }
  111. void DHTMessageTracker::handleTimeoutEntry(DHTMessageTrackerEntry* entry)
  112. {
  113. try {
  114. auto& node = entry->getTargetNode();
  115. A2_LOG_DEBUG(fmt("Message timeout: To:%s:%u", node->getIPAddress().c_str(),
  116. node->getPort()));
  117. node->updateRTT(std::chrono::duration_cast<std::chrono::milliseconds>(
  118. entry->getElapsed()));
  119. node->timeout();
  120. if (node->isBad()) {
  121. A2_LOG_DEBUG(fmt("Marked bad: %s:%u", node->getIPAddress().c_str(),
  122. node->getPort()));
  123. routingTable_->dropNode(node);
  124. }
  125. auto& callback = entry->getCallback();
  126. if (callback) {
  127. callback->onTimeout(node);
  128. }
  129. }
  130. catch (RecoverableException& e) {
  131. A2_LOG_INFO_EX("Exception thrown while handling timeouts.", e);
  132. }
  133. }
  134. void DHTMessageTracker::handleTimeout()
  135. {
  136. entries_.erase(
  137. std::remove_if(std::begin(entries_), std::end(entries_),
  138. [&](const std::unique_ptr<DHTMessageTrackerEntry>& ent) {
  139. if (ent->isTimeout()) {
  140. handleTimeoutEntry(ent.get());
  141. return true;
  142. }
  143. else {
  144. return false;
  145. }
  146. }),
  147. std::end(entries_));
  148. }
  149. const DHTMessageTrackerEntry*
  150. DHTMessageTracker::getEntryFor(const DHTMessage* message) const
  151. {
  152. for (auto& ent : entries_) {
  153. if (ent->match(message->getTransactionID(),
  154. message->getRemoteNode()->getIPAddress(),
  155. message->getRemoteNode()->getPort())) {
  156. return ent.get();
  157. }
  158. }
  159. return nullptr;
  160. }
  161. size_t DHTMessageTracker::countEntry() const { return entries_.size(); }
  162. void DHTMessageTracker::setRoutingTable(DHTRoutingTable* routingTable)
  163. {
  164. routingTable_ = routingTable;
  165. }
  166. void DHTMessageTracker::setMessageFactory(DHTMessageFactory* factory)
  167. {
  168. factory_ = factory;
  169. }
  170. } // namespace aria2