SegmentMan.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. SegmentEntryHandle getSegmentEntryByIndex(int32_t index);
  77. SegmentEntryHandle getSegmentEntryByCuid(int32_t cuid);
  78. SegmentEntries::iterator getSegmentEntryIteratorByCuid(int32_t cuid);
  79. public:
  80. SegmentMan(const Option* option,
  81. const DownloadContextHandle& downloadContext = 0,
  82. const PieceStorageHandle& pieceStorage = 0);
  83. ~SegmentMan();
  84. // Initializes totalSize, isSplittable, downloadStarted, errors.
  85. // Clears command queue. Also, closes diskWriter.
  86. void init();
  87. /**
  88. * The total number of bytes to download.
  89. * If Transfer-Encoding is Chunked or Content-Length header is not provided,
  90. * then this value is set to be 0.
  91. */
  92. int64_t getTotalLength() const;
  93. /**
  94. * Returs true when the download has finished.
  95. * If downloadStarted is false or the number of the segments of this object
  96. * holds is 0, then returns false.
  97. */
  98. bool downloadFinished() const;
  99. /**
  100. * Returns a vacant segment.
  101. * If there is no vacant segment, then returns a segment instance whose
  102. * isNull call is true.
  103. */
  104. Segments getInFlightSegment(int32_t cuid);
  105. SegmentHandle getSegment(int32_t cuid);
  106. /**
  107. * Returns a segment whose index is index.
  108. * If it has already assigned
  109. * to another cuid or has been downloaded, then returns a segment instance
  110. * whose isNull call is true.
  111. */
  112. SegmentHandle getSegment(int32_t cuid, int32_t index);
  113. /**
  114. * Updates download status.
  115. */
  116. //bool updateSegment(int cuid, const Segment& segment);
  117. /**
  118. * Cancels all the segment which the command having given cuid
  119. * uses.
  120. */
  121. void cancelSegment(int32_t cuid);
  122. /**
  123. * Tells SegmentMan that the segment has been downloaded successfully.
  124. */
  125. bool completeSegment(int32_t cuid, const SegmentHandle& segment);
  126. /**
  127. * Injects PieceStorage.
  128. */
  129. void setPieceStorage(const PieceStorageHandle& pieceStorage);
  130. /**
  131. * Injects DownloadContext.
  132. */
  133. void setDownloadContext(const DownloadContextHandle& downloadContext);
  134. /**
  135. * Returns true if the segment whose index is index has been downloaded.
  136. */
  137. bool hasSegment(int32_t index) const;
  138. /**
  139. * Returns the length of bytes downloaded.
  140. */
  141. int64_t getDownloadLength() const;
  142. /**
  143. * Registers given peerStat if it has not been registerd.
  144. * Otherwise does nothing.
  145. */
  146. void registerPeerStat(const PeerStatHandle& peerStat);
  147. /**
  148. * Returns peerStat whose cuid is given cuid. If it is not found, returns
  149. * 0.
  150. */
  151. PeerStatHandle getPeerStat(int32_t cuid) const;
  152. /**
  153. * Returns current download speed in bytes per sec.
  154. */
  155. int32_t calculateDownloadSpeed() const;
  156. int32_t countFreePieceFrom(int32_t index) const;
  157. };
  158. typedef SharedHandle<SegmentMan> SegmentManHandle;
  159. #endif // _D_SEGMENT_MAN_H_