SegmentMan.h 7.8 KB

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