PieceStorage.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_PIECE_STORAGE_H
  36. #define D_PIECE_STORAGE_H
  37. #include "common.h"
  38. #include <string>
  39. #include <vector>
  40. #include <memory>
  41. #include "TimerA2.h"
  42. #include "Command.h"
  43. namespace aria2 {
  44. class Piece;
  45. #ifdef ENABLE_BITTORRENT
  46. class Peer;
  47. #endif // ENABLE_BITTORRENT
  48. class DiskAdaptor;
  49. class WrDiskCache;
  50. class PieceStorage {
  51. public:
  52. virtual ~PieceStorage() = default;
  53. #ifdef ENABLE_BITTORRENT
  54. /**
  55. * Returns true if the peer has a piece that localhost doesn't have.
  56. * Otherwise returns false.
  57. */
  58. virtual bool hasMissingPiece(const std::shared_ptr<Peer>& peer) = 0;
  59. // Stores pieces that the peer has but localhost doesn't. Those
  60. // pieces will be marked "used" status in order to prevent other
  61. // command from get the same piece. But in end game mode, same
  62. // piece may be got by several commands. This function stores N
  63. // pieces where the sum of missing block of first N-1 pieces is
  64. // less than minMissingBlocks and the sum of missing block of N
  65. // pieces is greater than or equal to minMissingBlocks. If there is
  66. // M missing pieces left where M < N, M pieces are stored.
  67. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  68. size_t minMissingBlocks,
  69. const std::shared_ptr<Peer>& peer,
  70. cuid_t cuid) = 0;
  71. // Same as getMissingPiece(pieces, minMissingBlocks, peer), but the
  72. // indexes in excludedIndexes are excluded.
  73. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  74. size_t minMissingBlocks,
  75. const std::shared_ptr<Peer>& peer,
  76. const std::vector<size_t>& excludedIndexes,
  77. cuid_t cuid) = 0;
  78. // Stores pieces that the peer has but localhost doesn't. Only
  79. // pieces that declared as "fast" are stored. Those pieces stored
  80. // will be marked "used" status in order to prevent other command
  81. // from get the same piece. But in end game mode, same piece may be
  82. // got by several commands. This function stores N pieces where the
  83. // sum of missing block of first N-1 pieces is less than
  84. // minMissingBlocks and the sum of missing block of N pieces is
  85. // greater than or equal to minMissingBlocks. If there is M missing
  86. // pieces left where M < N, M pieces are stored.
  87. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  88. size_t minMissingBlocks,
  89. const std::shared_ptr<Peer>& peer,
  90. cuid_t cuid) = 0;
  91. // Same as getMissingFastPiece(pieces, minMissingBlocks, peer), but
  92. // the indexes in excludedIndexes are excluded.
  93. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  94. size_t minMissingBlocks,
  95. const std::shared_ptr<Peer>& peer,
  96. const std::vector<size_t>& excludedIndexes,
  97. cuid_t cuid) = 0;
  98. /**
  99. * Returns a piece that the peer has but localhost doesn't.
  100. * The piece will be marked "used" status in order to prevent other command
  101. * from get the same piece. But in end game mode, same piece may be returned
  102. * to several commands.
  103. */
  104. virtual std::shared_ptr<Piece>
  105. getMissingPiece(const std::shared_ptr<Peer>& peer, cuid_t cuid) = 0;
  106. /**
  107. * Same as getMissingPiece(const std::shared_ptr<Peer>& peer), but the indexes
  108. * in
  109. * excludedIndexes are excluded.
  110. */
  111. virtual std::shared_ptr<Piece>
  112. getMissingPiece(const std::shared_ptr<Peer>& peer,
  113. const std::vector<size_t>& excludedIndexes, cuid_t cuid) = 0;
  114. #endif // ENABLE_BITTORRENT
  115. // Returns true if there is at least one missing and unused piece.
  116. virtual bool hasMissingUnusedPiece() = 0;
  117. /**
  118. * Returns a missing piece if available. Otherwise returns 0;
  119. * If ignoreBitfield is set, indexes of true bit are excluded.
  120. */
  121. virtual std::shared_ptr<Piece>
  122. getMissingPiece(size_t minSplitSize, const unsigned char* ignoreBitfield,
  123. size_t length, cuid_t cuid) = 0;
  124. /**
  125. * Returns a missing piece whose index is index.
  126. * If a piece whose index is index is already acquired or currently used,
  127. * then returns 0.
  128. * Also returns 0 if any of missing piece is not available.
  129. */
  130. virtual std::shared_ptr<Piece> getMissingPiece(size_t index, cuid_t cuid) = 0;
  131. /**
  132. * Returns the piece denoted by index.
  133. * No status of the piece is changed in this method.
  134. */
  135. virtual std::shared_ptr<Piece> getPiece(size_t index) = 0;
  136. /**
  137. * Marks the piece whose index is index as missing.
  138. */
  139. virtual void markPieceMissing(size_t index) = 0;
  140. /**
  141. * Tells that the download of the specified piece completes.
  142. */
  143. virtual void completePiece(const std::shared_ptr<Piece>& piece) = 0;
  144. /**
  145. * Tells that the download of the specified piece is canceled.
  146. */
  147. virtual void cancelPiece(const std::shared_ptr<Piece>& piece,
  148. cuid_t cuid) = 0;
  149. /**
  150. * Returns true if the specified piece is already downloaded.
  151. * Otherwise returns false.
  152. */
  153. virtual bool hasPiece(size_t index) = 0;
  154. virtual bool isPieceUsed(size_t index) = 0;
  155. virtual int64_t getTotalLength() = 0;
  156. virtual int64_t getFilteredTotalLength() = 0;
  157. virtual int64_t getCompletedLength() = 0;
  158. virtual int64_t getFilteredCompletedLength() = 0;
  159. virtual void setupFileFilter() = 0;
  160. virtual void clearFileFilter() = 0;
  161. /**
  162. * Returns true if download has completed.
  163. * If file filter is enabled, then returns true if those files have
  164. * downloaded.
  165. */
  166. virtual bool downloadFinished() = 0;
  167. /**
  168. * Returns true if all files have downloaded.
  169. * The file filter is ignored.
  170. */
  171. virtual bool allDownloadFinished() = 0;
  172. /**
  173. * Initializes DiskAdaptor.
  174. * TODO add better documentation here.
  175. */
  176. virtual void initStorage() = 0;
  177. virtual const unsigned char* getBitfield() = 0;
  178. virtual void setBitfield(const unsigned char* bitfield,
  179. size_t bitfieldLength) = 0;
  180. virtual size_t getBitfieldLength() = 0;
  181. virtual bool isSelectiveDownloadingMode() = 0;
  182. virtual bool isEndGame() = 0;
  183. virtual void enterEndGame() = 0;
  184. // TODO We can remove this.
  185. virtual void setEndGamePieceNum(size_t num) = 0;
  186. virtual std::shared_ptr<DiskAdaptor> getDiskAdaptor() = 0;
  187. virtual WrDiskCache* getWrDiskCache() = 0;
  188. // Flushes write disk cache for in-flight piece and evicts them.
  189. virtual void flushWrDiskCacheEntry() = 0;
  190. virtual int32_t getPieceLength(size_t index) = 0;
  191. /**
  192. * Adds piece index to advertise to other commands. They send have message
  193. * based on this information.
  194. */
  195. virtual void advertisePiece(cuid_t cuid, size_t index,
  196. Timer registerdTime) = 0;
  197. /**
  198. * indexes is filled with piece index which is not advertised by the
  199. * caller command and newer than lastHaveIndex.
  200. */
  201. virtual uint64_t getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
  202. cuid_t myCuid,
  203. uint64_t lastHaveIndex) = 0;
  204. /**
  205. * Removes have entry if its registeredTime is at least as old as
  206. * expiry.
  207. */
  208. virtual void removeAdvertisedPiece(const Timer& expiry) = 0;
  209. /**
  210. * Sets all bits in bitfield to 1.
  211. */
  212. virtual void markAllPiecesDone() = 0;
  213. /**
  214. * Sets all bits in bitfield(0 to length) to 1.
  215. */
  216. virtual void markPiecesDone(int64_t length) = 0;
  217. virtual void
  218. addInFlightPiece(const std::vector<std::shared_ptr<Piece>>& pieces) = 0;
  219. virtual size_t countInFlightPiece() = 0;
  220. virtual void
  221. getInFlightPieces(std::vector<std::shared_ptr<Piece>>& pieces) = 0;
  222. virtual void addPieceStats(size_t index) = 0;
  223. virtual void addPieceStats(const unsigned char* bitfield,
  224. size_t bitfieldLength) = 0;
  225. virtual void subtractPieceStats(const unsigned char* bitfield,
  226. size_t bitfieldLength) = 0;
  227. virtual void updatePieceStats(const unsigned char* newBitfield,
  228. size_t newBitfieldLength,
  229. const unsigned char* oldBitfield) = 0;
  230. // Returns index x where all pieces in [index+1, x-1], inclusive,
  231. // are not used and not completed. If all pieces after index+1 are
  232. // used or completed, returns the number of pieces.
  233. virtual size_t getNextUsedIndex(size_t index) = 0;
  234. // Called when system detects download is not finished
  235. virtual void onDownloadIncomplete() = 0;
  236. };
  237. } // namespace aria2
  238. #endif // D_PIECE_STORAGE_H