RequestGroup.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #ifndef _D_REQUEST_GROUP_H_
  36. #define _D_REQUEST_GROUP_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include "SharedHandle.h"
  41. #include "TransferStat.h"
  42. #include "TimeA2.h"
  43. #include "Request.h"
  44. #include "DownloadResult.h"
  45. #include "URIResult.h"
  46. namespace aria2 {
  47. class DownloadEngine;
  48. class SegmentMan;
  49. class SegmentManFactory;
  50. class Command;
  51. class DownloadCommand;
  52. class DownloadContext;
  53. class PieceStorage;
  54. class BtProgressInfoFile;
  55. class Dependency;
  56. class PreDownloadHandler;
  57. class PostDownloadHandler;
  58. class DiskWriterFactory;
  59. class Option;
  60. class Logger;
  61. class RequestGroup;
  62. class CheckIntegrityEntry;
  63. class DownloadResult;
  64. class ServerHost;
  65. class URISelector;
  66. #ifdef ENABLE_BITTORRENT
  67. class BtRuntime;
  68. class PeerStorage;
  69. #endif // ENABLE_BITTORRENT
  70. class RequestGroup {
  71. private:
  72. static int32_t _gidCounter;
  73. int32_t _gid;
  74. std::deque<std::string> _uris;
  75. std::deque<std::string> _spentUris;
  76. unsigned int _numConcurrentCommand;
  77. /**
  78. * This is the number of connections used in streaming protocol(http/ftp)
  79. */
  80. unsigned int _numStreamConnection;
  81. unsigned int _numCommand;
  82. SharedHandle<SegmentMan> _segmentMan;
  83. SharedHandle<SegmentManFactory> _segmentManFactory;
  84. SharedHandle<DownloadContext> _downloadContext;
  85. SharedHandle<PieceStorage> _pieceStorage;
  86. SharedHandle<BtProgressInfoFile> _progressInfoFile;
  87. SharedHandle<DiskWriterFactory> _diskWriterFactory;
  88. SharedHandle<Dependency> _dependency;
  89. std::deque<SharedHandle<ServerHost> > _serverHosts;
  90. bool _fileAllocationEnabled;
  91. bool _preLocalFileCheckEnabled;
  92. bool _haltRequested;
  93. bool _forceHaltRequested;
  94. // URIResult is stored in the ascending order of the time when its result is
  95. // available.
  96. std::deque<URIResult> _uriResults;
  97. bool _singleHostMultiConnectionEnabled;
  98. std::deque<SharedHandle<PreDownloadHandler> > _preDownloadHandlers;
  99. std::deque<SharedHandle<PostDownloadHandler> > _postDownloadHandlers;
  100. std::deque<std::string> _acceptFeatures;
  101. std::deque<std::string> _acceptTypes;
  102. SharedHandle<URISelector> _uriSelector;
  103. Time _lastModifiedTime;
  104. unsigned int _fileNotFoundCount;
  105. // Timeout used for HTTP/FTP downloads.
  106. time_t _timeout;
  107. // How many times HTTP/FTP download should retry.
  108. unsigned int _maxTries;
  109. #ifdef ENABLE_BITTORRENT
  110. WeakHandle<BtRuntime> _btRuntime;
  111. WeakHandle<PeerStorage> _peerStorage;
  112. #endif // ENABLE_BITTORRENT
  113. // This flag just indicates that the downloaded file is not saved disk but
  114. // just sits in memory.
  115. bool _inMemoryDownload;
  116. unsigned int _maxDownloadSpeedLimit;
  117. unsigned int _maxUploadSpeedLimit;
  118. const Option* _option;
  119. Logger* _logger;
  120. void validateFilename(const std::string& expectedFilename,
  121. const std::string& actualFilename) const;
  122. void validateTotalLength(uint64_t expectedTotalLength,
  123. uint64_t actualTotalLength) const;
  124. void initializePreDownloadHandler();
  125. void initializePostDownloadHandler();
  126. bool tryAutoFileRenaming();
  127. // Returns the result code of this RequestGroup.
  128. // If the download finished, then returns DownloadResult::FINISHED.
  129. // If the download didn't finish and error result is available in _uriResults,
  130. // then last result code is returned.
  131. // Otherwise returns DownloadResult::UNKNOWN_ERROR.
  132. DownloadResult::RESULT downloadResult() const;
  133. public:
  134. RequestGroup(const Option* option, const std::deque<std::string>& uris);
  135. ~RequestGroup();
  136. /**
  137. * Reinitializes SegmentMan based on current property values and
  138. * returns new one.
  139. */
  140. SharedHandle<SegmentMan> initSegmentMan();
  141. SharedHandle<SegmentMan> getSegmentMan() const;
  142. // Returns first bootstrap commands to initiate a download.
  143. // If this is HTTP/FTP download and file size is unknown, only 1 command
  144. // (usually, HttpInitiateConnection or FtpInitiateConnection) will be created
  145. // with its Request object having Requet::METHOD_HEAD in its method.
  146. // This behavior can be changed by providing 3rd argument.
  147. // The method has effect only for using HTTP request including FTP via HTTP
  148. // proxy.
  149. void createInitialCommand(std::deque<Command*>& commands,
  150. DownloadEngine* e,
  151. const std::string& method = Request::METHOD_HEAD);
  152. void createNextCommandWithAdj(std::deque<Command*>& commands,
  153. DownloadEngine* e, int numAdj);
  154. void createNextCommand(std::deque<Command*>& commands,
  155. DownloadEngine* e, unsigned int numCommand,
  156. const std::string& method = Request::METHOD_GET);
  157. void addURI(const std::string& uri)
  158. {
  159. _uris.push_back(uri);
  160. }
  161. bool downloadFinished() const;
  162. bool allDownloadFinished() const;
  163. void closeFile();
  164. std::string getFilePath() const;
  165. uint64_t getTotalLength() const;
  166. uint64_t getCompletedLength() const;
  167. const std::deque<std::string>& getRemainingUris() const
  168. {
  169. return _uris;
  170. }
  171. const std::deque<std::string>& getSpentUris() const
  172. {
  173. return _spentUris;
  174. }
  175. void getURIs(std::deque<std::string>& uris) const;
  176. /**
  177. * Compares expected filename with specified actualFilename.
  178. * The expected filename refers to FileEntry::getBasename() of the first
  179. * element of DownloadContext::getFileEntries()
  180. */
  181. void validateFilename(const std::string& actualFilename) const;
  182. void validateTotalLength(uint64_t actualTotalLength) const;
  183. void setSegmentManFactory(const SharedHandle<SegmentManFactory>& segmentManFactory);
  184. void setNumConcurrentCommand(unsigned int num)
  185. {
  186. _numConcurrentCommand = num;
  187. }
  188. unsigned int getNumConcurrentCommand() const;
  189. int32_t getGID() const
  190. {
  191. return _gid;
  192. }
  193. TransferStat calculateStat();
  194. SharedHandle<DownloadContext> getDownloadContext() const;
  195. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  196. SharedHandle<PieceStorage> getPieceStorage() const;
  197. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  198. SharedHandle<BtProgressInfoFile> getProgressInfoFile() const;
  199. void setProgressInfoFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  200. void increaseStreamConnection();
  201. void decreaseStreamConnection();
  202. unsigned int getNumConnection() const;
  203. void increaseNumCommand();
  204. void decreaseNumCommand();
  205. unsigned int getNumCommand() const
  206. {
  207. return _numCommand;
  208. }
  209. // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
  210. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  211. SharedHandle<DiskWriterFactory> getDiskWriterFactory() const;
  212. void setFileAllocationEnabled(bool f)
  213. {
  214. _fileAllocationEnabled = f;
  215. }
  216. bool isFileAllocationEnabled() const
  217. {
  218. return _fileAllocationEnabled;
  219. }
  220. bool needsFileAllocation() const;
  221. /**
  222. * Setting _preLocalFileCheckEnabled to false, then skip the check to see
  223. * if a file is already exists and control file exists etc.
  224. * Always open file with DiskAdaptor::initAndOpenFile()
  225. */
  226. void setPreLocalFileCheckEnabled(bool f)
  227. {
  228. _preLocalFileCheckEnabled = f;
  229. }
  230. bool isPreLocalFileCheckEnabled() const
  231. {
  232. return _preLocalFileCheckEnabled;
  233. }
  234. void setHaltRequested(bool f);
  235. void setForceHaltRequested(bool f);
  236. bool isHaltRequested() const
  237. {
  238. return _haltRequested;
  239. }
  240. bool isForceHaltRequested() const
  241. {
  242. return _forceHaltRequested;
  243. }
  244. void addURIResult(std::string uri, DownloadResult::RESULT result);
  245. const std::deque<URIResult>& getURIResults() const;
  246. // Extracts URIResult whose _result is r and stores them into res.
  247. // The extracted URIResults are removed from _uriResults.
  248. void extractURIResult(std::deque<URIResult>& res, DownloadResult::RESULT r);
  249. void dependsOn(const SharedHandle<Dependency>& dep);
  250. bool isDependencyResolved();
  251. void releaseRuntimeResource(DownloadEngine* e);
  252. void postDownloadProcessing(std::deque<SharedHandle<RequestGroup> >& groups);
  253. void addPostDownloadHandler(const SharedHandle<PostDownloadHandler>& handler);
  254. void clearPostDowloadHandler();
  255. void preDownloadProcessing();
  256. void addPreDownloadHandler(const SharedHandle<PreDownloadHandler>& handler);
  257. void clearPreDowloadHandler();
  258. void processCheckIntegrityEntry(std::deque<Command*>& commands,
  259. const SharedHandle<CheckIntegrityEntry>& entry,
  260. DownloadEngine* e);
  261. void initPieceStorage();
  262. bool downloadFinishedByFileLength();
  263. void loadAndOpenFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  264. void shouldCancelDownloadForSafety();
  265. void adjustFilename(const SharedHandle<BtProgressInfoFile>& infoFile);
  266. SharedHandle<DownloadResult> createDownloadResult() const;
  267. const Option* getOption() const
  268. {
  269. return _option;
  270. }
  271. bool isSingleHostMultiConnectionEnabled() const
  272. {
  273. return _singleHostMultiConnectionEnabled;
  274. }
  275. void setSingleHostMultiConnectionEnabled(bool f)
  276. {
  277. _singleHostMultiConnectionEnabled = f;
  278. }
  279. /**
  280. * Registers given ServerHost.
  281. */
  282. void registerServerHost(const SharedHandle<ServerHost>& serverHost);
  283. /**
  284. * Returns ServerHost whose cuid is given cuid. If it is not found, returns
  285. * 0.
  286. */
  287. SharedHandle<ServerHost> searchServerHost(int32_t cuid) const;
  288. SharedHandle<ServerHost> searchServerHost(const std::string& hostname) const;
  289. void removeServerHost(int32_t cuid);
  290. void removeURIWhoseHostnameIs(const std::string& hostname);
  291. void removeIdenticalURI(const std::string& uri);
  292. void reportDownloadFinished();
  293. const std::deque<std::string>& getAcceptFeatures() const;
  294. void addAcceptFeatureHeader(const std::string& feature);
  295. void removeAcceptFeatureHeader(const std::string& feature);
  296. const std::deque<std::string>& getAcceptTypes() const;
  297. void addAcceptType(const std::string& type);
  298. void removeAcceptType(const std::string& type);
  299. static const std::string ACCEPT_METALINK;
  300. void setURISelector(const SharedHandle<URISelector>& uriSelector);
  301. void applyLastModifiedTimeToLocalFiles();
  302. void updateLastModifiedTime(const Time& time);
  303. void increaseAndValidateFileNotFoundCount();
  304. // Just set inMemoryDownload flag true.
  305. void markInMemoryDownload();
  306. // Returns inMemoryDownload flag.
  307. bool inMemoryDownload() const;
  308. void tuneDownloadCommand(DownloadCommand* command);
  309. void setTimeout(time_t timeout);
  310. time_t getTimeout() const;
  311. void setMaxTries(unsigned int maxTries);
  312. unsigned int getMaxTries() const;
  313. // Returns true if current download speed exceeds
  314. // _maxDownloadSpeedLimit. Always returns false if
  315. // _maxDownloadSpeedLimit == 0. Otherwise returns false.
  316. bool doesDownloadSpeedExceed();
  317. // Returns true if current upload speed exceeds
  318. // _maxUploadSpeedLimit. Always returns false if
  319. // _maxUploadSpeedLimit == 0. Otherwise returns false.
  320. bool doesUploadSpeedExceed();
  321. unsigned int getMaxDownloadSpeedLimit() const
  322. {
  323. return _maxDownloadSpeedLimit;
  324. }
  325. unsigned int getMaxUploadSpeedLimit() const
  326. {
  327. return _maxUploadSpeedLimit;
  328. }
  329. };
  330. typedef SharedHandle<RequestGroup> RequestGroupHandle;
  331. typedef WeakHandle<RequestGroup> RequestGroupWeakHandle;
  332. typedef std::deque<RequestGroupHandle> RequestGroups;
  333. } // namespace aria2
  334. #endif // _D_REQUEST_GROUP_H_