RequestGroup.h 13 KB

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