DownloadContext.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 pieceHashAlgo_;
  58. std::string checksum_;
  59. std::string checksumHashAlgo_;
  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. public:
  69. DownloadContext();
  70. // Convenient constructor that creates single file download. path
  71. // should be escaped with util::escapePath(...).
  72. DownloadContext(size_t pieceLength,
  73. uint64_t totalLength,
  74. const std::string& path = A2STR::NIL);
  75. ~DownloadContext();
  76. const std::string& getPieceHash(size_t index) const;
  77. const std::vector<std::string>& getPieceHashes() const
  78. {
  79. return pieceHashes_;
  80. }
  81. template<typename InputIterator>
  82. void setPieceHashes(InputIterator first, InputIterator last)
  83. {
  84. pieceHashes_.assign(first, last);
  85. }
  86. uint64_t getTotalLength() const;
  87. bool knowsTotalLength() const { return knowsTotalLength_; }
  88. void markTotalLengthIsUnknown() { knowsTotalLength_ = false; }
  89. void markTotalLengthIsKnown() { knowsTotalLength_ = true; }
  90. const std::vector<SharedHandle<FileEntry> >& getFileEntries() const
  91. {
  92. return fileEntries_;
  93. }
  94. const SharedHandle<FileEntry>& getFirstFileEntry() const
  95. {
  96. assert(!fileEntries_.empty());
  97. return fileEntries_[0];
  98. }
  99. // This function returns first FileEntry whose isRequested() returns
  100. // true. If there is no such FileEntry, returns
  101. // SharedHandle<FileEntry>().
  102. SharedHandle<FileEntry> getFirstRequestedFileEntry() const;
  103. size_t countRequestedFileEntry() const;
  104. template<typename InputIterator>
  105. void setFileEntries(InputIterator first, InputIterator last)
  106. {
  107. fileEntries_.assign(first, last);
  108. }
  109. size_t getPieceLength() const { return pieceLength_; }
  110. void setPieceLength(size_t length) { pieceLength_ = length; }
  111. size_t getNumPieces() const;
  112. const std::string& getPieceHashAlgo() const { return pieceHashAlgo_; }
  113. void setPieceHashAlgo(const std::string& algo);
  114. const std::string& getChecksum() const { return checksum_; }
  115. void setChecksum(const std::string& checksum);
  116. const std::string& getChecksumHashAlgo() const { return checksumHashAlgo_; }
  117. void setChecksumHashAlgo(const std::string& algo);
  118. // The representative path name for this context. It is used as a
  119. // part of .aria2 control file. If basePath_ is set, returns
  120. // basePath_. Otherwise, the first FileEntry's getFilePath() is
  121. // returned.
  122. const std::string& getBasePath() const;
  123. void setBasePath(const std::string& basePath);
  124. const SharedHandle<Signature>& getSignature() const { return signature_; }
  125. void setSignature(const SharedHandle<Signature>& signature);
  126. RequestGroup* getOwnerRequestGroup() { return ownerRequestGroup_; }
  127. void setOwnerRequestGroup(RequestGroup* owner)
  128. {
  129. ownerRequestGroup_ = owner;
  130. }
  131. void setFileFilter(IntSequence seq);
  132. // Sets file path for specified index. index starts from 1. The
  133. // index is the same used in setFileFilter(). Please note that path
  134. // is not the actual file path. The actual file path is
  135. // getDir()+"/"+path. path is not escaped by util::escapePath() in
  136. // this function.
  137. void setFilePathWithIndex(size_t index, const std::string& path);
  138. // Returns true if hash check(whole file hash, not piece hash) is
  139. // need to be done
  140. bool isChecksumVerificationNeeded() const;
  141. // Returns true if whole hash(not piece hash) is available.
  142. bool isChecksumVerificationAvailable() const;
  143. // Returns true if piece hash(not whole file hash) is available.
  144. bool isPieceHashVerificationAvailable() const;
  145. void setChecksumVerified(bool f)
  146. {
  147. checksumVerified_ = f;
  148. }
  149. void setAttribute
  150. (const std::string& key, const SharedHandle<ContextAttribute>& value);
  151. const SharedHandle<ContextAttribute>& getAttribute(const std::string& key);
  152. bool hasAttribute(const std::string& key) const;
  153. void resetDownloadStartTime();
  154. void resetDownloadStopTime();
  155. const Timer& getDownloadStopTime() const
  156. {
  157. return downloadStopTime_;
  158. }
  159. int64_t calculateSessionTime() const;
  160. // Returns FileEntry at given offset. SharedHandle<FileEntry>() is
  161. // returned if no such FileEntry is found.
  162. SharedHandle<FileEntry> findFileEntryByOffset(off_t offset) const;
  163. void releaseRuntimeResource();
  164. };
  165. } // namespace aria2
  166. #endif // D_DOWNLOAD_CONTEXT_H