RequestGroup.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <deque>
  40. #include "SharedHandle.h"
  41. #include "TransferStat.h"
  42. #include "TimeA2.h"
  43. namespace aria2 {
  44. class DownloadEngine;
  45. class SegmentMan;
  46. class SegmentManFactory;
  47. class Command;
  48. class DownloadContext;
  49. class PieceStorage;
  50. class BtProgressInfoFile;
  51. class Dependency;
  52. class PreDownloadHandler;
  53. class PostDownloadHandler;
  54. class DiskWriterFactory;
  55. class Option;
  56. class Logger;
  57. class RequestGroup;
  58. class CheckIntegrityEntry;
  59. class DownloadResult;
  60. class ServerHost;
  61. class URISelector;
  62. #ifdef ENABLE_BITTORRENT
  63. class BtRuntime;
  64. class PeerStorage;
  65. #endif // ENABLE_BITTORRENT
  66. class RequestGroup {
  67. private:
  68. static int32_t _gidCounter;
  69. int32_t _gid;
  70. std::deque<std::string> _uris;
  71. std::deque<std::string> _spentUris;
  72. unsigned int _numConcurrentCommand;
  73. /**
  74. * This is the number of connections used in streaming protocol(http/ftp)
  75. */
  76. unsigned int _numStreamConnection;
  77. unsigned int _numCommand;
  78. SharedHandle<SegmentMan> _segmentMan;
  79. SharedHandle<SegmentManFactory> _segmentManFactory;
  80. SharedHandle<DownloadContext> _downloadContext;
  81. SharedHandle<PieceStorage> _pieceStorage;
  82. SharedHandle<BtProgressInfoFile> _progressInfoFile;
  83. SharedHandle<DiskWriterFactory> _diskWriterFactory;
  84. SharedHandle<Dependency> _dependency;
  85. std::deque<SharedHandle<ServerHost> > _serverHosts;
  86. bool _fileAllocationEnabled;
  87. bool _preLocalFileCheckEnabled;
  88. bool _haltRequested;
  89. bool _forceHaltRequested;
  90. bool _singleHostMultiConnectionEnabled;
  91. std::deque<SharedHandle<PreDownloadHandler> > _preDownloadHandlers;
  92. std::deque<SharedHandle<PostDownloadHandler> > _postDownloadHandlers;
  93. std::deque<std::string> _acceptFeatures;
  94. std::deque<std::string> _acceptTypes;
  95. SharedHandle<URISelector> _uriSelector;
  96. Time _lastModifiedTime;
  97. unsigned int _fileNotFoundCount;
  98. #ifdef ENABLE_BITTORRENT
  99. WeakHandle<BtRuntime> _btRuntime;
  100. WeakHandle<PeerStorage> _peerStorage;
  101. #endif // ENABLE_BITTORRENT
  102. // This flag just indicates that the downloaded file is not saved disk but
  103. // just sits in memory.
  104. bool _inMemoryDownload;
  105. const Option* _option;
  106. Logger* _logger;
  107. void validateFilename(const std::string& expectedFilename,
  108. const std::string& actualFilename) const;
  109. void validateTotalLength(uint64_t expectedTotalLength,
  110. uint64_t actualTotalLength) const;
  111. void initializePreDownloadHandler();
  112. void initializePostDownloadHandler();
  113. bool tryAutoFileRenaming();
  114. public:
  115. RequestGroup(const Option* option, const std::deque<std::string>& uris);
  116. ~RequestGroup();
  117. /**
  118. * Reinitializes SegmentMan based on current property values and
  119. * returns new one.
  120. */
  121. SharedHandle<SegmentMan> initSegmentMan();
  122. SharedHandle<SegmentMan> getSegmentMan() const;
  123. void createInitialCommand(std::deque<Command*>& commands,
  124. DownloadEngine* e);
  125. void createNextCommandWithAdj(std::deque<Command*>& commands,
  126. DownloadEngine* e, int numAdj);
  127. void createNextCommand(std::deque<Command*>& commands,
  128. DownloadEngine* e, unsigned int numCommand,
  129. const std::string& method = "GET");
  130. void addURI(const std::string& uri)
  131. {
  132. _uris.push_back(uri);
  133. }
  134. bool downloadFinished() const;
  135. bool allDownloadFinished() const;
  136. void closeFile();
  137. std::string getFilePath() const;
  138. uint64_t getTotalLength() const;
  139. uint64_t getCompletedLength() const;
  140. const std::deque<std::string>& getRemainingUris() const
  141. {
  142. return _uris;
  143. }
  144. const std::deque<std::string>& getSpentUris() const
  145. {
  146. return _spentUris;
  147. }
  148. void getURIs(std::deque<std::string>& uris) const;
  149. /**
  150. * Compares expected filename with specified actualFilename.
  151. * The expected filename refers to FileEntry::getBasename() of the first
  152. * element of DownloadContext::getFileEntries()
  153. */
  154. void validateFilename(const std::string& actualFilename) const;
  155. void validateTotalLength(uint64_t actualTotalLength) const;
  156. void setSegmentManFactory(const SharedHandle<SegmentManFactory>& segmentManFactory);
  157. void setNumConcurrentCommand(unsigned int num)
  158. {
  159. _numConcurrentCommand = num;
  160. }
  161. unsigned int getNumConcurrentCommand() const;
  162. int32_t getGID() const
  163. {
  164. return _gid;
  165. }
  166. TransferStat calculateStat();
  167. SharedHandle<DownloadContext> getDownloadContext() const;
  168. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  169. SharedHandle<PieceStorage> getPieceStorage() const;
  170. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  171. SharedHandle<BtProgressInfoFile> getProgressInfoFile() const;
  172. void setProgressInfoFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  173. void increaseStreamConnection();
  174. void decreaseStreamConnection();
  175. unsigned int getNumConnection() const;
  176. void increaseNumCommand();
  177. void decreaseNumCommand();
  178. unsigned int getNumCommand() const
  179. {
  180. return _numCommand;
  181. }
  182. // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
  183. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  184. SharedHandle<DiskWriterFactory> getDiskWriterFactory() const;
  185. void setFileAllocationEnabled(bool f)
  186. {
  187. _fileAllocationEnabled = f;
  188. }
  189. bool isFileAllocationEnabled() const
  190. {
  191. return _fileAllocationEnabled;
  192. }
  193. bool needsFileAllocation() const;
  194. /**
  195. * Setting _preLocalFileCheckEnabled to false, then skip the check to see
  196. * if a file is already exists and control file exists etc.
  197. * Always open file with DiskAdaptor::initAndOpenFile()
  198. */
  199. void setPreLocalFileCheckEnabled(bool f)
  200. {
  201. _preLocalFileCheckEnabled = f;
  202. }
  203. bool isPreLocalFileCheckEnabled() const
  204. {
  205. return _preLocalFileCheckEnabled;
  206. }
  207. void setHaltRequested(bool f);
  208. void setForceHaltRequested(bool f);
  209. bool isHaltRequested() const
  210. {
  211. return _haltRequested;
  212. }
  213. bool isForceHaltRequested() const
  214. {
  215. return _forceHaltRequested;
  216. }
  217. void dependsOn(const SharedHandle<Dependency>& dep);
  218. bool isDependencyResolved();
  219. void releaseRuntimeResource(DownloadEngine* e);
  220. void postDownloadProcessing(std::deque<SharedHandle<RequestGroup> >& groups);
  221. void addPostDownloadHandler(const SharedHandle<PostDownloadHandler>& handler);
  222. void clearPostDowloadHandler();
  223. void preDownloadProcessing();
  224. void addPreDownloadHandler(const SharedHandle<PreDownloadHandler>& handler);
  225. void clearPreDowloadHandler();
  226. void processCheckIntegrityEntry(std::deque<Command*>& commands,
  227. const SharedHandle<CheckIntegrityEntry>& entry,
  228. DownloadEngine* e);
  229. void initPieceStorage();
  230. bool downloadFinishedByFileLength();
  231. void loadAndOpenFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
  232. void shouldCancelDownloadForSafety();
  233. SharedHandle<DownloadResult> createDownloadResult() const;
  234. const Option* getOption() const
  235. {
  236. return _option;
  237. }
  238. bool isSingleHostMultiConnectionEnabled() const
  239. {
  240. return _singleHostMultiConnectionEnabled;
  241. }
  242. void setSingleHostMultiConnectionEnabled(bool f)
  243. {
  244. _singleHostMultiConnectionEnabled = f;
  245. }
  246. /**
  247. * Registers given ServerHost.
  248. */
  249. void registerServerHost(const SharedHandle<ServerHost>& serverHost);
  250. /**
  251. * Returns ServerHost whose cuid is given cuid. If it is not found, returns
  252. * 0.
  253. */
  254. SharedHandle<ServerHost> searchServerHost(int32_t cuid) const;
  255. SharedHandle<ServerHost> searchServerHost(const std::string& hostname) const;
  256. void removeServerHost(int32_t cuid);
  257. void removeURIWhoseHostnameIs(const std::string& hostname);
  258. void removeIdenticalURI(const std::string& uri);
  259. void reportDownloadFinished();
  260. const std::deque<std::string>& getAcceptFeatures() const;
  261. void addAcceptFeatureHeader(const std::string& feature);
  262. void removeAcceptFeatureHeader(const std::string& feature);
  263. const std::deque<std::string>& getAcceptTypes() const;
  264. void addAcceptType(const std::string& type);
  265. void removeAcceptType(const std::string& type);
  266. static const std::string ACCEPT_METALINK;
  267. void setURISelector(const SharedHandle<URISelector>& uriSelector);
  268. void applyLastModifiedTimeToLocalFiles();
  269. void updateLastModifiedTime(const Time& time);
  270. void increaseAndValidateFileNotFoundCount();
  271. // Just set inMemoryDownload flag true.
  272. void markInMemoryDownload();
  273. // Returns inMemoryDownload flag.
  274. bool inMemoryDownload() const;
  275. };
  276. typedef SharedHandle<RequestGroup> RequestGroupHandle;
  277. typedef WeakHandle<RequestGroup> RequestGroupWeakHandle;
  278. typedef std::deque<RequestGroupHandle> RequestGroups;
  279. } // namespace aria2
  280. #endif // _D_REQUEST_GROUP_H_