SegmentMan.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "SharedHandle.h"
  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. SharedHandle<Segment> segment;
  56. SegmentEntry(cuid_t cuid, const SharedHandle<Segment>& segment);
  57. ~SegmentEntry();
  58. // Don't allow copying
  59. SegmentEntry(const SegmentEntry&);
  60. SegmentEntry& operator=(const SegmentEntry&);
  61. };
  62. typedef std::deque<SharedHandle<SegmentEntry> > SegmentEntries;
  63. /**
  64. * This class holds the download progress of the one download entry.
  65. */
  66. class SegmentMan {
  67. private:
  68. const Option* option_;
  69. SharedHandle<DownloadContext> downloadContext_;
  70. SharedHandle<PieceStorage> pieceStorage_;
  71. SegmentEntries usedSegmentEntries_;
  72. // Remember writtenLength for each segment. The key is an index of a
  73. // segment. The value is writtenLength for that segment.
  74. std::map<size_t, int32_t> segmentWrittenLengthMemo_;
  75. // Used for calculating download speed.
  76. std::vector<SharedHandle<PeerStat> > peerStats_;
  77. // Keep track of fastest PeerStat for each server
  78. std::vector<SharedHandle<PeerStat> > fastestPeerStats_;
  79. BitfieldMan ignoreBitfield_;
  80. SharedHandle<Segment> checkoutSegment(cuid_t cuid,
  81. const SharedHandle<Piece>& piece);
  82. void cancelSegmentInternal(cuid_t cuid, const SharedHandle<Segment>& segment);
  83. public:
  84. SegmentMan(const Option* option,
  85. const SharedHandle<DownloadContext>& downloadContext,
  86. const SharedHandle<PieceStorage>& pieceStorage);
  87. ~SegmentMan();
  88. // Initializes totalSize, isSplittable, downloadStarted, errors.
  89. // Clears command queue. Also, closes diskWriter.
  90. void init();
  91. /**
  92. * The total number of bytes to download.
  93. * If Transfer-Encoding is Chunked or Content-Length header is not provided,
  94. * then this value is set to be 0.
  95. */
  96. int64_t getTotalLength() const;
  97. /**
  98. * Returs true when the download has finished.
  99. * If downloadStarted is false or the number of the segments of this object
  100. * holds is 0, then returns false.
  101. */
  102. bool downloadFinished() const;
  103. /**
  104. * Fill segments which are assigned to the command whose CUID is cuid.
  105. * This function doesn't clear passed segments.
  106. */
  107. void getInFlightSegment(std::vector<SharedHandle<Segment> >& segments,
  108. cuid_t cuid);
  109. SharedHandle<Segment> getSegment(cuid_t cuid, size_t minSplitSize);
  110. // Checkouts segments in the range of fileEntry and push back to
  111. // segments until segments.size() < maxSegments holds false
  112. void getSegment(std::vector<SharedHandle<Segment> >& segments,
  113. cuid_t cuid,
  114. size_t minSplitSize,
  115. const SharedHandle<FileEntry>& fileEntry,
  116. size_t maxSegments);
  117. /**
  118. * Returns a segment whose index is index.
  119. * If it has already assigned
  120. * to another cuid or has been downloaded, then returns a segment instance
  121. * whose isNull call is true.
  122. */
  123. SharedHandle<Segment> getSegmentWithIndex(cuid_t cuid, size_t index);
  124. // Returns a currently used segment whose index is index and written
  125. // length is 0. The current owner(in idle state) of segment cancels
  126. // the segment and cuid command acquires the ownership of the
  127. // segment. If no such segment exists, returns null.
  128. SharedHandle<Segment> getCleanSegmentIfOwnerIsIdle(cuid_t cuid, size_t index);
  129. /**
  130. * Updates download status.
  131. */
  132. //bool updateSegment(int cuid, const Segment& segment);
  133. /**
  134. * Cancels all the segment which the command having given cuid
  135. * uses.
  136. */
  137. void cancelSegment(cuid_t cuid);
  138. void cancelSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
  139. void cancelAllSegments();
  140. void eraseSegmentWrittenLengthMemo();
  141. /**
  142. * Tells SegmentMan that the segment has been downloaded successfully.
  143. */
  144. bool completeSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
  145. /**
  146. * Injects PieceStorage.
  147. */
  148. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  149. /**
  150. * Injects DownloadContext.
  151. */
  152. void setDownloadContext(const SharedHandle<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 SharedHandle<PeerStat>& peerStat);
  165. const std::vector<SharedHandle<PeerStat> >& getPeerStats() const
  166. {
  167. return peerStats_;
  168. }
  169. SharedHandle<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 SharedHandle<PeerStat>& peerStat);
  175. const std::vector<SharedHandle<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 SharedHandle<FileEntry>& fileEntry);
  182. // Includes segments that fileEntry covers in segment selection.
  183. void recognizeSegmentFor(const SharedHandle<FileEntry>& fileEntry);
  184. bool allSegmentsIgnored() const;
  185. };
  186. } // namespace aria2
  187. #endif // D_SEGMENT_MAN_H