RequestGroup.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 "RequestGroup.h"
  36. #include "DownloadEngine.h"
  37. #include "DefaultSegmentManFactory.h"
  38. #include "NullProgressInfoFile.h"
  39. #include "SegmentManFactory.h"
  40. #include "Dependency.h"
  41. #include "prefs.h"
  42. #include "InitiateConnectionCommandFactory.h"
  43. #include "CUIDCounter.h"
  44. #include "File.h"
  45. #include "message.h"
  46. #include "Util.h"
  47. #include "BtRegistry.h"
  48. #include "LogFactory.h"
  49. #include "DiskAdaptor.h"
  50. #include "DiskWriterFactory.h"
  51. #include "RecoverableException.h"
  52. #include "StreamCheckIntegrityEntry.h"
  53. #include "CheckIntegrityCommand.h"
  54. #include "UnknownLengthPieceStorage.h"
  55. #include "SingleFileDownloadContext.h"
  56. #include "DlAbortEx.h"
  57. #include "DownloadFailureException.h"
  58. #include "RequestGroupMan.h"
  59. #include "DefaultBtProgressInfoFile.h"
  60. #include "DefaultPieceStorage.h"
  61. #include "PostDownloadHandler.h"
  62. #ifdef ENABLE_MESSAGE_DIGEST
  63. # include "CheckIntegrityCommand.h"
  64. #endif // ENABLE_MESSAGE_DIGEST
  65. #ifdef ENABLE_BITTORRENT
  66. # include "BtCheckIntegrityEntry.h"
  67. # include "DefaultPeerStorage.h"
  68. # include "DefaultBtAnnounce.h"
  69. # include "BtSetup.h"
  70. # include "BtFileAllocationEntry.h"
  71. # include "BtPostDownloadHandler.h"
  72. #endif // ENABLE_BITTORRENT
  73. #ifdef ENABLE_METALINK
  74. # include "MetalinkPostDownloadHandler.h"
  75. #endif // ENABLE_METALINK
  76. #include <cerrno>
  77. int32_t RequestGroup::_gidCounter = 0;
  78. RequestGroup::RequestGroup(const Option* option,
  79. const Strings& uris):
  80. _gid(++_gidCounter),
  81. _hintTotalLength(0),
  82. _uris(uris),
  83. _numConcurrentCommand(0),
  84. _numStreamConnection(0),
  85. _numCommand(0),
  86. _segmentMan(0),
  87. _segmentManFactory(new DefaultSegmentManFactory(option)),
  88. _downloadContext(0),
  89. _pieceStorage(0),
  90. _progressInfoFile(new NullProgressInfoFile()),
  91. _diskWriterFactory(0),
  92. _dependency(0),
  93. _preLocalFileCheckEnabled(true),
  94. _haltRequested(false),
  95. _option(option),
  96. _logger(LogFactory::getInstance())
  97. {
  98. if(_option->get(PREF_FILE_ALLOCATION) == V_PREALLOC) {
  99. _fileAllocationEnabled = true;
  100. } else {
  101. _fileAllocationEnabled = false;
  102. }
  103. initializePostDownloadHandler();
  104. }
  105. RequestGroup::~RequestGroup() {}
  106. SegmentManHandle RequestGroup::initSegmentMan()
  107. {
  108. _segmentMan = _segmentManFactory->createNewInstance(_downloadContext,
  109. _pieceStorage);
  110. return _segmentMan;
  111. }
  112. bool RequestGroup::downloadFinished() const
  113. {
  114. if(_pieceStorage.isNull()) {
  115. return false;
  116. } else {
  117. return _pieceStorage->downloadFinished();
  118. }
  119. }
  120. bool RequestGroup::allDownloadFinished() const
  121. {
  122. if(_pieceStorage.isNull()) {
  123. return false;
  124. } else {
  125. return _pieceStorage->allDownloadFinished();
  126. }
  127. }
  128. void RequestGroup::closeFile()
  129. {
  130. if(!_pieceStorage.isNull()) {
  131. _pieceStorage->getDiskAdaptor()->closeFile();
  132. }
  133. }
  134. Commands RequestGroup::createInitialCommand(DownloadEngine* e)
  135. {
  136. #ifdef ENABLE_BITTORRENT
  137. {
  138. BtContextHandle btContext = _downloadContext;
  139. if(!btContext.isNull()) {
  140. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  141. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  142. getFilePath().c_str());
  143. }
  144. if(btContext->getFileEntries().size() > 1) {
  145. // this is really multi file torrent.
  146. // clear http/ftp uris because the current implementation does not
  147. // allow integrating multi-file torrent and http/ftp.
  148. _logger->debug("Clearing http/ftp URIs because the current implementation does not allow integrating multi-file torrent and http/ftp.");
  149. _uris.clear();
  150. }
  151. initPieceStorage();
  152. BtProgressInfoFileHandle progressInfoFile =
  153. new DefaultBtProgressInfoFile(_downloadContext,
  154. _pieceStorage,
  155. _option);
  156. BtRegistry::registerBtContext(btContext->getInfoHashAsString(), btContext);
  157. BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
  158. _pieceStorage);
  159. BtRegistry::registerBtProgressInfoFile(btContext->getInfoHashAsString(),
  160. progressInfoFile);
  161. BtRuntimeHandle btRuntime = new BtRuntime();
  162. btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT));
  163. BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime);
  164. PeerStorageHandle peerStorage = new DefaultPeerStorage(btContext, _option);
  165. BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), peerStorage);
  166. BtAnnounceHandle btAnnounce = new DefaultBtAnnounce(btContext, _option);
  167. BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce);
  168. btAnnounce->shuffleAnnounce();
  169. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  170. new PeerObjectCluster());
  171. // Call Load, Save and file allocation command here
  172. if(progressInfoFile->exists()) {
  173. // load .aria2 file if it exists.
  174. progressInfoFile->load();
  175. _pieceStorage->getDiskAdaptor()->openFile();
  176. } else {
  177. if(_pieceStorage->getDiskAdaptor()->fileExists()) {
  178. if(_option->get(PREF_CHECK_INTEGRITY) != V_TRUE &&
  179. _option->get(PREF_ALLOW_OVERWRITE) != V_TRUE) {
  180. _logger->error(MSG_FILE_ALREADY_EXISTS,
  181. getFilePath().c_str(),
  182. progressInfoFile->getFilename().c_str());
  183. // TODO we need this->haltRequested = true?
  184. return Commands();
  185. } else {
  186. _pieceStorage->getDiskAdaptor()->openFile();
  187. }
  188. } else {
  189. _pieceStorage->getDiskAdaptor()->openFile();
  190. }
  191. }
  192. _progressInfoFile = progressInfoFile;
  193. CheckIntegrityEntryHandle entry = new BtCheckIntegrityEntry(this);
  194. return processCheckIntegrityEntry(entry, e);
  195. }
  196. }
  197. #endif // ENABLE_BITTORRENT
  198. // TODO I assume here when totallength is set to DownloadContext and it is
  199. // not 0, then filepath is also set DownloadContext correctly....
  200. if(_downloadContext->getTotalLength() == 0) {
  201. return createNextCommand(e, 1);
  202. }else {
  203. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  204. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  205. getFilePath().c_str());
  206. }
  207. initPieceStorage();
  208. BtProgressInfoFileHandle infoFile =
  209. new DefaultBtProgressInfoFile(_downloadContext, _pieceStorage, _option);
  210. if(!infoFile->exists() && downloadFinishedByFileLength()) {
  211. return Commands();
  212. }
  213. loadAndOpenFile(infoFile);
  214. return processCheckIntegrityEntry(new StreamCheckIntegrityEntry(0, this), e);
  215. }
  216. }
  217. Commands RequestGroup::processCheckIntegrityEntry(const CheckIntegrityEntryHandle& entry, DownloadEngine* e)
  218. {
  219. #ifdef ENABLE_MESSAGE_DIGEST
  220. if(File(getFilePath()).size() > 0 &&
  221. e->option->get(PREF_CHECK_INTEGRITY) == V_TRUE &&
  222. entry->isValidationReady()) {
  223. entry->initValidator();
  224. CheckIntegrityCommand* command =
  225. new CheckIntegrityCommand(CUIDCounterSingletonHolder::instance()->newID(), this, e, entry);
  226. Commands commands;
  227. commands.push_back(command);
  228. return commands;
  229. } else
  230. #endif // ENABLE_MESSAGE_DIGEST
  231. {
  232. return entry->onDownloadIncomplete(e);
  233. }
  234. }
  235. void RequestGroup::initPieceStorage()
  236. {
  237. if(_downloadContext->getTotalLength() == 0) {
  238. UnknownLengthPieceStorageHandle ps = new UnknownLengthPieceStorage(_downloadContext, _option);
  239. if(!_diskWriterFactory.isNull()) {
  240. ps->setDiskWriterFactory(_diskWriterFactory);
  241. }
  242. _pieceStorage = ps;
  243. } else {
  244. DefaultPieceStorageHandle ps = new DefaultPieceStorage(_downloadContext, _option);
  245. if(!_diskWriterFactory.isNull()) {
  246. ps->setDiskWriterFactory(_diskWriterFactory);
  247. }
  248. _pieceStorage = ps;
  249. }
  250. _pieceStorage->initStorage();
  251. initSegmentMan();
  252. }
  253. bool RequestGroup::downloadFinishedByFileLength()
  254. {
  255. if(_option->get(PREF_CHECK_INTEGRITY) == V_TRUE &&
  256. !_downloadContext->getPieceHashes().empty()) {
  257. return false;
  258. }
  259. // TODO consider the case when the getFilePath() returns dir path.
  260. File outfile(getFilePath());
  261. if(outfile.exists() && getTotalLength() == outfile.size()) {
  262. _pieceStorage->markAllPiecesDone();
  263. _logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, _gid, getFilePath().c_str());
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. }
  269. void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoFile)
  270. {
  271. try {
  272. if(!isPreLocalFileCheckEnabled()) {
  273. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  274. return;
  275. }
  276. if(progressInfoFile->exists()) {
  277. progressInfoFile->load();
  278. _pieceStorage->getDiskAdaptor()->openExistingFile();
  279. } else {
  280. File outfile(getFilePath());
  281. if(outfile.exists() && _option->get(PREF_CONTINUE) == V_TRUE) {
  282. if(getTotalLength() < outfile.size()) {
  283. throw new DlAbortEx(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE,
  284. getFilePath().c_str(),
  285. Util::llitos(outfile.size()).c_str(),
  286. Util::llitos(getTotalLength()).c_str());
  287. }
  288. _pieceStorage->getDiskAdaptor()->openExistingFile();
  289. _pieceStorage->markPiecesDone(outfile.size());
  290. } else {
  291. #ifdef ENABLE_MESSAGE_DIGEST
  292. if(outfile.exists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
  293. _pieceStorage->getDiskAdaptor()->openExistingFile();
  294. } else {
  295. shouldCancelDownloadForSafety();
  296. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  297. }
  298. #else // ENABLE_MESSAGE_DIGEST
  299. shouldCancelDownloadForSafety();
  300. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  301. #endif // ENABLE_MESSAGE_DIGEST
  302. }
  303. }
  304. setProgressInfoFile(progressInfoFile);
  305. } catch(RecoverableException* e) {
  306. throw new DownloadFailureException(e, EX_DOWNLOAD_ABORTED);
  307. }
  308. }
  309. void RequestGroup::shouldCancelDownloadForSafety()
  310. {
  311. File outfile(getFilePath());
  312. if(outfile.exists() && !_progressInfoFile->exists()) {
  313. if(_option->get(PREF_AUTO_FILE_RENAMING) == V_TRUE) {
  314. if(tryAutoFileRenaming()) {
  315. _logger->notice("File already exists. Renamed to %s.",
  316. getFilePath().c_str());
  317. } else {
  318. _logger->notice("File renaming failed: %s", getFilePath().c_str());
  319. throw new DownloadFailureException(EX_DOWNLOAD_ABORTED);
  320. }
  321. } else if(_option->get(PREF_ALLOW_OVERWRITE) != V_TRUE) {
  322. _logger->notice(MSG_FILE_ALREADY_EXISTS,
  323. getFilePath().c_str(),
  324. _progressInfoFile->getFilename().c_str());
  325. throw new DownloadFailureException(EX_DOWNLOAD_ABORTED);
  326. }
  327. }
  328. }
  329. bool RequestGroup::tryAutoFileRenaming()
  330. {
  331. string filepath = getFilePath();
  332. if(filepath.empty()) {
  333. return false;
  334. }
  335. for(int32_t i = 1; i < 10000; ++i) {
  336. File newfile(filepath+"."+Util::itos(i));
  337. if(!newfile.exists()) {
  338. SingleFileDownloadContextHandle(_downloadContext)->setUFilename(newfile.getBasename());
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. Commands RequestGroup::createNextCommandWithAdj(DownloadEngine* e, int32_t numAdj)
  345. {
  346. int32_t numCommand = _numConcurrentCommand == 0 ? _uris.size() : _numConcurrentCommand+numAdj;
  347. return createNextCommand(e, numCommand, "GET");
  348. }
  349. Commands RequestGroup::createNextCommand(DownloadEngine* e, int32_t numCommand, const string& method)
  350. {
  351. Commands commands;
  352. for(;!_uris.empty() && numCommand--; _uris.pop_front()) {
  353. string uri = _uris.front();
  354. _spentUris.push_back(uri);
  355. RequestHandle req = new Request();
  356. req->setReferer(_option->get(PREF_REFERER));
  357. req->setMethod(method);
  358. if(req->setUrl(uri)) {
  359. commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), req, this, e));
  360. } else {
  361. _logger->error(MSG_UNRECOGNIZED_URI, req->getUrl().c_str());
  362. }
  363. }
  364. return commands;
  365. }
  366. string RequestGroup::getFilePath() const
  367. {
  368. assert(!_downloadContext.isNull());
  369. if(_downloadContext.isNull()) {
  370. return "";
  371. } else {
  372. return _downloadContext->getActualBasePath();
  373. }
  374. }
  375. int64_t RequestGroup::getTotalLength() const
  376. {
  377. if(_pieceStorage.isNull()) {
  378. return 0;
  379. } else {
  380. if(_pieceStorage->isSelectiveDownloadingMode()) {
  381. return _pieceStorage->getFilteredTotalLength();
  382. } else {
  383. return _pieceStorage->getTotalLength();
  384. }
  385. }
  386. }
  387. int64_t RequestGroup::getCompletedLength() const
  388. {
  389. if(_pieceStorage.isNull()) {
  390. return 0;
  391. } else {
  392. if(_pieceStorage->isSelectiveDownloadingMode()) {
  393. return _pieceStorage->getFilteredCompletedLength();
  394. } else {
  395. return _pieceStorage->getCompletedLength();
  396. }
  397. }
  398. }
  399. void RequestGroup::validateFilename(const string& expectedFilename,
  400. const string& actualFilename) const
  401. {
  402. if(expectedFilename.empty()) {
  403. return;
  404. }
  405. if(expectedFilename != actualFilename) {
  406. throw new DlAbortEx(EX_FILENAME_MISMATCH,
  407. expectedFilename.c_str(),
  408. actualFilename.c_str());
  409. }
  410. }
  411. void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
  412. int64_t actualTotalLength) const
  413. {
  414. if(expectedTotalLength <= 0) {
  415. return;
  416. }
  417. if(expectedTotalLength != actualTotalLength) {
  418. throw new DlAbortEx(EX_SIZE_MISMATCH,
  419. Util::llitos(expectedTotalLength, true).c_str(),
  420. Util::llitos(actualTotalLength, true).c_str());
  421. }
  422. }
  423. void RequestGroup::validateFilename(const string& actualFilename) const
  424. {
  425. validateFilename(_downloadContext->getFileEntries().front()->getBasename(), actualFilename);
  426. }
  427. void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
  428. {
  429. validateTotalLength(getTotalLength(), actualTotalLength);
  430. }
  431. void RequestGroup::validateFilenameByHint(const string& actualFilename) const
  432. {
  433. validateFilename(_hintFilename, actualFilename);
  434. }
  435. void RequestGroup::validateTotalLengthByHint(int64_t actualTotalLength) const
  436. {
  437. validateTotalLength(_hintTotalLength, actualTotalLength);
  438. }
  439. void RequestGroup::increaseStreamConnection()
  440. {
  441. ++_numStreamConnection;
  442. }
  443. void RequestGroup::decreaseStreamConnection()
  444. {
  445. --_numStreamConnection;
  446. }
  447. int32_t RequestGroup::getNumConnection() const
  448. {
  449. int32_t numConnection = _numStreamConnection;
  450. #ifdef ENABLE_BITTORRENT
  451. {
  452. BtContextHandle btContext = _downloadContext;
  453. if(!btContext.isNull()) {
  454. BtRuntimeHandle btRuntime = BT_RUNTIME(btContext);
  455. if(!btRuntime.isNull()) {
  456. numConnection += btRuntime->getConnections();
  457. }
  458. }
  459. }
  460. #endif // ENABLE_BITTORRENT
  461. return numConnection;
  462. }
  463. void RequestGroup::increaseNumCommand()
  464. {
  465. ++_numCommand;
  466. }
  467. void RequestGroup::decreaseNumCommand()
  468. {
  469. --_numCommand;
  470. }
  471. TransferStat RequestGroup::calculateStat()
  472. {
  473. TransferStat stat;
  474. #ifdef ENABLE_BITTORRENT
  475. {
  476. BtContextHandle btContext = _downloadContext;
  477. if(!btContext.isNull()) {
  478. PeerStorageHandle peerStorage = PEER_STORAGE(btContext);
  479. if(!peerStorage.isNull()) {
  480. stat = peerStorage->calculateStat();
  481. }
  482. }
  483. }
  484. #endif // ENABLE_BITTORRENT
  485. if(!_segmentMan.isNull()) {
  486. stat.setDownloadSpeed(stat.getDownloadSpeed()+_segmentMan->calculateDownloadSpeed());
  487. }
  488. return stat;
  489. }
  490. void RequestGroup::setHaltRequested(bool f)
  491. {
  492. _haltRequested = f;
  493. #ifdef ENABLE_BITTORRENT
  494. {
  495. BtContextHandle btContext = _downloadContext;
  496. if(!btContext.isNull()) {
  497. BtRuntimeHandle btRuntime = BT_RUNTIME(btContext);
  498. if(!btRuntime.isNull()) {
  499. btRuntime->setHalt(f);
  500. }
  501. }
  502. }
  503. #endif // ENABLE_BITTORRENT
  504. }
  505. void RequestGroup::releaseRuntimeResource()
  506. {
  507. #ifdef ENABLE_BITTORRENT
  508. BtContextHandle btContext = _downloadContext;
  509. if(!btContext.isNull()) {
  510. BtContextHandle btContextInReg = BtRegistry::getBtContext(btContext->getInfoHashAsString());
  511. if(!btContextInReg.isNull() &&
  512. btContextInReg->getOwnerRequestGroup()->getGID() ==
  513. btContext->getOwnerRequestGroup()->getGID()) {
  514. BtRegistry::unregister(btContext->getInfoHashAsString());
  515. }
  516. }
  517. #endif // ENABLE_BITTORRENT
  518. if(!_pieceStorage.isNull()) {
  519. _pieceStorage->removeAdvertisedPiece(0);
  520. }
  521. }
  522. RequestGroups RequestGroup::postDownloadProcessing()
  523. {
  524. _logger->debug("Finding PostDownloadHandler for path %s.", getFilePath().c_str());
  525. try {
  526. for(PostDownloadHandlers::const_iterator itr = _postDownloadHandlers.begin();
  527. itr != _postDownloadHandlers.end(); ++itr) {
  528. if((*itr)->canHandle(getFilePath())) {
  529. return (*itr)->getNextRequestGroups(getFilePath());
  530. }
  531. }
  532. } catch(RecoverableException* ex) {
  533. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  534. delete ex;
  535. return RequestGroups();
  536. }
  537. _logger->debug("No PostDownloadHandler found.");
  538. return RequestGroups();
  539. }
  540. void RequestGroup::initializePostDownloadHandler()
  541. {
  542. #ifdef ENABLE_BITTORRENT
  543. _postDownloadHandlers.push_back(new BtPostDownloadHandler(_option));
  544. #endif // ENABLE_BITTORRENT
  545. #ifdef ENABLE_METALINK
  546. _postDownloadHandlers.push_back(new MetalinkPostDownloadHandler(_option));
  547. #endif // ENABLE_METALINK
  548. }
  549. Strings RequestGroup::getUris() const
  550. {
  551. Strings temp(_spentUris.begin(), _spentUris.end());
  552. temp.insert(temp.end(), _uris.begin(), _uris.end());
  553. return temp;
  554. }
  555. bool RequestGroup::isDependencyResolved()
  556. {
  557. if(_dependency.isNull()) {
  558. return true;
  559. }
  560. return _dependency->resolve();
  561. }
  562. void RequestGroup::setSegmentManFactory(const SegmentManFactoryHandle& segmentManFactory)
  563. {
  564. _segmentManFactory = segmentManFactory;
  565. }
  566. void RequestGroup::dependsOn(const DependencyHandle& dep)
  567. {
  568. _dependency = dep;
  569. }
  570. void RequestGroup::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  571. {
  572. _diskWriterFactory = diskWriterFactory;
  573. }
  574. DiskWriterFactoryHandle RequestGroup::getDiskWriterFactory() const
  575. {
  576. return _diskWriterFactory;
  577. }
  578. void RequestGroup::addPostDownloadHandler(const PostDownloadHandlerHandle& handler)
  579. {
  580. _postDownloadHandlers.push_back(handler);
  581. }
  582. void RequestGroup::clearPostDowloadHandler()
  583. {
  584. _postDownloadHandlers.clear();
  585. }
  586. SegmentManHandle RequestGroup::getSegmentMan() const
  587. {
  588. return _segmentMan;
  589. }
  590. DownloadContextHandle RequestGroup::getDownloadContext() const
  591. {
  592. return _downloadContext;
  593. }
  594. void RequestGroup::setDownloadContext(const DownloadContextHandle& downloadContext)
  595. {
  596. _downloadContext = downloadContext;
  597. }
  598. PieceStorageHandle RequestGroup::getPieceStorage() const
  599. {
  600. return _pieceStorage;
  601. }
  602. void RequestGroup::setPieceStorage(const PieceStorageHandle& pieceStorage)
  603. {
  604. _pieceStorage = pieceStorage;
  605. }
  606. BtProgressInfoFileHandle RequestGroup::getProgressInfoFile() const
  607. {
  608. return _progressInfoFile;
  609. }
  610. void RequestGroup::setProgressInfoFile(const BtProgressInfoFileHandle& progressInfoFile)
  611. {
  612. _progressInfoFile = progressInfoFile;
  613. }
  614. bool RequestGroup::needsFileAllocation() const
  615. {
  616. return isFileAllocationEnabled() &&
  617. _option->getAsLLInt(PREF_NO_FILE_ALLOCATION_LIMIT) <= getTotalLength() &&
  618. !_pieceStorage->getDiskAdaptor()->fileAllocationIterator()->finished();
  619. }