UnknownLengthPieceStorage.h 8.8 KB

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