UnknownLengthPieceStorage.h 7.6 KB

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