RequestGroup.h 14 KB

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