SegmentMan.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "TimeA2.h"
  43. #include "Command.h"
  44. #include "BitfieldMan.h"
  45. namespace aria2 {
  46. class Segment;
  47. class Logger;
  48. class Option;
  49. class PeerStat;
  50. class DownloadContext;
  51. class PieceStorage;
  52. class Piece;
  53. class FileEntry;
  54. class SegmentEntry {
  55. public:
  56. cuid_t cuid;
  57. SharedHandle<Segment> segment;
  58. public:
  59. SegmentEntry(cuid_t cuid, const SharedHandle<Segment>& segment);
  60. ~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. Logger* logger;
  71. SharedHandle<DownloadContext> _downloadContext;
  72. SharedHandle<PieceStorage> _pieceStorage;
  73. SegmentEntries usedSegmentEntries;
  74. // Remember writtenLength for each segment. The key is an index of a
  75. // segment. The value is writtenLength for that segment.
  76. std::map<size_t, size_t> _segmentWrittenLengthMemo;
  77. // Used for calculating download speed.
  78. std::vector<SharedHandle<PeerStat> > peerStats;
  79. // Keep track of fastest PeerStat for each server
  80. std::vector<SharedHandle<PeerStat> > _fastestPeerStats;
  81. // key: PeerStat's cuid, value: its download speed
  82. std::map<cuid_t, unsigned int> _peerStatDlspdMap;
  83. Time _lastPeerStatDlspdMapUpdated;
  84. unsigned int _cachedDlspd;
  85. BitfieldMan _ignoreBitfield;
  86. SharedHandle<Segment> checkoutSegment(cuid_t cuid,
  87. const SharedHandle<Piece>& piece);
  88. void cancelSegment(const SharedHandle<Segment>& segment);
  89. public:
  90. SegmentMan(const Option* option,
  91. const SharedHandle<DownloadContext>& downloadContext,
  92. const SharedHandle<PieceStorage>& pieceStorage);
  93. ~SegmentMan();
  94. // Initializes totalSize, isSplittable, downloadStarted, errors.
  95. // Clears command queue. Also, closes diskWriter.
  96. void init();
  97. /**
  98. * The total number of bytes to download.
  99. * If Transfer-Encoding is Chunked or Content-Length header is not provided,
  100. * then this value is set to be 0.
  101. */
  102. uint64_t getTotalLength() const;
  103. /**
  104. * Returs true when the download has finished.
  105. * If downloadStarted is false or the number of the segments of this object
  106. * holds is 0, then returns false.
  107. */
  108. bool downloadFinished() const;
  109. /**
  110. * Fill segments which are assigned to the command whose CUID is cuid.
  111. * This function doesn't clear passed segments.
  112. */
  113. void getInFlightSegment(std::vector<SharedHandle<Segment> >& segments,
  114. cuid_t cuid);
  115. SharedHandle<Segment> getSegment(cuid_t cuid);
  116. // Checkouts segments in the range of fileEntry and push back to
  117. // segments until segments.size() < maxSegments holds false
  118. void getSegment(std::vector<SharedHandle<Segment> >& segments,
  119. cuid_t cuid,
  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> getSegment(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. /**
  140. * Tells SegmentMan that the segment has been downloaded successfully.
  141. */
  142. bool completeSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
  143. /**
  144. * Injects PieceStorage.
  145. */
  146. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  147. /**
  148. * Injects DownloadContext.
  149. */
  150. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  151. /**
  152. * Returns true if the segment whose index is index has been downloaded.
  153. */
  154. bool hasSegment(size_t index) const;
  155. /**
  156. * Returns the length of bytes downloaded.
  157. */
  158. uint64_t getDownloadLength() const;
  159. // If there is inactive PeerStat in peerStats, it is replaced with
  160. // given peerStat. If no such PeerStat exist, the given peerStat is
  161. // inserted.
  162. void registerPeerStat(const SharedHandle<PeerStat>& peerStat);
  163. const std::vector<SharedHandle<PeerStat> >& getPeerStats() const
  164. {
  165. return peerStats;
  166. }
  167. // If there is slower PeerStat than given peerStat for the same
  168. // hostname and protocol in _fastestPeerStats, the former is
  169. // replaced with latter. If there are no PeerStat with same hostname
  170. // and protocol with given peerStat, given peerStat is inserted.
  171. void updateFastestPeerStat(const SharedHandle<PeerStat>& peerStat);
  172. const std::vector<SharedHandle<PeerStat> >& getFastestPeerStats() const
  173. {
  174. return _fastestPeerStats;
  175. }
  176. /**
  177. * Returns current download speed in bytes per sec.
  178. */
  179. unsigned int calculateDownloadSpeed();
  180. void updateDownloadSpeedFor(const SharedHandle<PeerStat>& pstat);
  181. /**
  182. * Returns the downloaded bytes in this session.
  183. */
  184. uint64_t calculateSessionDownloadLength() const;
  185. size_t countFreePieceFrom(size_t index) const;
  186. // Excludes segments that fileEntry covers from segment selection.
  187. void ignoreSegmentFor(const SharedHandle<FileEntry>& fileEntry);
  188. // Includes segments that fileEntry covers in segment selection.
  189. void recognizeSegmentFor(const SharedHandle<FileEntry>& fileEntry);
  190. bool allSegmentsIgnored() const;
  191. };
  192. } // namespace aria2
  193. #endif // _D_SEGMENT_MAN_H_