SegmentMan.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <map>
  40. #include "SharedHandle.h"
  41. #include "TimeA2.h"
  42. #include "Command.h"
  43. #include "BitfieldMan.h"
  44. namespace aria2 {
  45. class Segment;
  46. class Logger;
  47. class Option;
  48. class PeerStat;
  49. class DownloadContext;
  50. class PieceStorage;
  51. class Piece;
  52. class FileEntry;
  53. class SegmentEntry {
  54. public:
  55. cuid_t cuid;
  56. SharedHandle<Segment> segment;
  57. public:
  58. SegmentEntry(cuid_t cuid, const SharedHandle<Segment>& segment);
  59. ~SegmentEntry();
  60. };
  61. typedef SharedHandle<SegmentEntry> SegmentEntryHandle;
  62. typedef std::deque<SegmentEntryHandle> 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. Logger* logger;
  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::deque<SharedHandle<PeerStat> > peerStats;
  78. // Keep track of fastest PeerStat for each server
  79. std::deque<SharedHandle<PeerStat> > _fastestPeerStats;
  80. // key: PeerStat's cuid, value: its download speed
  81. std::map<cuid_t, unsigned int> _peerStatDlspdMap;
  82. Time _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::deque<SharedHandle<Segment> >& segments,
  113. cuid_t cuid);
  114. SharedHandle<Segment> getSegment(cuid_t cuid);
  115. // Checkouts segments in the range of fileEntry and push back to
  116. // segments until segments.size() < maxSegments holds false
  117. void getSegment(std::deque<SharedHandle<Segment> >& segments,
  118. cuid_t cuid,
  119. const SharedHandle<FileEntry>& fileEntry,
  120. size_t maxSegments);
  121. /**
  122. * Returns a segment whose index is index.
  123. * If it has already assigned
  124. * to another cuid or has been downloaded, then returns a segment instance
  125. * whose isNull call is true.
  126. */
  127. SharedHandle<Segment> getSegment(cuid_t cuid, 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 SharedHandle<Segment>& segment);
  138. /**
  139. * Tells SegmentMan that the segment has been downloaded successfully.
  140. */
  141. bool completeSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
  142. /**
  143. * Injects PieceStorage.
  144. */
  145. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  146. /**
  147. * Injects DownloadContext.
  148. */
  149. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  150. /**
  151. * Returns true if the segment whose index is index has been downloaded.
  152. */
  153. bool hasSegment(size_t index) const;
  154. /**
  155. * Returns the length of bytes downloaded.
  156. */
  157. uint64_t getDownloadLength() const;
  158. // If there is inactive PeerStat in peerStats, it is replaced with
  159. // given peerStat. If no such PeerStat exist, the given peerStat is
  160. // inserted.
  161. void registerPeerStat(const SharedHandle<PeerStat>& peerStat);
  162. const std::deque<SharedHandle<PeerStat> >& getPeerStats() const
  163. {
  164. return peerStats;
  165. }
  166. // If there is slower PeerStat than given peerStat for the same
  167. // hostname and protocol in _fastestPeerStats, the former is
  168. // replaced with latter. If there are no PeerStat with same hostname
  169. // and protocol with given peerStat, given peerStat is inserted.
  170. void updateFastestPeerStat(const SharedHandle<PeerStat>& peerStat);
  171. const std::deque<SharedHandle<PeerStat> >& getFastestPeerStats() const
  172. {
  173. return _fastestPeerStats;
  174. }
  175. /**
  176. * Returns current download speed in bytes per sec.
  177. */
  178. unsigned int calculateDownloadSpeed();
  179. void updateDownloadSpeedFor(const SharedHandle<PeerStat>& pstat);
  180. /**
  181. * Returns the downloaded bytes in this session.
  182. */
  183. uint64_t calculateSessionDownloadLength() const;
  184. size_t countFreePieceFrom(size_t index) const;
  185. // Excludes segments that fileEntry covers from segment selection.
  186. void ignoreSegmentFor(const SharedHandle<FileEntry>& fileEntry);
  187. // Includes segments that fileEntry covers in segment selection.
  188. void recognizeSegmentFor(const SharedHandle<FileEntry>& fileEntry);
  189. };
  190. typedef SharedHandle<SegmentMan> SegmentManHandle;
  191. } // namespace aria2
  192. #endif // _D_SEGMENT_MAN_H_