DHTPeerLookupTask.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "DHTPeerLookupTask.h"
  36. #include "Peer.h"
  37. #include "DHTGetPeersReplyMessage.h"
  38. #include "Logger.h"
  39. #include "LogFactory.h"
  40. #include "DHTMessageFactory.h"
  41. #include "DHTNode.h"
  42. #include "DHTNodeLookupEntry.h"
  43. #include "DHTMessageDispatcher.h"
  44. #include "DHTMessageCallback.h"
  45. #include "PeerStorage.h"
  46. #include "util.h"
  47. #include "DHTBucket.h"
  48. #include "bittorrent_helper.h"
  49. #include "DHTPeerLookupTaskCallback.h"
  50. #include "DHTQueryMessage.h"
  51. #include "DHTGetPeersMessage.h"
  52. #include "DHTAnnouncePeerMessage.h"
  53. #include "fmt.h"
  54. namespace aria2 {
  55. DHTPeerLookupTask::DHTPeerLookupTask(
  56. const std::shared_ptr<DownloadContext>& downloadContext, uint16_t tcpPort)
  57. : DHTAbstractNodeLookupTask<DHTGetPeersReplyMessage>(
  58. bittorrent::getInfoHash(downloadContext)),
  59. tcpPort_(tcpPort)
  60. {
  61. }
  62. void DHTPeerLookupTask::getNodesFromMessage(
  63. std::vector<std::shared_ptr<DHTNode>>& nodes,
  64. const DHTGetPeersReplyMessage* message)
  65. {
  66. auto& knodes = message->getClosestKNodes();
  67. nodes.insert(std::end(nodes), std::begin(knodes), std::end(knodes));
  68. }
  69. void DHTPeerLookupTask::onReceivedInternal(
  70. const DHTGetPeersReplyMessage* message)
  71. {
  72. std::shared_ptr<DHTNode> remoteNode = message->getRemoteNode();
  73. tokenStorage_[util::toHex(remoteNode->getID(), DHT_ID_LENGTH)] =
  74. message->getToken();
  75. peerStorage_->addPeer(message->getValues());
  76. A2_LOG_INFO(fmt("Received %lu peers.",
  77. static_cast<unsigned long>(message->getValues().size())));
  78. }
  79. std::unique_ptr<DHTMessage>
  80. DHTPeerLookupTask::createMessage(const std::shared_ptr<DHTNode>& remoteNode)
  81. {
  82. return getMessageFactory()->createGetPeersMessage(remoteNode, getTargetID());
  83. }
  84. std::unique_ptr<DHTMessageCallback> DHTPeerLookupTask::createCallback()
  85. {
  86. return make_unique<DHTPeerLookupTaskCallback>(this);
  87. }
  88. void DHTPeerLookupTask::onFinish()
  89. {
  90. A2_LOG_DEBUG(fmt("Peer lookup for %s finished",
  91. util::toHex(getTargetID(), DHT_ID_LENGTH).c_str()));
  92. // send announce_peer message to K closest nodes
  93. size_t num = DHTBucket::K;
  94. for (auto i = std::begin(getEntries()), eoi = std::end(getEntries());
  95. i != eoi && num > 0; ++i) {
  96. if (!(*i)->used) {
  97. continue;
  98. }
  99. auto& node = (*i)->node;
  100. std::string idHex = util::toHex(node->getID(), DHT_ID_LENGTH);
  101. std::string token = tokenStorage_[idHex];
  102. if (token.empty()) {
  103. A2_LOG_DEBUG(fmt("Token is empty for ID:%s", idHex.c_str()));
  104. continue;
  105. }
  106. getMessageDispatcher()->addMessageToQueue(
  107. getMessageFactory()->createAnnouncePeerMessage(
  108. node,
  109. getTargetID(), // this is infoHash
  110. tcpPort_, token));
  111. --num;
  112. }
  113. }
  114. void DHTPeerLookupTask::setPeerStorage(const std::shared_ptr<PeerStorage>& ps)
  115. {
  116. peerStorage_ = ps;
  117. }
  118. } // namespace aria2