DownloadEngineFactory.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "ByteArrayDiskWriter.h"
  40. #include "Util.h"
  41. #ifdef ENABLE_BITTORRENT
  42. # include "PeerListenCommand.h"
  43. # include "TrackerWatcherCommand.h"
  44. # include "TrackerUpdateCommand.h"
  45. # include "TorrentAutoSaveCommand.h"
  46. # include "SeedCheckCommand.h"
  47. # include "PeerChokeCommand.h"
  48. # include "HaveEraseCommand.h"
  49. # include "UnionSeedCriteria.h"
  50. # include "TimeSeedCriteria.h"
  51. # include "ShareRatioSeedCriteria.h"
  52. #endif // ENABLE_BITTORRENT
  53. ConsoleDownloadEngine*
  54. DownloadEngineFactory::newConsoleEngine(const Option* op,
  55. const Requests& requests,
  56. const Requests& reserved)
  57. {
  58. ConsoleDownloadEngine* e = new ConsoleDownloadEngine();
  59. e->option = op;
  60. e->segmentMan = new SegmentMan();
  61. e->segmentMan->diskWriter = new DefaultDiskWriter();
  62. e->segmentMan->dir = op->get(PREF_DIR);
  63. e->segmentMan->ufilename = op->get(PREF_OUT);
  64. e->segmentMan->option = op;
  65. e->segmentMan->reserved = reserved;
  66. int cuidCounter = 1;
  67. for(Requests::const_iterator itr = requests.begin();
  68. itr != requests.end();
  69. itr++, cuidCounter++) {
  70. e->commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuidCounter, *itr, e));
  71. }
  72. return e;
  73. }
  74. #ifdef ENABLE_BITTORRENT
  75. TorrentConsoleDownloadEngine*
  76. DownloadEngineFactory::newTorrentConsoleEngine(const Option* op,
  77. const string& torrentFile,
  78. const Strings& targetFiles)
  79. {
  80. Request* req = new Request();
  81. req->isTorrent = true;
  82. req->setTrackerEvent(Request::STARTED);
  83. TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine();
  84. te->option = op;
  85. ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
  86. te->segmentMan = new SegmentMan();
  87. te->segmentMan->diskWriter = byteArrayDiskWriter;
  88. te->segmentMan->option = op;
  89. te->torrentMan = new TorrentMan();
  90. te->torrentMan->setStoreDir(op->get(PREF_DIR));
  91. te->torrentMan->option = op;
  92. te->torrentMan->req = req;
  93. Integers selectIndexes;
  94. Util::unfoldRange(op->get(PREF_SELECT_FILE), selectIndexes);
  95. if(selectIndexes.size()) {
  96. te->torrentMan->setup(torrentFile, selectIndexes);
  97. } else {
  98. te->torrentMan->setup(torrentFile, targetFiles);
  99. }
  100. PeerListenCommand* listenCommand =
  101. new PeerListenCommand(te->torrentMan->getNewCuid(), te);
  102. int port;
  103. int listenPort = op->getAsInt(PREF_LISTEN_PORT);
  104. if(listenPort == -1) {
  105. port = listenCommand->bindPort(6881, 6999);
  106. } else {
  107. port = listenCommand->bindPort(listenPort, listenPort);
  108. }
  109. if(port == -1) {
  110. printf(_("Errors occurred while binding port.\n"));
  111. exit(EXIT_FAILURE);
  112. }
  113. te->torrentMan->setPort(port);
  114. te->commands.push_back(listenCommand);
  115. te->commands.push_back(new TrackerWatcherCommand(te->torrentMan->getNewCuid(),
  116. te,
  117. te->torrentMan->minInterval));
  118. te->commands.push_back(new TrackerUpdateCommand(te->torrentMan->getNewCuid(),
  119. te));
  120. te->commands.push_back(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(),
  121. te,
  122. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  123. te->commands.push_back(new PeerChokeCommand(te->torrentMan->getNewCuid(),
  124. te, 10));
  125. te->commands.push_back(new HaveEraseCommand(te->torrentMan->getNewCuid(),
  126. te, 10));
  127. SharedHandle<UnionSeedCriteria> unionCri = new UnionSeedCriteria();
  128. if(op->defined(PREF_SEED_TIME)) {
  129. unionCri->addSeedCriteria(new TimeSeedCriteria(op->getAsInt(PREF_SEED_TIME)*60));
  130. }
  131. if(op->defined(PREF_SEED_RATIO)) {
  132. unionCri->addSeedCriteria(new ShareRatioSeedCriteria(op->getAsDouble(PREF_SEED_RATIO), te->torrentMan));
  133. }
  134. if(unionCri->getSeedCriterion().size() > 0) {
  135. te->commands.push_back(new SeedCheckCommand(te->torrentMan->getNewCuid(),
  136. te,
  137. unionCri));
  138. }
  139. return te;
  140. }
  141. #endif // ENABLE_BITTORRENT