SegmentMan.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. namespace aria2 {
  44. class Segment;
  45. class Logger;
  46. class Option;
  47. class PeerStat;
  48. class DownloadContext;
  49. class PieceStorage;
  50. class Piece;
  51. class SegmentEntry {
  52. public:
  53. cuid_t cuid;
  54. SharedHandle<Segment> segment;
  55. public:
  56. SegmentEntry(cuid_t cuid, const SharedHandle<Segment>& segment);
  57. ~SegmentEntry();
  58. };
  59. typedef SharedHandle<SegmentEntry> SegmentEntryHandle;
  60. typedef std::deque<SegmentEntryHandle> SegmentEntries;
  61. /**
  62. * This class holds the download progress of the one download entry.
  63. */
  64. class SegmentMan {
  65. private:
  66. const Option* _option;
  67. Logger* logger;
  68. SharedHandle<DownloadContext> _downloadContext;
  69. SharedHandle<PieceStorage> _pieceStorage;
  70. SegmentEntries usedSegmentEntries;
  71. std::deque<SharedHandle<PeerStat> > peerStats;
  72. // key: PeerStat's cuid, value: its download speed
  73. std::map<cuid_t, unsigned int> _peerStatDlspdMap;
  74. Time _lastPeerStatDlspdMapUpdated;
  75. unsigned int _cachedDlspd;
  76. SharedHandle<Segment> checkoutSegment(cuid_t cuid,
  77. const SharedHandle<Piece>& piece);
  78. SharedHandle<SegmentEntry> findSlowerSegmentEntry
  79. (const SharedHandle<PeerStat>& peerStat);
  80. public:
  81. SegmentMan(const Option* option,
  82. const SharedHandle<DownloadContext>& downloadContext,
  83. const SharedHandle<PieceStorage>& pieceStorage);
  84. ~SegmentMan();
  85. // Initializes totalSize, isSplittable, downloadStarted, errors.
  86. // Clears command queue. Also, closes diskWriter.
  87. void init();
  88. /**
  89. * The total number of bytes to download.
  90. * If Transfer-Encoding is Chunked or Content-Length header is not provided,
  91. * then this value is set to be 0.
  92. */
  93. uint64_t getTotalLength() const;
  94. /**
  95. * Returs true when the download has finished.
  96. * If downloadStarted is false or the number of the segments of this object
  97. * holds is 0, then returns false.
  98. */
  99. bool downloadFinished() const;
  100. /**
  101. * Fill segments which are assigned to the command whose CUID is cuid.
  102. * This function doesn't clear passed segments.
  103. */
  104. void getInFlightSegment(std::deque<SharedHandle<Segment> >& segments,
  105. cuid_t cuid);
  106. SharedHandle<Segment> getSegment(cuid_t cuid);
  107. /**
  108. * Returns a segment whose index is index.
  109. * If it has already assigned
  110. * to another cuid or has been downloaded, then returns a segment instance
  111. * whose isNull call is true.
  112. */
  113. SharedHandle<Segment> getSegment(cuid_t cuid, size_t index);
  114. /**
  115. * Updates download status.
  116. */
  117. //bool updateSegment(int cuid, const Segment& segment);
  118. /**
  119. * Cancels all the segment which the command having given cuid
  120. * uses.
  121. */
  122. void cancelSegment(cuid_t cuid);
  123. /**
  124. * Tells SegmentMan that the segment has been downloaded successfully.
  125. */
  126. bool completeSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
  127. /**
  128. * Injects PieceStorage.
  129. */
  130. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  131. /**
  132. * Injects DownloadContext.
  133. */
  134. void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
  135. /**
  136. * Returns true if the segment whose index is index has been downloaded.
  137. */
  138. bool hasSegment(size_t index) const;
  139. /**
  140. * Returns the length of bytes downloaded.
  141. */
  142. uint64_t getDownloadLength() const;
  143. /**
  144. * Registers given peerStat if it has not been registerd and returns true.
  145. * Otherwise does nothing and returns false.
  146. */
  147. bool registerPeerStat(const SharedHandle<PeerStat>& peerStat);
  148. /**
  149. * Returns peerStat whose cuid is given cuid. If it is not found, returns
  150. * 0.
  151. */
  152. SharedHandle<PeerStat> getPeerStat(cuid_t cuid) const;
  153. const std::deque<SharedHandle<PeerStat> >& getPeerStats() const;
  154. /**
  155. * Returns current download speed in bytes per sec.
  156. */
  157. unsigned int calculateDownloadSpeed();
  158. void updateDownloadSpeedFor(const SharedHandle<PeerStat>& pstat);
  159. /**
  160. * Returns the downloaded bytes in this session.
  161. */
  162. uint64_t calculateSessionDownloadLength() const;
  163. size_t countFreePieceFrom(size_t index) const;
  164. };
  165. typedef SharedHandle<SegmentMan> SegmentManHandle;
  166. } // namespace aria2
  167. #endif // _D_SEGMENT_MAN_H_