RequestGroup.h 6.8 KB

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