DownloadContext.h 6.5 KB

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