SegmentMan.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_SEGMENT_MAN_H_
  23. #define _D_SEGMENT_MAN_H_
  24. #include "common.h"
  25. #include "Logger.h"
  26. #include "Segment.h"
  27. #include "Option.h"
  28. #include "SegmentSplitter.h"
  29. using namespace std;
  30. #define SEGMENT_FILE_EXTENSION ".aria2"
  31. /**
  32. * This class holds the download progress of the one download entry.
  33. */
  34. class SegmentMan {
  35. private:
  36. void read(FILE* file);
  37. FILE* openSegFile(string segFilename, string mode) const;
  38. public:
  39. /**
  40. * The total number of bytes to download.
  41. * If Transfer-Encoding is Chunked or Content-Length header is not provided,
  42. * then this value is set to be 0.
  43. */
  44. long long int totalSize;
  45. /**
  46. * Represents whether this download is splittable.
  47. * In Split download(or segmented download), http client establishes
  48. * more than one connections to the server, and downloads sevral parts of
  49. * a file at the same time. This boosts download speed.
  50. * This value is true by default. If total number of bytes is not known or
  51. * Chunked transfer encoding is used, then this value is set to be 0 by
  52. * DownloadCommand class.
  53. */
  54. bool isSplittable;
  55. /**
  56. * Represents whether the download is start or not.
  57. * The default value is false.
  58. */
  59. bool downloadStarted;
  60. /**
  61. * Holds segments.
  62. */
  63. Segments segments;
  64. /**
  65. * Respresents the file name of the downloaded file.
  66. * If the URL does not contain file name part(http://www.rednoah.com/, for
  67. * example), this value may be 0 length string.
  68. * The default value is 0 length string.
  69. */
  70. string filename;
  71. /**
  72. * directory to store a file
  73. */
  74. string dir;
  75. /**
  76. * User defined file name for downloaded content
  77. */
  78. string ufilename;
  79. const Logger* logger;
  80. const Option* option;
  81. SegmentSplitter* splitter;
  82. SegmentMan();
  83. ~SegmentMan();
  84. /**
  85. * Returns dir+"/"+filename.
  86. * If filename is empty, then returns dir+"/"+"inex.html";
  87. */
  88. string getFilePath() const {
  89. return (dir == "" ? "." : dir)+"/"+
  90. (ufilename == "" ?
  91. (filename == "" ? "index.html" : filename) : ufilename);
  92. }
  93. string getSegmentFilePath() const {
  94. return getFilePath()+SEGMENT_FILE_EXTENSION;
  95. }
  96. /**
  97. * Sets the cuid of the holded segments with specified cuid to 0.
  98. */
  99. void unregisterId(int cuid);
  100. /**
  101. * There is a segment available for DownloadCommand specified by cuid,
  102. * fills segment and returns true.
  103. * There is no segment available, then returns false and segment is
  104. * undefined in this case.
  105. *
  106. * @param segment segment to attach for cuid.
  107. * @param cuid cuid of DownloadCommand.
  108. * @returns true: there is a segment available, false: there is no segment
  109. * available.
  110. */
  111. bool getSegment(Segment& segment, int cuid);
  112. /**
  113. * Updates the ds value of the specified segment.
  114. * Only a segment x is updated where x.sp == sgment.sp &amp;&amp; x.ep ==
  115. * segment.ep &amp;&amp; x.ds == segment.ds &amp;&amp;x.cuid == segment.cuid
  116. * is true.
  117. *
  118. * @param segment segment to update
  119. */
  120. void updateSegment(const Segment& segment);
  121. /**
  122. * Returns true only if the segment data file exists.
  123. * The file name of the segment data is filename appended by ".aria2".
  124. * If isSplittable is false, then returns simply false without any operation.
  125. */
  126. bool segmentFileExists() const;
  127. /**
  128. * Loads the segment data file.
  129. * If isSplittable is false, then returns without any operation.
  130. */
  131. void load();
  132. /**
  133. * Saves the segment data file.
  134. * If isSplittable is false, then returns without any operation.
  135. */
  136. void save() const;
  137. /**
  138. * Removes the segment data file.
  139. * If isSplittable is false, then returns without any operation.
  140. */
  141. void remove() const;
  142. /**
  143. * Returs true when the download has finished.
  144. * If downloadStarted is false or the number of the segments of this object
  145. * holds is 0, then returns false.
  146. */
  147. bool finished() const;
  148. /**
  149. * if finished() is true, then call remove()
  150. */
  151. void removeIfFinished() const;
  152. /**
  153. * Returns the total number of bytes to be downloaded.
  154. */
  155. long long int getDownloadedSize() const;
  156. };
  157. #endif // _D_SEGMENT_MAN_H_