RequestGroup.h 9.2 KB

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