FileEntry.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_FILE_ENTRY_H
  36. #define D_FILE_ENTRY_H
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <vector>
  41. #include <ostream>
  42. #include <set>
  43. #include <memory>
  44. #include "File.h"
  45. #include "Request.h"
  46. #include "URIResult.h"
  47. #include "error_code.h"
  48. #include "A2STR.h"
  49. #include "TimerA2.h"
  50. #include "util.h"
  51. #include "a2functional.h"
  52. namespace aria2 {
  53. class URISelector;
  54. class ServerStatMan;
  55. class FileEntry {
  56. public:
  57. typedef std::set<std::shared_ptr<Request>, RefLess<Request>>
  58. InFlightRequestSet;
  59. private:
  60. class RequestFaster {
  61. public:
  62. bool operator()(const std::shared_ptr<Request>& lhs,
  63. const std::shared_ptr<Request>& rhs) const;
  64. };
  65. typedef std::set<std::shared_ptr<Request>, RequestFaster> RequestPool;
  66. int64_t length_;
  67. int64_t offset_;
  68. std::deque<std::string> uris_;
  69. std::deque<std::string> spentUris_;
  70. // URIResult is stored in the ascending order of the time when its result is
  71. // available.
  72. std::deque<URIResult> uriResults_;
  73. RequestPool requestPool_;
  74. InFlightRequestSet inFlightRequests_;
  75. std::string path_;
  76. std::string contentType_;
  77. std::string originalName_;
  78. // path_ without parent directory component. This is primarily used
  79. // to change directory (PREF_DIR option).
  80. std::string suffixPath_;
  81. Timer lastFasterReplace_;
  82. int maxConnectionPerServer_;
  83. bool requested_;
  84. bool uniqueProtocol_;
  85. void storePool(const std::shared_ptr<Request>& request);
  86. public:
  87. FileEntry();
  88. FileEntry(std::string path, int64_t length, int64_t offset,
  89. const std::vector<std::string>& uris = std::vector<std::string>());
  90. ~FileEntry();
  91. FileEntry& operator=(const FileEntry& entry);
  92. std::string getBasename() const;
  93. std::string getDirname() const;
  94. const std::string& getPath() const { return path_; }
  95. void setPath(std::string path);
  96. int64_t getLength() const { return length_; }
  97. void setLength(int64_t length) { length_ = length; }
  98. int64_t getOffset() const { return offset_; }
  99. void setOffset(int64_t offset) { offset_ = offset; }
  100. int64_t getLastOffset() { return offset_ + length_; }
  101. bool isRequested() const { return requested_; }
  102. void setRequested(bool flag) { requested_ = flag; }
  103. const std::deque<std::string>& getRemainingUris() const { return uris_; }
  104. std::deque<std::string>& getRemainingUris() { return uris_; }
  105. const std::deque<std::string>& getSpentUris() const { return spentUris_; }
  106. // Exposed for unittest
  107. std::deque<std::string>& getSpentUris() { return spentUris_; }
  108. size_t setUris(const std::vector<std::string>& uris);
  109. template <typename InputIterator>
  110. size_t addUris(InputIterator first, InputIterator last)
  111. {
  112. size_t count = 0;
  113. for (; first != last; ++first) {
  114. if (addUri(*first)) {
  115. ++count;
  116. }
  117. }
  118. return count;
  119. }
  120. bool addUri(const std::string& uri);
  121. bool insertUri(const std::string& uri, size_t pos);
  122. // Returns uris_ and spentUris_ in single std::vector<std::string>.
  123. std::vector<std::string> getUris() const;
  124. void setContentType(std::string contentType);
  125. const std::string& getContentType() const { return contentType_; }
  126. // If pooled Request object is available, one of them is removed
  127. // from the pool and returned. If pool is empty, then select URI
  128. // using selectUri(selector) and construct Request object using it
  129. // and return the Request object. If referer is given, it is set to
  130. // newly created Request. If Request object is retrieved from the
  131. // pool, referer is ignored. If method is given, it is set to newly
  132. // created Request. If Request object is retrieved from the pool,
  133. // method is ignored. If uriReuse is true and selector does not
  134. // returns Request object either because uris_ is empty or all URI
  135. // are not be usable because maxConnectionPerServer_ limit, then
  136. // reuse used URIs and do selection again.
  137. std::shared_ptr<Request>
  138. getRequest(URISelector* selector, bool uriReuse,
  139. const std::vector<std::pair<size_t, std::string>>& usedHosts,
  140. const std::string& referer = A2STR::NIL,
  141. const std::string& method = Request::METHOD_GET);
  142. // Finds pooled Request object which is faster than passed one,
  143. // comparing their PeerStat objects. If such Request is found, it is
  144. // removed from the pool and returned.
  145. std::shared_ptr<Request>
  146. findFasterRequest(const std::shared_ptr<Request>& base);
  147. // Finds faster server using ServerStatMan.
  148. std::shared_ptr<Request> findFasterRequest(
  149. const std::shared_ptr<Request>& base,
  150. const std::vector<std::pair<size_t, std::string>>& usedHosts,
  151. const std::shared_ptr<ServerStatMan>& serverStatMan);
  152. void poolRequest(const std::shared_ptr<Request>& request);
  153. bool removeRequest(const std::shared_ptr<Request>& request);
  154. size_t countInFlightRequest() const;
  155. size_t countPooledRequest() const;
  156. const InFlightRequestSet& getInFlightRequests() const
  157. {
  158. return inFlightRequests_;
  159. }
  160. bool operator<(const FileEntry& fileEntry) const;
  161. bool exists() const;
  162. // Translate global offset goff to file local offset.
  163. int64_t gtoloff(int64_t goff) const;
  164. void removeURIWhoseHostnameIs(const std::string& hostname);
  165. void removeIdenticalURI(const std::string& uri);
  166. void addURIResult(std::string uri, error_code::Value result);
  167. const std::deque<URIResult>& getURIResults() const { return uriResults_; }
  168. // Extracts URIResult whose _result is r and stores them into res.
  169. // The extracted URIResults are removed from uriResults_.
  170. void extractURIResult(std::deque<URIResult>& res, error_code::Value r);
  171. void setMaxConnectionPerServer(int n) { maxConnectionPerServer_ = n; }
  172. int getMaxConnectionPerServer() const { return maxConnectionPerServer_; }
  173. // Reuse URIs which have not emitted error so far and whose host
  174. // component is not included in ignore. The reusable URIs are
  175. // appended to uris_ maxConnectionPerServer_ times.
  176. void reuseUri(const std::vector<std::string>& ignore);
  177. void releaseRuntimeResource();
  178. // Push URIs in pooled or in-flight requests to the front of uris_.
  179. void putBackRequest();
  180. void setOriginalName(std::string originalName);
  181. const std::string& getOriginalName() const { return originalName_; }
  182. void setSuffixPath(std::string suffixPath);
  183. const std::string& getSuffixPath() const { return suffixPath_; }
  184. bool removeUri(const std::string& uri);
  185. bool emptyRequestUri() const;
  186. void setUniqueProtocol(bool f) { uniqueProtocol_ = f; }
  187. bool isUniqueProtocol() const { return uniqueProtocol_; }
  188. };
  189. // Returns the first FileEntry which isRequested() method returns
  190. // true. If no such FileEntry exists, then returns
  191. // std::shared_ptr<FileEntry>().
  192. template <typename InputIterator>
  193. std::shared_ptr<FileEntry> getFirstRequestedFileEntry(InputIterator first,
  194. InputIterator last)
  195. {
  196. for (; first != last; ++first) {
  197. if ((*first)->isRequested()) {
  198. return *first;
  199. }
  200. }
  201. return nullptr;
  202. }
  203. // Counts the number of files selected in the given iterator range
  204. // [first, last).
  205. template <typename InputIterator>
  206. size_t countRequestedFileEntry(InputIterator first, InputIterator last)
  207. {
  208. size_t count = 0;
  209. for (; first != last; ++first) {
  210. if ((*first)->isRequested()) {
  211. ++count;
  212. }
  213. }
  214. return count;
  215. }
  216. // Returns true if at least one requested FileEntry has URIs.
  217. template <typename InputIterator>
  218. bool isUriSuppliedForRequsetFileEntry(InputIterator first, InputIterator last)
  219. {
  220. for (; first != last; ++first) {
  221. if ((*first)->isRequested() && !(*first)->getRemainingUris().empty()) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. // Writes filename to given o. If memory is true, the output is
  228. // "[MEMORY]" plus the basename of the filename. If there is no
  229. // FileEntry, writes "n/a" to o.
  230. void writeFilePath(std::ostream& o, const std::shared_ptr<FileEntry>& entry,
  231. bool memory);
  232. // Writes first filename to given o. If memory is true, the output is
  233. // "[MEMORY]" plus the basename of the first filename. If there is no
  234. // FileEntry, writes "n/a" to o. If more than 1 FileEntry are in the
  235. // iterator range [first, last), "(Nmore)" is written at the end where
  236. // N is the number of files in iterator range [first, last) minus 1.
  237. template <typename InputIterator>
  238. void writeFilePath(InputIterator first, InputIterator last, std::ostream& o,
  239. bool memory)
  240. {
  241. std::shared_ptr<FileEntry> e = getFirstRequestedFileEntry(first, last);
  242. if (!e) {
  243. o << "n/a";
  244. }
  245. else {
  246. writeFilePath(o, e, memory);
  247. if (!e->getPath().empty()) {
  248. size_t count = countRequestedFileEntry(first, last);
  249. if (count > 1) {
  250. o << " (" << count - 1 << "more)";
  251. }
  252. }
  253. }
  254. }
  255. } // namespace aria2
  256. #endif // D_FILE_ENTRY_H