RequestGroup.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 <vector>
  41. #include "SharedHandle.h"
  42. #include "TransferStat.h"
  43. #include "TimeA2.h"
  44. #include "Request.h"
  45. #include "DownloadResultCode.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 URISelector;
  65. class URIResult;
  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. size_t _numConcurrentCommand;
  82. /**
  83. * This is the number of connections used in streaming protocol(http/ftp)
  84. */
  85. unsigned int _numStreamConnection;
  86. unsigned int _numCommand;
  87. SharedHandle<SegmentMan> _segmentMan;
  88. SharedHandle<SegmentManFactory> _segmentManFactory;
  89. SharedHandle<DownloadContext> _downloadContext;
  90. SharedHandle<PieceStorage> _pieceStorage;
  91. bool _saveControlFile;
  92. SharedHandle<BtProgressInfoFile> _progressInfoFile;
  93. SharedHandle<DiskWriterFactory> _diskWriterFactory;
  94. SharedHandle<Dependency> _dependency;
  95. bool _fileAllocationEnabled;
  96. bool _preLocalFileCheckEnabled;
  97. bool _haltRequested;
  98. bool _forceHaltRequested;
  99. HaltReason _haltReason;
  100. std::deque<SharedHandle<PreDownloadHandler> > _preDownloadHandlers;
  101. std::deque<SharedHandle<PostDownloadHandler> > _postDownloadHandlers;
  102. std::deque<std::string> _acceptTypes;
  103. SharedHandle<URISelector> _uriSelector;
  104. Time _lastModifiedTime;
  105. unsigned int _fileNotFoundCount;
  106. // Timeout used for HTTP/FTP downloads.
  107. time_t _timeout;
  108. #ifdef ENABLE_BITTORRENT
  109. WeakHandle<BtRuntime> _btRuntime;
  110. WeakHandle<PeerStorage> _peerStorage;
  111. #endif // ENABLE_BITTORRENT
  112. // This flag just indicates that the downloaded file is not saved disk but
  113. // just sits in memory.
  114. bool _inMemoryDownload;
  115. unsigned int _maxDownloadSpeedLimit;
  116. unsigned int _maxUploadSpeedLimit;
  117. SharedHandle<URIResult> _lastUriResult;
  118. // If this download generates another downloads when completed(for
  119. // example, downloads generated by PostDownloadHandler), this field
  120. // has the GID of generated RequestGroups. empty list means there is
  121. // no such RequestGroup.
  122. std::vector<int32_t> _followedByGIDs;
  123. // If this download is a part of another download(for example,
  124. // downloading torrent file described in Metalink file), this field
  125. // has the GID of parent RequestGroup. 0 means this is a parent
  126. // RequestGroup.
  127. int32_t _belongsToGID;
  128. Logger* _logger;
  129. void validateFilename(const std::string& expectedFilename,
  130. const std::string& actualFilename) const;
  131. void initializePreDownloadHandler();
  132. void initializePostDownloadHandler();
  133. bool tryAutoFileRenaming();
  134. // Returns the result code of this RequestGroup. If the download
  135. // finished, then returns downloadresultcode::FINISHED. If the
  136. // download didn't finish and error result is available in
  137. // _uriResults, then last result code is returned. Otherwise
  138. // returns downloadresultcode::UNKNOWN_ERROR.
  139. downloadresultcode::RESULT downloadResult() const;
  140. public:
  141. // The copy of option is stored in RequestGroup object.
  142. RequestGroup(const SharedHandle<Option>& option);
  143. ~RequestGroup();
  144. /**
  145. * Reinitializes SegmentMan based on current property values and
  146. * returns new one.
  147. */
  148. const SharedHandle<SegmentMan>& initSegmentMan();
  149. const SharedHandle<SegmentMan>& getSegmentMan() const
  150. {
  151. return _segmentMan;
  152. }
  153. // Returns first bootstrap commands to initiate a download.
  154. // If this is HTTP/FTP download and file size is unknown, only 1 command
  155. // (usually, HttpInitiateConnection or FtpInitiateConnection) will be created.
  156. void createInitialCommand(std::deque<Command*>& commands,
  157. DownloadEngine* e);
  158. void createNextCommandWithAdj(std::deque<Command*>& commands,
  159. DownloadEngine* e, int numAdj);
  160. void createNextCommand(std::deque<Command*>& commands,
  161. DownloadEngine* e, unsigned int numCommand);
  162. bool downloadFinished() const;
  163. bool allDownloadFinished() const;
  164. void closeFile();
  165. std::string getFirstFilePath() const;
  166. uint64_t getTotalLength() const;
  167. uint64_t getCompletedLength() const;
  168. /**
  169. * Compares expected filename with specified actualFilename.
  170. * The expected filename refers to FileEntry::getBasename() of the first
  171. * element of DownloadContext::getFileEntries()
  172. */
  173. void validateFilename(const std::string& actualFilename) const;
  174. void validateTotalLength(uint64_t expectedTotalLength,
  175. uint64_t actualTotalLength) const;
  176. void validateTotalLength(uint64_t actualTotalLength) const;
  177. void setSegmentManFactory(const SharedHandle<SegmentManFactory>& segmentManFactory);
  178. void setNumConcurrentCommand(unsigned int num)
  179. {
  180. _numConcurrentCommand = num;
  181. }
  182. unsigned int getNumConcurrentCommand() const
  183. {
  184. return _numConcurrentCommand;
  185. }
  186. int32_t getGID() const
  187. {
  188. return _gid;
  189. }
  190. TransferStat calculateStat();
  191. const SharedHandle<DownloadContext>& getDownloadContext() const
  192. {
  193. return _downloadContext;
  194. }
  195. // This function also calls
  196. // downloadContext->setOwnerRequestGroup(this).
  197. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  198. const SharedHandle<PieceStorage>& getPieceStorage() const
  199. {
  200. return _pieceStorage;
  201. }
  202. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  203. void setProgressInfoFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  204. void increaseStreamConnection();
  205. void decreaseStreamConnection();
  206. // Returns the number of connections used in HTTP(S)/FTP.
  207. unsigned int getNumStreamConnection() { return _numStreamConnection; }
  208. unsigned int getNumConnection() const;
  209. void increaseNumCommand();
  210. void decreaseNumCommand();
  211. unsigned int getNumCommand() const
  212. {
  213. return _numCommand;
  214. }
  215. // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
  216. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  217. const SharedHandle<DiskWriterFactory>& getDiskWriterFactory() const
  218. {
  219. return _diskWriterFactory;
  220. }
  221. void setFileAllocationEnabled(bool f)
  222. {
  223. _fileAllocationEnabled = f;
  224. }
  225. bool isFileAllocationEnabled() const
  226. {
  227. return _fileAllocationEnabled;
  228. }
  229. bool needsFileAllocation() const;
  230. /**
  231. * Setting _preLocalFileCheckEnabled to false, then skip the check to see
  232. * if a file is already exists and control file exists etc.
  233. * Always open file with DiskAdaptor::initAndOpenFile()
  234. */
  235. void setPreLocalFileCheckEnabled(bool f)
  236. {
  237. _preLocalFileCheckEnabled = f;
  238. }
  239. bool isPreLocalFileCheckEnabled() const
  240. {
  241. return _preLocalFileCheckEnabled;
  242. }
  243. void setHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
  244. void setForceHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
  245. bool isHaltRequested() const
  246. {
  247. return _haltRequested;
  248. }
  249. bool isForceHaltRequested() const
  250. {
  251. return _forceHaltRequested;
  252. }
  253. void dependsOn(const SharedHandle<Dependency>& dep);
  254. bool isDependencyResolved();
  255. void releaseRuntimeResource(DownloadEngine* e);
  256. void postDownloadProcessing(std::deque<SharedHandle<RequestGroup> >& groups);
  257. void addPostDownloadHandler(const SharedHandle<PostDownloadHandler>& handler);
  258. void clearPostDownloadHandler();
  259. void preDownloadProcessing();
  260. void addPreDownloadHandler(const SharedHandle<PreDownloadHandler>& handler);
  261. void clearPreDownloadHandler();
  262. void processCheckIntegrityEntry(std::deque<Command*>& commands,
  263. const SharedHandle<CheckIntegrityEntry>& entry,
  264. DownloadEngine* e);
  265. void initPieceStorage();
  266. bool downloadFinishedByFileLength();
  267. void loadAndOpenFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  268. void shouldCancelDownloadForSafety();
  269. void adjustFilename(const SharedHandle<BtProgressInfoFile>& infoFile);
  270. SharedHandle<DownloadResult> createDownloadResult() const;
  271. const SharedHandle<Option>& getOption() const
  272. {
  273. return _option;
  274. }
  275. void reportDownloadFinished();
  276. const std::deque<std::string>& getAcceptTypes() const
  277. {
  278. return _acceptTypes;
  279. }
  280. void addAcceptType(const std::string& type);
  281. void removeAcceptType(const std::string& type);
  282. static const std::string ACCEPT_METALINK;
  283. void setURISelector(const SharedHandle<URISelector>& uriSelector);
  284. const SharedHandle<URISelector>& getURISelector() const
  285. {
  286. return _uriSelector;
  287. }
  288. void applyLastModifiedTimeToLocalFiles();
  289. void updateLastModifiedTime(const Time& time);
  290. void increaseAndValidateFileNotFoundCount();
  291. // Just set inMemoryDownload flag true.
  292. void markInMemoryDownload();
  293. // Returns inMemoryDownload flag.
  294. bool inMemoryDownload() const
  295. {
  296. return _inMemoryDownload;
  297. }
  298. void setTimeout(time_t timeout);
  299. time_t getTimeout() const
  300. {
  301. return _timeout;
  302. }
  303. // Returns true if current download speed exceeds
  304. // _maxDownloadSpeedLimit. Always returns false if
  305. // _maxDownloadSpeedLimit == 0. Otherwise returns false.
  306. bool doesDownloadSpeedExceed();
  307. // Returns true if current upload speed exceeds
  308. // _maxUploadSpeedLimit. Always returns false if
  309. // _maxUploadSpeedLimit == 0. Otherwise returns false.
  310. bool doesUploadSpeedExceed();
  311. unsigned int getMaxDownloadSpeedLimit() const
  312. {
  313. return _maxDownloadSpeedLimit;
  314. }
  315. void setMaxDownloadSpeedLimit(unsigned int speed)
  316. {
  317. _maxDownloadSpeedLimit = speed;
  318. }
  319. unsigned int getMaxUploadSpeedLimit() const
  320. {
  321. return _maxUploadSpeedLimit;
  322. }
  323. void setMaxUploadSpeedLimit(unsigned int speed)
  324. {
  325. _maxUploadSpeedLimit = speed;
  326. }
  327. void setLastUriResult(std::string uri, downloadresultcode::RESULT result);
  328. void saveControlFile() const;
  329. void removeControlFile() const;
  330. void enableSaveControlFile() { _saveControlFile = true; }
  331. void disableSaveControlFile() { _saveControlFile = false; }
  332. template<typename InputIterator>
  333. void followedBy(InputIterator groupFirst, InputIterator groupLast)
  334. {
  335. _followedByGIDs.clear();
  336. for(; groupFirst != groupLast; ++groupFirst) {
  337. _followedByGIDs.push_back((*groupFirst)->getGID());
  338. }
  339. }
  340. const std::vector<int32_t>& followedBy() const
  341. {
  342. return _followedByGIDs;
  343. }
  344. void belongsTo(int32_t gid)
  345. {
  346. _belongsToGID = gid;
  347. }
  348. int32_t belongsTo() const
  349. {
  350. return _belongsToGID;
  351. }
  352. static void resetGIDCounter() { _gidCounter = 0; }
  353. static int32_t newGID();
  354. };
  355. typedef SharedHandle<RequestGroup> RequestGroupHandle;
  356. typedef WeakHandle<RequestGroup> RequestGroupWeakHandle;
  357. typedef std::deque<RequestGroupHandle> RequestGroups;
  358. } // namespace aria2
  359. #endif // _D_REQUEST_GROUP_H_