UnknownLengthPieceStorage.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. namespace aria2 {
  39. class Option;
  40. class DownloadContext;
  41. class DiskWriterFactory;
  42. class DirectDiskAdaptor;
  43. class BitfieldMan;
  44. class UnknownLengthPieceStorage : public PieceStorage {
  45. private:
  46. std::shared_ptr<DownloadContext> downloadContext_;
  47. std::shared_ptr<DirectDiskAdaptor> diskAdaptor_;
  48. std::shared_ptr<DiskWriterFactory> diskWriterFactory_;
  49. int64_t totalLength_;
  50. std::unique_ptr<BitfieldMan> bitfield_;
  51. bool downloadFinished_;
  52. std::shared_ptr<Piece> piece_;
  53. void createBitfield();
  54. public:
  55. UnknownLengthPieceStorage(
  56. const std::shared_ptr<DownloadContext>& downloadContext);
  57. virtual ~UnknownLengthPieceStorage();
  58. #ifdef ENABLE_BITTORRENT
  59. /**
  60. * Returns true if the peer has a piece that localhost doesn't have.
  61. * Otherwise returns false.
  62. */
  63. virtual bool
  64. hasMissingPiece(const std::shared_ptr<Peer>& peer) CXX11_OVERRIDE;
  65. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  66. size_t minMissingBlocks,
  67. const std::shared_ptr<Peer>& peer,
  68. cuid_t cuid) CXX11_OVERRIDE;
  69. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  70. size_t minMissingBlocks,
  71. const std::shared_ptr<Peer>& peer,
  72. const std::vector<size_t>& excludedIndexes,
  73. cuid_t cuid) CXX11_OVERRIDE;
  74. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  75. size_t minMissingBlocks,
  76. const std::shared_ptr<Peer>& peer,
  77. cuid_t cuid) CXX11_OVERRIDE;
  78. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  79. size_t minMissingBlocks,
  80. const std::shared_ptr<Peer>& peer,
  81. const std::vector<size_t>& excludedIndexes,
  82. cuid_t cuid) CXX11_OVERRIDE;
  83. virtual std::shared_ptr<Piece>
  84. getMissingPiece(const std::shared_ptr<Peer>& peer,
  85. cuid_t cuid) CXX11_OVERRIDE;
  86. virtual std::shared_ptr<Piece>
  87. getMissingPiece(const std::shared_ptr<Peer>& peer,
  88. const std::vector<size_t>& excludedIndexes,
  89. cuid_t cuid) CXX11_OVERRIDE;
  90. #endif // ENABLE_BITTORRENT
  91. virtual bool hasMissingUnusedPiece() CXX11_OVERRIDE;
  92. /**
  93. * Returns a missing piece if available. Otherwise returns 0;
  94. */
  95. virtual std::shared_ptr<Piece>
  96. getMissingPiece(size_t minSplitSize, const unsigned char* ignoreBitfield,
  97. size_t length, cuid_t cuid) CXX11_OVERRIDE;
  98. /**
  99. * Returns a missing piece whose index is index.
  100. * If a piece whose index is index is already acquired or currently used,
  101. * then returns 0.
  102. * Also returns 0 if any of missing piece is not available.
  103. */
  104. virtual std::shared_ptr<Piece> getMissingPiece(size_t index,
  105. cuid_t cuid) CXX11_OVERRIDE;
  106. /**
  107. * Returns the piece denoted by index.
  108. * No status of the piece is changed in this method.
  109. */
  110. virtual std::shared_ptr<Piece> getPiece(size_t index) CXX11_OVERRIDE;
  111. /**
  112. * Tells that the download of the specfied piece completes.
  113. */
  114. virtual void
  115. completePiece(const std::shared_ptr<Piece>& piece) CXX11_OVERRIDE;
  116. /**
  117. * Tells that the download of the specified piece is canceled.
  118. */
  119. virtual void cancelPiece(const std::shared_ptr<Piece>& piece,
  120. cuid_t cuid) CXX11_OVERRIDE;
  121. /**
  122. * Returns true if the specified piece is already downloaded.
  123. * Otherwise returns false.
  124. */
  125. virtual bool hasPiece(size_t index) CXX11_OVERRIDE;
  126. virtual bool isPieceUsed(size_t index) CXX11_OVERRIDE;
  127. virtual int64_t getTotalLength() CXX11_OVERRIDE { return totalLength_; }
  128. virtual int64_t getFilteredTotalLength() CXX11_OVERRIDE
  129. {
  130. return totalLength_;
  131. }
  132. virtual int64_t getCompletedLength() CXX11_OVERRIDE
  133. {
  134. // TODO we have to return actual completed length here?
  135. return totalLength_;
  136. }
  137. virtual int64_t getFilteredCompletedLength() CXX11_OVERRIDE
  138. {
  139. return getCompletedLength();
  140. }
  141. virtual void setupFileFilter() CXX11_OVERRIDE {}
  142. virtual void clearFileFilter() CXX11_OVERRIDE {}
  143. /**
  144. * Returns true if download has completed.
  145. * If file filter is enabled, then returns true if those files have
  146. * downloaded.
  147. */
  148. virtual bool downloadFinished() CXX11_OVERRIDE { return downloadFinished_; }
  149. /**
  150. * Returns true if all files have downloaded.
  151. * The file filter is ignored.
  152. */
  153. virtual bool allDownloadFinished() CXX11_OVERRIDE
  154. {
  155. return downloadFinished();
  156. }
  157. /**
  158. * Initializes DiskAdaptor.
  159. * TODO add better documentation here.
  160. */
  161. virtual void initStorage() CXX11_OVERRIDE;
  162. virtual const unsigned char* getBitfield() CXX11_OVERRIDE;
  163. virtual void setBitfield(const unsigned char* bitfield,
  164. size_t bitfieldLength) CXX11_OVERRIDE
  165. {
  166. }
  167. virtual size_t getBitfieldLength() CXX11_OVERRIDE;
  168. virtual bool isSelectiveDownloadingMode() CXX11_OVERRIDE { return false; }
  169. virtual bool isEndGame() CXX11_OVERRIDE { return false; }
  170. virtual void enterEndGame() CXX11_OVERRIDE {}
  171. virtual void setEndGamePieceNum(size_t num) CXX11_OVERRIDE {}
  172. virtual std::shared_ptr<DiskAdaptor> getDiskAdaptor() CXX11_OVERRIDE;
  173. virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE { return nullptr; }
  174. virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE {}
  175. virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE;
  176. virtual void advertisePiece(cuid_t cuid, size_t index,
  177. Timer registeredTime) CXX11_OVERRIDE
  178. {
  179. }
  180. /**
  181. * indexes is filled with piece index which is not advertised by the
  182. * caller command and newer than lastHaveIndex.
  183. */
  184. virtual uint64_t
  185. getAdvertisedPieceIndexes(std::vector<size_t>& indexes, cuid_t myCuid,
  186. uint64_t lastHaveIndex) CXX11_OVERRIDE
  187. {
  188. }
  189. virtual void removeAdvertisedPiece(const Timer& expiry) CXX11_OVERRIDE {}
  190. /**
  191. * Sets all bits in bitfield to 1.
  192. */
  193. virtual void markAllPiecesDone() CXX11_OVERRIDE;
  194. virtual void markPiecesDone(int64_t length) CXX11_OVERRIDE;
  195. virtual void markPieceMissing(size_t index) CXX11_OVERRIDE;
  196. /**
  197. * Do nothing because loading in-flight piece is not supported for this
  198. * class.
  199. */
  200. virtual void addInFlightPiece(
  201. const std::vector<std::shared_ptr<Piece>>& pieces) CXX11_OVERRIDE
  202. {
  203. }
  204. virtual size_t countInFlightPiece() CXX11_OVERRIDE { return 0; }
  205. virtual void
  206. getInFlightPieces(std::vector<std::shared_ptr<Piece>>& pieces) CXX11_OVERRIDE;
  207. virtual void addPieceStats(size_t index) CXX11_OVERRIDE {}
  208. virtual void addPieceStats(const unsigned char* bitfield,
  209. size_t bitfieldLength) CXX11_OVERRIDE
  210. {
  211. }
  212. virtual void subtractPieceStats(const unsigned char* bitfield,
  213. size_t bitfieldLength) CXX11_OVERRIDE
  214. {
  215. }
  216. virtual void updatePieceStats(const unsigned char* newBitfield,
  217. size_t newBitfieldLength,
  218. const unsigned char* oldBitfield) CXX11_OVERRIDE
  219. {
  220. }
  221. virtual size_t getNextUsedIndex(size_t index) CXX11_OVERRIDE { return 0; }
  222. void setDiskWriterFactory(
  223. const std::shared_ptr<DiskWriterFactory>& diskWriterFactory);
  224. virtual void onDownloadIncomplete() CXX11_OVERRIDE {}
  225. };
  226. } // namespace aria2
  227. #endif // D_UNKNOWN_LENGTH_PIECE_STORAGE_H