FileEntry.h 9.5 KB

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