RequestGroup.cc 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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 <cassert>
  37. #include <algorithm>
  38. #include "PostDownloadHandler.h"
  39. #include "DownloadEngine.h"
  40. #include "DefaultSegmentManFactory.h"
  41. #include "SegmentMan.h"
  42. #include "NullProgressInfoFile.h"
  43. #include "Dependency.h"
  44. #include "prefs.h"
  45. #include "InitiateConnectionCommandFactory.h"
  46. #include "File.h"
  47. #include "message.h"
  48. #include "Util.h"
  49. #include "BtRegistry.h"
  50. #include "LogFactory.h"
  51. #include "Logger.h"
  52. #include "DiskAdaptor.h"
  53. #include "DiskWriterFactory.h"
  54. #include "RecoverableException.h"
  55. #include "StreamCheckIntegrityEntry.h"
  56. #include "CheckIntegrityCommand.h"
  57. #include "UnknownLengthPieceStorage.h"
  58. #include "BtContext.h"
  59. #include "SingleFileDownloadContext.h"
  60. #include "DlAbortEx.h"
  61. #include "DownloadFailureException.h"
  62. #include "RequestGroupMan.h"
  63. #include "DefaultBtProgressInfoFile.h"
  64. #include "DefaultPieceStorage.h"
  65. #include "DownloadHandlerFactory.h"
  66. #include "MemoryBufferPreDownloadHandler.h"
  67. #include "DownloadHandlerConstants.h"
  68. #include "ServerHost.h"
  69. #include "Option.h"
  70. #include "FileEntry.h"
  71. #include "Request.h"
  72. #include "FileAllocationIterator.h"
  73. #include "StringFormat.h"
  74. #include "A2STR.h"
  75. #include "URISelector.h"
  76. #include "InOrderURISelector.h"
  77. #include "PieceSelector.h"
  78. #ifdef ENABLE_MESSAGE_DIGEST
  79. # include "CheckIntegrityCommand.h"
  80. #endif // ENABLE_MESSAGE_DIGEST
  81. #ifdef ENABLE_BITTORRENT
  82. # include "BtCheckIntegrityEntry.h"
  83. # include "DefaultPeerStorage.h"
  84. # include "DefaultBtAnnounce.h"
  85. # include "BtRuntime.h"
  86. # include "BtSetup.h"
  87. # include "BtFileAllocationEntry.h"
  88. # include "BtPostDownloadHandler.h"
  89. # include "DHTSetup.h"
  90. # include "DHTRegistry.h"
  91. # include "BtMessageFactory.h"
  92. # include "BtRequestFactory.h"
  93. # include "BtMessageDispatcher.h"
  94. # include "BtMessageReceiver.h"
  95. # include "PeerConnection.h"
  96. # include "ExtensionMessageFactory.h"
  97. # include "DHTPeerAnnounceStorage.h"
  98. # include "DHTEntryPointNameResolveCommand.h"
  99. # include "LongestSequencePieceSelector.h"
  100. #endif // ENABLE_BITTORRENT
  101. #ifdef ENABLE_METALINK
  102. # include "MetalinkPostDownloadHandler.h"
  103. #endif // ENABLE_METALINK
  104. namespace aria2 {
  105. int32_t RequestGroup::_gidCounter = 0;
  106. const std::string RequestGroup::ACCEPT_METALINK = "application/metalink+xml";
  107. RequestGroup::RequestGroup(const Option* option,
  108. const std::deque<std::string>& uris):
  109. _gid(++_gidCounter),
  110. _uris(uris),
  111. _numConcurrentCommand(option->getAsInt(PREF_SPLIT)),
  112. _numStreamConnection(0),
  113. _numCommand(0),
  114. _segmentManFactory(new DefaultSegmentManFactory(option)),
  115. _progressInfoFile(new NullProgressInfoFile()),
  116. _preLocalFileCheckEnabled(true),
  117. _haltRequested(false),
  118. _forceHaltRequested(false),
  119. _singleHostMultiConnectionEnabled(true),
  120. _uriSelector(new InOrderURISelector()),
  121. _lastModifiedTime(Time::null()),
  122. _fileNotFoundCount(0),
  123. _timeout(option->getAsInt(PREF_TIMEOUT)),
  124. _maxTries(option->getAsInt(PREF_MAX_TRIES)),
  125. _inMemoryDownload(false),
  126. _maxDownloadSpeedLimit(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT)),
  127. _maxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT)),
  128. _option(option),
  129. _logger(LogFactory::getInstance())
  130. {
  131. if(_option->get(PREF_FILE_ALLOCATION) == V_PREALLOC) {
  132. _fileAllocationEnabled = true;
  133. } else {
  134. _fileAllocationEnabled = false;
  135. }
  136. // Add types to be sent as a Accept header value here.
  137. // It would be good to put this value in Option so that user can tweak
  138. // and add this list.
  139. // ACCEPT_METALINK is used for `transparent metalink'.
  140. addAcceptType(ACCEPT_METALINK);
  141. if(!_option->getAsBool(PREF_DRY_RUN)) {
  142. initializePreDownloadHandler();
  143. initializePostDownloadHandler();
  144. }
  145. }
  146. RequestGroup::~RequestGroup() {}
  147. SegmentManHandle RequestGroup::initSegmentMan()
  148. {
  149. _segmentMan = _segmentManFactory->createNewInstance(_downloadContext,
  150. _pieceStorage);
  151. return _segmentMan;
  152. }
  153. bool RequestGroup::downloadFinished() const
  154. {
  155. if(_pieceStorage.isNull()) {
  156. return false;
  157. } else {
  158. return _pieceStorage->downloadFinished();
  159. }
  160. }
  161. bool RequestGroup::allDownloadFinished() const
  162. {
  163. if(_pieceStorage.isNull()) {
  164. return false;
  165. } else {
  166. return _pieceStorage->allDownloadFinished();
  167. }
  168. }
  169. DownloadResult::RESULT RequestGroup::downloadResult() const
  170. {
  171. if (downloadFinished())
  172. return DownloadResult::FINISHED;
  173. else {
  174. if (_uriResults.empty()) {
  175. return DownloadResult::UNKNOWN_ERROR;
  176. } else {
  177. return _uriResults.back().getResult();
  178. }
  179. }
  180. }
  181. void RequestGroup::closeFile()
  182. {
  183. if(!_pieceStorage.isNull()) {
  184. _pieceStorage->getDiskAdaptor()->closeFile();
  185. }
  186. }
  187. void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
  188. DownloadEngine* e,
  189. const std::string& method)
  190. {
  191. #ifdef ENABLE_BITTORRENT
  192. {
  193. BtContextHandle btContext = dynamic_pointer_cast<BtContext>(_downloadContext);
  194. if(!btContext.isNull()) {
  195. if(_option->getAsBool(PREF_DRY_RUN)) {
  196. throw DownloadFailureException
  197. ("Cancel BitTorrent download in dry-run context.");
  198. }
  199. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  200. throw DownloadFailureException
  201. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  202. getFilePath().c_str()).str());
  203. }
  204. initPieceStorage();
  205. if(btContext->getFileEntries().size() > 1) {
  206. // this is really multi file torrent.
  207. // clear http/ftp uris because the current implementation does not
  208. // allow integrating multi-file torrent and http/ftp.
  209. _logger->debug("Clearing http/ftp URIs because the current implementation does not allow integrating multi-file torrent and http/ftp.");
  210. _uris.clear();
  211. _pieceStorage->setFileFilter(btContext->getFileFilter());
  212. }
  213. SharedHandle<DefaultBtProgressInfoFile>
  214. progressInfoFile(new DefaultBtProgressInfoFile(_downloadContext,
  215. _pieceStorage,
  216. _option));
  217. SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
  218. btRegistry->registerBtContext(btContext->getInfoHashAsString(),
  219. btContext);
  220. btRegistry->registerPieceStorage(btContext->getInfoHashAsString(),
  221. _pieceStorage);
  222. btRegistry->registerBtProgressInfoFile(btContext->getInfoHashAsString(),
  223. progressInfoFile);
  224. BtRuntimeHandle btRuntime(new BtRuntime());
  225. btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT));
  226. btRuntime->setMaxPeers(_option->getAsInt(PREF_BT_MAX_PEERS));
  227. btRegistry->registerBtRuntime(btContext->getInfoHashAsString(),
  228. btRuntime);
  229. _btRuntime = btRuntime;
  230. progressInfoFile->setBtRuntime(btRuntime);
  231. SharedHandle<DefaultPeerStorage> peerStorage
  232. (new DefaultPeerStorage(btContext, _option));
  233. peerStorage->setBtRuntime(btRuntime);
  234. peerStorage->setPieceStorage(_pieceStorage);
  235. btRegistry->registerPeerStorage(btContext->getInfoHashAsString(),
  236. peerStorage);
  237. _peerStorage = peerStorage;
  238. progressInfoFile->setPeerStorage(peerStorage);
  239. SharedHandle<DefaultBtAnnounce> btAnnounce
  240. (new DefaultBtAnnounce(btContext, _option));
  241. btAnnounce->setBtRuntime(btRuntime);
  242. btAnnounce->setPieceStorage(_pieceStorage);
  243. btAnnounce->setPeerStorage(peerStorage);
  244. btRegistry->registerBtAnnounce(btContext->getInfoHashAsString(),
  245. btAnnounce);
  246. btAnnounce->shuffleAnnounce();
  247. // Remove the control file if download file doesn't exist
  248. if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) {
  249. progressInfoFile->removeFile();
  250. _logger->notice(MSG_REMOVED_DEFUNCT_CONTROL_FILE,
  251. progressInfoFile->getFilename().c_str(),
  252. _pieceStorage->getDiskAdaptor()->getFilePath().c_str());
  253. }
  254. // First, make DiskAdaptor read-only mode.
  255. _pieceStorage->getDiskAdaptor()->enableReadOnly();
  256. // Call Load, Save and file allocation command here
  257. if(progressInfoFile->exists()) {
  258. // load .aria2 file if it exists.
  259. progressInfoFile->load();
  260. _pieceStorage->getDiskAdaptor()->openFile();
  261. } else {
  262. if(_pieceStorage->getDiskAdaptor()->fileExists()) {
  263. if(!_option->getAsBool(PREF_CHECK_INTEGRITY) &&
  264. !_option->getAsBool(PREF_ALLOW_OVERWRITE) &&
  265. !_option->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
  266. // TODO we need this->haltRequested = true?
  267. throw DownloadFailureException
  268. (StringFormat
  269. (MSG_FILE_ALREADY_EXISTS,
  270. _pieceStorage->getDiskAdaptor()->getFilePath().c_str()).str());
  271. } else {
  272. _pieceStorage->getDiskAdaptor()->openFile();
  273. }
  274. if(_option->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
  275. _pieceStorage->markAllPiecesDone();
  276. }
  277. } else {
  278. _pieceStorage->getDiskAdaptor()->openFile();
  279. }
  280. }
  281. _progressInfoFile = progressInfoFile;
  282. if(!btContext->isPrivate() && _option->getAsBool(PREF_ENABLE_DHT)) {
  283. std::deque<Command*> commands;
  284. DHTSetup().setup(commands, e, _option);
  285. e->addCommand(commands);
  286. if(!btContext->getNodes().empty() && DHTSetup::initialized()) {
  287. DHTEntryPointNameResolveCommand* command =
  288. new DHTEntryPointNameResolveCommand(e->newCUID(), e,
  289. btContext->getNodes());
  290. command->setTaskQueue(DHTRegistry::_taskQueue);
  291. command->setTaskFactory(DHTRegistry::_taskFactory);
  292. command->setRoutingTable(DHTRegistry::_routingTable);
  293. command->setLocalNode(DHTRegistry::_localNode);
  294. e->commands.push_back(command);
  295. }
  296. }
  297. CheckIntegrityEntryHandle entry(new BtCheckIntegrityEntry(this));
  298. // --bt-seed-unverified=true is given and download has completed, skip
  299. // validation for piece hashes.
  300. if(_option->getAsBool(PREF_BT_SEED_UNVERIFIED) &&
  301. _pieceStorage->downloadFinished()) {
  302. entry->onDownloadFinished(commands, e);
  303. } else {
  304. processCheckIntegrityEntry(commands, entry, e);
  305. }
  306. return;
  307. }
  308. }
  309. #endif // ENABLE_BITTORRENT
  310. // TODO I assume here when totallength is set to DownloadContext and it is
  311. // not 0, then filepath is also set DownloadContext correctly....
  312. if(_option->getAsBool(PREF_DRY_RUN) ||
  313. _downloadContext->getTotalLength() == 0) {
  314. createNextCommand(commands, e, 1, method);
  315. }else {
  316. if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) {
  317. throw DownloadFailureException
  318. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  319. getFilePath().c_str()).str());
  320. }
  321. initPieceStorage();
  322. BtProgressInfoFileHandle infoFile
  323. (new DefaultBtProgressInfoFile(_downloadContext, _pieceStorage, _option));
  324. if(infoFile->exists() || !downloadFinishedByFileLength()) {
  325. loadAndOpenFile(infoFile);
  326. SharedHandle<CheckIntegrityEntry> checkIntegrityEntry
  327. (new StreamCheckIntegrityEntry(SharedHandle<Request>(), this));
  328. processCheckIntegrityEntry(commands, checkIntegrityEntry, e);
  329. }
  330. }
  331. }
  332. void RequestGroup::processCheckIntegrityEntry(std::deque<Command*>& commands,
  333. const CheckIntegrityEntryHandle& entry,
  334. DownloadEngine* e)
  335. {
  336. #ifdef ENABLE_MESSAGE_DIGEST
  337. if(e->option->getAsBool(PREF_CHECK_INTEGRITY) &&
  338. entry->isValidationReady()) {
  339. entry->initValidator();
  340. entry->cutTrailingGarbage();
  341. e->_checkIntegrityMan->pushEntry(entry);
  342. } else
  343. #endif // ENABLE_MESSAGE_DIGEST
  344. {
  345. entry->onDownloadIncomplete(commands, e);
  346. }
  347. }
  348. void RequestGroup::initPieceStorage()
  349. {
  350. if(_downloadContext->knowsTotalLength()) {
  351. #ifdef ENABLE_BITTORRENT
  352. SharedHandle<DefaultPieceStorage> ps;
  353. SharedHandle<PieceSelector> selector;
  354. // Use LongestSequencePieceSelector when HTTP/FTP/BitTorrent integrated
  355. // downloads. Currently multi-file integrated download is not supported.
  356. if(!_uris.empty() &&
  357. _downloadContext->getFileEntries().size() == 1 &&
  358. !dynamic_pointer_cast<BtContext>(_downloadContext).isNull()) {
  359. _logger->debug("Using LongestSequencePieceSelector");
  360. selector.reset(new LongestSequencePieceSelector());
  361. }
  362. ps.reset(new DefaultPieceStorage(_downloadContext, _option, selector));
  363. #else // !ENABLE_BITTORRENT
  364. SharedHandle<DefaultPieceStorage> ps
  365. (new DefaultPieceStorage(_downloadContext, _option));
  366. #endif // !ENABLE_BITTORRENT
  367. if(!_diskWriterFactory.isNull()) {
  368. ps->setDiskWriterFactory(_diskWriterFactory);
  369. }
  370. _pieceStorage = ps;
  371. } else {
  372. UnknownLengthPieceStorageHandle ps
  373. (new UnknownLengthPieceStorage(_downloadContext, _option));
  374. if(!_diskWriterFactory.isNull()) {
  375. ps->setDiskWriterFactory(_diskWriterFactory);
  376. }
  377. _pieceStorage = ps;
  378. }
  379. _pieceStorage->initStorage();
  380. initSegmentMan();
  381. }
  382. bool RequestGroup::downloadFinishedByFileLength()
  383. {
  384. // assuming that a control file doesn't exist.
  385. if(!isPreLocalFileCheckEnabled() ||
  386. _option->getAsBool(PREF_ALLOW_OVERWRITE) ||
  387. (_option->getAsBool(PREF_CHECK_INTEGRITY) &&
  388. !_downloadContext->getPieceHashes().empty())) {
  389. return false;
  390. }
  391. // TODO consider the case when the getFilePath() returns dir path.
  392. File outfile(getFilePath());
  393. if(outfile.exists() && getTotalLength() == outfile.size()) {
  394. _pieceStorage->markAllPiecesDone();
  395. _logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, _gid, getFilePath().c_str());
  396. return true;
  397. } else {
  398. return false;
  399. }
  400. }
  401. void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoFile)
  402. {
  403. try {
  404. if(!isPreLocalFileCheckEnabled()) {
  405. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  406. return;
  407. }
  408. // Remove the control file if download file doesn't exist
  409. if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) {
  410. progressInfoFile->removeFile();
  411. _logger->notice(MSG_REMOVED_DEFUNCT_CONTROL_FILE,
  412. progressInfoFile->getFilename().c_str(),
  413. _pieceStorage->getDiskAdaptor()->getFilePath().c_str());
  414. }
  415. while(1) {
  416. if(progressInfoFile->exists()) {
  417. progressInfoFile->load();
  418. _pieceStorage->getDiskAdaptor()->openExistingFile();
  419. } else {
  420. File outfile(getFilePath());
  421. if(outfile.exists() && _option->getAsBool(PREF_CONTINUE) &&
  422. outfile.size() <= getTotalLength()) {
  423. _pieceStorage->getDiskAdaptor()->openExistingFile();
  424. _pieceStorage->markPiecesDone(outfile.size());
  425. } else {
  426. #ifdef ENABLE_MESSAGE_DIGEST
  427. if(outfile.exists() && _option->getAsBool(PREF_CHECK_INTEGRITY)) {
  428. _pieceStorage->getDiskAdaptor()->openExistingFile();
  429. } else {
  430. #endif // ENABLE_MESSAGE_DIGEST
  431. shouldCancelDownloadForSafety();
  432. // call updateFilename here in case when filename is renamed
  433. // by tryAutoFileRenaming()
  434. progressInfoFile->updateFilename();
  435. if(progressInfoFile->exists()) {
  436. // Close DiskAdaptor here. Renmaed file will be opened in the
  437. // next loop .
  438. _pieceStorage->getDiskAdaptor()->closeFile();
  439. continue;
  440. }
  441. _pieceStorage->getDiskAdaptor()->initAndOpenFile();
  442. #ifdef ENABLE_MESSAGE_DIGEST
  443. }
  444. #endif // ENABLE_MESSAGE_DIGEST
  445. }
  446. }
  447. setProgressInfoFile(progressInfoFile);
  448. break;
  449. }
  450. } catch(RecoverableException& e) {
  451. throw DownloadFailureException
  452. (StringFormat(EX_DOWNLOAD_ABORTED).str(), e);
  453. }
  454. }
  455. // assuming that a control file does not exist
  456. void RequestGroup::shouldCancelDownloadForSafety()
  457. {
  458. if(_option->getAsBool(PREF_ALLOW_OVERWRITE)) {
  459. return;
  460. }
  461. File outfile(getFilePath());
  462. if(outfile.exists()) {
  463. if(_option->getAsBool(PREF_AUTO_FILE_RENAMING)) {
  464. if(tryAutoFileRenaming()) {
  465. _logger->notice(MSG_FILE_RENAMED, getFilePath().c_str());
  466. } else {
  467. throw DownloadFailureException
  468. (StringFormat("File renaming failed: %s",
  469. getFilePath().c_str()).str());
  470. }
  471. } else {
  472. throw DownloadFailureException
  473. (StringFormat(MSG_FILE_ALREADY_EXISTS,
  474. getFilePath().c_str()).str());
  475. }
  476. }
  477. }
  478. bool RequestGroup::tryAutoFileRenaming()
  479. {
  480. std::string filepath = getFilePath();
  481. if(filepath.empty()) {
  482. return false;
  483. }
  484. SingleFileDownloadContextHandle ctx =
  485. dynamic_pointer_cast<SingleFileDownloadContext>(_downloadContext);
  486. // Make a copy of ctx.
  487. SingleFileDownloadContextHandle tempCtx(new SingleFileDownloadContext(*ctx.get()));
  488. DefaultBtProgressInfoFile tempInfoFile(tempCtx, SharedHandle<PieceStorage>(), 0);
  489. for(unsigned int i = 1; i < 10000; ++i) {
  490. File newfile(filepath+"."+Util::uitos(i));
  491. std::string newFilename = newfile.getBasename();
  492. tempCtx->setUFilename(newFilename);
  493. tempInfoFile.updateFilename();
  494. if(!newfile.exists() || (newfile.exists() && tempInfoFile.exists())) {
  495. ctx->setUFilename(newFilename);
  496. return true;
  497. }
  498. }
  499. return false;
  500. }
  501. void RequestGroup::createNextCommandWithAdj(std::deque<Command*>& commands,
  502. DownloadEngine* e, int numAdj)
  503. {
  504. int numCommand;
  505. if(getTotalLength() == 0) {
  506. numCommand = 1+numAdj;
  507. } else {
  508. if(_numConcurrentCommand == 0) {
  509. // TODO remove _uris.size() support
  510. numCommand = _uris.size();
  511. } else {
  512. numCommand = _numConcurrentCommand;
  513. }
  514. numCommand = std::min(static_cast<int>(_downloadContext->getNumPieces()),
  515. numCommand);
  516. numCommand += numAdj;
  517. }
  518. if(numCommand > 0) {
  519. createNextCommand(commands, e, numCommand);
  520. }
  521. }
  522. void RequestGroup::createNextCommand(std::deque<Command*>& commands,
  523. DownloadEngine* e,
  524. unsigned int numCommand,
  525. const std::string& method)
  526. {
  527. std::deque<std::string> pendingURIs;
  528. for(; numCommand--; ) {
  529. std::string uri = _uriSelector->select(_uris);
  530. if(uri.empty())
  531. continue;
  532. RequestHandle req(new Request());
  533. if(req->setUrl(uri)) {
  534. ServerHostHandle sv;
  535. if(!_singleHostMultiConnectionEnabled){
  536. sv = searchServerHost(req->getHost());
  537. }
  538. if(sv.isNull()) {
  539. _spentUris.push_back(uri);
  540. req->setReferer(_option->get(PREF_REFERER));
  541. req->setMethod(method);
  542. Command* command =
  543. InitiateConnectionCommandFactory::createInitiateConnectionCommand
  544. (e->newCUID(), req, this, e);
  545. ServerHostHandle sv(new ServerHost(command->getCuid(), req->getHost()));
  546. registerServerHost(sv);
  547. // give a chance to be executed in the next loop in DownloadEngine
  548. command->setStatus(Command::STATUS_ONESHOT_REALTIME);
  549. commands.push_back(command);
  550. } else {
  551. pendingURIs.push_back(uri);
  552. }
  553. } else {
  554. _logger->error(MSG_UNRECOGNIZED_URI, req->getUrl().c_str());
  555. }
  556. }
  557. _uris.insert(_uris.begin(), pendingURIs.begin(), pendingURIs.end());
  558. }
  559. std::string RequestGroup::getFilePath() const
  560. {
  561. assert(!_downloadContext.isNull());
  562. if(inMemoryDownload()) {
  563. static const std::string DIR_MEMORY("[MEMORY]");
  564. return DIR_MEMORY+File(_downloadContext->getActualBasePath()).getBasename();
  565. } else {
  566. return _downloadContext->getActualBasePath();
  567. }
  568. }
  569. uint64_t RequestGroup::getTotalLength() const
  570. {
  571. if(_pieceStorage.isNull()) {
  572. return 0;
  573. } else {
  574. if(_pieceStorage->isSelectiveDownloadingMode()) {
  575. return _pieceStorage->getFilteredTotalLength();
  576. } else {
  577. return _pieceStorage->getTotalLength();
  578. }
  579. }
  580. }
  581. uint64_t RequestGroup::getCompletedLength() const
  582. {
  583. if(_pieceStorage.isNull()) {
  584. return 0;
  585. } else {
  586. if(_pieceStorage->isSelectiveDownloadingMode()) {
  587. return _pieceStorage->getFilteredCompletedLength();
  588. } else {
  589. return _pieceStorage->getCompletedLength();
  590. }
  591. }
  592. }
  593. void RequestGroup::validateFilename(const std::string& expectedFilename,
  594. const std::string& actualFilename) const
  595. {
  596. if(expectedFilename.empty()) {
  597. return;
  598. }
  599. if(expectedFilename != actualFilename) {
  600. throw DlAbortEx(StringFormat(EX_FILENAME_MISMATCH,
  601. expectedFilename.c_str(),
  602. actualFilename.c_str()).str());
  603. }
  604. }
  605. void RequestGroup::validateTotalLength(uint64_t expectedTotalLength,
  606. uint64_t actualTotalLength) const
  607. {
  608. if(expectedTotalLength <= 0) {
  609. return;
  610. }
  611. if(expectedTotalLength != actualTotalLength) {
  612. throw DlAbortEx
  613. (StringFormat(EX_SIZE_MISMATCH,
  614. Util::itos(expectedTotalLength, true).c_str(),
  615. Util::itos(actualTotalLength, true).c_str()).str());
  616. }
  617. }
  618. void RequestGroup::validateFilename(const std::string& actualFilename) const
  619. {
  620. validateFilename(_downloadContext->getFileEntries().front()->getBasename(), actualFilename);
  621. }
  622. void RequestGroup::validateTotalLength(uint64_t actualTotalLength) const
  623. {
  624. validateTotalLength(getTotalLength(), actualTotalLength);
  625. }
  626. void RequestGroup::increaseStreamConnection()
  627. {
  628. ++_numStreamConnection;
  629. }
  630. void RequestGroup::decreaseStreamConnection()
  631. {
  632. --_numStreamConnection;
  633. }
  634. unsigned int RequestGroup::getNumConnection() const
  635. {
  636. unsigned int numConnection = _numStreamConnection;
  637. #ifdef ENABLE_BITTORRENT
  638. if(!_btRuntime.isNull()) {
  639. numConnection += _btRuntime->getConnections();
  640. }
  641. #endif // ENABLE_BITTORRENT
  642. return numConnection;
  643. }
  644. void RequestGroup::increaseNumCommand()
  645. {
  646. ++_numCommand;
  647. }
  648. void RequestGroup::decreaseNumCommand()
  649. {
  650. --_numCommand;
  651. }
  652. TransferStat RequestGroup::calculateStat()
  653. {
  654. TransferStat stat;
  655. #ifdef ENABLE_BITTORRENT
  656. if(!_peerStorage.isNull()) {
  657. stat = _peerStorage->calculateStat();
  658. }
  659. #endif // ENABLE_BITTORRENT
  660. if(!_segmentMan.isNull()) {
  661. stat.setDownloadSpeed(stat.getDownloadSpeed()+_segmentMan->calculateDownloadSpeed());
  662. }
  663. return stat;
  664. }
  665. void RequestGroup::setHaltRequested(bool f)
  666. {
  667. _haltRequested = f;
  668. #ifdef ENABLE_BITTORRENT
  669. if(!_btRuntime.isNull()) {
  670. _btRuntime->setHalt(f);
  671. }
  672. #endif // ENABLE_BITTORRENT
  673. }
  674. void RequestGroup::setForceHaltRequested(bool f)
  675. {
  676. setHaltRequested(f);
  677. _forceHaltRequested = f;
  678. }
  679. void RequestGroup::releaseRuntimeResource(DownloadEngine* e)
  680. {
  681. #ifdef ENABLE_BITTORRENT
  682. BtContextHandle btContext = dynamic_pointer_cast<BtContext>(_downloadContext);
  683. if(!btContext.isNull()) {
  684. SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
  685. BtContextHandle btContextInReg =
  686. btRegistry->getBtContext(btContext->getInfoHashAsString());
  687. if(!btContextInReg.isNull() &&
  688. btContextInReg->getOwnerRequestGroup()->getGID() ==
  689. btContext->getOwnerRequestGroup()->getGID()) {
  690. btRegistry->unregister(btContext->getInfoHashAsString());
  691. if(!DHTRegistry::_peerAnnounceStorage.isNull()) {
  692. DHTRegistry::_peerAnnounceStorage->
  693. removeLocalPeerAnnounce(btContext->getInfoHash());
  694. }
  695. }
  696. }
  697. #endif // ENABLE_BITTORRENT
  698. if(!_pieceStorage.isNull()) {
  699. _pieceStorage->removeAdvertisedPiece(0);
  700. }
  701. }
  702. void RequestGroup::preDownloadProcessing()
  703. {
  704. _logger->debug("Finding PreDownloadHandler for path %s.", getFilePath().c_str());
  705. try {
  706. for(PreDownloadHandlers::const_iterator itr = _preDownloadHandlers.begin();
  707. itr != _preDownloadHandlers.end(); ++itr) {
  708. if((*itr)->canHandle(this)) {
  709. (*itr)->execute(this);
  710. return;
  711. }
  712. }
  713. } catch(RecoverableException& ex) {
  714. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  715. return;
  716. }
  717. _logger->debug("No PreDownloadHandler found.");
  718. return;
  719. }
  720. void RequestGroup::postDownloadProcessing
  721. (std::deque<SharedHandle<RequestGroup> >& groups)
  722. {
  723. _logger->debug("Finding PostDownloadHandler for path %s.", getFilePath().c_str());
  724. try {
  725. for(PostDownloadHandlers::const_iterator itr = _postDownloadHandlers.begin();
  726. itr != _postDownloadHandlers.end(); ++itr) {
  727. if((*itr)->canHandle(this)) {
  728. (*itr)->getNextRequestGroups(groups, this);
  729. return;
  730. }
  731. }
  732. } catch(RecoverableException& ex) {
  733. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  734. }
  735. _logger->debug("No PostDownloadHandler found.");
  736. }
  737. void RequestGroup::initializePreDownloadHandler()
  738. {
  739. #ifdef ENABLE_BITTORRENT
  740. if(_option->get(PREF_FOLLOW_TORRENT) == V_MEM) {
  741. _preDownloadHandlers.push_back(DownloadHandlerFactory::getBtPreDownloadHandler());
  742. }
  743. #endif // ENABLE_BITTORRENT
  744. #ifdef ENABLE_METALINK
  745. if(_option->get(PREF_FOLLOW_METALINK) == V_MEM) {
  746. _preDownloadHandlers.push_back(DownloadHandlerFactory::getMetalinkPreDownloadHandler());
  747. }
  748. #endif // ENABLE_METALINK
  749. }
  750. void RequestGroup::initializePostDownloadHandler()
  751. {
  752. #ifdef ENABLE_BITTORRENT
  753. if(_option->getAsBool(PREF_FOLLOW_TORRENT) ||
  754. _option->get(PREF_FOLLOW_TORRENT) == V_MEM) {
  755. _postDownloadHandlers.push_back(DownloadHandlerFactory::getBtPostDownloadHandler());
  756. }
  757. #endif // ENABLE_BITTORRENT
  758. #ifdef ENABLE_METALINK
  759. if(_option->getAsBool(PREF_FOLLOW_METALINK) ||
  760. _option->get(PREF_FOLLOW_METALINK) == V_MEM) {
  761. _postDownloadHandlers.push_back(DownloadHandlerFactory::getMetalinkPostDownloadHandler());
  762. }
  763. #endif // ENABLE_METALINK
  764. }
  765. void RequestGroup::getURIs(std::deque<std::string>& uris) const
  766. {
  767. uris.insert(uris.end(), _spentUris.begin(), _spentUris.end());
  768. uris.insert(uris.end(), _uris.begin(), _uris.end());
  769. }
  770. bool RequestGroup::isDependencyResolved()
  771. {
  772. if(_dependency.isNull()) {
  773. return true;
  774. }
  775. return _dependency->resolve();
  776. }
  777. void RequestGroup::setSegmentManFactory(const SegmentManFactoryHandle& segmentManFactory)
  778. {
  779. _segmentManFactory = segmentManFactory;
  780. }
  781. void RequestGroup::dependsOn(const DependencyHandle& dep)
  782. {
  783. _dependency = dep;
  784. }
  785. void RequestGroup::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  786. {
  787. _diskWriterFactory = diskWriterFactory;
  788. }
  789. DiskWriterFactoryHandle RequestGroup::getDiskWriterFactory() const
  790. {
  791. return _diskWriterFactory;
  792. }
  793. void RequestGroup::addPostDownloadHandler(const PostDownloadHandlerHandle& handler)
  794. {
  795. _postDownloadHandlers.push_back(handler);
  796. }
  797. void RequestGroup::addPreDownloadHandler(const PreDownloadHandlerHandle& handler)
  798. {
  799. _preDownloadHandlers.push_back(handler);
  800. }
  801. void RequestGroup::clearPostDowloadHandler()
  802. {
  803. _postDownloadHandlers.clear();
  804. }
  805. void RequestGroup::clearPreDowloadHandler()
  806. {
  807. _preDownloadHandlers.clear();
  808. }
  809. SegmentManHandle RequestGroup::getSegmentMan() const
  810. {
  811. return _segmentMan;
  812. }
  813. DownloadContextHandle RequestGroup::getDownloadContext() const
  814. {
  815. return _downloadContext;
  816. }
  817. void RequestGroup::setDownloadContext(const DownloadContextHandle& downloadContext)
  818. {
  819. _downloadContext = downloadContext;
  820. }
  821. PieceStorageHandle RequestGroup::getPieceStorage() const
  822. {
  823. return _pieceStorage;
  824. }
  825. void RequestGroup::setPieceStorage(const PieceStorageHandle& pieceStorage)
  826. {
  827. _pieceStorage = pieceStorage;
  828. }
  829. BtProgressInfoFileHandle RequestGroup::getProgressInfoFile() const
  830. {
  831. return _progressInfoFile;
  832. }
  833. void RequestGroup::setProgressInfoFile(const BtProgressInfoFileHandle& progressInfoFile)
  834. {
  835. _progressInfoFile = progressInfoFile;
  836. }
  837. bool RequestGroup::needsFileAllocation() const
  838. {
  839. return isFileAllocationEnabled() &&
  840. (uint64_t)_option->getAsLLInt(PREF_NO_FILE_ALLOCATION_LIMIT) <= getTotalLength() &&
  841. !_pieceStorage->getDiskAdaptor()->fileAllocationIterator()->finished();
  842. }
  843. DownloadResultHandle RequestGroup::createDownloadResult() const
  844. {
  845. std::deque<std::string> uris;
  846. getURIs(uris);
  847. uint64_t sessionDownloadLength = 0;
  848. #ifdef ENABLE_BITTORRENT
  849. if(!_peerStorage.isNull()) {
  850. sessionDownloadLength +=
  851. _peerStorage->calculateStat().getSessionDownloadLength();
  852. }
  853. #endif // ENABLE_BITTORRENT
  854. if(!_segmentMan.isNull()) {
  855. sessionDownloadLength +=
  856. _segmentMan->calculateSessionDownloadLength();
  857. }
  858. return
  859. SharedHandle<DownloadResult>
  860. (new DownloadResult(_gid,
  861. getFilePath(),
  862. getTotalLength(),
  863. uris.empty() ? A2STR::NIL:uris.front(),
  864. uris.size(),
  865. sessionDownloadLength,
  866. _downloadContext->calculateSessionTime(),
  867. downloadResult()));
  868. }
  869. void RequestGroup::registerServerHost(const ServerHostHandle& serverHost)
  870. {
  871. _serverHosts.push_back(serverHost);
  872. }
  873. class FindServerHostByCUID
  874. {
  875. private:
  876. int32_t _cuid;
  877. public:
  878. FindServerHostByCUID(int32_t cuid):_cuid(cuid) {}
  879. bool operator()(const ServerHostHandle& sv) const
  880. {
  881. return sv->getCuid() == _cuid;
  882. }
  883. };
  884. ServerHostHandle RequestGroup::searchServerHost(int32_t cuid) const
  885. {
  886. std::deque<SharedHandle<ServerHost> >::const_iterator itr =
  887. std::find_if(_serverHosts.begin(), _serverHosts.end(), FindServerHostByCUID(cuid));
  888. if(itr == _serverHosts.end()) {
  889. return SharedHandle<ServerHost>();
  890. } else {
  891. return *itr;
  892. }
  893. }
  894. class FindServerHostByHostname
  895. {
  896. private:
  897. std::string _hostname;
  898. public:
  899. FindServerHostByHostname(const std::string& hostname):_hostname(hostname) {}
  900. bool operator()(const ServerHostHandle& sv) const
  901. {
  902. return sv->getHostname() == _hostname;
  903. }
  904. };
  905. ServerHostHandle RequestGroup::searchServerHost(const std::string& hostname) const
  906. {
  907. std::deque<SharedHandle<ServerHost> >::const_iterator itr =
  908. std::find_if(_serverHosts.begin(), _serverHosts.end(), FindServerHostByHostname(hostname));
  909. if(itr == _serverHosts.end()) {
  910. return SharedHandle<ServerHost>();
  911. } else {
  912. return *itr;
  913. }
  914. }
  915. void RequestGroup::removeServerHost(int32_t cuid)
  916. {
  917. _serverHosts.erase(std::remove_if(_serverHosts.begin(), _serverHosts.end(), FindServerHostByCUID(cuid)), _serverHosts.end());
  918. }
  919. void RequestGroup::removeURIWhoseHostnameIs(const std::string& hostname)
  920. {
  921. std::deque<std::string> newURIs;
  922. Request req;
  923. for(std::deque<std::string>::const_iterator itr = _uris.begin(); itr != _uris.end(); ++itr) {
  924. if(((*itr).find(hostname) == std::string::npos) ||
  925. (req.setUrl(*itr) && (req.getHost() != hostname))) {
  926. newURIs.push_back(*itr);
  927. }
  928. }
  929. _logger->debug("GUID#%d - Removed %d duplicate hostname URIs",
  930. _gid, _uris.size()-newURIs.size());
  931. _uris = newURIs;
  932. }
  933. void RequestGroup::removeIdenticalURI(const std::string& uri)
  934. {
  935. _uris.erase(std::remove(_uris.begin(), _uris.end(), uri), _uris.end());
  936. }
  937. void RequestGroup::reportDownloadFinished()
  938. {
  939. _logger->notice(MSG_FILE_DOWNLOAD_COMPLETED,
  940. getFilePath().c_str());
  941. _uriSelector->resetCounters();
  942. #ifdef ENABLE_BITTORRENT
  943. SharedHandle<BtContext> ctx = dynamic_pointer_cast<BtContext>(_downloadContext);
  944. if(!ctx.isNull()) {
  945. TransferStat stat = calculateStat();
  946. double shareRatio = ((stat.getAllTimeUploadLength()*10)/getCompletedLength())/10.0;
  947. _logger->notice(MSG_SHARE_RATIO_REPORT,
  948. shareRatio,
  949. Util::abbrevSize(stat.getAllTimeUploadLength()).c_str(),
  950. Util::abbrevSize(getCompletedLength()).c_str());
  951. }
  952. #endif // ENABLE_BITTORRENT
  953. }
  954. const std::deque<std::string>& RequestGroup::getAcceptFeatures() const
  955. {
  956. return _acceptFeatures;
  957. }
  958. void RequestGroup::addAcceptFeatureHeader(const std::string& feature)
  959. {
  960. if(std::find(_acceptFeatures.begin(), _acceptFeatures.end(), feature) == _acceptFeatures.end()) {
  961. _acceptFeatures.push_back(feature);
  962. }
  963. }
  964. void RequestGroup::removeAcceptFeatureHeader(const std::string& feature)
  965. {
  966. std::deque<std::string>::iterator i = std::find(_acceptFeatures.begin(), _acceptFeatures.end(), feature);
  967. if(i != _acceptFeatures.end()) {
  968. _acceptFeatures.erase(i);
  969. }
  970. }
  971. const std::deque<std::string>& RequestGroup::getAcceptTypes() const
  972. {
  973. return _acceptTypes;
  974. }
  975. void RequestGroup::addAcceptType(const std::string& type)
  976. {
  977. if(std::find(_acceptTypes.begin(), _acceptTypes.end(), type) == _acceptTypes.end()) {
  978. _acceptTypes.push_back(type);
  979. }
  980. }
  981. void RequestGroup::removeAcceptType(const std::string& type)
  982. {
  983. _acceptTypes.erase(std::remove(_acceptTypes.begin(), _acceptTypes.end(), type),
  984. _acceptTypes.end());
  985. }
  986. void RequestGroup::setURISelector(const SharedHandle<URISelector>& uriSelector)
  987. {
  988. _uriSelector = uriSelector;
  989. }
  990. void RequestGroup::applyLastModifiedTimeToLocalFiles()
  991. {
  992. if(!_pieceStorage.isNull() && _lastModifiedTime.good()) {
  993. time_t t = _lastModifiedTime.getTime();
  994. _logger->info("Applying Last-Modified time: %s in local time zone",
  995. ctime(&t));
  996. size_t n =
  997. _pieceStorage->getDiskAdaptor()->utime(Time(), _lastModifiedTime);
  998. _logger->info("Last-Modified attrs of %lu files were updated.",
  999. static_cast<unsigned long>(n));
  1000. }
  1001. }
  1002. void RequestGroup::updateLastModifiedTime(const Time& time)
  1003. {
  1004. if(time.good() && _lastModifiedTime < time) {
  1005. _lastModifiedTime = time;
  1006. }
  1007. }
  1008. void RequestGroup::increaseAndValidateFileNotFoundCount()
  1009. {
  1010. ++_fileNotFoundCount;
  1011. const unsigned int maxCount = _option->getAsInt(PREF_MAX_FILE_NOT_FOUND);
  1012. if(maxCount > 0 && _fileNotFoundCount >= maxCount &&
  1013. _segmentMan->calculateSessionDownloadLength() == 0) {
  1014. throw DownloadFailureException
  1015. (StringFormat("Reached max-file-not-found count=%u", maxCount).str(),
  1016. DownloadResult::MAX_FILE_NOT_FOUND);
  1017. }
  1018. }
  1019. unsigned int RequestGroup::getNumConcurrentCommand() const
  1020. {
  1021. return _numConcurrentCommand;
  1022. }
  1023. void RequestGroup::markInMemoryDownload()
  1024. {
  1025. _inMemoryDownload = true;
  1026. }
  1027. bool RequestGroup::inMemoryDownload() const
  1028. {
  1029. return _inMemoryDownload;
  1030. }
  1031. void RequestGroup::tuneDownloadCommand(DownloadCommand* command)
  1032. {
  1033. _uriSelector->tuneDownloadCommand(_uris, command);
  1034. }
  1035. void RequestGroup::addURIResult(std::string uri, DownloadResult::RESULT result)
  1036. {
  1037. _uriResults.push_back(URIResult(uri, result));
  1038. }
  1039. const std::deque<URIResult>& RequestGroup::getURIResults() const
  1040. {
  1041. return _uriResults;
  1042. }
  1043. class FindURIResultByResult {
  1044. private:
  1045. DownloadResult::RESULT _r;
  1046. public:
  1047. FindURIResultByResult(DownloadResult::RESULT r):_r(r) {}
  1048. bool operator()(const URIResult& uriResult) const
  1049. {
  1050. return uriResult.getResult() == _r;
  1051. }
  1052. };
  1053. void RequestGroup::extractURIResult
  1054. (std::deque<URIResult>& res, DownloadResult::RESULT r)
  1055. {
  1056. std::deque<URIResult>::iterator i =
  1057. std::stable_partition(_uriResults.begin(), _uriResults.end(),
  1058. FindURIResultByResult(r));
  1059. std::copy(_uriResults.begin(), i, std::back_inserter(res));
  1060. _uriResults.erase(_uriResults.begin(), i);
  1061. }
  1062. void RequestGroup::setTimeout(time_t timeout)
  1063. {
  1064. _timeout = timeout;
  1065. }
  1066. time_t RequestGroup::getTimeout() const
  1067. {
  1068. return _timeout;
  1069. }
  1070. void RequestGroup::setMaxTries(unsigned int maxTries)
  1071. {
  1072. _maxTries = maxTries;
  1073. }
  1074. unsigned int RequestGroup::getMaxTries() const
  1075. {
  1076. return _maxTries;
  1077. }
  1078. bool RequestGroup::doesDownloadSpeedExceed()
  1079. {
  1080. return _maxDownloadSpeedLimit > 0 &&
  1081. _maxDownloadSpeedLimit < calculateStat().getDownloadSpeed();
  1082. }
  1083. bool RequestGroup::doesUploadSpeedExceed()
  1084. {
  1085. return _maxUploadSpeedLimit > 0 &&
  1086. _maxUploadSpeedLimit < calculateStat().getUploadSpeed();
  1087. }
  1088. } // namespace aria2