diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-06-09 03:04:38 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-06-09 03:15:10 +0900 |
| commit | b0f440e631efe18099ced96b604447b5fdce28d8 (patch) | |
| tree | d4a705616a58c314eab568772d5a3c4cc53bd69b /src | |
| parent | 99cd73c0926f0c07d27777fde2f15816e8040030 (diff) | |
Rewrite Time with chrono
Diffstat (limited to 'src')
| -rw-r--r-- | src/AdaptiveURISelector.cc | 2 | ||||
| -rw-r--r-- | src/DHTRoutingTableDeserializer.cc | 4 | ||||
| -rw-r--r-- | src/DHTRoutingTableSerializer.cc | 2 | ||||
| -rw-r--r-- | src/File.cc | 8 | ||||
| -rw-r--r-- | src/HttpRequest.cc | 2 | ||||
| -rw-r--r-- | src/HttpResponse.cc | 2 | ||||
| -rw-r--r-- | src/MultiUrlRequestInfo.cc | 4 | ||||
| -rw-r--r-- | src/RequestGroupMan.cc | 2 | ||||
| -rw-r--r-- | src/RequestGroupMan.h | 2 | ||||
| -rw-r--r-- | src/ServerStat.cc | 2 | ||||
| -rw-r--r-- | src/ServerStatMan.cc | 23 | ||||
| -rw-r--r-- | src/ServerStatMan.h | 3 | ||||
| -rw-r--r-- | src/TimeA2.cc | 135 | ||||
| -rw-r--r-- | src/TimeA2.h | 76 |
14 files changed, 62 insertions, 205 deletions
diff --git a/src/AdaptiveURISelector.cc b/src/AdaptiveURISelector.cc index c00d15ad..9696e0e8 100644 --- a/src/AdaptiveURISelector.cc +++ b/src/AdaptiveURISelector.cc @@ -336,7 +336,7 @@ std::string AdaptiveURISelector::getFirstToTestUri power = (int)pow(2.0, (float)counter); /* We test the mirror another time if it has not been * tested since 2^counter days */ - if(ss->getLastUpdated().difference() > power*24*60*60) { + if(ss->getLastUpdated().difference() > std::chrono::hours(power * 24)) { return u; } } diff --git a/src/DHTRoutingTableDeserializer.cc b/src/DHTRoutingTableDeserializer.cc index 22cb4254..490f3f18 100644 --- a/src/DHTRoutingTableDeserializer.cc +++ b/src/DHTRoutingTableDeserializer.cc @@ -131,12 +131,12 @@ void DHTRoutingTableDeserializer::deserialize(const std::string& filename) // time if(version == 2) { READ_CHECK(fp, &temp32, sizeof(temp32)); - serializedTime_.setTimeInSec(ntohl(temp32)); + serializedTime_.setTimeFromEpoch(ntohl(temp32)); // 4bytes reserved readBytes(fp, buf, buf.size(), 4); } else { READ_CHECK(fp, &temp64, sizeof(temp64)); - serializedTime_.setTimeInSec(ntoh64(temp64)); + serializedTime_.setTimeFromEpoch(ntoh64(temp64)); } // localnode diff --git a/src/DHTRoutingTableSerializer.cc b/src/DHTRoutingTableSerializer.cc index dc4a27cb..bffac680 100644 --- a/src/DHTRoutingTableSerializer.cc +++ b/src/DHTRoutingTableSerializer.cc @@ -101,7 +101,7 @@ void DHTRoutingTableSerializer::serialize(const std::string& filename) WRITE_CHECK(fp, header, 8); // write save date - uint64_t ntime = hton64(Time().getTime()); + uint64_t ntime = hton64(Time().getTimeFromEpoch()); WRITE_CHECK(fp, &ntime, sizeof(ntime)); // localnode diff --git a/src/File.cc b/src/File.cc index 3237c6c1..b90087aa 100644 --- a/src/File.cc +++ b/src/File.cc @@ -257,14 +257,14 @@ bool File::utime(const Time& actime, const Time& modtime) const { #if defined(HAVE_UTIMES) && !defined(__MINGW32__) struct timeval times[2] = { - { actime.getTime(), 0 }, - { modtime.getTime(), 0 } + { actime.getTimeFromEpoch(), 0 }, + { modtime.getTimeFromEpoch(), 0 } }; return utimes(name_.c_str(), times) == 0; #else // !HAVE_UTIMES a2utimbuf ub; - ub.actime = actime.getTime(); - ub.modtime = modtime.getTime(); + ub.actime = actime.getTimeFromEpoch(); + ub.modtime = modtime.getTimeFromEpoch(); return a2utime(utf8ToWChar(name_).c_str(), &ub) == 0; #endif // !HAVE_UTIMES } diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index baf0bb93..e380a305 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -232,7 +232,7 @@ std::string HttpRequest::createRequest() std::string path = getDir(); path += getFile(); auto cookies = cookieStorage_->criteriaFind(getHost(), path, - Time().getTime(), + Time().getTimeFromEpoch(), getProtocol() == "https"); for(auto c : cookies) { cookiesValue += c->toString(); diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index d456b62a..02f4b134 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -144,7 +144,7 @@ void HttpResponse::retrieveCookie() ((*r.first).second, httpRequest_->getHost(), httpRequest_->getDir(), - now.getTime()); + now.getTimeFromEpoch()); } } diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 458cf641..82df08d5 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -217,7 +217,7 @@ int MultiUrlRequestInfo::prepare() File cookieFile(option_->get(PREF_LOAD_COOKIES)); if(cookieFile.isFile() && e_->getCookieStorage()->load(cookieFile.getPath(), - Time().getTime())) { + Time().getTimeFromEpoch())) { A2_LOG_INFO(fmt("Loaded cookies from '%s'.", cookieFile.getPath().c_str())); } else { @@ -278,7 +278,7 @@ int MultiUrlRequestInfo::prepare() if(!serverStatIf.empty()) { e_->getRequestGroupMan()->loadServerStat(serverStatIf); e_->getRequestGroupMan()->removeStaleServerStat - (option_->getAsInt(PREF_SERVER_STAT_TIMEOUT)); + (std::chrono::seconds(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT))); } e_->setStatCalc(getStatCalc(option_)); if(uriListParser_) { diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index ba4ed963..4363228a 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -897,7 +897,7 @@ bool RequestGroupMan::saveServerStat(const std::string& filename) const return serverStatMan_->save(filename); } -void RequestGroupMan::removeStaleServerStat(time_t timeout) +void RequestGroupMan::removeStaleServerStat(const std::chrono::seconds& timeout) { serverStatMan_->removeStaleServerStat(timeout); } diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index fa08f182..d45014cf 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -270,7 +270,7 @@ public: bool saveServerStat(const std::string& filename) const; - void removeStaleServerStat(time_t timeout); + void removeStaleServerStat(const std::chrono::seconds& timeout); // Returns true if current download speed exceeds // maxOverallDownloadSpeedLimit_. Always returns false if diff --git a/src/ServerStat.cc b/src/ServerStat.cc index 253b599c..14a6dba6 100644 --- a/src/ServerStat.cc +++ b/src/ServerStat.cc @@ -211,7 +211,7 @@ std::string ServerStat::toString() const getDownloadSpeed(), getSingleConnectionAvgSpeed(), getMultiConnectionAvgSpeed(), - getLastUpdated().getTime(), + getLastUpdated().getTimeFromEpoch(), getCounter(), STATUS_STRING[getStatus()]); } diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index 4c2e46f2..ad8e4e41 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -233,26 +233,11 @@ bool ServerStatMan::load(const std::string& filename) return true; } -namespace { -class FindStaleServerStat { -private: - time_t timeout_; - Time time_; -public: - FindStaleServerStat(time_t timeout):timeout_(timeout) {} - - bool operator()(const std::shared_ptr<ServerStat>& ss) const - { - return ss->getLastUpdated().difference(time_) >= timeout_; - } -}; -} // namespace - -void ServerStatMan::removeStaleServerStat(time_t timeout) +void ServerStatMan::removeStaleServerStat(const std::chrono::seconds& timeout) { - FindStaleServerStat finder(timeout); - for(auto i = serverStats_.begin(), eoi = serverStats_.end(); i != eoi;) { - if(finder(*i)) { + auto now = Time(); + for(auto i = std::begin(serverStats_); i != std::end(serverStats_);) { + if((*i)->getLastUpdated().difference(now) >= timeout) { serverStats_.erase(i++); } else { ++i; diff --git a/src/ServerStatMan.h b/src/ServerStatMan.h index 1b1a4674..1b77fc7c 100644 --- a/src/ServerStatMan.h +++ b/src/ServerStatMan.h @@ -62,7 +62,8 @@ public: bool save(const std::string& filename) const; - void removeStaleServerStat(time_t timeout); + void removeStaleServerStat(const std::chrono::seconds& timeout); + private: typedef std::set<std::shared_ptr<ServerStat>, DerefLess<std::shared_ptr<ServerStat> > > ServerStatSet; diff --git a/src/TimeA2.cc b/src/TimeA2.cc index 8e60cf20..bc226684 100644 --- a/src/TimeA2.cc +++ b/src/TimeA2.cc @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2015 Tatsuhiro Tsujikawa * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,140 +42,40 @@ namespace aria2 { -Time::Time():good_(true) +Time::Time() : tp_(Clock::now()), good_(true) { - reset(); } -Time::Time(const Time& time) +Time::Time(time_t t) : tp_(Clock::from_time_t(t)), good_(true) { - tv_ = time.tv_; - good_ = time.good_; } -Time::Time(time_t sec):good_(true) +void Time::reset() { - setTimeInSec(sec); + tp_ = Clock::now(); + good_ = true; } -Time::Time(const struct timeval& tv):good_(true) +Time::Clock::duration Time::difference() const { - tv_ = tv; + return Clock::now() - tp_; } -Time::~Time() {} - -Time& Time::operator=(const Time& time) -{ - if(this != &time) { - tv_ = time.tv_; - good_ = time.good_; - } - return *this; -} - -bool Time::operator<(const Time& time) const -{ - return util::difftv(time.tv_, tv_) > 0; -} - -void Time::reset() { - gettimeofday(&tv_, nullptr); -} - -struct timeval Time::getCurrentTime() const { - struct timeval now; - gettimeofday(&now, nullptr); - return now; -} - -bool Time::elapsed(time_t sec) const { - // Because of gettimeofday called from getCurrentTime() is slow, and most of - // the time this function is called before specified time passes, we first do - // simple test using time. - // Then only when the further test is required, call gettimeofday. - time_t now = time(nullptr); - if(tv_.tv_sec+sec < now) { - return true; - } else if(tv_.tv_sec+sec == now) { - return - util::difftv(getCurrentTime(), tv_) >= static_cast<int64_t>(sec)*1000000; - } else { - return false; - } -} - -bool Time::elapsedInMillis(int64_t millis) const { - return util::difftv(getCurrentTime(), tv_)/1000 >= millis; -} - -bool Time::isNewer(const Time& time) const { - return util::difftv(tv_, time.tv_) > 0; -} - -time_t Time::difference() const -{ - return util::difftv(getCurrentTime(), tv_)/1000000; -} - -time_t Time::difference(const struct timeval& now) const -{ - return util::difftv(now, tv_)/1000000; -} - -int64_t Time::differenceInMillis() const { - return util::difftv(getCurrentTime(), tv_)/1000; -} - -int64_t Time::differenceInMillis(const struct timeval& now) const +Time::Clock::duration Time::difference(const Time& time) const { - return util::difftv(now, tv_)/1000; + return time.tp_ - tp_; } -bool Time::isZero() const +void Time::setTimeFromEpoch(time_t t) { - return tv_.tv_sec == 0 && tv_.tv_usec == 0; -} - -int64_t Time::getTimeInMicros() const -{ - return (int64_t)tv_.tv_sec*1000*1000+tv_.tv_usec; -} - -int64_t Time::getTimeInMillis() const -{ - return (int64_t)tv_.tv_sec*1000+tv_.tv_usec/1000; -} - -time_t Time::getTime() const -{ - return tv_.tv_sec; -} - -void Time::setTimeInSec(time_t sec) { - tv_.tv_sec = sec; - tv_.tv_usec = 0; -} - -void Time::advance(time_t sec) -{ - tv_.tv_sec += sec; -} - -bool Time::good() const -{ - return good_; -} - -bool Time::bad() const -{ - return !good_; + tp_ = Clock::from_time_t(t); + good_ = true; } std::string Time::toHTTPDate() const { char buf[32]; - time_t t = getTime(); + time_t t = getTimeFromEpoch(); struct tm* tms = gmtime(&t); // returned struct is statically allocated. size_t r = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", tms); return std::string(&buf[0], &buf[r]); @@ -241,11 +141,4 @@ Time Time::parseHTTPDate(const std::string& datetime) return Time::null(); } -Time Time::null() -{ - Time t(0); - t.good_ = false; - return t; -} - } // namespace aria2 diff --git a/src/TimeA2.h b/src/TimeA2.h index fa0c70cd..68c3cd85 100644 --- a/src/TimeA2.h +++ b/src/TimeA2.h @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2015 Tatsuhiro Tsujikawa * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,71 +46,45 @@ namespace aria2 { class Time { -private: - struct timeval tv_; - - bool good_; - - struct timeval getCurrentTime() const; public: + using Clock = std::chrono::system_clock; + // The time value is initialized so that it represents the time at which // this object was created. Time(); - Time(const Time& time); + Time(const Time& time) = default; + Time(Time&& time) = default; Time(time_t sec); - Time(const struct timeval& tv); - - ~Time(); - Time& operator=(const Time& time); + Time& operator=(const Time& time) = default; + Time& operator=(Time&& time) = default; - bool operator<(const Time& time) const; + bool operator<(const Time& time) const { return tp_ < time.tp_; } + bool operator>(const Time& time) const { return time < *this; } + bool operator<=(const Time& time) const { return !(time < *this); } + bool operator>=(const Time& time) const { return !(*this < time); } // Makes this object's time value up to date. void reset(); - bool elapsed(time_t sec) const; - - bool elapsedInMillis(int64_t millis) const; - - time_t difference() const; - - time_t difference(const struct timeval& now) const; - - time_t difference(const Time& now) const - { - return difference(now.tv_); - } + Clock::duration difference() const; + Clock::duration difference(const Time& now) const; - int64_t differenceInMillis() const; + const Clock::time_point& getTime() const { return tp_; } - int64_t differenceInMillis(const struct timeval& now) const; + void setTimeFromEpoch(time_t sec); + time_t getTimeFromEpoch() const { return Clock::to_time_t(tp_); } - int64_t differenceInMillis(const Time& now) const + template <typename duration> + void advance(const duration& t) { - return differenceInMillis(now.tv_); + tp_ += t; } - // Returns true if this object's time value is zero. - bool isZero() const; - - int64_t getTimeInMicros() const; - - int64_t getTimeInMillis() const; - - // Returns this object's time value in seconds. - time_t getTime() const; - - void setTimeInSec(time_t sec); - - bool isNewer(const Time& time) const; + bool good() const { return good_; } + bool bad() const { return !good_; } - void advance(time_t sec); - - bool good() const; - - // Returns !good() - bool bad() const; + static Time null() { return Time(0, false); } std::string toHTTPDate() const; @@ -142,7 +116,11 @@ public: // these functions. static Time parseHTTPDate(const std::string& datetime); - static Time null(); +private: + Time(time_t t, bool good) : tp_(Clock::from_time_t(t)), good_(good) {} + + Clock::time_point tp_; + bool good_; }; } // namespace aria2 |
