UnknownLengthPieceStorage.h 7.3 KB

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