RequestGroup.h 8.9 KB

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