RequestGroup.h 14 KB

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