UnknownLengthPieceStorage.h 7.9 KB

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