UnknownLengthPieceStorage.h 9.2 KB

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