DefaultPieceStorage.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "BtContext.h"
  39. #include "DiskAdaptor.h"
  40. #include "BitfieldMan.h"
  41. #include "Logger.h"
  42. #include "Option.h"
  43. #include "Piece.h"
  44. #include "FileAllocator.h"
  45. #define END_GAME_PIECE_NUM 20
  46. class HaveEntry {
  47. private:
  48. int cuid;
  49. int index;
  50. Time registeredTime;
  51. public:
  52. HaveEntry(int cuid, int index):
  53. cuid(cuid),
  54. index(index) {}
  55. int getCuid() const { return cuid; }
  56. int getIndex() const { return index; }
  57. const Time& getRegisteredTime() const { return registeredTime; }
  58. };
  59. typedef deque<HaveEntry> Haves;
  60. class DefaultPieceStorage : public PieceStorage {
  61. private:
  62. BtContextHandle btContext;
  63. BitfieldMan* bitfieldMan;
  64. DiskAdaptorHandle diskAdaptor;
  65. Pieces usedPieces;
  66. uint32_t endGamePieceNum;
  67. Logger* logger;
  68. const Option* option;
  69. Haves haves;
  70. FileAllocatorHandle createFileAllocator();
  71. int getMissingPieceIndex(const PeerHandle& peer);
  72. int getMissingFastPieceIndex(const PeerHandle& peer);
  73. PieceHandle checkOutPiece(int index);
  74. int deleteUsedPiecesByFillRate(int fillRate, int toDelete);
  75. void reduceUsedPieces(int delMax);
  76. void deleteUsedPiece(const PieceHandle& piece);
  77. PieceHandle findUsedPiece(int index) const;
  78. public:
  79. DefaultPieceStorage(BtContextHandle btContext, const Option* option);
  80. virtual ~DefaultPieceStorage();
  81. virtual bool hasMissingPiece(const PeerHandle& peer);
  82. virtual PieceHandle getMissingPiece(const PeerHandle& peer);
  83. virtual PieceHandle getMissingFastPiece(const PeerHandle& peer);
  84. virtual PieceHandle getPiece(int index);
  85. virtual void completePiece(const PieceHandle& piece);
  86. virtual void cancelPiece(const PieceHandle& piece);
  87. virtual bool hasPiece(int index);
  88. virtual long long int getTotalLength();
  89. virtual long long int getFilteredTotalLength();
  90. virtual long long int getCompletedLength();
  91. virtual long long int getFilteredCompletedLength();
  92. virtual void initStorage();
  93. virtual void setFileFilter(const Strings& filePaths);
  94. virtual void setFileFilter(const Integers& fileIndexes);
  95. virtual void clearFileFilter();
  96. virtual bool downloadFinished();
  97. virtual void setBitfield(const unsigned char* bitfield,
  98. int bitfieldLength);
  99. virtual int getBitfieldLength();
  100. virtual const unsigned char* getBitfield();
  101. void setEndGamePieceNum(uint32_t num) {
  102. endGamePieceNum = num;
  103. }
  104. uint32_t getEndGamePieceNum() const {
  105. return endGamePieceNum;
  106. }
  107. virtual bool isSelectiveDownloadingMode();
  108. virtual void finishSelectiveDownloadingMode();
  109. virtual bool isEndGame();
  110. virtual DiskAdaptorHandle getDiskAdaptor();
  111. virtual int getPieceLength(int index);
  112. virtual void advertisePiece(int cuid, int index);
  113. virtual Integers getAdvertisedPieceIndexes(int myCuid,
  114. const Time& lastCheckTime);
  115. virtual void removeAdvertisedPiece(int elapsed);
  116. /**
  117. * This method is made private for test purpose only.
  118. */
  119. void addUsedPiece(const PieceHandle& piece);
  120. };
  121. #endif // _D_DEFAULT_PIECE_STORAGE_H_