DownloadContext.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_DOWNLOAD_CONTEXT_H
  36. #define D_DOWNLOAD_CONTEXT_H
  37. #include "common.h"
  38. #include <cassert>
  39. #include <string>
  40. #include <vector>
  41. #include <memory>
  42. #include "TimerA2.h"
  43. #include "A2STR.h"
  44. #include "ValueBase.h"
  45. #include "SegList.h"
  46. #include "ContextAttribute.h"
  47. #include "NetStat.h"
  48. namespace aria2 {
  49. class RequestGroup;
  50. class Signature;
  51. class FileEntry;
  52. class DownloadContext
  53. {
  54. private:
  55. std::vector<std::shared_ptr<FileEntry> > fileEntries_;
  56. std::vector<std::string> pieceHashes_;
  57. int32_t pieceLength_;
  58. std::string pieceHashType_;
  59. std::string digest_;
  60. std::string hashType_;
  61. bool checksumVerified_;
  62. std::string basePath_;
  63. bool knowsTotalLength_;
  64. RequestGroup* ownerRequestGroup_;
  65. std::vector<std::unique_ptr<ContextAttribute> > attrs_;
  66. NetStat netStat_;
  67. Timer downloadStopTime_;
  68. std::shared_ptr<Signature> signature_;
  69. // This member variable is required to avoid to use parse Metalink
  70. // (including both Metalink XML and Metalink/HTTP) twice.
  71. bool acceptMetalink_;
  72. public:
  73. DownloadContext();
  74. // Convenient constructor that creates single file download. path
  75. // should be escaped with util::escapePath(...).
  76. DownloadContext(int32_t pieceLength,
  77. int64_t totalLength,
  78. const std::string& path = A2STR::NIL);
  79. ~DownloadContext();
  80. const std::string& getPieceHash(size_t index) const;
  81. const std::vector<std::string>& getPieceHashes() const
  82. {
  83. return pieceHashes_;
  84. }
  85. template<typename InputIterator>
  86. void setPieceHashes
  87. (const std::string& hashType,
  88. InputIterator first, InputIterator last)
  89. {
  90. pieceHashType_ = hashType;
  91. pieceHashes_.assign(first, last);
  92. }
  93. int64_t getTotalLength() const;
  94. bool knowsTotalLength() const { return knowsTotalLength_; }
  95. void markTotalLengthIsUnknown() { knowsTotalLength_ = false; }
  96. void markTotalLengthIsKnown() { knowsTotalLength_ = true; }
  97. const std::vector<std::shared_ptr<FileEntry> >& getFileEntries() const
  98. {
  99. return fileEntries_;
  100. }
  101. const std::shared_ptr<FileEntry>& getFirstFileEntry() const
  102. {
  103. assert(!fileEntries_.empty());
  104. return fileEntries_[0];
  105. }
  106. // This function returns first FileEntry whose isRequested() returns
  107. // true. If there is no such FileEntry, returns
  108. // std::shared_ptr<FileEntry>().
  109. std::shared_ptr<FileEntry> getFirstRequestedFileEntry() const;
  110. size_t countRequestedFileEntry() const;
  111. template<typename InputIterator>
  112. void setFileEntries(InputIterator first, InputIterator last)
  113. {
  114. fileEntries_.assign(first, last);
  115. }
  116. int32_t getPieceLength() const { return pieceLength_; }
  117. void setPieceLength(int32_t length) { pieceLength_ = length; }
  118. size_t getNumPieces() const;
  119. const std::string& getPieceHashType() const { return pieceHashType_; }
  120. const std::string& getDigest() const { return digest_; }
  121. const std::string& getHashType() const { return hashType_; }
  122. void setDigest(const std::string& hashType, const std::string& digest);
  123. // The representative path name for this context. It is used as a
  124. // part of .aria2 control file. If basePath_ is set, returns
  125. // basePath_. Otherwise, the first FileEntry's getFilePath() is
  126. // returned.
  127. const std::string& getBasePath() const;
  128. void setBasePath(const std::string& basePath);
  129. const std::shared_ptr<Signature>& getSignature() const { return signature_; }
  130. void setSignature(const std::shared_ptr<Signature>& signature);
  131. RequestGroup* getOwnerRequestGroup() { return ownerRequestGroup_; }
  132. void setOwnerRequestGroup(RequestGroup* owner)
  133. {
  134. ownerRequestGroup_ = owner;
  135. }
  136. // sgl must be normalized before the call.
  137. void setFileFilter(SegList<int>& sgl);
  138. // Sets file path for specified index. index starts from 1. The
  139. // index is the same used in setFileFilter(). path is not escaped by
  140. // util::escapePath() in this function.
  141. void setFilePathWithIndex(size_t index, const std::string& path);
  142. // Returns true if hash check(whole file hash, not piece hash) is
  143. // need to be done
  144. bool isChecksumVerificationNeeded() const;
  145. // Returns true if whole hash(not piece hash) is available.
  146. bool isChecksumVerificationAvailable() const;
  147. // Returns true if piece hash(not whole file hash) is available.
  148. bool isPieceHashVerificationAvailable() const;
  149. void setChecksumVerified(bool f)
  150. {
  151. checksumVerified_ = f;
  152. }
  153. void setAttribute
  154. (ContextAttributeType key, std::unique_ptr<ContextAttribute>&& value);
  155. ContextAttribute* getAttribute(ContextAttributeType key);
  156. bool hasAttribute(ContextAttributeType key) const;
  157. void resetDownloadStartTime();
  158. void resetDownloadStopTime();
  159. const Timer& getDownloadStopTime() const
  160. {
  161. return downloadStopTime_;
  162. }
  163. int64_t calculateSessionTime() const;
  164. // Returns FileEntry at given offset. std::shared_ptr<FileEntry>() is
  165. // returned if no such FileEntry is found.
  166. std::shared_ptr<FileEntry> findFileEntryByOffset(int64_t offset) const;
  167. void releaseRuntimeResource();
  168. void setAcceptMetalink(bool f)
  169. {
  170. acceptMetalink_ = f;
  171. }
  172. bool getAcceptMetalink() const
  173. {
  174. return acceptMetalink_;
  175. }
  176. NetStat& getNetStat()
  177. {
  178. return netStat_;
  179. }
  180. // This method also updates global download length held by
  181. // RequestGroupMan via getOwnerRequestGroup().
  182. void updateDownloadLength(size_t bytes);
  183. // This method also updates global upload length held by
  184. // RequestGroupMan via getOwnerRequestGroup().
  185. void updateUploadLength(size_t bytes);
  186. };
  187. } // namespace aria2
  188. #endif // D_DOWNLOAD_CONTEXT_H