UnknownLengthPieceStorage.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. off_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. cuid_t cuid);
  67. virtual void getMissingPiece
  68. (std::vector<SharedHandle<Piece> >& pieces,
  69. size_t minMissingBlocks,
  70. const SharedHandle<Peer>& peer,
  71. const std::vector<size_t>& excludedIndexes,
  72. cuid_t cuid);
  73. virtual void getMissingFastPiece
  74. (std::vector<SharedHandle<Piece> >& pieces,
  75. size_t minMissingBlocks,
  76. const SharedHandle<Peer>& peer,
  77. cuid_t cuid);
  78. virtual void getMissingFastPiece
  79. (std::vector<SharedHandle<Piece> >& pieces,
  80. size_t minMissingBlocks,
  81. const SharedHandle<Peer>& peer,
  82. const std::vector<size_t>& excludedIndexes,
  83. cuid_t cuid);
  84. virtual SharedHandle<Piece> getMissingPiece
  85. (const SharedHandle<Peer>& peer,
  86. cuid_t cuid);
  87. virtual SharedHandle<Piece> getMissingPiece
  88. (const SharedHandle<Peer>& peer,
  89. const std::vector<size_t>& excludedIndexes,
  90. cuid_t cuid);
  91. #endif // ENABLE_BITTORRENT
  92. virtual bool hasMissingUnusedPiece();
  93. /**
  94. * Returns a missing piece if available. Otherwise returns 0;
  95. */
  96. virtual SharedHandle<Piece> getMissingPiece
  97. (size_t minSplitSize,
  98. const unsigned char* ignoreBitfield,
  99. size_t length,
  100. cuid_t cuid);
  101. /**
  102. * Returns a missing piece whose index is index.
  103. * If a piece whose index is index is already acquired or currently used,
  104. * then returns 0.
  105. * Also returns 0 if any of missing piece is not available.
  106. */
  107. virtual SharedHandle<Piece> getMissingPiece(size_t index, cuid_t cuid);
  108. /**
  109. * Returns the piece denoted by index.
  110. * No status of the piece is changed in this method.
  111. */
  112. virtual SharedHandle<Piece> getPiece(size_t index);
  113. /**
  114. * Tells that the download of the specfied piece completes.
  115. */
  116. virtual void completePiece(const SharedHandle<Piece>& piece);
  117. /**
  118. * Tells that the download of the specified piece is canceled.
  119. */
  120. virtual void cancelPiece(const SharedHandle<Piece>& piece, cuid_t cuid);
  121. /**
  122. * Returns true if the specified piece is already downloaded.
  123. * Otherwise returns false.
  124. */
  125. virtual bool hasPiece(size_t index);
  126. virtual bool isPieceUsed(size_t index);
  127. virtual off_t getTotalLength()
  128. {
  129. return totalLength_;
  130. }
  131. virtual off_t getFilteredTotalLength()
  132. {
  133. return totalLength_;
  134. }
  135. virtual off_t getCompletedLength()
  136. {
  137. // TODO we have to return actual completed length here?
  138. return totalLength_;
  139. }
  140. virtual off_t getFilteredCompletedLength()
  141. {
  142. return getCompletedLength();
  143. }
  144. virtual void setupFileFilter() {}
  145. virtual void clearFileFilter() {}
  146. /**
  147. * Returns true if download has completed.
  148. * If file filter is enabled, then returns true if those files have
  149. * downloaded.
  150. */
  151. virtual bool downloadFinished()
  152. {
  153. return downloadFinished_;
  154. }
  155. /**
  156. * Returns true if all files have downloaded.
  157. * The file filter is ignored.
  158. */
  159. virtual bool allDownloadFinished()
  160. {
  161. return downloadFinished();
  162. }
  163. /**
  164. * Initializes DiskAdaptor.
  165. * TODO add better documentation here.
  166. */
  167. virtual void initStorage();
  168. virtual const unsigned char* getBitfield()
  169. {
  170. return 0;
  171. }
  172. virtual void setBitfield(const unsigned char* bitfield,
  173. size_t bitfieldLength) {}
  174. virtual size_t getBitfieldLength()
  175. {
  176. return 0;
  177. }
  178. virtual bool isSelectiveDownloadingMode()
  179. {
  180. return false;
  181. }
  182. virtual bool isEndGame()
  183. {
  184. return false;
  185. }
  186. virtual void enterEndGame() {}
  187. virtual void setEndGamePieceNum(size_t num) {}
  188. virtual SharedHandle<DiskAdaptor> getDiskAdaptor();
  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(off_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. typedef SharedHandle<UnknownLengthPieceStorage> UnknownLengthPieceStorageHandle;
  238. } // namespace aria2
  239. #endif // D_UNKNOWN_LENGTH_PIECE_STORAGE_H