DownloadEngineFactory.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "DownloadEngineFactory.h"
  36. #include "prefs.h"
  37. #include "DefaultDiskWriter.h"
  38. #include "InitiateConnectionCommandFactory.h"
  39. #include "Util.h"
  40. #include "FileAllocator.h"
  41. #include "FileAllocationMonitor.h"
  42. #include "FillRequestGroupCommand.h"
  43. #include "CUIDCounter.h"
  44. #include "FileAllocationDispatcherCommand.h"
  45. #include "FileAllocationMan.h"
  46. #include "CheckIntegrityMan.h"
  47. #ifdef ENABLE_BITTORRENT
  48. # include "PeerListenCommand.h"
  49. # include "TrackerWatcherCommand.h"
  50. # include "TrackerUpdateCommand.h"
  51. # include "TorrentAutoSaveCommand.h"
  52. # include "SeedCheckCommand.h"
  53. # include "PeerChokeCommand.h"
  54. # include "HaveEraseCommand.h"
  55. # include "ActivePeerConnectionCommand.h"
  56. # include "UnionSeedCriteria.h"
  57. # include "TimeSeedCriteria.h"
  58. # include "ShareRatioSeedCriteria.h"
  59. # include "DefaultPieceStorage.h"
  60. # include "DefaultPeerStorage.h"
  61. # include "DefaultBtAnnounce.h"
  62. # include "DefaultBtProgressInfoFile.h"
  63. #endif // ENABLE_BITTORRENT
  64. ConsoleDownloadEngine*
  65. DownloadEngineFactory::newConsoleEngine(const Option* op,
  66. const RequestGroups& requestGroups)
  67. {
  68. // set PREF_OUT parameter to requestGroup in non-multi download mode.
  69. if(requestGroups.size() == 1) {
  70. requestGroups.front()->setUserDefinedFilename(op->get(PREF_OUT));
  71. }
  72. RequestGroups workingSet;
  73. RequestGroups reservedSet;
  74. if(op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS) < (int32_t)requestGroups.size()) {
  75. copy(requestGroups.begin(), requestGroups.begin()+op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS), back_inserter(workingSet));
  76. copy(requestGroups.begin()+op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS),
  77. requestGroups.end(), back_inserter(reservedSet));
  78. } else {
  79. workingSet = requestGroups;
  80. }
  81. ConsoleDownloadEngine* e = new ConsoleDownloadEngine();
  82. e->option = op;
  83. RequestGroupManHandle requestGroupMan = new RequestGroupMan(workingSet,
  84. op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS));
  85. requestGroupMan->addReservedGroup(reservedSet);
  86. e->_requestGroupMan = requestGroupMan;
  87. e->_fileAllocationMan = new FileAllocationMan();
  88. e->_checkIntegrityMan = new CheckIntegrityMan();
  89. e->commands.push_back(new FillRequestGroupCommand(CUIDCounterSingletonHolder::instance()->newID(), e, 1));
  90. e->commands.push_back(new FileAllocationDispatcherCommand(CUIDCounterSingletonHolder::instance()->newID(), e));
  91. return e;
  92. }
  93. ConsoleDownloadEngine*
  94. DownloadEngineFactory::newConsoleEngine(const Option* op,
  95. const Requests& requests,
  96. const Requests& reserved)
  97. {
  98. ConsoleDownloadEngine* e = new ConsoleDownloadEngine();
  99. e->option = op;
  100. // e->segmentMan = new SegmentMan();
  101. // e->segmentMan->diskWriter = DefaultDiskWriter::createNewDiskWriter(op);
  102. // e->segmentMan->dir = op->get(PREF_DIR);
  103. // e->segmentMan->ufilename = op->get(PREF_OUT);
  104. // e->segmentMan->option = op;
  105. // e->segmentMan->reserved = reserved;
  106. // int cuidCounter = 1;
  107. // for(Requests::const_iterator itr = requests.begin();
  108. // itr != requests.end();
  109. // itr++, cuidCounter++) {
  110. // e->commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuidCounter, *itr, e));
  111. // }
  112. return e;
  113. }
  114. #ifdef ENABLE_BITTORRENT
  115. TorrentConsoleDownloadEngine*
  116. DownloadEngineFactory::newTorrentConsoleEngine(const BtContextHandle& btContext,
  117. const Option* op,
  118. const Strings& targetFiles)
  119. {
  120. TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine();
  121. te->option = op;
  122. RequestGroupManHandle requestGroupMan = new RequestGroupMan();
  123. te->_requestGroupMan = requestGroupMan;
  124. // ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
  125. // te->segmentMan = new SegmentMan();
  126. // te->segmentMan->diskWriter = byteArrayDiskWriter;
  127. // te->segmentMan->option = op;
  128. BtRuntimeHandle btRuntime(new BtRuntime());
  129. BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime);
  130. PieceStorageHandle pieceStorage(new DefaultPieceStorage(btContext, op));
  131. BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), pieceStorage);
  132. PeerStorageHandle peerStorage(new DefaultPeerStorage(btContext, op));
  133. BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), peerStorage);
  134. BtAnnounceHandle btAnnounce(new DefaultBtAnnounce(btContext, op));
  135. BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce);
  136. btAnnounce->shuffleAnnounce();
  137. BtProgressInfoFileHandle btProgressInfoFile(new DefaultBtProgressInfoFile(btContext, op));
  138. BtRegistry::registerBtProgressInfoFile(btContext->getInfoHashAsString(),
  139. btProgressInfoFile);
  140. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  141. new PeerObjectCluster());
  142. /*
  143. DefaultBtMessageFactoryAdaptorHandle factoryAdaptor =
  144. new DefaultBtMessageFactoryAdaptor();
  145. BtRegistry::registerBtMessageFactoryAdaptor(btContext->getInfoHashAsString(),
  146. factoryAdaptor);
  147. BtMessageFactoryClusterHandle factoryCluster = new BtMessageFactoryCluster();
  148. BtRegistry::registerBtMessageFactoryCluster(btContext->getInfoHashAsString(),
  149. factoryCluster);
  150. BtMessageDispatcherClusterHandle dispatcherCluster =
  151. new BtMessageDispatcherCluster();
  152. BtRegistry::registerBtMessageDispatcherCluster(btContext->getInfoHashAsString(),
  153. dispatcherCluster);
  154. */
  155. te->setBtContext(btContext);
  156. // initialize file storage
  157. pieceStorage->initStorage();
  158. Integers selectIndexes;
  159. Util::unfoldRange(op->get(PREF_SELECT_FILE), selectIndexes);
  160. if(selectIndexes.size()) {
  161. pieceStorage->setFileFilter(selectIndexes);
  162. } else {
  163. pieceStorage->setFileFilter(targetFiles);
  164. }
  165. PeerListenCommand* listenCommand =
  166. new PeerListenCommand(CUIDCounterSingletonHolder::instance()->newID(),
  167. te, btContext);
  168. int port;
  169. int listenPort = op->getAsInt(PREF_LISTEN_PORT);
  170. if(listenPort == -1) {
  171. port = listenCommand->bindPort(6881, 6999);
  172. } else {
  173. port = listenCommand->bindPort(listenPort, listenPort);
  174. }
  175. if(port == -1) {
  176. printf(_("Errors occurred while binding port.\n"));
  177. exit(EXIT_FAILURE);
  178. }
  179. btRuntime->setListenPort(port);
  180. te->commands.push_back(listenCommand);
  181. te->commands.push_back(new TrackerWatcherCommand(CUIDCounterSingletonHolder::instance()->newID(),
  182. te,
  183. btContext));
  184. te->commands.push_back(new TrackerUpdateCommand(CUIDCounterSingletonHolder::instance()->newID(),
  185. te,
  186. btContext));
  187. te->commands.push_back(new TorrentAutoSaveCommand(CUIDCounterSingletonHolder::instance()->newID(),
  188. te,
  189. btContext,
  190. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  191. te->commands.push_back(new PeerChokeCommand(CUIDCounterSingletonHolder::instance()->newID(),
  192. te,
  193. btContext,
  194. 10));
  195. te->commands.push_back(new HaveEraseCommand(CUIDCounterSingletonHolder::instance()->newID(),
  196. te,
  197. btContext,
  198. 10));
  199. te->commands.push_back(new ActivePeerConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(),
  200. te,
  201. btContext,
  202. 30));
  203. SharedHandle<UnionSeedCriteria> unionCri = new UnionSeedCriteria();
  204. if(op->defined(PREF_SEED_TIME)) {
  205. unionCri->addSeedCriteria(new TimeSeedCriteria(op->getAsInt(PREF_SEED_TIME)*60));
  206. }
  207. if(op->defined(PREF_SEED_RATIO)) {
  208. unionCri->addSeedCriteria(new ShareRatioSeedCriteria(op->getAsDouble(PREF_SEED_RATIO), btContext));
  209. }
  210. if(unionCri->getSeedCriterion().size() > 0) {
  211. te->commands.push_back(new SeedCheckCommand(CUIDCounterSingletonHolder::instance()->newID(),
  212. te,
  213. btContext,
  214. unionCri));
  215. }
  216. return te;
  217. }
  218. #endif // ENABLE_BITTORRENT