RequestGroup.h 6.1 KB

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