SegmentMan.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_SEGMENT_MAN_H
  36. #define D_SEGMENT_MAN_H
  37. #include "common.h"
  38. #include <deque>
  39. #include <vector>
  40. #include <map>
  41. #include <memory>
  42. #include "TimerA2.h"
  43. #include "Command.h"
  44. #include "BitfieldMan.h"
  45. namespace aria2 {
  46. class Segment;
  47. class Option;
  48. class PeerStat;
  49. class DownloadContext;
  50. class PieceStorage;
  51. class Piece;
  52. class FileEntry;
  53. struct SegmentEntry {
  54. cuid_t cuid;
  55. std::shared_ptr<Segment> segment;
  56. SegmentEntry(cuid_t cuid, const std::shared_ptr<Segment>& segment);
  57. ~SegmentEntry();
  58. // Don't allow copying
  59. SegmentEntry(const SegmentEntry&);
  60. SegmentEntry& operator=(const SegmentEntry&);
  61. };
  62. typedef std::deque<std::shared_ptr<SegmentEntry>> SegmentEntries;
  63. /**
  64. * This class holds the download progress of the one download entry.
  65. */
  66. class SegmentMan {
  67. private:
  68. std::shared_ptr<DownloadContext> downloadContext_;
  69. std::shared_ptr<PieceStorage> pieceStorage_;
  70. SegmentEntries usedSegmentEntries_;
  71. // Remember writtenLength for each segment. The key is an index of a
  72. // segment. The value is writtenLength for that segment.
  73. std::map<size_t, int64_t> segmentWrittenLengthMemo_;
  74. // Used for calculating download speed.
  75. std::vector<std::shared_ptr<PeerStat>> peerStats_;
  76. // Keep track of fastest PeerStat for each server
  77. std::vector<std::shared_ptr<PeerStat>> fastestPeerStats_;
  78. BitfieldMan ignoreBitfield_;
  79. std::shared_ptr<Segment> checkoutSegment(cuid_t cuid,
  80. const std::shared_ptr<Piece>& piece);
  81. void cancelSegmentInternal(cuid_t cuid,
  82. const std::shared_ptr<Segment>& segment);
  83. public:
  84. SegmentMan(const std::shared_ptr<DownloadContext>& downloadContext,
  85. const std::shared_ptr<PieceStorage>& pieceStorage);
  86. ~SegmentMan();
  87. // Initializes totalSize, isSplittable, downloadStarted, errors.
  88. // Clears command queue. Also, closes diskWriter.
  89. void init();
  90. /**
  91. * The total number of bytes to download.
  92. * If Transfer-Encoding is Chunked or Content-Length header is not provided,
  93. * then this value is set to be 0.
  94. */
  95. int64_t getTotalLength() const;
  96. /**
  97. * Returs true when the download has finished.
  98. * If downloadStarted is false or the number of the segments of this object
  99. * holds is 0, then returns false.
  100. */
  101. bool downloadFinished() const;
  102. /**
  103. * Fill segments which are assigned to the command whose CUID is cuid.
  104. * This function doesn't clear passed segments.
  105. */
  106. void getInFlightSegment(std::vector<std::shared_ptr<Segment>>& segments,
  107. cuid_t cuid);
  108. std::shared_ptr<Segment> getSegment(cuid_t cuid, size_t minSplitSize);
  109. // Checkouts segments in the range of fileEntry and push back to
  110. // segments until segments.size() < maxSegments holds false
  111. void getSegment(std::vector<std::shared_ptr<Segment>>& segments, cuid_t cuid,
  112. size_t minSplitSize,
  113. const std::shared_ptr<FileEntry>& fileEntry,
  114. size_t maxSegments);
  115. /**
  116. * Returns a segment whose index is index.
  117. * If it has already assigned
  118. * to another cuid or has been downloaded, then returns a segment instance
  119. * whose isNull call is true.
  120. */
  121. std::shared_ptr<Segment> getSegmentWithIndex(cuid_t cuid, size_t index);
  122. // Returns a currently used segment whose index is index and written
  123. // length is 0. The current owner(in idle state) of segment cancels
  124. // the segment and cuid command acquires the ownership of the
  125. // segment. If no such segment exists, returns null.
  126. std::shared_ptr<Segment> getCleanSegmentIfOwnerIsIdle(cuid_t cuid,
  127. size_t index);
  128. /**
  129. * Updates download status.
  130. */
  131. // bool updateSegment(int cuid, const Segment& segment);
  132. /**
  133. * Cancels all the segment which the command having given cuid
  134. * uses.
  135. */
  136. void cancelSegment(cuid_t cuid);
  137. void cancelSegment(cuid_t cuid, const std::shared_ptr<Segment>& segment);
  138. void cancelAllSegments();
  139. void eraseSegmentWrittenLengthMemo();
  140. /**
  141. * Tells SegmentMan that the segment has been downloaded successfully.
  142. */
  143. bool completeSegment(cuid_t cuid, const std::shared_ptr<Segment>& segment);
  144. /**
  145. * Injects PieceStorage.
  146. */
  147. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  148. /**
  149. * Injects DownloadContext.
  150. */
  151. void
  152. setDownloadContext(const std::shared_ptr<DownloadContext>& downloadContext);
  153. /**
  154. * Returns true if the segment whose index is index has been downloaded.
  155. */
  156. bool hasSegment(size_t index) const;
  157. /**
  158. * Returns the length of bytes downloaded.
  159. */
  160. int64_t getDownloadLength() const;
  161. // If there is inactive PeerStat in peerStats_, it is replaced with
  162. // given peerStat. If no such PeerStat exist, the given peerStat is
  163. // inserted.
  164. void registerPeerStat(const std::shared_ptr<PeerStat>& peerStat);
  165. const std::vector<std::shared_ptr<PeerStat>>& getPeerStats() const
  166. {
  167. return peerStats_;
  168. }
  169. std::shared_ptr<PeerStat> getPeerStat(cuid_t cuid) const;
  170. // If there is slower PeerStat than given peerStat for the same
  171. // hostname and protocol in fastestPeerStats_, the former is
  172. // replaced with latter. If there are no PeerStat with same hostname
  173. // and protocol with given peerStat, given peerStat is inserted.
  174. void updateFastestPeerStat(const std::shared_ptr<PeerStat>& peerStat);
  175. const std::vector<std::shared_ptr<PeerStat>>& getFastestPeerStats() const
  176. {
  177. return fastestPeerStats_;
  178. }
  179. size_t countFreePieceFrom(size_t index) const;
  180. // Excludes segments that fileEntry covers from segment selection.
  181. void ignoreSegmentFor(const std::shared_ptr<FileEntry>& fileEntry);
  182. // Includes segments that fileEntry covers in segment selection.
  183. void recognizeSegmentFor(const std::shared_ptr<FileEntry>& fileEntry);
  184. bool allSegmentsIgnored() const;
  185. };
  186. } // namespace aria2
  187. #endif // D_SEGMENT_MAN_H