DownloadEngineFactory.cc 9.1 KB

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