FileEntry.h 9.1 KB

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