/* */ #include "FileEntry.h" #include #include #include "Util.h" #include "URISelector.h" #include "LogFactory.h" namespace aria2 { FileEntry::FileEntry(const std::string& path, uint64_t length, off_t offset, const std::deque& uris): path(path), _uris(uris), length(length), offset(offset), extracted(false), requested(true), _logger(LogFactory::getInstance()) {} FileEntry::FileEntry(): length(0), offset(0), extracted(false), requested(false), _logger(LogFactory::getInstance()) {} FileEntry::~FileEntry() {} void FileEntry::setupDir() { Util::mkdirs(File(path).getDirname()); } FileEntry& FileEntry::operator=(const FileEntry& entry) { if(this != &entry) { path = entry.path; length = entry.length; offset = entry.offset; extracted = entry.extracted; requested = entry.requested; } return *this; } bool FileEntry::operator<(const FileEntry& fileEntry) const { return offset < fileEntry.offset; } bool FileEntry::exists() const { return File(getPath()).exists(); } off_t FileEntry::gtoloff(off_t goff) const { assert(offset <= goff); return goff-offset; } void FileEntry::getUris(std::deque& uris) const { uris.insert(uris.end(), _spentUris.begin(), _spentUris.end()); uris.insert(uris.end(), _uris.begin(), _uris.end()); } std::string FileEntry::selectUri(const SharedHandle& uriSelector) { return uriSelector->select(this); } SharedHandle FileEntry::getRequest(const SharedHandle& selector) { SharedHandle req; if(_requestPool.empty()) { while(1) { std::string uri = selector->select(this); if(uri.empty()) { return req; } req.reset(new Request()); if(req->setUrl(uri)) { _spentUris.push_back(uri); _inFlightRequests.push_back(req); return req; } else { req.reset(); } } } else { req = _requestPool.front(); _requestPool.pop_front(); _inFlightRequests.push_back(req); return req; } } SharedHandle FileEntry::findFasterRequest(const SharedHandle& base) { if(_requestPool.empty()) { return SharedHandle(); } const SharedHandle& fastest = _requestPool.front()->getPeerStat(); if(fastest.isNull()) { return SharedHandle(); } const SharedHandle& basestat = base->getPeerStat(); // TODO1.5 hard coded value. See PREF_STARTUP_IDLE_TIME const int startupIdleTime = 10; if(basestat.isNull() || (basestat->getDownloadStartTime().elapsed(startupIdleTime) && fastest->getAvgDownloadSpeed()*0.8 > basestat->calculateDownloadSpeed())){ // TODO1.5 we should consider that "fastest" is very slow. SharedHandle fastestRequest = _requestPool.front(); _requestPool.pop_front(); return fastestRequest; } return SharedHandle(); } class RequestFaster { public: bool operator()(const SharedHandle& lhs, const SharedHandle& rhs) const { if(lhs->getPeerStat().isNull()) { return false; } if(rhs->getPeerStat().isNull()) { return true; } return lhs->getPeerStat()->getAvgDownloadSpeed() > rhs->getPeerStat()->getAvgDownloadSpeed(); } }; void FileEntry::storePool(const SharedHandle& request) { const SharedHandle& peerStat = request->getPeerStat(); if(!peerStat.isNull()) { // We need to calculate average download speed here in order to // store Request in the right position in the pool. peerStat->calculateAvgDownloadSpeed(); } std::deque >::iterator i = std::lower_bound(_requestPool.begin(), _requestPool.end(), request, RequestFaster()); _requestPool.insert(i, request); } void FileEntry::poolRequest(const SharedHandle& request) { removeRequest(request); storePool(request); } bool FileEntry::removeRequest(const SharedHandle& request) { for(std::deque >::iterator i = _inFlightRequests.begin(); i != _inFlightRequests.end(); ++i) { if((*i).get() == request.get()) { _inFlightRequests.erase(i); return true; } } return false; } void FileEntry::removeURIWhoseHostnameIs(const std::string& hostname) { std::deque newURIs; Request req; for(std::deque::const_iterator itr = _uris.begin(); itr != _uris.end(); ++itr) { if(((*itr).find(hostname) == std::string::npos) || (req.setUrl(*itr) && (req.getHost() != hostname))) { newURIs.push_back(*itr); } } _logger->debug("Removed %d duplicate hostname URIs for path=%s", _uris.size()-newURIs.size(), getPath().c_str()); _uris = newURIs; } void FileEntry::removeIdenticalURI(const std::string& uri) { _uris.erase(std::remove(_uris.begin(), _uris.end(), uri), _uris.end()); } void FileEntry::addURIResult(std::string uri, downloadresultcode::RESULT result) { _uriResults.push_back(URIResult(uri, result)); } class FindURIResultByResult { private: downloadresultcode::RESULT _r; public: FindURIResultByResult(downloadresultcode::RESULT r):_r(r) {} bool operator()(const URIResult& uriResult) const { return uriResult.getResult() == _r; } }; void FileEntry::extractURIResult (std::deque& res, downloadresultcode::RESULT r) { std::deque::iterator i = std::stable_partition(_uriResults.begin(), _uriResults.end(), FindURIResultByResult(r)); std::copy(_uriResults.begin(), i, std::back_inserter(res)); _uriResults.erase(_uriResults.begin(), i); } } // namespace aria2