SegmentMan.h 5.3 KB

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