RequestGroup.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "SegmentMan.h"
  39. #include "LogFactory.h"
  40. #include "Command.h"
  41. #include "SegmentManFactory.h"
  42. #include "DefaultSegmentManFactory.h"
  43. #ifdef ENABLE_MESSAGE_DIGEST
  44. # include "ChunkChecksum.h"
  45. # include "Checksum.h"
  46. #endif // ENABLE_MESSAGE_DIGEST
  47. class DownloadCommand;
  48. class DownloadEngine;
  49. class RequestGroup {
  50. private:
  51. int32_t _gid;
  52. int64_t _hintTotalLength;
  53. string _hintFilename;
  54. string _ufilename;
  55. string _topDir;
  56. Strings _uris;
  57. Strings _spentUris;
  58. SegmentManHandle _segmentMan;
  59. SegmentManFactoryHandle _segmentManFactory;
  60. const Option* _option;
  61. const Logger* logger;
  62. #ifdef ENABLE_MESSAGE_DIGEST
  63. ChunkChecksumHandle _chunkChecksum;
  64. ChecksumHandle _checksum;
  65. #endif // ENABLE_MESSAGE_DIGEST
  66. int32_t _numConcurrentCommand;
  67. void validateFilename(const string& expectedFilename,
  68. const string& actualFilename) const;
  69. void validateTotalLength(int64_t expectedTotalLength,
  70. int64_t actualTotalLength) const;
  71. public:
  72. int32_t numConnection;
  73. bool isTorrent;
  74. RequestGroup(const Strings& uris, const Option* option):
  75. _gid(0),
  76. _hintTotalLength(0),
  77. _uris(uris),
  78. _segmentMan(0),
  79. _segmentManFactory(new DefaultSegmentManFactory(option)),
  80. _option(option),
  81. logger(LogFactory::getInstance()),
  82. #ifdef ENABLE_MESSAGE_DIGEST
  83. _chunkChecksum(0),
  84. _checksum(0),
  85. #endif // ENABLE_MESSAGE_DIGEST
  86. _numConcurrentCommand(0),
  87. numConnection(0),
  88. isTorrent(false) {}
  89. RequestGroup(const string& uri, const Option* option):
  90. _gid(0),
  91. _hintTotalLength(0),
  92. _segmentMan(0),
  93. _segmentManFactory(new DefaultSegmentManFactory(option)),
  94. _option(option),
  95. logger(LogFactory::getInstance()),
  96. #ifdef ENABLE_MESSAGE_DIGEST
  97. _chunkChecksum(0),
  98. _checksum(0),
  99. #endif // ENABLE_MESSAGE_DIGEST
  100. _numConcurrentCommand(0),
  101. numConnection(0),
  102. isTorrent(false)
  103. {
  104. _uris.push_back(uri);
  105. }
  106. /**
  107. * Reinitializes SegmentMan based on current property values and
  108. * returns new one.
  109. */
  110. SegmentManHandle initSegmentMan();
  111. SegmentManHandle getSegmentMan() const
  112. {
  113. return _segmentMan;
  114. }
  115. Commands createNextCommandWithAdj(DownloadEngine* e, int32_t numAdj);
  116. Commands createNextCommand(DownloadEngine* e, int32_t numCommand, const string& method = "GET");
  117. void addURI(const string& uri)
  118. {
  119. _uris.push_back(uri);
  120. }
  121. #ifdef ENABLE_MESSAGE_DIGEST
  122. void setChunkChecksum(const ChunkChecksumHandle& chunkChecksum)
  123. {
  124. _chunkChecksum = chunkChecksum;
  125. }
  126. ChunkChecksumHandle getChunkChecksum() const
  127. {
  128. return _chunkChecksum;
  129. }
  130. void setChecksum(const ChecksumHandle& checksum)
  131. {
  132. _checksum = checksum;
  133. }
  134. ChecksumHandle getChecksum() const
  135. {
  136. return _checksum;
  137. }
  138. #endif // ENABLE_MESSAGE_DIGEST
  139. void initBitfield();
  140. void openExistingFile();
  141. void markAllPiecesDone();
  142. void markExistingPiecesDone();
  143. void markPieceDone(int64_t length);
  144. void shouldCancelDownloadForSafety();
  145. void initAndOpenFile();
  146. bool needsFileAllocation() const;
  147. bool downloadFinished() const
  148. {
  149. if(_segmentMan.isNull()) {
  150. return false;
  151. } else {
  152. return _segmentMan->finished();
  153. }
  154. }
  155. void load()
  156. {
  157. _segmentMan->load();
  158. }
  159. void save()
  160. {
  161. _segmentMan->save();
  162. }
  163. void remove()
  164. {
  165. _segmentMan->remove();
  166. }
  167. void closeFile()
  168. {
  169. _segmentMan->diskWriter->closeFile();
  170. }
  171. bool fileExists() const;
  172. bool segmentFileExists() const;
  173. string getFilePath() const;
  174. string getDir() const;
  175. int64_t getExistingFileLength() const;
  176. int64_t getTotalLength() const
  177. {
  178. return _segmentMan->totalSize;
  179. }
  180. int64_t getDownloadLength() const
  181. {
  182. return _segmentMan->getDownloadLength();
  183. }
  184. void loadAndOpenFile();
  185. void prepareForNextAction(int cuid, const RequestHandle& req, DownloadEngine* e, DownloadCommand* downloadCommand = 0);
  186. bool downloadFinishedByFileLength();
  187. const string& getHintFilename() const
  188. {
  189. return _hintFilename;
  190. }
  191. void setHintFilename(const string& filename)
  192. {
  193. _hintFilename = filename;
  194. }
  195. int64_t getHintTotalLength() const
  196. {
  197. return _hintTotalLength;
  198. }
  199. void setHintTotalLength(int64_t totalLength)
  200. {
  201. _hintTotalLength = totalLength;
  202. }
  203. const Strings& getRemainingUris() const
  204. {
  205. return _uris;
  206. }
  207. const Strings& getSpentUris() const
  208. {
  209. return _spentUris;
  210. }
  211. Strings getUris() const
  212. {
  213. Strings temp(_spentUris.begin(), _spentUris.end());
  214. temp.insert(temp.end(), _uris.begin(), _uris.end());
  215. return temp;
  216. }
  217. /**
  218. * Compares expected filename with specified actualFilename.
  219. * The expected filename refers to SegmentMan::filename.
  220. */
  221. void validateFilename(const string& actualFilename) const;
  222. void validateTotalLength(int64_t actualTotalLength) const;
  223. /**
  224. * Compares expected filename with specified actualFilename.
  225. * The expected filename refers to RequestGroup::hintFilename.
  226. */
  227. void validateFilenameByHint(const string& actualFilename) const;
  228. void validateTotalLengthByHint(int64_t actualTotalLength) const;
  229. void setSegmentManFactory(const SegmentManFactoryHandle& segmentManFactory)
  230. {
  231. _segmentManFactory = segmentManFactory;
  232. }
  233. void setNumConcurrentCommand(int32_t num)
  234. {
  235. _numConcurrentCommand = num;
  236. }
  237. void setUserDefinedFilename(const string& filename);
  238. void setGID(int32_t gid)
  239. {
  240. _gid = gid;
  241. }
  242. int32_t getGID() const
  243. {
  244. return _gid;
  245. }
  246. void setTopDir(const string& topDir)
  247. {
  248. _topDir = topDir;
  249. }
  250. int32_t calculateDownloadSpeed() const
  251. {
  252. return _segmentMan->calculateDownloadSpeed();
  253. }
  254. };
  255. typedef SharedHandle<RequestGroup> RequestGroupHandle;
  256. typedef deque<RequestGroupHandle> RequestGroups;
  257. #endif // _D_REQUEST_GROUP_H_