RequestGroup.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 <memory>
  42. #include "TransferStat.h"
  43. #include "TimeA2.h"
  44. #include "Request.h"
  45. #include "error_code.h"
  46. #include "MetadataInfo.h"
  47. #include "GroupId.h"
  48. namespace aria2 {
  49. class DownloadEngine;
  50. class SegmentMan;
  51. class Command;
  52. class DownloadCommand;
  53. class DownloadContext;
  54. class PieceStorage;
  55. class BtProgressInfoFile;
  56. class Dependency;
  57. class PreDownloadHandler;
  58. class PostDownloadHandler;
  59. class DiskWriterFactory;
  60. class Option;
  61. class RequestGroup;
  62. class CheckIntegrityEntry;
  63. struct DownloadResult;
  64. class URISelector;
  65. class URIResult;
  66. class RequestGroupMan;
  67. #ifdef ENABLE_BITTORRENT
  68. class BtRuntime;
  69. class PeerStorage;
  70. #endif // ENABLE_BITTORRENT
  71. class RequestGroup {
  72. public:
  73. enum HaltReason {
  74. NONE,
  75. SHUTDOWN_SIGNAL,
  76. USER_REQUEST
  77. };
  78. enum State {
  79. // Waiting in the reserved queue
  80. STATE_WAITING,
  81. // Download has begun
  82. STATE_ACTIVE
  83. };
  84. private:
  85. // If this download is a part of another download(for example,
  86. // downloading torrent file described in Metalink file), this field
  87. // has the GID of parent RequestGroup. 0 means this is a parent
  88. // RequestGroup.
  89. a2_gid_t belongsToGID_;
  90. std::shared_ptr<GroupId> gid_;
  91. std::shared_ptr<Option> option_;
  92. std::shared_ptr<SegmentMan> segmentMan_;
  93. std::shared_ptr<DownloadContext> downloadContext_;
  94. std::shared_ptr<PieceStorage> pieceStorage_;
  95. std::shared_ptr<BtProgressInfoFile> progressInfoFile_;
  96. std::shared_ptr<DiskWriterFactory> diskWriterFactory_;
  97. std::shared_ptr<Dependency> dependency_;
  98. std::unique_ptr<URISelector> uriSelector_;
  99. std::shared_ptr<MetadataInfo> metadataInfo_;
  100. RequestGroupMan* requestGroupMan_;
  101. #ifdef ENABLE_BITTORRENT
  102. BtRuntime* btRuntime_;
  103. PeerStorage* peerStorage_;
  104. #endif // ENABLE_BITTORRENT
  105. // If this download generates another downloads when completed(for
  106. // example, downloads generated by PostDownloadHandler), this field
  107. // has the GID of generated RequestGroups. empty list means there is
  108. // no such RequestGroup.
  109. std::vector<a2_gid_t> followedByGIDs_;
  110. std::vector<const PreDownloadHandler*> preDownloadHandlers_;
  111. std::vector<const PostDownloadHandler*> postDownloadHandlers_;
  112. Time lastModifiedTime_;
  113. // Timeout used for HTTP/FTP downloads.
  114. std::chrono::seconds timeout_;
  115. int state_;
  116. int numConcurrentCommand_;
  117. /**
  118. * This is the number of connections used in streaming protocol(http/ftp)
  119. */
  120. int numStreamConnection_;
  121. int numStreamCommand_;
  122. int numCommand_;
  123. int fileNotFoundCount_;
  124. int maxDownloadSpeedLimit_;
  125. int maxUploadSpeedLimit_;
  126. int resumeFailureCount_;
  127. HaltReason haltReason_;
  128. error_code::Value lastErrorCode_;
  129. bool saveControlFile_;
  130. bool fileAllocationEnabled_;
  131. bool preLocalFileCheckEnabled_;
  132. bool haltRequested_;
  133. bool forceHaltRequested_;
  134. bool pauseRequested_;
  135. // This flag just indicates that the downloaded file is not saved disk but
  136. // just sits in memory.
  137. bool inMemoryDownload_;
  138. bool seedOnly_;
  139. void validateFilename(const std::string& expectedFilename,
  140. const std::string& actualFilename) const;
  141. void initializePreDownloadHandler();
  142. void initializePostDownloadHandler();
  143. void tryAutoFileRenaming();
  144. // Returns the result code of this RequestGroup. If the download
  145. // finished, then returns error_code::FINISHED. If the
  146. // download didn't finish and error result is available in
  147. // _uriResults, then last result code is returned. Otherwise
  148. // returns error_code::UNKNOWN_ERROR.
  149. error_code::Value downloadResult() const;
  150. void removeDefunctControlFile
  151. (const std::shared_ptr<BtProgressInfoFile>& progressInfoFile);
  152. public:
  153. RequestGroup(const std::shared_ptr<GroupId>& gid,
  154. const std::shared_ptr<Option>& option);
  155. ~RequestGroup();
  156. bool isCheckIntegrityReady();
  157. const std::shared_ptr<SegmentMan>& getSegmentMan() const
  158. {
  159. return segmentMan_;
  160. }
  161. std::unique_ptr<CheckIntegrityEntry> createCheckIntegrityEntry();
  162. // Returns first bootstrap commands to initiate a download.
  163. // If this is HTTP/FTP download and file size is unknown, only 1 command
  164. // (usually, HttpInitiateConnection or FtpInitiateConnection) will be created.
  165. void createInitialCommand(std::vector<std::unique_ptr<Command>>& commands,
  166. DownloadEngine* e);
  167. void createNextCommandWithAdj(std::vector<std::unique_ptr<Command>>& commands,
  168. DownloadEngine* e, int numAdj);
  169. void createNextCommand(std::vector<std::unique_ptr<Command>>& commands,
  170. DownloadEngine* e, int numCommand);
  171. void createNextCommand(std::vector<std::unique_ptr<Command>>& commands,
  172. DownloadEngine* e);
  173. bool downloadFinished() const;
  174. bool allDownloadFinished() const;
  175. void closeFile();
  176. std::string getFirstFilePath() const;
  177. int64_t getTotalLength() const;
  178. int64_t getCompletedLength() const;
  179. inline int64_t getPendingLength() const
  180. {
  181. return getTotalLength() - getCompletedLength();
  182. }
  183. /**
  184. * Compares expected filename with specified actualFilename.
  185. * The expected filename refers to FileEntry::getBasename() of the first
  186. * element of DownloadContext::getFileEntries()
  187. */
  188. void validateFilename(const std::string& actualFilename) const;
  189. void validateTotalLength(int64_t expectedTotalLength,
  190. int64_t actualTotalLength) const;
  191. void validateTotalLength(int64_t actualTotalLength) const;
  192. void setNumConcurrentCommand(int num)
  193. {
  194. numConcurrentCommand_ = num;
  195. }
  196. int getNumConcurrentCommand() const
  197. {
  198. return numConcurrentCommand_;
  199. }
  200. a2_gid_t getGID() const
  201. {
  202. return gid_->getNumericId();
  203. }
  204. const std::shared_ptr<GroupId>& getGroupId() const
  205. {
  206. return gid_;
  207. }
  208. TransferStat calculateStat() const;
  209. const std::shared_ptr<DownloadContext>& getDownloadContext() const
  210. {
  211. return downloadContext_;
  212. }
  213. // This function also calls
  214. // downloadContext->setOwnerRequestGroup(this).
  215. void setDownloadContext(const std::shared_ptr<DownloadContext>& downloadContext);
  216. const std::shared_ptr<PieceStorage>& getPieceStorage() const
  217. {
  218. return pieceStorage_;
  219. }
  220. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  221. void setProgressInfoFile
  222. (const std::shared_ptr<BtProgressInfoFile>& progressInfoFile);
  223. void increaseStreamCommand();
  224. void decreaseStreamCommand();
  225. void increaseStreamConnection();
  226. void decreaseStreamConnection();
  227. int getNumConnection() const;
  228. void increaseNumCommand();
  229. void decreaseNumCommand();
  230. int getNumCommand() const
  231. {
  232. return numCommand_;
  233. }
  234. // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
  235. void setDiskWriterFactory(const std::shared_ptr<DiskWriterFactory>& diskWriterFactory);
  236. const std::shared_ptr<DiskWriterFactory>& getDiskWriterFactory() const
  237. {
  238. return diskWriterFactory_;
  239. }
  240. void setFileAllocationEnabled(bool f)
  241. {
  242. fileAllocationEnabled_ = f;
  243. }
  244. bool isFileAllocationEnabled() const
  245. {
  246. return fileAllocationEnabled_;
  247. }
  248. bool needsFileAllocation() const;
  249. /**
  250. * Setting preLocalFileCheckEnabled_ to false, then skip the check to see
  251. * if a file is already exists and control file exists etc.
  252. * Always open file with DiskAdaptor::initAndOpenFile()
  253. */
  254. void setPreLocalFileCheckEnabled(bool f)
  255. {
  256. preLocalFileCheckEnabled_ = f;
  257. }
  258. bool isPreLocalFileCheckEnabled() const
  259. {
  260. return preLocalFileCheckEnabled_;
  261. }
  262. void setHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
  263. void setForceHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
  264. bool isHaltRequested() const
  265. {
  266. return haltRequested_;
  267. }
  268. bool isForceHaltRequested() const
  269. {
  270. return forceHaltRequested_;
  271. }
  272. void setPauseRequested(bool f);
  273. bool isPauseRequested() const
  274. {
  275. return pauseRequested_;
  276. }
  277. void dependsOn(const std::shared_ptr<Dependency>& dep);
  278. bool isDependencyResolved();
  279. void releaseRuntimeResource(DownloadEngine* e);
  280. void postDownloadProcessing(std::vector<std::shared_ptr<RequestGroup> >& groups);
  281. void addPostDownloadHandler(const PostDownloadHandler* handler);
  282. void clearPostDownloadHandler();
  283. void preDownloadProcessing();
  284. void addPreDownloadHandler(const PreDownloadHandler* handler);
  285. void clearPreDownloadHandler();
  286. void processCheckIntegrityEntry
  287. (std::vector<std::unique_ptr<Command>>& commands,
  288. std::unique_ptr<CheckIntegrityEntry> entry,
  289. DownloadEngine* e);
  290. // Initializes pieceStorage_ and segmentMan_. We guarantee that
  291. // either both of pieceStorage_ and segmentMan_ are initialized or
  292. // they are not.
  293. void initPieceStorage();
  294. void dropPieceStorage();
  295. bool downloadFinishedByFileLength();
  296. void loadAndOpenFile(const std::shared_ptr<BtProgressInfoFile>& progressInfoFile);
  297. void shouldCancelDownloadForSafety();
  298. void adjustFilename(const std::shared_ptr<BtProgressInfoFile>& infoFile);
  299. std::shared_ptr<DownloadResult> createDownloadResult() const;
  300. const std::shared_ptr<Option>& getOption() const
  301. {
  302. return option_;
  303. }
  304. void reportDownloadFinished();
  305. void setURISelector(std::unique_ptr<URISelector> uriSelector);
  306. const std::unique_ptr<URISelector>& getURISelector() const
  307. {
  308. return uriSelector_;
  309. }
  310. void applyLastModifiedTimeToLocalFiles();
  311. void updateLastModifiedTime(const Time& time);
  312. void increaseAndValidateFileNotFoundCount();
  313. // Just set inMemoryDownload flag true.
  314. void markInMemoryDownload();
  315. // Returns inMemoryDownload flag.
  316. bool inMemoryDownload() const
  317. {
  318. return inMemoryDownload_;
  319. }
  320. void setTimeout(std::chrono::seconds timeout);
  321. const std::chrono::seconds& getTimeout() const { return timeout_; }
  322. // Returns true if current download speed exceeds
  323. // maxDownloadSpeedLimit_. Always returns false if
  324. // maxDownloadSpeedLimit_ == 0. Otherwise returns false.
  325. bool doesDownloadSpeedExceed();
  326. // Returns true if current upload speed exceeds
  327. // maxUploadSpeedLimit_. Always returns false if
  328. // maxUploadSpeedLimit_ == 0. Otherwise returns false.
  329. bool doesUploadSpeedExceed();
  330. int getMaxDownloadSpeedLimit() const
  331. {
  332. return maxDownloadSpeedLimit_;
  333. }
  334. void setMaxDownloadSpeedLimit(int speed)
  335. {
  336. maxDownloadSpeedLimit_ = speed;
  337. }
  338. int getMaxUploadSpeedLimit() const
  339. {
  340. return maxUploadSpeedLimit_;
  341. }
  342. void setMaxUploadSpeedLimit(int speed)
  343. {
  344. maxUploadSpeedLimit_ = speed;
  345. }
  346. void setLastErrorCode(error_code::Value code)
  347. {
  348. lastErrorCode_ = code;
  349. }
  350. error_code::Value getLastErrorCode() const
  351. {
  352. return lastErrorCode_;
  353. }
  354. void saveControlFile() const;
  355. void removeControlFile() const;
  356. void enableSaveControlFile() { saveControlFile_ = true; }
  357. void disableSaveControlFile() { saveControlFile_ = false; }
  358. template<typename InputIterator>
  359. void followedBy(InputIterator groupFirst, InputIterator groupLast)
  360. {
  361. followedByGIDs_.clear();
  362. for(; groupFirst != groupLast; ++groupFirst) {
  363. followedByGIDs_.push_back((*groupFirst)->getGID());
  364. }
  365. }
  366. const std::vector<a2_gid_t>& followedBy() const
  367. {
  368. return followedByGIDs_;
  369. }
  370. void belongsTo(a2_gid_t gid)
  371. {
  372. belongsToGID_ = gid;
  373. }
  374. a2_gid_t belongsTo() const
  375. {
  376. return belongsToGID_;
  377. }
  378. void setRequestGroupMan(RequestGroupMan* requestGroupMan)
  379. {
  380. requestGroupMan_ = requestGroupMan;
  381. }
  382. RequestGroupMan* getRequestGroupMan()
  383. {
  384. return requestGroupMan_;
  385. }
  386. int getResumeFailureCount() const
  387. {
  388. return resumeFailureCount_;
  389. }
  390. void increaseResumeFailureCount()
  391. {
  392. ++resumeFailureCount_;
  393. }
  394. bool p2pInvolved() const;
  395. void setMetadataInfo(const std::shared_ptr<MetadataInfo>& info)
  396. {
  397. metadataInfo_ = info;
  398. }
  399. const std::shared_ptr<MetadataInfo>& getMetadataInfo() const
  400. {
  401. return metadataInfo_;
  402. }
  403. int getState() const
  404. {
  405. return state_;
  406. }
  407. void setState(int state)
  408. {
  409. state_ = state;
  410. }
  411. bool isSeedOnlyEnabled()
  412. {
  413. return seedOnly_;
  414. }
  415. void enableSeedOnly();
  416. };
  417. } // namespace aria2
  418. #endif // D_REQUEST_GROUP_H