FileEntry.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include "FileEntry.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "util.h"
  39. #include "URISelector.h"
  40. #include "LogFactory.h"
  41. #include "wallclock.h"
  42. namespace aria2 {
  43. FileEntry::FileEntry(const std::string& path,
  44. uint64_t length,
  45. off_t offset,
  46. const std::vector<std::string>& uris):
  47. path(path), _uris(uris.begin(), uris.end()), length(length), offset(offset),
  48. requested(true),
  49. _singleHostMultiConnection(true),
  50. _logger(LogFactory::getInstance()) {}
  51. FileEntry::FileEntry():
  52. length(0), offset(0), requested(false),
  53. _singleHostMultiConnection(true),
  54. _logger(LogFactory::getInstance()) {}
  55. FileEntry::~FileEntry() {}
  56. void FileEntry::setupDir()
  57. {
  58. util::mkdirs(File(path).getDirname());
  59. }
  60. FileEntry& FileEntry::operator=(const FileEntry& entry)
  61. {
  62. if(this != &entry) {
  63. path = entry.path;
  64. length = entry.length;
  65. offset = entry.offset;
  66. requested = entry.requested;
  67. }
  68. return *this;
  69. }
  70. bool FileEntry::operator<(const FileEntry& fileEntry) const
  71. {
  72. return offset < fileEntry.offset;
  73. }
  74. bool FileEntry::exists() const
  75. {
  76. return File(getPath()).exists();
  77. }
  78. off_t FileEntry::gtoloff(off_t goff) const
  79. {
  80. assert(offset <= goff);
  81. return goff-offset;
  82. }
  83. void FileEntry::getUris(std::vector<std::string>& uris) const
  84. {
  85. uris.insert(uris.end(), _spentUris.begin(), _spentUris.end());
  86. uris.insert(uris.end(), _uris.begin(), _uris.end());
  87. }
  88. std::string FileEntry::selectUri(const SharedHandle<URISelector>& uriSelector)
  89. {
  90. return uriSelector->select(this);
  91. }
  92. template<typename InputIterator>
  93. static bool inFlightHost(InputIterator first, InputIterator last,
  94. const std::string& hostname)
  95. {
  96. // TODO redirection should be considered here. We need to parse
  97. // original URI to get hostname.
  98. for(; first != last; ++first) {
  99. if((*first)->getHost() == hostname) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. SharedHandle<Request>
  106. FileEntry::getRequest
  107. (const SharedHandle<URISelector>& selector,
  108. const std::string& referer,
  109. const std::string& method)
  110. {
  111. SharedHandle<Request> req;
  112. if(_requestPool.empty()) {
  113. std::vector<std::string> pending;
  114. while(1) {
  115. std::string uri = selector->select(this);
  116. if(uri.empty()) {
  117. return req;
  118. }
  119. req.reset(new Request());
  120. if(req->setUri(uri)) {
  121. if(!_singleHostMultiConnection) {
  122. if(inFlightHost(_inFlightRequests.begin(), _inFlightRequests.end(),
  123. req->getHost())) {
  124. pending.push_back(uri);
  125. req.reset();
  126. continue;
  127. }
  128. }
  129. req->setReferer(referer);
  130. req->setMethod(method);
  131. _spentUris.push_back(uri);
  132. _inFlightRequests.push_back(req);
  133. break;
  134. } else {
  135. req.reset();
  136. }
  137. }
  138. _uris.insert(_uris.begin(), pending.begin(), pending.end());
  139. } else {
  140. req = _requestPool.front();
  141. _requestPool.pop_front();
  142. _inFlightRequests.push_back(req);
  143. }
  144. return req;
  145. }
  146. SharedHandle<Request>
  147. FileEntry::findFasterRequest(const SharedHandle<Request>& base)
  148. {
  149. if(_requestPool.empty()) {
  150. return SharedHandle<Request>();
  151. }
  152. const SharedHandle<PeerStat>& fastest = _requestPool.front()->getPeerStat();
  153. if(fastest.isNull()) {
  154. return SharedHandle<Request>();
  155. }
  156. const SharedHandle<PeerStat>& basestat = base->getPeerStat();
  157. // TODO hard coded value. See PREF_STARTUP_IDLE_TIME
  158. const int startupIdleTime = 10;
  159. if(basestat.isNull() ||
  160. (basestat->getDownloadStartTime().
  161. difference(global::wallclock) >= startupIdleTime &&
  162. fastest->getAvgDownloadSpeed()*0.8 > basestat->calculateDownloadSpeed())){
  163. // TODO we should consider that "fastest" is very slow.
  164. SharedHandle<Request> fastestRequest = _requestPool.front();
  165. _requestPool.pop_front();
  166. _inFlightRequests.push_back(fastestRequest);
  167. return fastestRequest;
  168. }
  169. return SharedHandle<Request>();
  170. }
  171. class RequestFaster {
  172. public:
  173. bool operator()(const SharedHandle<Request>& lhs,
  174. const SharedHandle<Request>& rhs) const
  175. {
  176. if(lhs->getPeerStat().isNull()) {
  177. return false;
  178. }
  179. if(rhs->getPeerStat().isNull()) {
  180. return true;
  181. }
  182. return
  183. lhs->getPeerStat()->getAvgDownloadSpeed() > rhs->getPeerStat()->getAvgDownloadSpeed();
  184. }
  185. };
  186. void FileEntry::storePool(const SharedHandle<Request>& request)
  187. {
  188. const SharedHandle<PeerStat>& peerStat = request->getPeerStat();
  189. if(!peerStat.isNull()) {
  190. // We need to calculate average download speed here in order to
  191. // store Request in the right position in the pool.
  192. peerStat->calculateAvgDownloadSpeed();
  193. }
  194. std::deque<SharedHandle<Request> >::iterator i =
  195. std::lower_bound(_requestPool.begin(), _requestPool.end(), request,
  196. RequestFaster());
  197. _requestPool.insert(i, request);
  198. }
  199. void FileEntry::poolRequest(const SharedHandle<Request>& request)
  200. {
  201. removeRequest(request);
  202. if(!request->removalRequested()) {
  203. storePool(request);
  204. }
  205. }
  206. bool FileEntry::removeRequest(const SharedHandle<Request>& request)
  207. {
  208. for(std::deque<SharedHandle<Request> >::iterator i =
  209. _inFlightRequests.begin(), eoi = _inFlightRequests.end();
  210. i != eoi; ++i) {
  211. if((*i).get() == request.get()) {
  212. _inFlightRequests.erase(i);
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. void FileEntry::removeURIWhoseHostnameIs(const std::string& hostname)
  219. {
  220. std::deque<std::string> newURIs;
  221. Request req;
  222. for(std::deque<std::string>::const_iterator itr = _uris.begin(),
  223. eoi = _uris.end(); itr != eoi; ++itr) {
  224. if(((*itr).find(hostname) == std::string::npos) ||
  225. (req.setUri(*itr) && (req.getHost() != hostname))) {
  226. newURIs.push_back(*itr);
  227. }
  228. }
  229. if(_logger->debug()) {
  230. _logger->debug("Removed %d duplicate hostname URIs for path=%s",
  231. _uris.size()-newURIs.size(), getPath().c_str());
  232. }
  233. _uris = newURIs;
  234. }
  235. void FileEntry::removeIdenticalURI(const std::string& uri)
  236. {
  237. _uris.erase(std::remove(_uris.begin(), _uris.end(), uri), _uris.end());
  238. }
  239. void FileEntry::addURIResult(std::string uri, downloadresultcode::RESULT result)
  240. {
  241. _uriResults.push_back(URIResult(uri, result));
  242. }
  243. class FindURIResultByResult {
  244. private:
  245. downloadresultcode::RESULT _r;
  246. public:
  247. FindURIResultByResult(downloadresultcode::RESULT r):_r(r) {}
  248. bool operator()(const URIResult& uriResult) const
  249. {
  250. return uriResult.getResult() == _r;
  251. }
  252. };
  253. void FileEntry::extractURIResult
  254. (std::deque<URIResult>& res, downloadresultcode::RESULT r)
  255. {
  256. std::deque<URIResult>::iterator i =
  257. std::stable_partition(_uriResults.begin(), _uriResults.end(),
  258. FindURIResultByResult(r));
  259. std::copy(_uriResults.begin(), i, std::back_inserter(res));
  260. _uriResults.erase(_uriResults.begin(), i);
  261. }
  262. void FileEntry::reuseUri(size_t num)
  263. {
  264. std::deque<std::string> uris = _spentUris;
  265. std::sort(uris.begin(), uris.end());
  266. uris.erase(std::unique(uris.begin(), uris.end()), uris.end());
  267. std::vector<std::string> errorUris(_uriResults.size());
  268. std::transform(_uriResults.begin(), _uriResults.end(),
  269. errorUris.begin(), std::mem_fun_ref(&URIResult::getURI));
  270. std::sort(errorUris.begin(), errorUris.end());
  271. errorUris.erase(std::unique(errorUris.begin(), errorUris.end()),
  272. errorUris.end());
  273. std::vector<std::string> reusableURIs;
  274. std::set_difference(uris.begin(), uris.end(),
  275. errorUris.begin(), errorUris.end(),
  276. std::back_inserter(reusableURIs));
  277. size_t ininum = reusableURIs.size();
  278. if(_logger->debug()) {
  279. _logger->debug("Found %u reusable URIs", static_cast<unsigned int>(ininum));
  280. for(std::vector<std::string>::const_iterator i = reusableURIs.begin(),
  281. eoi = reusableURIs.end(); i != eoi; ++i) {
  282. _logger->debug("URI=%s", (*i).c_str());
  283. }
  284. }
  285. // Reuse at least num URIs here to avoid to
  286. // run this process repeatedly.
  287. if(ininum > 0) {
  288. for(size_t i = 0; i < num/ininum; ++i) {
  289. _uris.insert(_uris.end(), reusableURIs.begin(), reusableURIs.end());
  290. }
  291. _uris.insert(_uris.end(), reusableURIs.begin(),
  292. reusableURIs.begin()+(num%ininum));
  293. if(_logger->debug()) {
  294. _logger->debug("Duplication complete: now %u URIs for reuse",
  295. static_cast<unsigned int>(_uris.size()));
  296. }
  297. }
  298. }
  299. void FileEntry::releaseRuntimeResource()
  300. {
  301. _requestPool.clear();
  302. _inFlightRequests.clear();
  303. }
  304. template<typename InputIterator, typename T>
  305. static InputIterator findRequestByUri
  306. (InputIterator first, InputIterator last, const T& uri)
  307. {
  308. for(; first != last; ++first) {
  309. if(!(*first)->removalRequested() && (*first)->getUri() == uri) {
  310. return first;
  311. }
  312. }
  313. return last;
  314. }
  315. bool FileEntry::removeUri(const std::string& uri)
  316. {
  317. std::deque<std::string>::iterator itr =
  318. std::find(_spentUris.begin(), _spentUris.end(), uri);
  319. if(itr == _spentUris.end()) {
  320. itr = std::find(_uris.begin(), _uris.end(), uri);
  321. if(itr == _uris.end()) {
  322. return false;
  323. } else {
  324. _uris.erase(itr);
  325. return true;
  326. }
  327. } else {
  328. _spentUris.erase(itr);
  329. SharedHandle<Request> req;
  330. std::deque<SharedHandle<Request> >::iterator riter =
  331. findRequestByUri(_inFlightRequests.begin(), _inFlightRequests.end(), uri);
  332. if(riter == _inFlightRequests.end()) {
  333. riter = findRequestByUri(_requestPool.begin(), _requestPool.end(), uri);
  334. if(riter == _requestPool.end()) {
  335. return true;
  336. } else {
  337. req = *riter;
  338. _requestPool.erase(riter);
  339. }
  340. } else {
  341. req = *riter;
  342. }
  343. req->requestRemoval();
  344. return true;
  345. }
  346. }
  347. } // namespace aria2