RequestGroup.cc 21 KB

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