PieceStorage.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_PIECE_STORAGE_H_
  36. #define _D_PIECE_STORAGE_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <vector>
  40. #include "SharedHandle.h"
  41. #include "TimerA2.h"
  42. #include "Command.h"
  43. namespace aria2 {
  44. class Piece;
  45. #ifdef ENABLE_BITTORRENT
  46. class Peer;
  47. #endif // ENABLE_BITTORRENT
  48. class DiskAdaptor;
  49. class PieceStorage {
  50. public:
  51. virtual ~PieceStorage() {}
  52. #ifdef ENABLE_BITTORRENT
  53. /**
  54. * Returns true if the peer has a piece that localhost doesn't have.
  55. * Otherwise returns false.
  56. */
  57. virtual bool hasMissingPiece(const SharedHandle<Peer>& peer) = 0;
  58. /**
  59. * Returns a piece that the peer has but localhost doesn't.
  60. * The piece will be marked "used" status in order to prevent other command
  61. * from get the same piece. But in end game mode, same piece may be returned
  62. * to several commands.
  63. */
  64. virtual SharedHandle<Piece>
  65. getMissingPiece(const SharedHandle<Peer>& peer) = 0;
  66. /**
  67. * Same as getMissingPiece(const SharedHandle<Peer>& peer), but the indexes in
  68. * excludedIndexes are excluded.
  69. */
  70. virtual SharedHandle<Piece> getMissingPiece
  71. (const SharedHandle<Peer>& peer,
  72. const std::vector<size_t>& excludedIndexes) = 0;
  73. /**
  74. * Returns a piece that the peer has but localhost doesn't.
  75. * Only pieces that declared as "fast" are returned.
  76. * The piece will be marked "used" status in order to prevent other command
  77. * from get the same piece. But in end game mode, same piece may be returned
  78. * to several commands.
  79. */
  80. virtual SharedHandle<Piece>
  81. getMissingFastPiece(const SharedHandle<Peer>& peer) = 0;
  82. /**
  83. * Same as getMissingFastPiece(const SharedHandle<Peer>& peer), but the
  84. * indexes in excludedIndexes are excluded.
  85. */
  86. virtual SharedHandle<Piece> getMissingFastPiece
  87. (const SharedHandle<Peer>& peer,
  88. const std::vector<size_t>& excludedIndexes) = 0;
  89. #endif // ENABLE_BITTORRENT
  90. // Returns true if there is at least one missing and unused piece.
  91. virtual bool hasMissingUnusedPiece() = 0;
  92. /**
  93. * Returns a missing piece if available. Otherwise returns 0;
  94. * If ignoreBitfield is set, indexes of true bit are excluded.
  95. */
  96. virtual SharedHandle<Piece> getSparseMissingUnusedPiece
  97. (size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length) = 0;
  98. /**
  99. * Returns a missing piece whose index is index.
  100. * If a piece whose index is index is already acquired or currently used,
  101. * then returns 0.
  102. * Also returns 0 if any of missing piece is not available.
  103. */
  104. virtual SharedHandle<Piece> getMissingPiece(size_t index) = 0;
  105. /**
  106. * Returns the piece denoted by index.
  107. * No status of the piece is changed in this method.
  108. */
  109. virtual SharedHandle<Piece> getPiece(size_t index) = 0;
  110. /**
  111. * Marks the piece whose index is index as missing.
  112. */
  113. virtual void markPieceMissing(size_t index) = 0;
  114. /**
  115. * Tells that the download of the specfied piece completes.
  116. */
  117. virtual void completePiece(const SharedHandle<Piece>& piece) = 0;
  118. /**
  119. * Tells that the download of the specified piece is canceled.
  120. */
  121. virtual void cancelPiece(const SharedHandle<Piece>& piece) = 0;
  122. /**
  123. * Returns true if the specified piece is already downloaded.
  124. * Otherwise returns false.
  125. */
  126. virtual bool hasPiece(size_t index) = 0;
  127. virtual bool isPieceUsed(size_t index) = 0;
  128. virtual uint64_t getTotalLength() = 0;
  129. virtual uint64_t getFilteredTotalLength() = 0;
  130. virtual uint64_t getCompletedLength() = 0;
  131. virtual uint64_t getFilteredCompletedLength() = 0;
  132. virtual void setupFileFilter() = 0;
  133. virtual void clearFileFilter() = 0;
  134. /**
  135. * Returns true if download has completed.
  136. * If file filter is enabled, then returns true if those files have
  137. * downloaded.
  138. */
  139. virtual bool downloadFinished() = 0;
  140. /**
  141. * Returns true if all files have downloaded.
  142. * The file filter is ignored.
  143. */
  144. virtual bool allDownloadFinished() = 0;
  145. /**
  146. * Initializes DiskAdaptor.
  147. * TODO add better documentation here.
  148. */
  149. virtual void initStorage() = 0;
  150. virtual const unsigned char* getBitfield() = 0;
  151. virtual void setBitfield(const unsigned char* bitfield,
  152. size_t bitfieldLength) = 0;
  153. virtual size_t getBitfieldLength() = 0;
  154. virtual bool isSelectiveDownloadingMode() = 0;
  155. virtual bool isEndGame() = 0;
  156. virtual void setEndGamePieceNum(size_t num) = 0;
  157. virtual SharedHandle<DiskAdaptor> getDiskAdaptor() = 0;
  158. virtual size_t getPieceLength(size_t index) = 0;
  159. /**
  160. * Adds piece index to advertise to other commands. They send have message
  161. * based on this information.
  162. */
  163. virtual void advertisePiece(cuid_t cuid, size_t index) = 0;
  164. /**
  165. * indexes is filled with piece index which is not advertised by the caller
  166. * command and newer than lastCheckTime.
  167. */
  168. virtual void getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
  169. cuid_t myCuid,
  170. const Timer& lastCheckTime) = 0;
  171. /**
  172. * Removes have entry if specified seconds have elapsed since its
  173. * registration.
  174. */
  175. virtual void removeAdvertisedPiece(time_t elapsed) = 0;
  176. /**
  177. * Sets all bits in bitfield to 1.
  178. */
  179. virtual void markAllPiecesDone() = 0;
  180. /**
  181. * Sets all bits in bitfield(0 to length) to 1.
  182. */
  183. virtual void markPiecesDone(uint64_t length) = 0;
  184. virtual void
  185. addInFlightPiece(const std::vector<SharedHandle<Piece> >& pieces) = 0;
  186. virtual size_t countInFlightPiece() = 0;
  187. virtual void getInFlightPieces(std::vector<SharedHandle<Piece> >& pieces) = 0;
  188. virtual void addPieceStats(size_t index) = 0;
  189. virtual void addPieceStats(const unsigned char* bitfield,
  190. size_t bitfieldLength) = 0;
  191. virtual void subtractPieceStats(const unsigned char* bitfield,
  192. size_t bitfieldLength) = 0;
  193. virtual void updatePieceStats(const unsigned char* newBitfield,
  194. size_t newBitfieldLength,
  195. const unsigned char* oldBitfield) = 0;
  196. // Returns index x where all pieces in [index+1, x-1], inclusive,
  197. // are not used and not completed. If all pieces after index+1 are
  198. // used or completed, returns the number of pieces.
  199. virtual size_t getNextUsedIndex(size_t index) = 0;
  200. };
  201. typedef SharedHandle<PieceStorage> PieceStorageHandle;
  202. } // namespace aria2
  203. #endif // _D_PIECE_STORAGE_H_