UnknownLengthPieceStorage.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_UNKNOWN_LENGTH_PIECE_STORAGE_H
  36. #define D_UNKNOWN_LENGTH_PIECE_STORAGE_H
  37. #include "PieceStorage.h"
  38. namespace aria2 {
  39. class Option;
  40. class DownloadContext;
  41. class DiskWriterFactory;
  42. class DirectDiskAdaptor;
  43. class UnknownLengthPieceStorage:public PieceStorage {
  44. private:
  45. SharedHandle<DownloadContext> downloadContext_;
  46. const Option* option_;
  47. SharedHandle<DirectDiskAdaptor> diskAdaptor_;
  48. SharedHandle<DiskWriterFactory> diskWriterFactory_;
  49. uint64_t totalLength_;
  50. bool downloadFinished_;
  51. SharedHandle<Piece> piece_;
  52. public:
  53. UnknownLengthPieceStorage(const SharedHandle<DownloadContext>& downloadContext,
  54. const Option* option);
  55. virtual ~UnknownLengthPieceStorage();
  56. #ifdef ENABLE_BITTORRENT
  57. /**
  58. * Returns true if the peer has a piece that localhost doesn't have.
  59. * Otherwise returns false.
  60. */
  61. virtual bool hasMissingPiece(const SharedHandle<Peer>& peer);
  62. virtual void getMissingPiece
  63. (std::vector<SharedHandle<Piece> >& pieces,
  64. size_t minMissingBlocks,
  65. const SharedHandle<Peer>& peer);
  66. virtual void getMissingPiece
  67. (std::vector<SharedHandle<Piece> >& pieces,
  68. size_t minMissingBlocks,
  69. const SharedHandle<Peer>& peer,
  70. const std::vector<size_t>& excludedIndexes);
  71. virtual void getMissingFastPiece
  72. (std::vector<SharedHandle<Piece> >& pieces,
  73. size_t minMissingBlocks,
  74. const SharedHandle<Peer>& peer);
  75. virtual void getMissingFastPiece
  76. (std::vector<SharedHandle<Piece> >& pieces,
  77. size_t minMissingBlocks,
  78. const SharedHandle<Peer>& peer,
  79. const std::vector<size_t>& excludedIndexes);
  80. virtual SharedHandle<Piece> getMissingPiece(const SharedHandle<Peer>& peer);
  81. virtual SharedHandle<Piece> getMissingPiece
  82. (const SharedHandle<Peer>& peer, const std::vector<size_t>& excludedIndexes);
  83. #endif // ENABLE_BITTORRENT
  84. virtual bool hasMissingUnusedPiece();
  85. /**
  86. * Returns a missing piece if available. Otherwise returns 0;
  87. */
  88. virtual SharedHandle<Piece> getSparseMissingUnusedPiece
  89. (size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length);
  90. /**
  91. * Returns a missing piece whose index is index.
  92. * If a piece whose index is index is already acquired or currently used,
  93. * then returns 0.
  94. * Also returns 0 if any of missing piece is not available.
  95. */
  96. virtual SharedHandle<Piece> getMissingPiece(size_t index);
  97. /**
  98. * Returns the piece denoted by index.
  99. * No status of the piece is changed in this method.
  100. */
  101. virtual SharedHandle<Piece> getPiece(size_t index);
  102. /**
  103. * Tells that the download of the specfied piece completes.
  104. */
  105. virtual void completePiece(const SharedHandle<Piece>& piece);
  106. /**
  107. * Tells that the download of the specified piece is canceled.
  108. */
  109. virtual void cancelPiece(const SharedHandle<Piece>& piece);
  110. /**
  111. * Returns true if the specified piece is already downloaded.
  112. * Otherwise returns false.
  113. */
  114. virtual bool hasPiece(size_t index);
  115. virtual bool isPieceUsed(size_t index);
  116. virtual uint64_t getTotalLength()
  117. {
  118. return totalLength_;
  119. }
  120. virtual uint64_t getFilteredTotalLength()
  121. {
  122. return totalLength_;
  123. }
  124. virtual uint64_t getCompletedLength()
  125. {
  126. // TODO we have to return actual completed length here?
  127. return totalLength_;
  128. }
  129. virtual uint64_t getFilteredCompletedLength()
  130. {
  131. return getCompletedLength();
  132. }
  133. virtual void setupFileFilter() {}
  134. virtual void clearFileFilter() {}
  135. /**
  136. * Returns true if download has completed.
  137. * If file filter is enabled, then returns true if those files have
  138. * downloaded.
  139. */
  140. virtual bool downloadFinished()
  141. {
  142. return downloadFinished_;
  143. }
  144. /**
  145. * Returns true if all files have downloaded.
  146. * The file filter is ignored.
  147. */
  148. virtual bool allDownloadFinished()
  149. {
  150. return downloadFinished();
  151. }
  152. /**
  153. * Initializes DiskAdaptor.
  154. * TODO add better documentation here.
  155. */
  156. virtual void initStorage();
  157. virtual const unsigned char* getBitfield()
  158. {
  159. return 0;
  160. }
  161. virtual void setBitfield(const unsigned char* bitfield,
  162. size_t bitfieldLength) {}
  163. virtual size_t getBitfieldLength()
  164. {
  165. return 0;
  166. }
  167. virtual bool isSelectiveDownloadingMode()
  168. {
  169. return false;
  170. }
  171. virtual bool isEndGame()
  172. {
  173. return false;
  174. }
  175. virtual void enterEndGame() {}
  176. virtual void setEndGamePieceNum(size_t num) {}
  177. virtual SharedHandle<DiskAdaptor> getDiskAdaptor();
  178. virtual size_t getPieceLength(size_t index);
  179. /**
  180. * Adds piece index to advertise to other commands. They send have message
  181. * based on this information.
  182. */
  183. virtual void advertisePiece(cuid_t cuid, size_t index) {}
  184. /**
  185. * Returns piece index which is not advertised by the caller command and
  186. * newer than lastCheckTime.
  187. */
  188. virtual void
  189. getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
  190. cuid_t myCuid, const Timer& lastCheckTime)
  191. {}
  192. /**
  193. * Removes have entry if specified seconds have elapsed since its
  194. * registration.
  195. */
  196. virtual void removeAdvertisedPiece(time_t elapsed) {}
  197. /**
  198. * Sets all bits in bitfield to 1.
  199. */
  200. virtual void markAllPiecesDone();
  201. virtual void markPiecesDone(uint64_t length);
  202. virtual void markPieceMissing(size_t index);
  203. /**
  204. * Do nothing because loading in-flight piece is not supported for this
  205. * class.
  206. */
  207. virtual void addInFlightPiece
  208. (const std::vector<SharedHandle<Piece> >& pieces) {}
  209. virtual size_t countInFlightPiece()
  210. {
  211. return 0;
  212. }
  213. virtual void getInFlightPieces(std::vector<SharedHandle<Piece> >& pieces);
  214. virtual void addPieceStats(size_t index) {}
  215. virtual void addPieceStats(const unsigned char* bitfield,
  216. size_t bitfieldLength) {}
  217. virtual void subtractPieceStats(const unsigned char* bitfield,
  218. size_t bitfieldLength) {}
  219. virtual void updatePieceStats(const unsigned char* newBitfield,
  220. size_t newBitfieldLength,
  221. const unsigned char* oldBitfield) {}
  222. virtual size_t getNextUsedIndex(size_t index) { return 0; }
  223. void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
  224. };
  225. typedef SharedHandle<UnknownLengthPieceStorage> UnknownLengthPieceStorageHandle;
  226. } // namespace aria2
  227. #endif // D_UNKNOWN_LENGTH_PIECE_STORAGE_H