SegmentMan.h 7.7 KB

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