FileEntry.h 9.9 KB

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