DownloadEngineFactory.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "DownloadEngineFactory.h"
  23. #include "prefs.h"
  24. #include "DefaultDiskWriter.h"
  25. #include "SplitSlowestSegmentSplitter.h"
  26. #include "InitiateConnectionCommandFactory.h"
  27. #include "ByteArrayDiskWriter.h"
  28. #include "Util.h"
  29. #ifdef ENABLE_BITTORRENT
  30. # include "PeerListenCommand.h"
  31. # include "TrackerWatcherCommand.h"
  32. # include "TrackerUpdateCommand.h"
  33. # include "TorrentAutoSaveCommand.h"
  34. # include "PeerChokeCommand.h"
  35. # include "HaveEraseCommand.h"
  36. #endif // ENABLE_BITTORRENT
  37. ConsoleDownloadEngine*
  38. DownloadEngineFactory::newConsoleEngine(const Option* op,
  39. const Requests& requests,
  40. const Requests& reserved)
  41. {
  42. ConsoleDownloadEngine* e = new ConsoleDownloadEngine();
  43. e->option = op;
  44. e->segmentMan = new SegmentMan();
  45. e->segmentMan->diskWriter = new DefaultDiskWriter();
  46. e->segmentMan->dir = op->get(PREF_DIR);
  47. e->segmentMan->ufilename = op->get(PREF_OUT);
  48. e->segmentMan->option = op;
  49. e->segmentMan->splitter = new SplitSlowestSegmentSplitter();
  50. e->segmentMan->splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  51. e->segmentMan->reserved = reserved;
  52. int cuidCounter = 1;
  53. for(Requests::const_iterator itr = requests.begin();
  54. itr != requests.end();
  55. itr++, cuidCounter++) {
  56. e->commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuidCounter, *itr, e));
  57. }
  58. return e;
  59. }
  60. #ifdef ENABLE_BITTORRENT
  61. TorrentConsoleDownloadEngine*
  62. DownloadEngineFactory::newTorrentConsoleEngine(const Option* op,
  63. const string& torrentFile,
  64. const Strings& targetFiles)
  65. {
  66. Request* req = new Request();
  67. req->isTorrent = true;
  68. req->setTrackerEvent(Request::STARTED);
  69. TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine();
  70. te->option = op;
  71. ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
  72. te->segmentMan = new SegmentMan();
  73. te->segmentMan->diskWriter = byteArrayDiskWriter;
  74. te->segmentMan->option = op;
  75. te->segmentMan->splitter = new SplitSlowestSegmentSplitter();
  76. te->segmentMan->splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  77. te->torrentMan = new TorrentMan();
  78. te->torrentMan->setStoreDir(op->get(PREF_DIR));
  79. te->torrentMan->option = op;
  80. te->torrentMan->req = req;
  81. Integers selectIndexes;
  82. Util::unfoldRange(op->get(PREF_SELECT_FILE), selectIndexes);
  83. if(selectIndexes.size()) {
  84. te->torrentMan->setup(torrentFile, selectIndexes);
  85. } else {
  86. te->torrentMan->setup(torrentFile, targetFiles);
  87. }
  88. PeerListenCommand* listenCommand =
  89. new PeerListenCommand(te->torrentMan->getNewCuid(), te);
  90. int port;
  91. int listenPort = op->getAsInt(PREF_LISTEN_PORT);
  92. if(listenPort == -1) {
  93. port = listenCommand->bindPort(6881, 6999);
  94. } else {
  95. port = listenCommand->bindPort(listenPort, listenPort);
  96. }
  97. if(port == -1) {
  98. printf(_("Errors occurred while binding port.\n"));
  99. exit(EXIT_FAILURE);
  100. }
  101. te->torrentMan->setPort(port);
  102. te->commands.push_back(listenCommand);
  103. te->commands.push_back(new TrackerWatcherCommand(te->torrentMan->getNewCuid(),
  104. te,
  105. te->torrentMan->minInterval));
  106. te->commands.push_back(new TrackerUpdateCommand(te->torrentMan->getNewCuid(),
  107. te));
  108. te->commands.push_back(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(),
  109. te,
  110. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  111. te->commands.push_back(new PeerChokeCommand(te->torrentMan->getNewCuid(),
  112. te, 10));
  113. te->commands.push_back(new HaveEraseCommand(te->torrentMan->getNewCuid(),
  114. te, 10));
  115. return te;
  116. }
  117. #endif // ENABLE_BITTORRENT