BtSetup.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "BtSetup.h"
  36. #include "RequestGroup.h"
  37. #include "DownloadEngine.h"
  38. #include "Option.h"
  39. #include "BtRegistry.h"
  40. #include "PeerListenCommand.h"
  41. #include "TrackerWatcherCommand.h"
  42. #include "SeedCheckCommand.h"
  43. #include "PeerChokeCommand.h"
  44. #include "ActivePeerConnectionCommand.h"
  45. #include "PeerListenCommand.h"
  46. #include "UnionSeedCriteria.h"
  47. #include "TimeSeedCriteria.h"
  48. #include "ShareRatioSeedCriteria.h"
  49. #include "prefs.h"
  50. #include "LogFactory.h"
  51. #include "Logger.h"
  52. #include "util.h"
  53. #include "IntSequence.h"
  54. #include "DHTGetPeersCommand.h"
  55. #include "DHTPeerAnnounceStorage.h"
  56. #include "DHTSetup.h"
  57. #include "DHTRegistry.h"
  58. #include "BtProgressInfoFile.h"
  59. #include "BtAnnounce.h"
  60. #include "BtRuntime.h"
  61. #include "bittorrent_helper.h"
  62. #include "BtStopDownloadCommand.h"
  63. namespace aria2 {
  64. BtSetup::BtSetup():_logger(LogFactory::getInstance()) {}
  65. void BtSetup::setup(std::deque<Command*>& commands,
  66. RequestGroup* requestGroup,
  67. DownloadEngine* e,
  68. const Option* option)
  69. {
  70. if(!requestGroup->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)){
  71. return;
  72. }
  73. const BDE& torrentAttrs =
  74. requestGroup->getDownloadContext()->getAttribute(bittorrent::BITTORRENT);
  75. bool metadataGetMode = !torrentAttrs.containsKey(bittorrent::METADATA);
  76. BtObject btObject =
  77. e->getBtRegistry()->get(torrentAttrs[bittorrent::INFO_HASH].s());
  78. SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage;
  79. SharedHandle<PeerStorage> peerStorage = btObject._peerStorage;
  80. SharedHandle<BtRuntime> btRuntime = btObject._btRuntime;
  81. SharedHandle<BtAnnounce> btAnnounce = btObject._btAnnounce;
  82. // commands
  83. {
  84. TrackerWatcherCommand* c =
  85. new TrackerWatcherCommand(e->newCUID(), requestGroup, e);
  86. c->setPeerStorage(peerStorage);
  87. c->setPieceStorage(pieceStorage);
  88. c->setBtRuntime(btRuntime);
  89. c->setBtAnnounce(btAnnounce);
  90. commands.push_back(c);
  91. }
  92. if(!metadataGetMode) {
  93. PeerChokeCommand* c =
  94. new PeerChokeCommand(e->newCUID(), e);
  95. c->setPeerStorage(peerStorage);
  96. c->setBtRuntime(btRuntime);
  97. commands.push_back(c);
  98. }
  99. {
  100. ActivePeerConnectionCommand* c =
  101. new ActivePeerConnectionCommand(e->newCUID(), requestGroup, e, 10);
  102. c->setBtRuntime(btRuntime);
  103. c->setPieceStorage(pieceStorage);
  104. c->setPeerStorage(peerStorage);
  105. c->setBtAnnounce(btAnnounce);
  106. commands.push_back(c);
  107. }
  108. if((metadataGetMode || torrentAttrs[bittorrent::PRIVATE].i() == 0) &&
  109. DHTSetup::initialized()) {
  110. DHTGetPeersCommand* command =
  111. new DHTGetPeersCommand(e->newCUID(), requestGroup, e);
  112. command->setTaskQueue(DHTRegistry::_taskQueue);
  113. command->setTaskFactory(DHTRegistry::_taskFactory);
  114. command->setBtRuntime(btRuntime);
  115. command->setPeerStorage(peerStorage);
  116. commands.push_back(command);
  117. }
  118. if(!metadataGetMode) {
  119. SharedHandle<UnionSeedCriteria> unionCri(new UnionSeedCriteria());
  120. if(option->defined(PREF_SEED_TIME)) {
  121. SharedHandle<SeedCriteria> cri
  122. (new TimeSeedCriteria(option->getAsInt(PREF_SEED_TIME)*60));
  123. unionCri->addSeedCriteria(cri);
  124. }
  125. {
  126. double ratio = option->getAsDouble(PREF_SEED_RATIO);
  127. if(ratio > 0.0) {
  128. SharedHandle<ShareRatioSeedCriteria> cri
  129. (new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO),
  130. requestGroup->getDownloadContext()));
  131. cri->setPieceStorage(pieceStorage);
  132. cri->setPeerStorage(peerStorage);
  133. unionCri->addSeedCriteria(cri);
  134. }
  135. }
  136. if(unionCri->getSeedCriterion().size() > 0) {
  137. SeedCheckCommand* c =
  138. new SeedCheckCommand(e->newCUID(), requestGroup, e, unionCri);
  139. c->setPieceStorage(pieceStorage);
  140. c->setBtRuntime(btRuntime);
  141. commands.push_back(c);
  142. }
  143. }
  144. if(PeerListenCommand::getNumInstance() == 0) {
  145. PeerListenCommand* listenCommand = PeerListenCommand::getInstance(e);
  146. IntSequence seq = util::parseIntRange(option->get(PREF_LISTEN_PORT));
  147. uint16_t port;
  148. if(listenCommand->bindPort(port, seq)) {
  149. btRuntime->setListenPort(port);
  150. // Add command to DownloadEngine directly.
  151. e->commands.push_back(listenCommand);
  152. } else {
  153. delete listenCommand;
  154. throw DL_ABORT_EX(_("Errors occurred while binding port.\n"));
  155. }
  156. } else {
  157. PeerListenCommand* listenCommand = PeerListenCommand::getInstance(e);
  158. btRuntime->setListenPort(listenCommand->getPort());
  159. }
  160. time_t btStopTimeout = option->getAsInt(PREF_BT_STOP_TIMEOUT);
  161. if(btStopTimeout > 0) {
  162. BtStopDownloadCommand* stopDownloadCommand =
  163. new BtStopDownloadCommand(e->newCUID(), requestGroup, e, btStopTimeout);
  164. stopDownloadCommand->setBtRuntime(btRuntime);
  165. stopDownloadCommand->setPieceStorage(pieceStorage);
  166. commands.push_back(stopDownloadCommand);
  167. }
  168. btRuntime->setReady(true);
  169. }
  170. } // namespace aria2