DefaultPieceStorage.h 8.7 KB

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