RequestGroup.h 8.5 KB

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