DHTPeerLookupTask.cc 4.3 KB

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