DownloadContext.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "SharedHandle.h"
  42. #include "TimerA2.h"
  43. #include "A2STR.h"
  44. #include "ValueBase.h"
  45. #include "IntSequence.h"
  46. namespace aria2 {
  47. class RequestGroup;
  48. class Signature;
  49. class FileEntry;
  50. class ContextAttribute;
  51. class DownloadContext
  52. {
  53. private:
  54. std::vector<SharedHandle<FileEntry> > fileEntries_;
  55. std::vector<std::string> pieceHashes_;
  56. size_t pieceLength_;
  57. std::string pieceHashType_;
  58. std::string digest_;
  59. std::string hashType_;
  60. bool checksumVerified_;
  61. std::string basePath_;
  62. bool knowsTotalLength_;
  63. RequestGroup* ownerRequestGroup_;
  64. std::map<std::string, SharedHandle<ContextAttribute> > attrs_;
  65. Timer downloadStartTime_;
  66. Timer downloadStopTime_;
  67. SharedHandle<Signature> signature_;
  68. // This member variable is required to avoid to parse Metalink/HTTP
  69. // Link header fields multiple times.
  70. bool metalinkServerContacted_;
  71. public:
  72. DownloadContext();
  73. // Convenient constructor that creates single file download. path
  74. // should be escaped with util::escapePath(...).
  75. DownloadContext(size_t pieceLength,
  76. uint64_t totalLength,
  77. const std::string& path = A2STR::NIL);
  78. ~DownloadContext();
  79. const std::string& getPieceHash(size_t index) const;
  80. const std::vector<std::string>& getPieceHashes() const
  81. {
  82. return pieceHashes_;
  83. }
  84. template<typename InputIterator>
  85. void setPieceHashes
  86. (const std::string& hashType,
  87. InputIterator first, InputIterator last)
  88. {
  89. pieceHashType_ = hashType;
  90. pieceHashes_.assign(first, last);
  91. }
  92. uint64_t getTotalLength() const;
  93. bool knowsTotalLength() const { return knowsTotalLength_; }
  94. void markTotalLengthIsUnknown() { knowsTotalLength_ = false; }
  95. void markTotalLengthIsKnown() { knowsTotalLength_ = true; }
  96. const std::vector<SharedHandle<FileEntry> >& getFileEntries() const
  97. {
  98. return fileEntries_;
  99. }
  100. const SharedHandle<FileEntry>& getFirstFileEntry() const
  101. {
  102. assert(!fileEntries_.empty());
  103. return fileEntries_[0];
  104. }
  105. // This function returns first FileEntry whose isRequested() returns
  106. // true. If there is no such FileEntry, returns
  107. // SharedHandle<FileEntry>().
  108. SharedHandle<FileEntry> getFirstRequestedFileEntry() const;
  109. size_t countRequestedFileEntry() const;
  110. template<typename InputIterator>
  111. void setFileEntries(InputIterator first, InputIterator last)
  112. {
  113. fileEntries_.assign(first, last);
  114. }
  115. size_t getPieceLength() const { return pieceLength_; }
  116. void setPieceLength(size_t length) { pieceLength_ = length; }
  117. size_t getNumPieces() const;
  118. const std::string& getPieceHashType() const { return pieceHashType_; }
  119. const std::string& getDigest() const { return digest_; }
  120. const std::string& getHashType() const { return hashType_; }
  121. void setDigest(const std::string& hashType, const std::string& digest);
  122. // The representative path name for this context. It is used as a
  123. // part of .aria2 control file. If basePath_ is set, returns
  124. // basePath_. Otherwise, the first FileEntry's getFilePath() is
  125. // returned.
  126. const std::string& getBasePath() const;
  127. void setBasePath(const std::string& basePath);
  128. const SharedHandle<Signature>& getSignature() const { return signature_; }
  129. void setSignature(const SharedHandle<Signature>& signature);
  130. RequestGroup* getOwnerRequestGroup() { return ownerRequestGroup_; }
  131. void setOwnerRequestGroup(RequestGroup* owner)
  132. {
  133. ownerRequestGroup_ = owner;
  134. }
  135. void setFileFilter(IntSequence seq);
  136. // Sets file path for specified index. index starts from 1. The
  137. // index is the same used in setFileFilter(). Please note that path
  138. // is not the actual file path. The actual file path is
  139. // getDir()+"/"+path. path is not escaped by util::escapePath() in
  140. // 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. (const std::string& key, const SharedHandle<ContextAttribute>& value);
  155. const SharedHandle<ContextAttribute>& getAttribute(const std::string& key);
  156. bool hasAttribute(const std::string& 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. SharedHandle<FileEntry>() is
  165. // returned if no such FileEntry is found.
  166. SharedHandle<FileEntry> findFileEntryByOffset(off_t offset) const;
  167. void releaseRuntimeResource();
  168. void setMetalinkServerContacted(bool f)
  169. {
  170. metalinkServerContacted_ = f;
  171. }
  172. bool getMetalinkServerContacted() const
  173. {
  174. return metalinkServerContacted_;
  175. }
  176. };
  177. } // namespace aria2
  178. #endif // D_DOWNLOAD_CONTEXT_H