RequestGroup.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <algorithm>
  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 Command;
  50. class DownloadCommand;
  51. class DownloadContext;
  52. class PieceStorage;
  53. class BtProgressInfoFile;
  54. class Dependency;
  55. class PreDownloadHandler;
  56. class PostDownloadHandler;
  57. class DiskWriterFactory;
  58. class Option;
  59. class Logger;
  60. class RequestGroup;
  61. class CheckIntegrityEntry;
  62. class DownloadResult;
  63. class URISelector;
  64. class URIResult;
  65. class RequestGroupMan;
  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<DownloadContext> _downloadContext;
  89. SharedHandle<PieceStorage> _pieceStorage;
  90. bool _saveControlFile;
  91. SharedHandle<BtProgressInfoFile> _progressInfoFile;
  92. SharedHandle<DiskWriterFactory> _diskWriterFactory;
  93. SharedHandle<Dependency> _dependency;
  94. bool _fileAllocationEnabled;
  95. bool _preLocalFileCheckEnabled;
  96. bool _haltRequested;
  97. bool _forceHaltRequested;
  98. HaltReason _haltReason;
  99. std::vector<SharedHandle<PreDownloadHandler> > _preDownloadHandlers;
  100. std::vector<SharedHandle<PostDownloadHandler> > _postDownloadHandlers;
  101. std::vector<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. #ifdef ENABLE_BITTORRENT
  108. WeakHandle<BtRuntime> _btRuntime;
  109. WeakHandle<PeerStorage> _peerStorage;
  110. #endif // ENABLE_BITTORRENT
  111. // This flag just indicates that the downloaded file is not saved disk but
  112. // just sits in memory.
  113. bool _inMemoryDownload;
  114. unsigned int _maxDownloadSpeedLimit;
  115. unsigned int _maxUploadSpeedLimit;
  116. SharedHandle<URIResult> _lastUriResult;
  117. // If this download generates another downloads when completed(for
  118. // example, downloads generated by PostDownloadHandler), this field
  119. // has the GID of generated RequestGroups. empty list means there is
  120. // no such RequestGroup.
  121. std::vector<int32_t> _followedByGIDs;
  122. // If this download is a part of another download(for example,
  123. // downloading torrent file described in Metalink file), this field
  124. // has the GID of parent RequestGroup. 0 means this is a parent
  125. // RequestGroup.
  126. int32_t _belongsToGID;
  127. RequestGroupMan* _requestGroupMan;
  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. void removeDefunctControlFile
  141. (const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  142. public:
  143. // The copy of option is stored in RequestGroup object.
  144. RequestGroup(const SharedHandle<Option>& option);
  145. ~RequestGroup();
  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. void createInitialCommand(std::vector<Command*>& commands,
  154. DownloadEngine* e);
  155. void createNextCommandWithAdj(std::vector<Command*>& commands,
  156. DownloadEngine* e, int numAdj);
  157. void createNextCommand(std::vector<Command*>& commands,
  158. DownloadEngine* e, unsigned int numCommand);
  159. void createNextCommand(std::vector<Command*>& commands, DownloadEngine* e);
  160. bool downloadFinished() const;
  161. bool allDownloadFinished() const;
  162. void closeFile();
  163. std::string getFirstFilePath() const;
  164. uint64_t getTotalLength() const;
  165. uint64_t getCompletedLength() const;
  166. /**
  167. * Compares expected filename with specified actualFilename.
  168. * The expected filename refers to FileEntry::getBasename() of the first
  169. * element of DownloadContext::getFileEntries()
  170. */
  171. void validateFilename(const std::string& actualFilename) const;
  172. void validateTotalLength(uint64_t expectedTotalLength,
  173. uint64_t actualTotalLength) const;
  174. void validateTotalLength(uint64_t actualTotalLength) const;
  175. void setNumConcurrentCommand(unsigned int num)
  176. {
  177. _numConcurrentCommand = num;
  178. }
  179. unsigned int getNumConcurrentCommand() const
  180. {
  181. return _numConcurrentCommand;
  182. }
  183. int32_t getGID() const
  184. {
  185. return _gid;
  186. }
  187. TransferStat calculateStat();
  188. const SharedHandle<DownloadContext>& getDownloadContext() const
  189. {
  190. return _downloadContext;
  191. }
  192. // This function also calls
  193. // downloadContext->setOwnerRequestGroup(this).
  194. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  195. const SharedHandle<PieceStorage>& getPieceStorage() const
  196. {
  197. return _pieceStorage;
  198. }
  199. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  200. void setProgressInfoFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  201. void increaseStreamConnection();
  202. void decreaseStreamConnection();
  203. // Returns the number of connections used in HTTP(S)/FTP.
  204. unsigned int getNumStreamConnection() { return _numStreamConnection; }
  205. unsigned int getNumConnection() const;
  206. void increaseNumCommand();
  207. void decreaseNumCommand();
  208. unsigned int getNumCommand() const
  209. {
  210. return _numCommand;
  211. }
  212. // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
  213. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  214. const SharedHandle<DiskWriterFactory>& getDiskWriterFactory() const
  215. {
  216. return _diskWriterFactory;
  217. }
  218. void setFileAllocationEnabled(bool f)
  219. {
  220. _fileAllocationEnabled = f;
  221. }
  222. bool isFileAllocationEnabled() const
  223. {
  224. return _fileAllocationEnabled;
  225. }
  226. bool needsFileAllocation() const;
  227. /**
  228. * Setting _preLocalFileCheckEnabled to false, then skip the check to see
  229. * if a file is already exists and control file exists etc.
  230. * Always open file with DiskAdaptor::initAndOpenFile()
  231. */
  232. void setPreLocalFileCheckEnabled(bool f)
  233. {
  234. _preLocalFileCheckEnabled = f;
  235. }
  236. bool isPreLocalFileCheckEnabled() const
  237. {
  238. return _preLocalFileCheckEnabled;
  239. }
  240. void setHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
  241. void setForceHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
  242. bool isHaltRequested() const
  243. {
  244. return _haltRequested;
  245. }
  246. bool isForceHaltRequested() const
  247. {
  248. return _forceHaltRequested;
  249. }
  250. void dependsOn(const SharedHandle<Dependency>& dep);
  251. bool isDependencyResolved();
  252. void releaseRuntimeResource(DownloadEngine* e);
  253. void postDownloadProcessing(std::vector<SharedHandle<RequestGroup> >& groups);
  254. void addPostDownloadHandler(const SharedHandle<PostDownloadHandler>& handler);
  255. void clearPostDownloadHandler();
  256. void preDownloadProcessing();
  257. void addPreDownloadHandler(const SharedHandle<PreDownloadHandler>& handler);
  258. void clearPreDownloadHandler();
  259. void processCheckIntegrityEntry(std::vector<Command*>& commands,
  260. const SharedHandle<CheckIntegrityEntry>& entry,
  261. DownloadEngine* e);
  262. // Initializes _pieceStorage and _segmentMan. We guarantee that
  263. // either both of _pieceStorage and _segmentMan are initialized or
  264. // they are not.
  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::vector<std::string>& getAcceptTypes() const
  277. {
  278. return _acceptTypes;
  279. }
  280. void addAcceptType(const std::string& type);
  281. template<typename InputIterator>
  282. void addAcceptType(InputIterator first, InputIterator last)
  283. {
  284. for(; first != last; ++first) {
  285. if(std::find(_acceptTypes.begin(), _acceptTypes.end(), *first) ==
  286. _acceptTypes.end()) {
  287. _acceptTypes.push_back(*first);
  288. }
  289. }
  290. }
  291. void removeAcceptType(const std::string& type);
  292. void setURISelector(const SharedHandle<URISelector>& uriSelector);
  293. const SharedHandle<URISelector>& getURISelector() const
  294. {
  295. return _uriSelector;
  296. }
  297. void applyLastModifiedTimeToLocalFiles();
  298. void updateLastModifiedTime(const Time& time);
  299. void increaseAndValidateFileNotFoundCount();
  300. // Just set inMemoryDownload flag true.
  301. void markInMemoryDownload();
  302. // Returns inMemoryDownload flag.
  303. bool inMemoryDownload() const
  304. {
  305. return _inMemoryDownload;
  306. }
  307. void setTimeout(time_t timeout);
  308. time_t getTimeout() const
  309. {
  310. return _timeout;
  311. }
  312. // Returns true if current download speed exceeds
  313. // _maxDownloadSpeedLimit. Always returns false if
  314. // _maxDownloadSpeedLimit == 0. Otherwise returns false.
  315. bool doesDownloadSpeedExceed();
  316. // Returns true if current upload speed exceeds
  317. // _maxUploadSpeedLimit. Always returns false if
  318. // _maxUploadSpeedLimit == 0. Otherwise returns false.
  319. bool doesUploadSpeedExceed();
  320. unsigned int getMaxDownloadSpeedLimit() const
  321. {
  322. return _maxDownloadSpeedLimit;
  323. }
  324. void setMaxDownloadSpeedLimit(unsigned int speed)
  325. {
  326. _maxDownloadSpeedLimit = speed;
  327. }
  328. unsigned int getMaxUploadSpeedLimit() const
  329. {
  330. return _maxUploadSpeedLimit;
  331. }
  332. void setMaxUploadSpeedLimit(unsigned int speed)
  333. {
  334. _maxUploadSpeedLimit = speed;
  335. }
  336. void setLastUriResult(std::string uri, downloadresultcode::RESULT result);
  337. void saveControlFile() const;
  338. void removeControlFile() const;
  339. void enableSaveControlFile() { _saveControlFile = true; }
  340. void disableSaveControlFile() { _saveControlFile = false; }
  341. template<typename InputIterator>
  342. void followedBy(InputIterator groupFirst, InputIterator groupLast)
  343. {
  344. _followedByGIDs.clear();
  345. for(; groupFirst != groupLast; ++groupFirst) {
  346. _followedByGIDs.push_back((*groupFirst)->getGID());
  347. }
  348. }
  349. const std::vector<int32_t>& followedBy() const
  350. {
  351. return _followedByGIDs;
  352. }
  353. void belongsTo(int32_t gid)
  354. {
  355. _belongsToGID = gid;
  356. }
  357. int32_t belongsTo() const
  358. {
  359. return _belongsToGID;
  360. }
  361. void setRequestGroupMan(RequestGroupMan* requestGroupMan)
  362. {
  363. _requestGroupMan = requestGroupMan;
  364. }
  365. static void resetGIDCounter() { _gidCounter = 0; }
  366. static int32_t newGID();
  367. };
  368. } // namespace aria2
  369. #endif // _D_REQUEST_GROUP_H_