DefaultPieceStorage.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_DEFAULT_PIECE_STORAGE_H
  36. #define D_DEFAULT_PIECE_STORAGE_H
  37. #include "PieceStorage.h"
  38. #include <deque>
  39. #include <set>
  40. #include "a2functional.h"
  41. namespace aria2 {
  42. class DownloadContext;
  43. class BitfieldMan;
  44. class Option;
  45. class DiskWriterFactory;
  46. class FileEntry;
  47. class PieceStatMan;
  48. class PieceSelector;
  49. class StreamPieceSelector;
  50. #define END_GAME_PIECE_NUM 20
  51. class HaveEntry {
  52. private:
  53. cuid_t cuid_;
  54. size_t index_;
  55. Timer registeredTime_;
  56. public:
  57. HaveEntry(cuid_t cuid, size_t index, const Timer& registeredTime)
  58. : cuid_(cuid), index_(index), registeredTime_(registeredTime)
  59. {
  60. }
  61. cuid_t getCuid() const { return cuid_; }
  62. size_t getIndex() const { return index_; }
  63. const Timer& getRegisteredTime() const { return registeredTime_; }
  64. };
  65. class DefaultPieceStorage : public PieceStorage {
  66. private:
  67. std::shared_ptr<DownloadContext> downloadContext_;
  68. std::unique_ptr<BitfieldMan> bitfieldMan_;
  69. std::shared_ptr<DiskAdaptor> diskAdaptor_;
  70. std::shared_ptr<DiskWriterFactory> diskWriterFactory_;
  71. typedef std::set<std::shared_ptr<Piece>, DerefLess<std::shared_ptr<Piece>>>
  72. UsedPieceSet;
  73. UsedPieceSet usedPieces_;
  74. bool endGame_;
  75. size_t endGamePieceNum_;
  76. const Option* option_;
  77. std::deque<HaveEntry> haves_;
  78. std::shared_ptr<PieceStatMan> pieceStatMan_;
  79. std::unique_ptr<PieceSelector> pieceSelector_;
  80. std::unique_ptr<StreamPieceSelector> streamPieceSelector_;
  81. WrDiskCache* wrDiskCache_;
  82. #ifdef ENABLE_BITTORRENT
  83. void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  84. size_t minMissingBlocks, const unsigned char* bitfield,
  85. size_t length, cuid_t cuid);
  86. void createFastIndexBitfield(BitfieldMan& bitfield,
  87. const std::shared_ptr<Peer>& peer);
  88. #endif // ENABLE_BITTORRENT
  89. std::shared_ptr<Piece> checkOutPiece(size_t index, cuid_t cuid);
  90. // size_t deleteUsedPiecesByFillRate(int fillRate, size_t toDelete);
  91. // void reduceUsedPieces(size_t upperBound);
  92. void deleteUsedPiece(const std::shared_ptr<Piece>& piece);
  93. std::shared_ptr<Piece> findUsedPiece(size_t index) const;
  94. // Returns the sum of completed length of in-flight pieces
  95. int64_t getInFlightPieceCompletedLength() const;
  96. // Returns the sum of completed length of in-flight pieces
  97. // intersecting filter ranges.
  98. int64_t getInFlightPieceFilteredCompletedLength() const;
  99. public:
  100. // Setting randomPieceStatsOrdering to true means a piece is chosen in
  101. // random when more than 2 pieces has the same rarity.
  102. // If it is set to false, a piece whose index is smallest has the highest
  103. // priority.
  104. DefaultPieceStorage(const std::shared_ptr<DownloadContext>& downloadContext,
  105. const Option* option);
  106. virtual ~DefaultPieceStorage();
  107. #ifdef ENABLE_BITTORRENT
  108. virtual bool
  109. hasMissingPiece(const std::shared_ptr<Peer>& peer) CXX11_OVERRIDE;
  110. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  111. size_t minMissingBlocks,
  112. const std::shared_ptr<Peer>& peer,
  113. cuid_t cuid) CXX11_OVERRIDE;
  114. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  115. size_t minMissingBlocks,
  116. const std::shared_ptr<Peer>& peer,
  117. const std::vector<size_t>& excludedIndexes,
  118. cuid_t cuid) CXX11_OVERRIDE;
  119. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  120. size_t minMissingBlocks,
  121. const std::shared_ptr<Peer>& peer,
  122. cuid_t cuid) CXX11_OVERRIDE;
  123. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  124. size_t minMissingBlocks,
  125. const std::shared_ptr<Peer>& peer,
  126. const std::vector<size_t>& excludedIndexes,
  127. cuid_t cuid) CXX11_OVERRIDE;
  128. virtual std::shared_ptr<Piece>
  129. getMissingPiece(const std::shared_ptr<Peer>& peer,
  130. cuid_t cuid) CXX11_OVERRIDE;
  131. virtual std::shared_ptr<Piece>
  132. getMissingPiece(const std::shared_ptr<Peer>& peer,
  133. const std::vector<size_t>& excludedIndexes,
  134. cuid_t cuid) CXX11_OVERRIDE;
  135. std::shared_ptr<Piece> getMissingFastPiece(const std::shared_ptr<Peer>& peer,
  136. cuid_t cuid);
  137. std::shared_ptr<Piece>
  138. getMissingFastPiece(const std::shared_ptr<Peer>& peer,
  139. const std::vector<size_t>& excludedIndexes, cuid_t cuid);
  140. #endif // ENABLE_BITTORRENT
  141. virtual bool hasMissingUnusedPiece() CXX11_OVERRIDE;
  142. virtual std::shared_ptr<Piece>
  143. getMissingPiece(size_t minSplitSize, const unsigned char* ignoreBitfield,
  144. size_t length, cuid_t cuid) CXX11_OVERRIDE;
  145. virtual std::shared_ptr<Piece> getMissingPiece(size_t index,
  146. cuid_t cuid) CXX11_OVERRIDE;
  147. virtual std::shared_ptr<Piece> getPiece(size_t index) CXX11_OVERRIDE;
  148. virtual void
  149. completePiece(const std::shared_ptr<Piece>& piece) CXX11_OVERRIDE;
  150. virtual void cancelPiece(const std::shared_ptr<Piece>& piece,
  151. cuid_t cuid) CXX11_OVERRIDE;
  152. virtual bool hasPiece(size_t index) CXX11_OVERRIDE;
  153. virtual bool isPieceUsed(size_t index) CXX11_OVERRIDE;
  154. virtual int64_t getTotalLength() CXX11_OVERRIDE;
  155. virtual int64_t getFilteredTotalLength() CXX11_OVERRIDE;
  156. virtual int64_t getCompletedLength() CXX11_OVERRIDE;
  157. virtual int64_t getFilteredCompletedLength() CXX11_OVERRIDE;
  158. virtual void initStorage() CXX11_OVERRIDE;
  159. virtual void setupFileFilter() CXX11_OVERRIDE;
  160. virtual void clearFileFilter() CXX11_OVERRIDE;
  161. virtual bool downloadFinished() CXX11_OVERRIDE;
  162. virtual bool allDownloadFinished() CXX11_OVERRIDE;
  163. virtual void setBitfield(const unsigned char* bitfield,
  164. size_t bitfieldLength) CXX11_OVERRIDE;
  165. virtual size_t getBitfieldLength() CXX11_OVERRIDE;
  166. virtual const unsigned char* getBitfield() CXX11_OVERRIDE;
  167. virtual void setEndGamePieceNum(size_t num) CXX11_OVERRIDE
  168. {
  169. endGamePieceNum_ = num;
  170. }
  171. size_t getEndGamePieceNum() const { return endGamePieceNum_; }
  172. virtual bool isSelectiveDownloadingMode() CXX11_OVERRIDE;
  173. virtual bool isEndGame() CXX11_OVERRIDE { return endGame_; }
  174. virtual void enterEndGame() CXX11_OVERRIDE { endGame_ = true; }
  175. virtual std::shared_ptr<DiskAdaptor> getDiskAdaptor() CXX11_OVERRIDE;
  176. virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE;
  177. virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE;
  178. virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE;
  179. virtual void advertisePiece(cuid_t cuid, size_t index) CXX11_OVERRIDE;
  180. virtual void
  181. getAdvertisedPieceIndexes(std::vector<size_t>& indexes, cuid_t myCuid,
  182. const Timer& lastCheckTime) CXX11_OVERRIDE;
  183. virtual void
  184. removeAdvertisedPiece(const std::chrono::seconds& elapsed) CXX11_OVERRIDE;
  185. virtual void markAllPiecesDone() CXX11_OVERRIDE;
  186. virtual void markPiecesDone(int64_t length) CXX11_OVERRIDE;
  187. virtual void markPieceMissing(size_t index) CXX11_OVERRIDE;
  188. virtual void addInFlightPiece(
  189. const std::vector<std::shared_ptr<Piece>>& pieces) CXX11_OVERRIDE;
  190. virtual size_t countInFlightPiece() CXX11_OVERRIDE;
  191. virtual void
  192. getInFlightPieces(std::vector<std::shared_ptr<Piece>>& pieces) CXX11_OVERRIDE;
  193. virtual void addPieceStats(size_t index) CXX11_OVERRIDE;
  194. virtual void addPieceStats(const unsigned char* bitfield,
  195. size_t bitfieldLength) CXX11_OVERRIDE;
  196. virtual void subtractPieceStats(const unsigned char* bitfield,
  197. size_t bitfieldLength) CXX11_OVERRIDE;
  198. virtual void
  199. updatePieceStats(const unsigned char* newBitfield, size_t newBitfieldLength,
  200. const unsigned char* oldBitfield) CXX11_OVERRIDE;
  201. virtual size_t getNextUsedIndex(size_t index) CXX11_OVERRIDE;
  202. virtual void onDownloadIncomplete() CXX11_OVERRIDE;
  203. /**
  204. * This method is made private for test purpose only.
  205. */
  206. void addUsedPiece(const std::shared_ptr<Piece>& piece);
  207. void setDiskWriterFactory(
  208. const std::shared_ptr<DiskWriterFactory>& diskWriterFactory);
  209. const std::shared_ptr<PieceStatMan>& getPieceStatMan() const
  210. {
  211. return pieceStatMan_;
  212. }
  213. void setPieceSelector(std::unique_ptr<PieceSelector> pieceSelector);
  214. const std::unique_ptr<PieceSelector>& getPieceSelector() const
  215. {
  216. return pieceSelector_;
  217. }
  218. std::unique_ptr<PieceSelector> popPieceSelector();
  219. void setWrDiskCache(WrDiskCache* wrDiskCache) { wrDiskCache_ = wrDiskCache; }
  220. };
  221. } // namespace aria2
  222. #endif // D_DEFAULT_PIECE_STORAGE_H