DownloadContext.h 7.0 KB

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