DefaultPieceStorage.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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),
  59. index_(index),
  60. registeredTime_(registeredTime) {}
  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>,
  72. DerefLess<std::shared_ptr<Piece> > > 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
  84. (std::vector<std::shared_ptr<Piece> >& pieces,
  85. size_t minMissingBlocks,
  86. const unsigned char* bitfield,
  87. size_t length,
  88. cuid_t cuid);
  89. void createFastIndexBitfield(BitfieldMan& bitfield,
  90. const std::shared_ptr<Peer>& peer);
  91. #endif // ENABLE_BITTORRENT
  92. std::shared_ptr<Piece> checkOutPiece(size_t index, cuid_t cuid);
  93. // size_t deleteUsedPiecesByFillRate(int fillRate, size_t toDelete);
  94. // void reduceUsedPieces(size_t upperBound);
  95. void deleteUsedPiece(const std::shared_ptr<Piece>& piece);
  96. std::shared_ptr<Piece> findUsedPiece(size_t index) const;
  97. // Returns the sum of completed length of in-flight pieces
  98. int64_t getInFlightPieceCompletedLength() const;
  99. // Returns the sum of completed length of in-flight pieces
  100. // intersecting filter ranges.
  101. int64_t getInFlightPieceFilteredCompletedLength() const;
  102. public:
  103. // Setting randomPieceStatsOrdering to true means a piece is chosen in
  104. // random when more than 2 pieces has the same rarity.
  105. // If it is set to false, a piece whose index is smallest has the highest
  106. // priority.
  107. DefaultPieceStorage(const std::shared_ptr<DownloadContext>& downloadContext,
  108. const Option* option);
  109. virtual ~DefaultPieceStorage();
  110. #ifdef ENABLE_BITTORRENT
  111. virtual bool hasMissingPiece(const std::shared_ptr<Peer>& peer)
  112. CXX11_OVERRIDE;
  113. virtual void getMissingPiece
  114. (std::vector<std::shared_ptr<Piece> >& pieces,
  115. size_t minMissingBlocks,
  116. const std::shared_ptr<Peer>& peer,
  117. cuid_t cuid) CXX11_OVERRIDE;
  118. virtual void getMissingPiece
  119. (std::vector<std::shared_ptr<Piece> >& pieces,
  120. size_t minMissingBlocks,
  121. const std::shared_ptr<Peer>& peer,
  122. const std::vector<size_t>& excludedIndexes,
  123. cuid_t cuid) CXX11_OVERRIDE;
  124. virtual void getMissingFastPiece
  125. (std::vector<std::shared_ptr<Piece> >& pieces,
  126. size_t minMissingBlocks,
  127. const std::shared_ptr<Peer>& peer,
  128. cuid_t cuid) CXX11_OVERRIDE;
  129. virtual void getMissingFastPiece
  130. (std::vector<std::shared_ptr<Piece> >& pieces,
  131. size_t minMissingBlocks,
  132. const std::shared_ptr<Peer>& peer,
  133. const std::vector<size_t>& excludedIndexes,
  134. cuid_t cuid) CXX11_OVERRIDE;
  135. virtual std::shared_ptr<Piece> getMissingPiece
  136. (const std::shared_ptr<Peer>& peer,
  137. cuid_t cuid) CXX11_OVERRIDE;
  138. virtual std::shared_ptr<Piece> getMissingPiece
  139. (const std::shared_ptr<Peer>& peer,
  140. const std::vector<size_t>& excludedIndexes,
  141. cuid_t cuid) CXX11_OVERRIDE;
  142. std::shared_ptr<Piece> getMissingFastPiece
  143. (const std::shared_ptr<Peer>& peer,
  144. cuid_t cuid);
  145. std::shared_ptr<Piece> getMissingFastPiece
  146. (const std::shared_ptr<Peer>& peer,
  147. const std::vector<size_t>& excludedIndexes,
  148. cuid_t cuid);
  149. #endif // ENABLE_BITTORRENT
  150. virtual bool hasMissingUnusedPiece() CXX11_OVERRIDE;
  151. virtual std::shared_ptr<Piece> getMissingPiece
  152. (size_t minSplitSize,
  153. const unsigned char* ignoreBitfield,
  154. size_t length,
  155. cuid_t cuid) CXX11_OVERRIDE;
  156. virtual std::shared_ptr<Piece> getMissingPiece(size_t index, cuid_t cuid)
  157. CXX11_OVERRIDE;
  158. virtual std::shared_ptr<Piece> getPiece(size_t index) CXX11_OVERRIDE;
  159. virtual void completePiece(const std::shared_ptr<Piece>& piece)
  160. CXX11_OVERRIDE;
  161. virtual void cancelPiece(const std::shared_ptr<Piece>& piece, cuid_t cuid)
  162. CXX11_OVERRIDE;
  163. virtual bool hasPiece(size_t index) CXX11_OVERRIDE;
  164. virtual bool isPieceUsed(size_t index) CXX11_OVERRIDE;
  165. virtual int64_t getTotalLength() CXX11_OVERRIDE;
  166. virtual int64_t getFilteredTotalLength() CXX11_OVERRIDE;
  167. virtual int64_t getCompletedLength() CXX11_OVERRIDE;
  168. virtual int64_t getFilteredCompletedLength() CXX11_OVERRIDE;
  169. virtual void initStorage() CXX11_OVERRIDE;
  170. virtual void setupFileFilter() CXX11_OVERRIDE;
  171. virtual void clearFileFilter() CXX11_OVERRIDE;
  172. virtual bool downloadFinished() CXX11_OVERRIDE;
  173. virtual bool allDownloadFinished() CXX11_OVERRIDE;
  174. virtual void setBitfield(const unsigned char* bitfield,
  175. size_t bitfieldLength) CXX11_OVERRIDE;
  176. virtual size_t getBitfieldLength() CXX11_OVERRIDE;
  177. virtual const unsigned char* getBitfield() CXX11_OVERRIDE;
  178. virtual void setEndGamePieceNum(size_t num) CXX11_OVERRIDE
  179. {
  180. endGamePieceNum_ = num;
  181. }
  182. size_t getEndGamePieceNum() const {
  183. return endGamePieceNum_;
  184. }
  185. virtual bool isSelectiveDownloadingMode() CXX11_OVERRIDE;
  186. virtual bool isEndGame() CXX11_OVERRIDE
  187. {
  188. return endGame_;
  189. }
  190. virtual void enterEndGame() CXX11_OVERRIDE
  191. {
  192. endGame_ = true;
  193. }
  194. virtual std::shared_ptr<DiskAdaptor> getDiskAdaptor() CXX11_OVERRIDE;
  195. virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE;
  196. virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE;
  197. virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE;
  198. virtual void advertisePiece(cuid_t cuid, size_t index) CXX11_OVERRIDE;
  199. virtual void
  200. getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
  201. cuid_t myCuid, const Timer& lastCheckTime)
  202. CXX11_OVERRIDE;
  203. virtual void
  204. removeAdvertisedPiece(const std::chrono::seconds& elapsed) CXX11_OVERRIDE;
  205. virtual void markAllPiecesDone() CXX11_OVERRIDE;
  206. virtual void markPiecesDone(int64_t length) CXX11_OVERRIDE;
  207. virtual void markPieceMissing(size_t index) CXX11_OVERRIDE;
  208. virtual void addInFlightPiece
  209. (const std::vector<std::shared_ptr<Piece> >& pieces) CXX11_OVERRIDE;
  210. virtual size_t countInFlightPiece() CXX11_OVERRIDE;
  211. virtual void getInFlightPieces
  212. (std::vector<std::shared_ptr<Piece> >& pieces) CXX11_OVERRIDE;
  213. virtual void addPieceStats(size_t index) CXX11_OVERRIDE;
  214. virtual void addPieceStats(const unsigned char* bitfield,
  215. size_t bitfieldLength) CXX11_OVERRIDE;
  216. virtual void subtractPieceStats(const unsigned char* bitfield,
  217. size_t bitfieldLength) CXX11_OVERRIDE;
  218. virtual void updatePieceStats(const unsigned char* newBitfield,
  219. size_t newBitfieldLength,
  220. const unsigned char* oldBitfield)
  221. CXX11_OVERRIDE;
  222. virtual size_t getNextUsedIndex(size_t index) CXX11_OVERRIDE;
  223. virtual void onDownloadIncomplete() CXX11_OVERRIDE;
  224. /**
  225. * This method is made private for test purpose only.
  226. */
  227. void addUsedPiece(const std::shared_ptr<Piece>& piece);
  228. void setDiskWriterFactory
  229. (const std::shared_ptr<DiskWriterFactory>& diskWriterFactory);
  230. const std::shared_ptr<PieceStatMan>& getPieceStatMan() const
  231. {
  232. return pieceStatMan_;
  233. }
  234. void setPieceSelector(std::unique_ptr<PieceSelector> pieceSelector);
  235. const std::unique_ptr<PieceSelector>& getPieceSelector() const
  236. {
  237. return pieceSelector_;
  238. }
  239. std::unique_ptr<PieceSelector> popPieceSelector();
  240. void setWrDiskCache(WrDiskCache* wrDiskCache)
  241. {
  242. wrDiskCache_ = wrDiskCache;
  243. }
  244. };
  245. } // namespace aria2
  246. #endif // D_DEFAULT_PIECE_STORAGE_H