DefaultPieceStorage.h 5.5 KB

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