SegmentMan.h 5.3 KB

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