/* */ #include "ServerStatMan.h" #include "ServerStat.h" #include "Util.h" #include "RecoverableException.h" #include #include #include #include namespace aria2 { ServerStatMan::ServerStatMan() {} ServerStatMan::~ServerStatMan() {} SharedHandle ServerStatMan::find(const std::string& hostname, const std::string& protocol) const { SharedHandle ss(new ServerStat(hostname, protocol)); std::deque >::const_iterator i = std::lower_bound(_serverStats.begin(), _serverStats.end(), ss); if(i != _serverStats.end() && (*i)->getHostname() == hostname && (*i)->getProtocol() == protocol) { return *i; } else { return SharedHandle(); } } bool ServerStatMan::add(const SharedHandle& serverStat) { std::deque >::iterator i = std::lower_bound(_serverStats.begin(), _serverStats.end(), serverStat); if(i != _serverStats.end() && (*i) == serverStat) { return false; } else { _serverStats.insert(i, serverStat); return true; } } bool ServerStatMan::save(std::ostream& out) const { std::copy(_serverStats.begin(), _serverStats.end(), std::ostream_iterator >(out, "\n")); return !out.bad(); } bool ServerStatMan::load(std::istream& in) { static const std::string S_HOST = "host"; static const std::string S_PROTOCOL = "protocol"; static const std::string S_DL_SPEED = "dl_speed"; static const std::string S_LAST_UPDATED = "last_updated"; static const std::string S_STATUS = "status"; std::string line; while(getline(in, line)) { Util::trimSelf(line); if(line.empty()) { continue; } std::deque items; Util::slice(items, line, ','); std::map m; for(std::deque::const_iterator i = items.begin(); i != items.end(); ++i) { std::pair p = Util::split(*i, "="); Util::trimSelf(p.first); Util::trimSelf(p.second); m[p.first] = p.second; } if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) { continue; } SharedHandle sstat(new ServerStat(m[S_HOST], m[S_PROTOCOL])); try { sstat->setDownloadSpeed(Util::parseUInt(m[S_DL_SPEED])); sstat->setLastUpdated(Time(Util::parseInt(m[S_LAST_UPDATED]))); sstat->setStatus(m[S_STATUS]); add(sstat); } catch(RecoverableException& e) { continue; } } return !in.bad(); } class FindStaleServerStat { private: time_t _timeout; public: FindStaleServerStat(time_t timeout):_timeout(timeout) {} bool operator()(const SharedHandle& ss) const { return ss->getLastUpdated().elapsed(_timeout); } }; void ServerStatMan::removeStaleServerStat(time_t timeout) { _serverStats.erase(std::remove_if(_serverStats.begin(), _serverStats.end(), FindStaleServerStat(timeout)), _serverStats.end()); } } // namespace aria2