void FileChannel::setRotation(const std::string& rotation) { std::string::const_iterator it = rotation.begin(); std::string::const_iterator end = rotation.end(); int n = 0; while (it != end && Ascii::isSpace(*it)) ++it; while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; } while (it != end && Ascii::isSpace(*it)) ++it; std::string unit; while (it != end && Ascii::isAlpha(*it)) unit += *it++; RotateStrategy* pStrategy = 0; if ((rotation.find(',') != std::string::npos) || (rotation.find(':') != std::string::npos)) { if (_times == "utc") pStrategy = new RotateAtTimeStrategy<DateTime>(rotation); else if (_times == "local") pStrategy = new RotateAtTimeStrategy<LocalDateTime>(rotation); else throw PropertyNotSupportedException("times", _times); } else if (unit == "daily") pStrategy = new RotateByIntervalStrategy(Timespan(1*Timespan::DAYS)); else if (unit == "weekly") pStrategy = new RotateByIntervalStrategy(Timespan(7*Timespan::DAYS)); else if (unit == "monthly") pStrategy = new RotateByIntervalStrategy(Timespan(30*Timespan::DAYS)); else if (unit == "seconds") // for testing only pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::SECONDS)); else if (unit == "minutes") pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::MINUTES)); else if (unit == "hours") pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::HOURS)); else if (unit == "days") pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::DAYS)); else if (unit == "weeks") pStrategy = new RotateByIntervalStrategy(Timespan(n*7*Timespan::DAYS)); else if (unit == "months") pStrategy = new RotateByIntervalStrategy(Timespan(n*30*Timespan::DAYS)); else if (unit == "K") pStrategy = new RotateBySizeStrategy(n*1024); else if (unit == "M") pStrategy = new RotateBySizeStrategy(n*1024*1024); else if (unit.empty()) pStrategy = new RotateBySizeStrategy(n); else if (unit != "never") throw InvalidArgumentException("rotation", rotation); delete _pRotateStrategy; _pRotateStrategy = pStrategy; _rotation = rotation; }
void FileChannel::setPurgeAge(const std::string& age) { std::string::const_iterator it = age.begin(); std::string::const_iterator end = age.end(); int n = 0; while (it != end && std::isspace(*it)) ++it; while (it != end && std::isdigit(*it)) { n *= 10; n += *it++ - '0'; } while (it != end && std::isspace(*it)) ++it; std::string unit; while (it != end && std::isalpha(*it)) unit += *it++; Timespan::TimeDiff factor = Timespan::SECONDS; if (unit == "minutes") factor = Timespan::MINUTES; else if (unit == "hours") factor = Timespan::HOURS; else if (unit == "days") factor = Timespan::DAYS; else if (unit == "weeks") factor = 7*Timespan::DAYS; else if (unit == "months") factor = 30*Timespan::DAYS; else if (unit != "seconds") throw InvalidArgumentException("purgeAge", age); delete _pPurgeStrategy; _pPurgeStrategy = new PurgeByAgeStrategy(Timespan(factor*n)); _purgeAge = age; }
ICMPSocketImpl::ICMPSocketImpl(IPAddress::Family family, int dataSize, int ttl, int timeout): RawSocketImpl(family, IPPROTO_ICMP), _icmpPacket(family, dataSize) { setOption(IPPROTO_IP, IP_TTL, ttl); setReceiveTimeout(Timespan(timeout)); }
void Timer::stop() { _active = false; _finished = Timespan(0); if(_selector) _selector->onTimerChanged(*this); }
void runSkedTaskIntervalTest() { Log("trace") << "Running Scheduled Task Test" << endl; // Schedule an interval task to run 3 times at 1 second intervals { ScheduledTask* task = new ScheduledTask(); IntervalTrigger* trigger = task->createTrigger<IntervalTrigger>(); trigger->interval = Timespan(1, 0); trigger->maxTimes = 3; scheduler.start(task); //task->start(); // Print to cout Log("debug") << "##### Sked Print Output:" << endl; scheduler.print(cout); Log("debug") << "##### Sked Print Output END" << endl; // Wait for the task to complete app.run(); } /* //ready.wait(); //ready.wait(); //ready.wait(); // Schedule to fire once now, and in two days time. { ScheduledTask* task = new ScheduledTask(); DailyTrigger* trigger = task->createTrigger<DailyTrigger>(); // 2 secs from now DateTime dt; Timespan ts(2, 0); dt += ts; trigger->timeOfDay = dt; // skip tomorrow trigger->daysExcluded.push_back((DaysOfTheWeek)(dt.dayOfWeek() + 1)); scheduler.schedule(task); // TODO: Assert running date ready.wait(); } */ app.run(); Log("trace") << "Running Scheduled Task Test: END" << endl; }
Timespan ClockImpl::stop() { QueryPerformanceCounter( &_stopValue ); _secondStopValue = GetTickCount(); LARGE_INTEGER delta; delta.QuadPart = _stopValue.QuadPart - _startValue.QuadPart; DWORD secondDelta = _secondStopValue - _secondStartValue; if( secondDelta > 100 ) { return Timespan(secondDelta / 1000 , ( secondDelta * 1000 ) % 1000000 ); } const long secs = static_cast<long>(delta.QuadPart / _frequency.QuadPart); const long usecs = static_cast<long>(((delta.QuadPart * 1000000) / _frequency.QuadPart ) % 1000000); return Timespan(secs, usecs); }
void FileChannel::setPurgeAge(const std::string& age) { if (setNoPurge(age)) return; std::string::const_iterator nextToDigit; int num = extractDigit(age, &nextToDigit); Timespan::TimeDiff factor = extractFactor(age, nextToDigit); setPurgeStrategy(new PurgeByAgeStrategy(Timespan(num * factor))); _purgeAge = age; }
bool ThreadPool::Future::FutureImpl::wait(Timespan timeout) const { MutexLock lock(_mutex); if (timeout > Timespan(0)) { Timespan until = Timespan::gettimeofday() + timeout; Timespan remaining; while (_state != Finished && _state != Failed && (remaining = until - Timespan::gettimeofday()) > Timespan(0)) { _stateChanged.wait(_mutex, remaining); } } else { while (_state != Finished && _state != Failed) _stateChanged.wait(_mutex); } return _state == Finished || _state == Failed; }
void RpcClientImpl::wait(Timespan timeout) { if (_socket.selector() == 0) throw std::logic_error("cannot run async rpc request without a selector"); Clock clock; if (timeout >= Timespan(0)) clock.start(); Timespan remaining = timeout; while (activeProcedure() != 0) { if (_socket.selector()->wait(remaining) == false) throw IOTimeout(); if (timeout >= Timespan(0)) { remaining = timeout - clock.stop(); if (remaining < Timespan(0)) remaining = Timespan(0); } } }
void Timer::start(const DateTime& startTime, const Milliseconds& interval) { if (interval <= Timespan(0)) throw std::logic_error("cannot run interval timer without interval"); if (_active) stop(); _active = true; _interval = interval; _once = false; Timespan systemTime = Clock::getSystemTicks(); struct tm tim; time_t sec = static_cast<time_t>(systemTime.totalSeconds()); localtime_r(&sec, &tim); DateTime now(tim.tm_year + 1900, tim.tm_mon + 1, tim.tm_mday, tim.tm_hour, tim.tm_min, tim.tm_sec, 0, systemTime.totalUSecs() % 1000000); if (startTime > now) { _finished = systemTime + (startTime - now); } else { // startTime =< now Timespan elapsed = now - startTime; uint64_t ticksElapsed = elapsed.totalMSecs() / interval.totalMSecs(); DateTime tickTime = startTime + (ticksElapsed + 1) * Timespan(interval); Timespan delay = tickTime - now; _finished = systemTime + (tickTime - now); } if (_selector) _selector->onTimerChanged(*this); }
void Timer::start(const Milliseconds& interval) { if (interval <= Timespan(0)) throw std::logic_error("cannot run interval timer without interval"); if (_active) stop(); _active = true; _interval = interval; _once = false; _finished = Clock::getSystemTicks() + _interval; if (_selector) _selector->onTimerChanged(*this); }
void FileChannel::setPurgeAge(const std::string& age) { delete _pPurgeStrategy; _pPurgeStrategy = 0; _purgeAge = "none"; if (age.empty() || 0 == icompare(age, "none")) return; std::string::const_iterator it = age.begin(); std::string::const_iterator end = age.end(); int n = 0; while (it != end && Ascii::isSpace(*it)) ++it; while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; } while (it != end && Ascii::isSpace(*it)) ++it; std::string unit; while (it != end && Ascii::isAlpha(*it)) unit += *it++; Timespan::TimeDiff factor = Timespan::SECONDS; if (unit == "minutes") factor = Timespan::MINUTES; else if (unit == "hours") factor = Timespan::HOURS; else if (unit == "days") factor = Timespan::DAYS; else if (unit == "weeks") factor = 7*Timespan::DAYS; else if (unit == "months") factor = 30 * Timespan::DAYS; else if (unit != "seconds") throw InvalidArgumentException("purgeAge", age); if (0 == n) throw InvalidArgumentException("Zero is not valid purge age."); _pPurgeStrategy = new PurgeByAgeStrategy(Timespan(factor * n)); _purgeAge = age; }
Timespan ClockImpl::getSystemTicks() { FILETIME ft; // win32 only : GetSystemTimeAsFileTime(&ft); SYSTEMTIME st; GetSystemTime( &st ); SystemTimeToFileTime( &st, &ft ); // number of 100-nanosecond intervals since January 1, 1601 (UTC) Pt::uint64_t tmpres = 0; tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; // convert to microseconds tmpres /= 10; return Timespan( static_cast<Pt::int64_t>(tmpres) ); //return Timespan( Pt::int64_t(1000) * GetTickCount() ); }
void DateTime::makeLocal(int tzd) { operator += (Timespan(((Timestamp::TimeDiff) tzd)*Timespan::SECONDS)); }
const Timespan Timespan::infinite () { return (Timespan(INFINITE)); }
Timespan Timespan::operator + (const Timespan& d) const { return Timespan(_span + d._span); }
Timespan Timespan::operator - (TimeDiff microSeconds) const { return Timespan(_span - microSeconds); }
Timespan Timespan::operator - (const Timespan& d) const { return Timespan(_span - d._span); }
Timespan Timespan::operator*(const double b) const { return Timespan(*this) *= b; }
void DateTime::makeUTC(int tzd) { operator -= (Timespan(((Timestamp::TimeDiff) tzd)*SECONDS)); }
Timespan Timespan::operator +(const Timespan& b) const { return Timespan(*this) += b; }
Timespan LocalDateTime::operator - (const LocalDateTime& dateTime) const { return Timespan((utcTime() - dateTime.utcTime())/10); }
bool SelectorImpl::waitUntil(Timespan until) { if (!_avail.empty()) until = Timespan(0); if (_isDirty) { _pollfds.clear(); // recalculate size size_t pollSize= 1; std::set<Selectable*>::iterator iter; for( iter= _devices.begin(); iter != _devices.end(); ++iter) { if( (*iter)->enabled() ) pollSize+= (*iter)->simpl().pollSize(); } pollfd pfd; pfd.fd = -1; pfd.events = 0; pfd.revents = 0; _pollfds.assign(pollSize, pfd); // add entries pollfd* pCurr= &_pollfds[0]; // insert event pipe pCurr->fd = _wakePipe[0]; pCurr->events = POLLIN; ++pCurr; for( iter= _devices.begin(); iter != _devices.end(); ++iter) { if( (*iter)->enabled() ) { const size_t availableSpace= &_pollfds.back() - pCurr + 1; size_t required = (*iter)->simpl().pollSize(); assert( required <= availableSpace); pCurr+= (*iter)->simpl().initializePoll( pCurr, required); } } _isDirty= false; } #ifdef HAVE_PPOLL struct timespec pollTimeout = { 0, 0 }; struct timespec* pollTimeoutP = 0; if (until >= Timespan(0)) pollTimeoutP = &pollTimeout; #else int pollTimeout = until == Timespan(0) ? 0 : -1; #endif int ret = -1; while (true) { if (until > Timespan(0)) { Timespan remaining = until - Timespan::gettimeofday(); if (remaining < Timespan(0)) remaining = Timespan(0); #ifdef HAVE_PPOLL pollTimeout.tv_sec = remaining.totalUSecs() / 1000000; pollTimeout.tv_nsec = (remaining.totalUSecs() % 1000000) * 1000; #else if (Milliseconds(remaining) >= std::numeric_limits<int>::max()) pollTimeout = std::numeric_limits<int>::max(); else pollTimeout = Milliseconds(remaining).ceil(); #endif log_debug("remaining " << remaining); } else log_debug("no timeout"); #ifdef HAVE_PPOLL log_debug("ppoll with " << _pollfds.size() << " fds, timeout=" << pollTimeout.tv_sec << "s " << pollTimeout.tv_nsec << "ns"); ret = ::ppoll(&_pollfds[0], _pollfds.size(), pollTimeoutP, 0); log_debug("ppoll returns " << ret); #else log_debug("poll with " << _pollfds.size() << " fds, timeout=" << pollTimeout << "ms"); ret = ::poll(&_pollfds[0], _pollfds.size(), pollTimeout); log_debug("poll returns " << ret); #endif if( ret != -1 ) break; if( errno != EINTR ) throw IOError("Could not poll on file descriptors"); } if( ret == 0 && _avail.empty() ) return false; bool avail = false; try { if (_pollfds[0].revents != 0) { if ( _pollfds[0].revents & POLL_ERROR_MASK) { throw IOError("poll error on event pipe"); } static char buffer[1024]; while(true) { int ret = ::read(_wakePipe[0], buffer, sizeof(buffer)); if(ret > 0) { avail = true; continue; } if (ret == -1) { if(errno == EINTR) continue; if(errno == EAGAIN) break; } throw IOError("Could not read from pipe"); } } for( _current = _devices.begin(); _current != _devices.end(); ) { Selectable* dev = *_current; if ( dev->enabled() && dev->simpl().checkPollEvent() ) { avail = true; } if (_current != _devices.end()) { if (*_current == dev) { ++_current; } } } } catch (...) { _current= _devices.end(); throw; } return avail; }
Timespan Timespan::operator + (TimeDiff microseconds) const { return Timespan(_span + microseconds); }
Timespan DateTime::operator - (const DateTime& dateTime) const { return Timespan((_utcTime - dateTime._utcTime)/10); }
Timespan Timespan::operator -(const Timespan& b) const { return Timespan(*this) -= b; }