DownloadContext.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "Signature.h"
  43. #include "TimeA2.h"
  44. #include "A2STR.h"
  45. #include "BDE.h"
  46. #include "IntSequence.h"
  47. #include "FileEntry.h"
  48. namespace aria2 {
  49. class RequestGroup;
  50. class DownloadContext
  51. {
  52. private:
  53. std::vector<SharedHandle<FileEntry> > _fileEntries;
  54. std::string _dir;
  55. std::vector<std::string> _pieceHashes;
  56. size_t _pieceLength;
  57. std::string _pieceHashAlgo;
  58. std::string _checksum;
  59. std::string _checksumHashAlgo;
  60. std::string _basePath;
  61. bool _knowsTotalLength;
  62. RequestGroup* _ownerRequestGroup;
  63. BDE _attrs;
  64. Time _downloadStartTime;
  65. Time _downloadStopTime;
  66. SharedHandle<Signature> _signature;
  67. void ensureAttrs();
  68. public:
  69. DownloadContext();
  70. // Convenient constructor that creates single file download.
  71. DownloadContext(size_t pieceLength,
  72. uint64_t totalLength,
  73. const std::string& path = A2STR::NIL);
  74. const std::string& getPieceHash(size_t index) const
  75. {
  76. if(index < _pieceHashes.size()) {
  77. return _pieceHashes[index];
  78. } else {
  79. return A2STR::NIL;
  80. }
  81. }
  82. const std::vector<std::string>& getPieceHashes() const
  83. {
  84. return _pieceHashes;
  85. }
  86. template<typename InputIterator>
  87. void setPieceHashes(InputIterator first, InputIterator last)
  88. {
  89. _pieceHashes.assign(first, last);
  90. }
  91. uint64_t getTotalLength() const
  92. {
  93. if(_fileEntries.empty()) {
  94. return 0;
  95. } else {
  96. return _fileEntries.back()->getLastOffset();
  97. }
  98. }
  99. bool knowsTotalLength() const { return _knowsTotalLength; }
  100. void markTotalLengthIsUnknown() { _knowsTotalLength = false; }
  101. void markTotalLengthIsKnown() { _knowsTotalLength = true; }
  102. const std::vector<SharedHandle<FileEntry> >& getFileEntries() const
  103. {
  104. return _fileEntries;
  105. }
  106. const SharedHandle<FileEntry>& getFirstFileEntry() const
  107. {
  108. assert(!_fileEntries.empty());
  109. return _fileEntries[0];
  110. }
  111. template<typename InputIterator>
  112. void setFileEntries(InputIterator first, InputIterator last)
  113. {
  114. _fileEntries.assign(first, last);
  115. }
  116. size_t getPieceLength() const { return _pieceLength; }
  117. void setPieceLength(size_t length) { _pieceLength = length; }
  118. size_t getNumPieces() const
  119. {
  120. if(_pieceLength == 0) {
  121. return 0;
  122. } else {
  123. assert(!_fileEntries.empty());
  124. return (_fileEntries.back()->getLastOffset()+_pieceLength-1)/_pieceLength;
  125. }
  126. }
  127. const std::string& getPieceHashAlgo() const { return _pieceHashAlgo; }
  128. void setPieceHashAlgo(const std::string& algo)
  129. {
  130. _pieceHashAlgo = algo;
  131. }
  132. const std::string& getChecksum() const { return _checksum; }
  133. void setChecksum(const std::string& checksum)
  134. {
  135. _checksum = checksum;
  136. }
  137. const std::string& getChecksumHashAlgo() const { return _checksumHashAlgo; }
  138. void setChecksumHashAlgo(const std::string& algo)
  139. {
  140. _checksumHashAlgo = algo;
  141. }
  142. // The representative path name for this context. It is used as a
  143. // part of .aria2 control file. If _basePath is set, returns
  144. // _basePath. Otherwise, the first FileEntry's getFilePath() is
  145. // returned.
  146. const std::string& getBasePath() const
  147. {
  148. if(_basePath.empty()) {
  149. assert(!_fileEntries.empty());
  150. return getFirstFileEntry()->getPath();
  151. } else {
  152. return _basePath;
  153. }
  154. }
  155. void setBasePath(const std::string& basePath) { _basePath = basePath; }
  156. const std::string& getDir() const { return _dir; }
  157. void setDir(const std::string& dir) { _dir = dir; }
  158. SharedHandle<Signature> getSignature() const { return _signature; }
  159. void setSignature(const SharedHandle<Signature>& signature)
  160. {
  161. _signature = signature;
  162. }
  163. RequestGroup* getOwnerRequestGroup() { return _ownerRequestGroup; }
  164. void setOwnerRequestGroup(RequestGroup* owner)
  165. {
  166. _ownerRequestGroup = owner;
  167. }
  168. void setFileFilter(IntSequence seq);
  169. // Sets file path for specified index. index starts from 1. The index
  170. // is the same used in setFileFilter(). Please note that path is
  171. // not the actual file path. The actual file path is
  172. // getDir()+"/"+path.
  173. void setFilePathWithIndex(size_t index, const std::string& path);
  174. void setAttribute(const std::string& key, const BDE& value);
  175. BDE& getAttribute(const std::string& key);
  176. bool hasAttribute(const std::string& key) const;
  177. void resetDownloadStartTime();
  178. void resetDownloadStopTime();
  179. int64_t calculateSessionTime() const;
  180. // Returns FileEntry at given offset. SharedHandle<FileEntry>() is
  181. // returned if no such FileEntry is found.
  182. SharedHandle<FileEntry> findFileEntryByOffset(off_t offset) const;
  183. void releaseRuntimeResource();
  184. };
  185. } // namespace aria2
  186. #endif // _D_DOWNLOAD_CONTEXT_H_