FileEntry.cc 12 KB

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