SegmentMan.h 4.9 KB

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