FileEntry.h 9.8 KB

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