PieceStorage.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_PIECE_STORAGE_H_
  36. #define _D_PIECE_STORAGE_H_
  37. #include "common.h"
  38. #include "SharedHandle.h"
  39. #include "TimeA2.h"
  40. #include "IntSequence.h"
  41. #include <string>
  42. #include <deque>
  43. namespace aria2 {
  44. class Piece;
  45. class Peer;
  46. class DiskAdaptor;
  47. class PieceStorage {
  48. public:
  49. virtual ~PieceStorage() {}
  50. /**
  51. * Returns true if the peer has a piece that localhost doesn't have.
  52. * Otherwise returns false.
  53. */
  54. virtual bool hasMissingPiece(const SharedHandle<Peer>& peer) = 0;
  55. /**
  56. * Returns a piece that the peer has but localhost doesn't.
  57. * The piece will be marked "used" status in order to prevent other command
  58. * from get the same piece. But in end game mode, same piece may be returned
  59. * to several commands.
  60. */
  61. virtual SharedHandle<Piece>
  62. getMissingPiece(const SharedHandle<Peer>& peer) = 0;
  63. /**
  64. * Returns a piece that the peer has but localhost doesn't.
  65. * Only pieces that declared as "fast" are returned.
  66. * The piece will be marked "used" status in order to prevent other command
  67. * from get the same piece. But in end game mode, same piece may be returned
  68. * to several commands.
  69. */
  70. virtual SharedHandle<Piece>
  71. getMissingFastPiece(const SharedHandle<Peer>& peer) = 0;
  72. /**
  73. * Returns a missing piece if available. Otherwise returns 0;
  74. */
  75. virtual SharedHandle<Piece> getMissingPiece() = 0;
  76. /**
  77. * Returns a missing piece whose index is index.
  78. * If a piece whose index is index is already acquired or currently used,
  79. * then returns 0.
  80. * Also returns 0 if any of missing piece is not available.
  81. */
  82. virtual SharedHandle<Piece> getMissingPiece(int32_t index) = 0;
  83. /**
  84. * Returns the piece denoted by index.
  85. * No status of the piece is changed in this method.
  86. */
  87. virtual SharedHandle<Piece> getPiece(int32_t index) = 0;
  88. /**
  89. * Marks the piece whose index is index as missing.
  90. */
  91. virtual void markPieceMissing(int32_t index) = 0;
  92. /**
  93. * Tells that the download of the specfied piece completes.
  94. */
  95. virtual void completePiece(const SharedHandle<Piece>& piece) = 0;
  96. /**
  97. * Tells that the download of the specified piece is canceled.
  98. */
  99. virtual void cancelPiece(const SharedHandle<Piece>& piece) = 0;
  100. /**
  101. * Returns true if the specified piece is already downloaded.
  102. * Otherwise returns false.
  103. */
  104. virtual bool hasPiece(int32_t index) = 0;
  105. virtual bool isPieceUsed(int32_t index) = 0;
  106. virtual int64_t getTotalLength() = 0;
  107. virtual int64_t getFilteredTotalLength() = 0;
  108. virtual int64_t getCompletedLength() = 0;
  109. virtual int64_t getFilteredCompletedLength() = 0;
  110. virtual void setFileFilter(const std::deque<std::string>& filePaths) = 0;
  111. virtual void setFileFilter(IntSequence seq) = 0;
  112. virtual void clearFileFilter() = 0;
  113. /**
  114. * Returns true if download has completed.
  115. * If file filter is enabled, then returns true if those files have
  116. * downloaded.
  117. */
  118. virtual bool downloadFinished() = 0;
  119. /**
  120. * Returns true if all files have downloaded.
  121. * The file filter is ignored.
  122. */
  123. virtual bool allDownloadFinished() = 0;
  124. /**
  125. * Initializes DiskAdaptor.
  126. * TODO add better documentation here.
  127. */
  128. virtual void initStorage() = 0;
  129. virtual const unsigned char* getBitfield() = 0;
  130. virtual void setBitfield(const unsigned char* bitfield,
  131. int32_t bitfieldLength) = 0;
  132. virtual int32_t getBitfieldLength() = 0;
  133. virtual bool isSelectiveDownloadingMode() = 0;
  134. virtual void finishSelectiveDownloadingMode() = 0;
  135. virtual bool isEndGame() = 0;
  136. virtual SharedHandle<DiskAdaptor> getDiskAdaptor() = 0;
  137. virtual int32_t getPieceLength(int32_t index) = 0;
  138. /**
  139. * Adds piece index to advertise to other commands. They send have message
  140. * based on this information.
  141. */
  142. virtual void advertisePiece(int32_t cuid, int32_t index) = 0;
  143. /**
  144. * Returns piece index which is not advertised by the caller command and
  145. * newer than lastCheckTime.
  146. */
  147. virtual std::deque<int32_t>
  148. getAdvertisedPieceIndexes(int32_t myCuid, const Time& lastCheckTime) = 0;
  149. /**
  150. * Removes have entry if specified seconds have elapsed since its
  151. * registration.
  152. */
  153. virtual void removeAdvertisedPiece(int32_t elapsed) = 0;
  154. /**
  155. * Sets all bits in bitfield to 1.
  156. */
  157. virtual void markAllPiecesDone() = 0;
  158. /**
  159. * Sets all bits in bitfield(0 to length) to 1.
  160. */
  161. virtual void markPiecesDone(int64_t length) = 0;
  162. virtual void
  163. addInFlightPiece(const std::deque<SharedHandle<Piece> >& pieces) = 0;
  164. virtual int32_t countInFlightPiece() = 0;
  165. virtual std::deque<SharedHandle<Piece> > getInFlightPieces() = 0;
  166. };
  167. typedef SharedHandle<PieceStorage> PieceStorageHandle;
  168. } // namespace aria2
  169. #endif // _D_PIECE_STORAGE_H_