RequestGroup.h 13 KB

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