DHTPeerLookupTask.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "fmt.h"
  52. namespace aria2 {
  53. DHTPeerLookupTask::DHTPeerLookupTask
  54. (const SharedHandle<DownloadContext>& downloadContext,
  55. uint16_t tcpPort)
  56. : DHTAbstractNodeLookupTask<DHTGetPeersReplyMessage>
  57. (bittorrent::getInfoHash(downloadContext)),
  58. tcpPort_(tcpPort)
  59. {}
  60. void
  61. DHTPeerLookupTask::getNodesFromMessage
  62. (std::vector<SharedHandle<DHTNode> >& nodes,
  63. const DHTGetPeersReplyMessage* message)
  64. {
  65. const std::vector<SharedHandle<DHTNode> >& knodes =
  66. message->getClosestKNodes();
  67. nodes.insert(nodes.end(), knodes.begin(), knodes.end());
  68. }
  69. void DHTPeerLookupTask::onReceivedInternal
  70. (const DHTGetPeersReplyMessage* message)
  71. {
  72. SharedHandle<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. SharedHandle<DHTMessage> DHTPeerLookupTask::createMessage
  80. (const SharedHandle<DHTNode>& remoteNode)
  81. {
  82. return getMessageFactory()->createGetPeersMessage(remoteNode, getTargetID());
  83. }
  84. SharedHandle<DHTMessageCallback> DHTPeerLookupTask::createCallback()
  85. {
  86. return SharedHandle<DHTPeerLookupTaskCallback>
  87. (new DHTPeerLookupTaskCallback(this));
  88. }
  89. void DHTPeerLookupTask::onFinish()
  90. {
  91. A2_LOG_DEBUG(fmt("Peer lookup for %s finished",
  92. util::toHex(getTargetID(), DHT_ID_LENGTH).c_str()));
  93. // send announce_peer message to K closest nodes
  94. size_t num = DHTBucket::K;
  95. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::const_iterator i =
  96. getEntries().begin(), eoi = getEntries().end();
  97. i != eoi && num > 0; ++i) {
  98. if(!(*i)->used) {
  99. continue;
  100. }
  101. const SharedHandle<DHTNode>& node = (*i)->node;
  102. std::string idHex = util::toHex(node->getID(), DHT_ID_LENGTH);
  103. std::string token = tokenStorage_[idHex];
  104. if(token.empty()) {
  105. A2_LOG_DEBUG(fmt("Token is empty for ID:%s", idHex.c_str()));
  106. continue;
  107. }
  108. SharedHandle<DHTMessage> m =
  109. getMessageFactory()->createAnnouncePeerMessage
  110. (node,
  111. getTargetID(), // this is infoHash
  112. tcpPort_,
  113. token);
  114. getMessageDispatcher()->addMessageToQueue(m);
  115. --num;
  116. }
  117. }
  118. void DHTPeerLookupTask::setPeerStorage(const SharedHandle<PeerStorage>& ps)
  119. {
  120. peerStorage_ = ps;
  121. }
  122. } // namespace aria2