UnknownLengthPieceStorage.h 7.9 KB

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