DefaultPieceStorage.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #define END_GAME_PIECE_NUM 20
  46. class HaveEntry {
  47. private:
  48. int32_t cuid;
  49. int32_t index;
  50. Time registeredTime;
  51. public:
  52. HaveEntry(int32_t cuid, int32_t index):
  53. cuid(cuid),
  54. index(index) {}
  55. int32_t getCuid() const { return cuid; }
  56. int32_t getIndex() const { return index; }
  57. const Time& getRegisteredTime() const { return registeredTime; }
  58. };
  59. typedef std::deque<HaveEntry> Haves;
  60. class DefaultPieceStorage : public PieceStorage {
  61. private:
  62. SharedHandle<DownloadContext> downloadContext;
  63. BitfieldMan* bitfieldMan;
  64. SharedHandle<DiskAdaptor> diskAdaptor;
  65. SharedHandle<DiskWriterFactory> _diskWriterFactory;
  66. std::deque<SharedHandle<Piece> > usedPieces;
  67. int32_t endGamePieceNum;
  68. Logger* logger;
  69. const Option* option;
  70. Haves haves;
  71. int32_t getMissingPieceIndex(const SharedHandle<Peer>& peer);
  72. int32_t getMissingFastPieceIndex(const SharedHandle<Peer>& peer);
  73. SharedHandle<Piece> checkOutPiece(int32_t index);
  74. int32_t deleteUsedPiecesByFillRate(int32_t fillRate, int32_t toDelete);
  75. void reduceUsedPieces(int32_t delMax);
  76. void deleteUsedPiece(const SharedHandle<Piece>& piece);
  77. SharedHandle<Piece> findUsedPiece(int32_t index) const;
  78. int32_t getInFlightPieceCompletedLength() const;
  79. public:
  80. DefaultPieceStorage(const SharedHandle<DownloadContext>& downloadContext, const Option* option);
  81. virtual ~DefaultPieceStorage();
  82. virtual bool hasMissingPiece(const SharedHandle<Peer>& peer);
  83. virtual SharedHandle<Piece> getMissingPiece(const SharedHandle<Peer>& peer);
  84. virtual SharedHandle<Piece> getMissingFastPiece(const SharedHandle<Peer>& peer);
  85. virtual SharedHandle<Piece> getMissingPiece();
  86. virtual SharedHandle<Piece> getMissingPiece(int32_t index);
  87. virtual SharedHandle<Piece> getPiece(int32_t index);
  88. virtual void completePiece(const SharedHandle<Piece>& piece);
  89. virtual void cancelPiece(const SharedHandle<Piece>& piece);
  90. virtual bool hasPiece(int32_t index);
  91. virtual bool isPieceUsed(int32_t index);
  92. virtual int64_t getTotalLength();
  93. virtual int64_t getFilteredTotalLength();
  94. virtual int64_t getCompletedLength();
  95. virtual int64_t getFilteredCompletedLength();
  96. virtual void initStorage();
  97. virtual void setFileFilter(const std::deque<std::string>& filePaths);
  98. virtual void setFileFilter(IntSequence seq);
  99. virtual void clearFileFilter();
  100. virtual bool downloadFinished();
  101. virtual bool allDownloadFinished();
  102. virtual void setBitfield(const unsigned char* bitfield,
  103. int32_t bitfieldLength);
  104. virtual int32_t getBitfieldLength();
  105. virtual const unsigned char* getBitfield();
  106. void setEndGamePieceNum(int32_t num) {
  107. endGamePieceNum = num;
  108. }
  109. int32_t getEndGamePieceNum() const {
  110. return endGamePieceNum;
  111. }
  112. virtual bool isSelectiveDownloadingMode();
  113. virtual void finishSelectiveDownloadingMode();
  114. virtual bool isEndGame();
  115. virtual SharedHandle<DiskAdaptor> getDiskAdaptor();
  116. virtual int32_t getPieceLength(int32_t index);
  117. virtual void advertisePiece(int32_t cuid, int32_t index);
  118. virtual std::deque<int32_t> getAdvertisedPieceIndexes(int32_t myCuid,
  119. const Time& lastCheckTime);
  120. virtual void removeAdvertisedPiece(int32_t elapsed);
  121. virtual void markAllPiecesDone();
  122. virtual void markPiecesDone(int64_t length);
  123. virtual void markPieceMissing(int32_t index);
  124. virtual void addInFlightPiece(const std::deque<SharedHandle<Piece> >& pieces);
  125. virtual int32_t countInFlightPiece();
  126. virtual std::deque<SharedHandle<Piece> > getInFlightPieces();
  127. /**
  128. * This method is made private for test purpose only.
  129. */
  130. void addUsedPiece(const SharedHandle<Piece>& piece);
  131. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  132. };
  133. typedef SharedHandle<DefaultPieceStorage> DefaultPieceStorageHandle;
  134. } // namespace aria2
  135. #endif // _D_DEFAULT_PIECE_STORAGE_H_