PieceStorage.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "Peer.h"
  39. #include "Piece.h"
  40. #include "DiskAdaptor.h"
  41. class PieceStorage {
  42. public:
  43. virtual ~PieceStorage() {}
  44. /**
  45. * Returns true if the peer has a piece that localhost doesn't have.
  46. * Otherwise returns false.
  47. */
  48. virtual bool hasMissingPiece(const PeerHandle& peer) = 0;
  49. /**
  50. * Returns a piece that the peer has but localhost doesn't.
  51. * The piece will be marked "used" status in order to prevent other command
  52. * from get the same piece. But in end game mode, same piece may be returned
  53. * to several commands.
  54. */
  55. virtual PieceHandle getMissingPiece(const PeerHandle& peer) = 0;
  56. /**
  57. * Returns a piece that the peer has but localhost doesn't.
  58. * Only pieces that declared as "fast" are returned.
  59. * The piece will be marked "used" status in order to prevent other command
  60. * from get the same piece. But in end game mode, same piece may be returned
  61. * to several commands.
  62. */
  63. virtual PieceHandle getMissingFastPiece(const PeerHandle& peer) = 0;
  64. /**
  65. * Returns the piece denoted by index.
  66. * No status of the piece is changed in this method.
  67. */
  68. virtual PieceHandle getPiece(int index) = 0;
  69. /**
  70. * Tells that the download of the specfied piece completes.
  71. */
  72. virtual void completePiece(const PieceHandle& piece) = 0;
  73. /**
  74. * Tells that the download of the specified piece is canceled.
  75. */
  76. virtual void cancelPiece(const PieceHandle& piece) = 0;
  77. /**
  78. * Returns true if the specified piece is already downloaded.
  79. * Otherwise returns false.
  80. */
  81. virtual bool hasPiece(int index) = 0;
  82. virtual long long int getTotalLength() = 0;
  83. virtual long long int getFilteredTotalLength() = 0;
  84. virtual long long int getCompletedLength() = 0;
  85. virtual long long int getFilteredCompletedLength() = 0;
  86. virtual void setFileFilter(const Strings& filePaths) = 0;
  87. virtual void setFileFilter(const Integers& fileIndexes) = 0;
  88. virtual void clearFileFilter() = 0;
  89. /**
  90. * Returns true if download has completed.
  91. * If file filter is enabled, then returns true if those files have
  92. * downloaded.
  93. */
  94. virtual bool downloadFinished() = 0;
  95. /**
  96. * Returns true if all files have downloaded.
  97. * The file filter is ignored.
  98. */
  99. virtual bool allDownloadFinished() = 0;
  100. /**
  101. * Initializes DiskAdaptor.
  102. * TODO add better documentation here.
  103. */
  104. virtual void initStorage() = 0;
  105. virtual const unsigned char* getBitfield() = 0;
  106. virtual void setBitfield(const unsigned char* bitfield,
  107. int bitfieldLength) = 0;
  108. virtual int getBitfieldLength() = 0;
  109. virtual bool isSelectiveDownloadingMode() = 0;
  110. virtual void finishSelectiveDownloadingMode() = 0;
  111. virtual bool isEndGame() = 0;
  112. virtual DiskAdaptorHandle getDiskAdaptor() = 0;
  113. virtual int getPieceLength(int index) = 0;
  114. /**
  115. * Adds piece index to advertise to other commands. They send have message
  116. * based on this information.
  117. */
  118. virtual void advertisePiece(int cuid, int index) = 0;
  119. /**
  120. * Returns piece index which is not advertised by the caller command and
  121. * newer than lastCheckTime.
  122. */
  123. virtual Integers getAdvertisedPieceIndexes(int myCuid,
  124. const Time& lastCheckTime) = 0;
  125. /**
  126. * Removes have entry if specified seconds have elapsed since its
  127. * registration.
  128. */
  129. virtual void removeAdvertisedPiece(int elapsed) = 0;
  130. /**
  131. * Sets all bits in bitfield to 1.
  132. */
  133. virtual void markAllPiecesDone() = 0;
  134. /**
  135. * Validates file integrity by comparing checksums.
  136. */
  137. virtual void checkIntegrity() = 0;
  138. };
  139. typedef SharedHandle<PieceStorage> PieceStorageHandle;
  140. #endif // _D_PIECE_STORAGE_H_