DefaultPieceStorage.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. namespace aria2 {
  39. class DownloadContext;
  40. class BitfieldMan;
  41. class Logger;
  42. class Option;
  43. class DiskWriterFactory;
  44. class FileEntry;
  45. class RarestPieceSelector;
  46. #define END_GAME_PIECE_NUM 20
  47. class HaveEntry {
  48. private:
  49. int32_t cuid;
  50. size_t index;
  51. Time registeredTime;
  52. public:
  53. HaveEntry(int32_t cuid, size_t index):
  54. cuid(cuid),
  55. index(index) {}
  56. int32_t getCuid() const { return cuid; }
  57. size_t getIndex() const { return index; }
  58. const Time& getRegisteredTime() const { return registeredTime; }
  59. };
  60. typedef std::deque<HaveEntry> Haves;
  61. class DefaultPieceStorage : public PieceStorage {
  62. private:
  63. SharedHandle<DownloadContext> downloadContext;
  64. BitfieldMan* bitfieldMan;
  65. SharedHandle<DiskAdaptor> diskAdaptor;
  66. SharedHandle<DiskWriterFactory> _diskWriterFactory;
  67. std::deque<SharedHandle<Piece> > usedPieces;
  68. size_t endGamePieceNum;
  69. Logger* logger;
  70. const Option* option;
  71. Haves haves;
  72. SharedHandle<RarestPieceSelector> _pieceSelector;
  73. bool getMissingPieceIndex(size_t& index,
  74. const unsigned char* bitfield, size_t& length);
  75. SharedHandle<Piece> getMissingPiece(const unsigned char* bitfield,
  76. size_t length);
  77. SharedHandle<Piece> getMissingPiece(const BitfieldMan& bitfield);
  78. void createFastIndexBitfield(BitfieldMan& bitfield,
  79. const SharedHandle<Peer>& peer);
  80. SharedHandle<Piece> checkOutPiece(size_t index);
  81. // size_t deleteUsedPiecesByFillRate(int fillRate, size_t toDelete);
  82. // void reduceUsedPieces(size_t upperBound);
  83. void deleteUsedPiece(const SharedHandle<Piece>& piece);
  84. SharedHandle<Piece> findUsedPiece(size_t index) const;
  85. size_t getInFlightPieceCompletedLength() const;
  86. public:
  87. // Setting randomPieceStatsOrdering to true means a piece is chosen in
  88. // random when more than 2 pieces has the same rarity.
  89. // If it is set to false, a piece whose index is smallest has the highest
  90. // priority.
  91. DefaultPieceStorage(const SharedHandle<DownloadContext>& downloadContext,
  92. const Option* option,
  93. bool randomPieceStatsOrdering = true);
  94. virtual ~DefaultPieceStorage();
  95. virtual bool hasMissingPiece(const SharedHandle<Peer>& peer);
  96. virtual SharedHandle<Piece> getMissingPiece(const SharedHandle<Peer>& peer);
  97. virtual SharedHandle<Piece> getMissingFastPiece(const SharedHandle<Peer>& peer);
  98. virtual SharedHandle<Piece> getMissingPiece
  99. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes);
  100. virtual SharedHandle<Piece> getMissingFastPiece
  101. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes);
  102. virtual SharedHandle<Piece> getMissingPiece();
  103. virtual SharedHandle<Piece> getMissingPiece(size_t index);
  104. virtual SharedHandle<Piece> getPiece(size_t index);
  105. virtual void completePiece(const SharedHandle<Piece>& piece);
  106. virtual void cancelPiece(const SharedHandle<Piece>& piece);
  107. virtual bool hasPiece(size_t index);
  108. virtual bool isPieceUsed(size_t index);
  109. virtual uint64_t getTotalLength();
  110. virtual uint64_t getFilteredTotalLength();
  111. virtual uint64_t getCompletedLength();
  112. virtual uint64_t getFilteredCompletedLength();
  113. virtual void initStorage();
  114. virtual void setFileFilter(const std::deque<std::string>& filePaths);
  115. virtual void setFileFilter(IntSequence seq);
  116. virtual void clearFileFilter();
  117. virtual bool downloadFinished();
  118. virtual bool allDownloadFinished();
  119. virtual void setBitfield(const unsigned char* bitfield,
  120. size_t bitfieldLength);
  121. virtual size_t getBitfieldLength();
  122. virtual const unsigned char* getBitfield();
  123. void setEndGamePieceNum(size_t num) {
  124. endGamePieceNum = num;
  125. }
  126. size_t getEndGamePieceNum() const {
  127. return endGamePieceNum;
  128. }
  129. virtual bool isSelectiveDownloadingMode();
  130. virtual void finishSelectiveDownloadingMode();
  131. virtual bool isEndGame();
  132. virtual SharedHandle<DiskAdaptor> getDiskAdaptor();
  133. virtual size_t getPieceLength(size_t index);
  134. virtual void advertisePiece(int32_t cuid, size_t index);
  135. virtual void
  136. getAdvertisedPieceIndexes(std::deque<size_t>& indexes,
  137. int32_t myCuid, const Time& lastCheckTime);
  138. virtual void removeAdvertisedPiece(time_t elapsed);
  139. virtual void markAllPiecesDone();
  140. virtual void markPiecesDone(uint64_t length);
  141. virtual void markPieceMissing(size_t index);
  142. virtual void addInFlightPiece(const std::deque<SharedHandle<Piece> >& pieces);
  143. virtual size_t countInFlightPiece();
  144. virtual void getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces);
  145. virtual void addPieceStats(size_t index);
  146. virtual void addPieceStats(const unsigned char* bitfield,
  147. size_t bitfieldLength);
  148. virtual void subtractPieceStats(const unsigned char* bitfield,
  149. size_t bitfieldLength);
  150. virtual void updatePieceStats(const unsigned char* newBitfield,
  151. size_t newBitfieldLength,
  152. const unsigned char* oldBitfield);
  153. /**
  154. * This method is made private for test purpose only.
  155. */
  156. void addUsedPiece(const SharedHandle<Piece>& piece);
  157. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  158. };
  159. typedef SharedHandle<DefaultPieceStorage> DefaultPieceStorageHandle;
  160. } // namespace aria2
  161. #endif // _D_DEFAULT_PIECE_STORAGE_H_