UnknownLengthPieceStorage.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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_UNKNOWN_LENGTH_PIECE_STORAGE_H_
  36. #define _D_UNKNOWN_LENGTH_PIECE_STORAGE_H_
  37. #include "PieceStorage.h"
  38. class Option;
  39. class DownloadContext;
  40. typedef SharedHandle<DownloadContext> DownloadContextHandle;
  41. class DiskWriterFactory;
  42. typedef SharedHandle<DiskWriterFactory> DiskWriterFactoryHandle;
  43. class DirectDiskAdaptor;
  44. typedef SharedHandle<DirectDiskAdaptor> DirectDiskAdaptorHandle;
  45. class UnknownLengthPieceStorage:public PieceStorage {
  46. private:
  47. DownloadContextHandle _downloadContext;
  48. const Option* _option;
  49. DirectDiskAdaptorHandle _diskAdaptor;
  50. DiskWriterFactoryHandle _diskWriterFactory;
  51. int64_t _totalLength;
  52. bool _downloadFinished;
  53. PieceHandle _piece;
  54. public:
  55. UnknownLengthPieceStorage(const DownloadContextHandle& downloadContext,
  56. const Option* option);
  57. virtual ~UnknownLengthPieceStorage();
  58. /**
  59. * Returns true if the peer has a piece that localhost doesn't have.
  60. * Otherwise returns false.
  61. */
  62. virtual bool hasMissingPiece(const PeerHandle& peer)
  63. {
  64. abort();
  65. }
  66. /**
  67. * Returns a piece that the peer has but localhost doesn't.
  68. * The piece will be marked "used" status in order to prevent other command
  69. * from get the same piece. But in end game mode, same piece may be returned
  70. * to several commands.
  71. */
  72. virtual PieceHandle getMissingPiece(const PeerHandle& peer)
  73. {
  74. abort();
  75. }
  76. /**
  77. * Returns a piece that the peer has but localhost doesn't.
  78. * Only pieces that declared as "fast" are returned.
  79. * The piece will be marked "used" status in order to prevent other command
  80. * from get the same piece. But in end game mode, same piece may be returned
  81. * to several commands.
  82. */
  83. virtual PieceHandle getMissingFastPiece(const PeerHandle& peer)
  84. {
  85. abort();
  86. }
  87. /**
  88. * Returns a missing piece if available. Otherwise returns 0;
  89. */
  90. virtual PieceHandle getMissingPiece();
  91. /**
  92. * Returns a missing piece whose index is index.
  93. * If a piece whose index is index is already acquired or currently used,
  94. * then returns 0.
  95. * Also returns 0 if any of missing piece is not available.
  96. */
  97. virtual PieceHandle getMissingPiece(int32_t index);
  98. /**
  99. * Returns the piece denoted by index.
  100. * No status of the piece is changed in this method.
  101. */
  102. virtual PieceHandle getPiece(int32_t index);
  103. /**
  104. * Tells that the download of the specfied piece completes.
  105. */
  106. virtual void completePiece(const PieceHandle& piece);
  107. /**
  108. * Tells that the download of the specified piece is canceled.
  109. */
  110. virtual void cancelPiece(const PieceHandle& piece);
  111. /**
  112. * Returns true if the specified piece is already downloaded.
  113. * Otherwise returns false.
  114. */
  115. virtual bool hasPiece(int32_t index);
  116. virtual bool isPieceUsed(int32_t index);
  117. virtual int64_t getTotalLength()
  118. {
  119. return _totalLength;
  120. }
  121. virtual int64_t getFilteredTotalLength()
  122. {
  123. return _totalLength;
  124. }
  125. virtual int64_t getCompletedLength()
  126. {
  127. // TODO we have to return actual completed length here?
  128. return _totalLength;
  129. }
  130. virtual int64_t getFilteredCompletedLength()
  131. {
  132. return getCompletedLength();
  133. }
  134. virtual void setFileFilter(const Strings& filePaths) {}
  135. virtual void setFileFilter(IntSequence seq) {}
  136. virtual void clearFileFilter() {}
  137. /**
  138. * Returns true if download has completed.
  139. * If file filter is enabled, then returns true if those files have
  140. * downloaded.
  141. */
  142. virtual bool downloadFinished()
  143. {
  144. return _downloadFinished;
  145. }
  146. /**
  147. * Returns true if all files have downloaded.
  148. * The file filter is ignored.
  149. */
  150. virtual bool allDownloadFinished()
  151. {
  152. return downloadFinished();
  153. }
  154. /**
  155. * Initializes DiskAdaptor.
  156. * TODO add better documentation here.
  157. */
  158. virtual void initStorage();
  159. virtual const unsigned char* getBitfield()
  160. {
  161. return 0;
  162. }
  163. virtual void setBitfield(const unsigned char* bitfield,
  164. int32_t bitfieldLength) {}
  165. virtual int32_t getBitfieldLength()
  166. {
  167. return 0;
  168. }
  169. virtual bool isSelectiveDownloadingMode()
  170. {
  171. return false;
  172. }
  173. virtual void finishSelectiveDownloadingMode() {}
  174. virtual bool isEndGame()
  175. {
  176. return false;
  177. }
  178. virtual DiskAdaptorHandle getDiskAdaptor();
  179. virtual int32_t getPieceLength(int32_t index);
  180. /**
  181. * Adds piece index to advertise to other commands. They send have message
  182. * based on this information.
  183. */
  184. virtual void advertisePiece(int32_t cuid, int32_t index) {}
  185. /**
  186. * Returns piece index which is not advertised by the caller command and
  187. * newer than lastCheckTime.
  188. */
  189. virtual Integers getAdvertisedPieceIndexes(int32_t myCuid,
  190. const Time& lastCheckTime)
  191. {
  192. return Integers();
  193. }
  194. /**
  195. * Removes have entry if specified seconds have elapsed since its
  196. * registration.
  197. */
  198. virtual void removeAdvertisedPiece(int32_t elapsed) {}
  199. /**
  200. * Sets all bits in bitfield to 1.
  201. */
  202. virtual void markAllPiecesDone();
  203. virtual void markPiecesDone(int64_t length)
  204. {
  205. // TODO not implemented yet
  206. abort();
  207. }
  208. virtual void markPieceMissing(int32_t index)
  209. {
  210. // TODO not implemented yet
  211. abort();
  212. }
  213. /**
  214. * Do nothing because loading in-flight piece is not supported for this
  215. * class.
  216. */
  217. virtual void addInFlightPiece(const Pieces& pieces) {}
  218. virtual int32_t countInFlightPiece()
  219. {
  220. return 0;
  221. }
  222. virtual Pieces getInFlightPieces();
  223. void setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory);
  224. };
  225. typedef SharedHandle<UnknownLengthPieceStorage> UnknownLengthPieceStorageHandle;
  226. #endif // _D_UNKNOWN_LENGTH_PIECE_STORAGE_H_