util: switch to boost::asio::basic_waitable_timer
Change-Id: I2724befc86482adf84f59063e65509a116bb1b46
Refs: #4431
diff --git a/src/common-pch.hpp b/src/common-pch.hpp
index 58c4513..12be185 100644
--- a/src/common-pch.hpp
+++ b/src/common-pch.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -40,7 +40,6 @@
// Boost headers to precompile
#include <boost/algorithm/string.hpp>
#include <boost/chrono.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/stream.hpp>
diff --git a/src/net/detail/network-monitor-impl-osx.hpp b/src/net/detail/network-monitor-impl-osx.hpp
index 809c638..93cd57a 100644
--- a/src/net/detail/network-monitor-impl-osx.hpp
+++ b/src/net/detail/network-monitor-impl-osx.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -37,6 +37,7 @@
#include <SystemConfiguration/SystemConfiguration.h>
#include <boost/asio/ip/udp.hpp>
+#include <map>
namespace ndn {
namespace net {
diff --git a/src/security/tpm/back-end-file.cpp b/src/security/tpm/back-end-file.cpp
index 1adcf1c..7c836d2 100644
--- a/src/security/tpm/back-end-file.cpp
+++ b/src/security/tpm/back-end-file.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2017 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,9 +24,11 @@
#include "../transform.hpp"
#include "../transform/private-key.hpp"
#include "../../encoding/buffer-stream.hpp"
-#include <unordered_map>
-#include <fstream>
+
#include <cstdlib>
+#include <fstream>
+#include <sys/stat.h>
+
#include <boost/filesystem.hpp>
namespace ndn {
@@ -45,12 +47,12 @@
keystorePath = boost::filesystem::path(dir);
}
#ifdef NDN_CXX_HAVE_TESTS
- else if (getenv("TEST_HOME") != nullptr) {
- keystorePath = boost::filesystem::path(getenv("TEST_HOME")) / ".ndn";
+ else if (std::getenv("TEST_HOME") != nullptr) {
+ keystorePath = boost::filesystem::path(std::getenv("TEST_HOME")) / ".ndn";
}
#endif // NDN_CXX_HAVE_TESTS
- else if (getenv("HOME") != nullptr) {
- keystorePath = boost::filesystem::path(getenv("HOME")) / ".ndn";
+ else if (std::getenv("HOME") != nullptr) {
+ keystorePath = boost::filesystem::path(std::getenv("HOME")) / ".ndn";
}
else {
keystorePath = boost::filesystem::current_path() / ".ndn";
@@ -191,7 +193,7 @@
key->savePkcs1Base64(os);
// set file permission
- chmod(fileName.c_str(), 0000400);
+ ::chmod(fileName.c_str(), 0000400);
}
} // namespace tpm
diff --git a/src/util/detail/monotonic-deadline-timer.hpp b/src/util/detail/monotonic-deadline-timer.hpp
deleted file mode 100644
index 002912c..0000000
--- a/src/util/detail/monotonic-deadline-timer.hpp
+++ /dev/null
@@ -1,93 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2017 Regents of the University of California.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- *
- * ndn-cxx library is free software: you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received copies of the GNU General Public License and GNU Lesser
- * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- */
-
-/**
- * This code is based on https://svn.boost.org/trac/boost/attachment/ticket/3504/MonotonicDeadlineTimer.h
- */
-
-#ifndef NDN_UTIL_DETAIL_MONOTONIC_DEADLINE_TIMER_HPP
-#define NDN_UTIL_DETAIL_MONOTONIC_DEADLINE_TIMER_HPP
-
-#include "../time.hpp"
-#include <boost/asio/basic_deadline_timer.hpp>
-#include <boost/asio/io_service.hpp>
-
-namespace boost {
-namespace asio {
-
-template<>
-struct time_traits<ndn::time::steady_clock>
-{
- using time_type = ndn::time::steady_clock::TimePoint;
- using duration_type = ndn::time::steady_clock::Duration;
-
- static time_type
- now()
- {
- return ndn::time::steady_clock::now();
- }
-
- static time_type
- add(const time_type& time, const duration_type& duration)
- {
- return time + duration;
- }
-
- static duration_type
- subtract(const time_type& timeLhs, const time_type& timeRhs)
- {
- return timeLhs - timeRhs;
- }
-
- static bool
- less_than(const time_type& timeLhs, const time_type& timeRhs)
- {
- return timeLhs < timeRhs;
- }
-
- static boost::posix_time::time_duration
- to_posix_duration(const duration_type& duration)
- {
- return ndn::time::steady_clock::to_posix_duration(duration);
- }
-};
-
-} // namespace asio
-} // namespace boost
-
-namespace ndn {
-namespace util {
-namespace detail {
-
-class MonotonicDeadlineTimer : public boost::asio::basic_deadline_timer<time::steady_clock>
-{
-public:
- MonotonicDeadlineTimer(boost::asio::io_service& ioService)
- : boost::asio::basic_deadline_timer<time::steady_clock>(ioService)
- {
- }
-};
-
-} // namespace detail
-} // namespace util
-} // namespace ndn
-
-#endif // NDN_UTIL_DETAIL_MONOTONIC_DEADLINE_TIMER_HPP
diff --git a/src/util/detail/steady-timer.hpp b/src/util/detail/steady-timer.hpp
new file mode 100644
index 0000000..4fb4e27
--- /dev/null
+++ b/src/util/detail/steady-timer.hpp
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_DETAIL_STEADY_TIMER_HPP
+#define NDN_UTIL_DETAIL_STEADY_TIMER_HPP
+
+#include "../time.hpp"
+
+#include <boost/asio/basic_waitable_timer.hpp>
+#include <boost/asio/wait_traits.hpp>
+
+namespace boost {
+namespace asio {
+
+template<>
+struct wait_traits<ndn::time::steady_clock>
+{
+ static ndn::time::steady_clock::duration
+ to_wait_duration(const ndn::time::steady_clock::duration& d)
+ {
+ return ndn::time::steady_clock::to_wait_duration(d);
+ }
+};
+
+} // namespace asio
+} // namespace boost
+
+namespace ndn {
+namespace util {
+namespace detail {
+
+class SteadyTimer : public boost::asio::basic_waitable_timer<time::steady_clock>
+{
+public:
+ using boost::asio::basic_waitable_timer<time::steady_clock>::basic_waitable_timer;
+};
+
+} // namespace detail
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_DETAIL_STEADY_TIMER_HPP
diff --git a/src/util/notification-subscriber.cpp b/src/util/notification-subscriber.cpp
index 56dee40..66ffd8c 100644
--- a/src/util/notification-subscriber.cpp
+++ b/src/util/notification-subscriber.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016 Regents of the University of California,
+/*
+ * Copyright (c) 2014-2018 Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -28,6 +28,8 @@
#include "notification-subscriber.hpp"
#include "random.hpp"
+#include <cmath>
+
namespace ndn {
namespace util {
@@ -186,8 +188,8 @@
m_lastNackSequenceNo = nackSequenceNo;
- return time::milliseconds(static_cast<uint32_t>(pow(2, m_attempts) * 100 +
- random::generateWord32() % 100));
+ return time::milliseconds(static_cast<time::milliseconds::rep>(std::pow(2, m_attempts) * 100 +
+ random::generateWord32() % 100));
}
} // namespace util
diff --git a/src/util/scheduler.cpp b/src/util/scheduler.cpp
index 3170f61..167b6e0 100644
--- a/src/util/scheduler.cpp
+++ b/src/util/scheduler.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -20,7 +20,7 @@
*/
#include "scheduler.hpp"
-#include "detail/monotonic-deadline-timer.hpp"
+#include "detail/steady-timer.hpp"
#include <boost/scope_exit.hpp>
@@ -76,7 +76,7 @@
}
Scheduler::Scheduler(boost::asio::io_service& ioService)
- : m_deadlineTimer(make_unique<detail::MonotonicDeadlineTimer>(ioService))
+ : m_timer(make_unique<detail::SteadyTimer>(ioService))
, m_isEventExecuting(false)
{
}
@@ -108,7 +108,7 @@
}
if (info->queueIt == m_queue.begin()) {
- m_deadlineTimer->cancel();
+ m_timer->cancel();
}
m_queue.erase(info->queueIt);
@@ -121,15 +121,15 @@
Scheduler::cancelAllEvents()
{
m_queue.clear();
- m_deadlineTimer->cancel();
+ m_timer->cancel();
}
void
Scheduler::scheduleNext()
{
if (!m_queue.empty()) {
- m_deadlineTimer->expires_from_now((*m_queue.begin())->expiresFromNow());
- m_deadlineTimer->async_wait(bind(&Scheduler::executeEvent, this, _1));
+ m_timer->expires_from_now((*m_queue.begin())->expiresFromNow());
+ m_timer->async_wait(bind(&Scheduler::executeEvent, this, _1));
}
}
@@ -148,9 +148,9 @@
} BOOST_SCOPE_EXIT_END
// process all expired events
- time::steady_clock::TimePoint now = time::steady_clock::now();
+ auto now = time::steady_clock::now();
while (!m_queue.empty()) {
- EventQueue::iterator head = m_queue.begin();
+ auto head = m_queue.begin();
shared_ptr<EventInfo> info = *head;
if (info->expireTime > now) {
break;
diff --git a/src/util/scheduler.hpp b/src/util/scheduler.hpp
index 6d04bd1..79fbf23 100644
--- a/src/util/scheduler.hpp
+++ b/src/util/scheduler.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -25,13 +25,14 @@
#include "time.hpp"
#include "../net/asio-fwd.hpp"
+#include <boost/system/error_code.hpp>
#include <set>
namespace ndn {
namespace util {
namespace detail {
-class MonotonicDeadlineTimer;
+class SteadyTimer;
} // namespace detail
namespace scheduler {
@@ -163,7 +164,7 @@
executeEvent(const boost::system::error_code& code);
private:
- unique_ptr<detail::MonotonicDeadlineTimer> m_deadlineTimer;
+ unique_ptr<detail::SteadyTimer> m_timer;
EventQueue m_queue;
bool m_isEventExecuting;
};
diff --git a/src/util/segment-fetcher.cpp b/src/util/segment-fetcher.cpp
index c8075e7..0d54915 100644
--- a/src/util/segment-fetcher.cpp
+++ b/src/util/segment-fetcher.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -26,6 +26,7 @@
#include "../lp/nack-header.hpp"
#include <boost/lexical_cast.hpp>
+#include <cmath>
namespace ndn {
namespace util {
@@ -162,7 +163,8 @@
reExpressInterest(origInterest, reExpressCount, self);
break;
case lp::NackReason::CONGESTION:
- m_scheduler.scheduleEvent(time::milliseconds(static_cast<uint32_t>(pow(2, reExpressCount + 1))),
+ using ms = time::milliseconds;
+ m_scheduler.scheduleEvent(ms(static_cast<ms::rep>(std::pow(2, reExpressCount + 1))),
bind(&SegmentFetcher::reExpressInterest, this,
origInterest, reExpressCount, self));
break;
diff --git a/src/util/time-custom-clock.hpp b/src/util/time-custom-clock.hpp
index 57579e5..43d525c 100644
--- a/src/util/time-custom-clock.hpp
+++ b/src/util/time-custom-clock.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -19,8 +19,8 @@
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
-#ifndef NDN_TIME_CUSTOM_CLOCK_HPP
-#define NDN_TIME_CUSTOM_CLOCK_HPP
+#ifndef NDN_UTIL_TIME_CUSTOM_CLOCK_HPP
+#define NDN_UTIL_TIME_CUSTOM_CLOCK_HPP
#include "time.hpp"
@@ -46,12 +46,12 @@
virtual std::string
getSince() const = 0;
- virtual boost::posix_time::time_duration
- toPosixDuration(const typename BaseClock::duration& duration) const = 0;
+ virtual typename BaseClock::duration
+ toWaitDuration(typename BaseClock::duration d) const = 0;
};
-typedef CustomClock<system_clock> CustomSystemClock;
-typedef CustomClock<steady_clock> CustomSteadyClock;
+using CustomSystemClock = CustomClock<system_clock>;
+using CustomSteadyClock = CustomClock<steady_clock>;
/**
* \brief Set custom system and steady clocks
@@ -66,4 +66,4 @@
} // namespace time
} // namespace ndn
-#endif // NDN_TIME_CUSTOM_CLOCK_HPP
+#endif // NDN_UTIL_TIME_CUSTOM_CLOCK_HPP
diff --git a/src/util/time-unit-test-clock.cpp b/src/util/time-unit-test-clock.cpp
index 58e1f03..0e34756 100644
--- a/src/util/time-unit-test-clock.cpp
+++ b/src/util/time-unit-test-clock.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -20,78 +20,75 @@
*/
#include "time-unit-test-clock.hpp"
-#include "detail/monotonic-deadline-timer.hpp"
+#include "detail/steady-timer.hpp"
+
+#include <chrono>
#include <thread>
namespace ndn {
namespace time {
-const std::chrono::microseconds SLEEP_AFTER_TIME_CHANGE(2);
-
-template<class BaseClock>
-UnitTestClock<BaseClock>::UnitTestClock(const nanoseconds& startTime)
+template<class BaseClock, class ClockTraits>
+UnitTestClock<BaseClock, ClockTraits>::UnitTestClock(nanoseconds startTime)
: m_currentTime(startTime)
{
}
-template<class BaseClock>
+template<class BaseClock, class ClockTraits>
std::string
-UnitTestClock<BaseClock>::getSince() const
+UnitTestClock<BaseClock, ClockTraits>::getSince() const
{
- return " since unit test clock advancements";
+ return " since unit test beginning";
}
-template<class BaseClock>
+template<class BaseClock, class ClockTraits>
typename BaseClock::time_point
-UnitTestClock<BaseClock>::getNow() const
+UnitTestClock<BaseClock, ClockTraits>::getNow() const
{
return typename BaseClock::time_point(duration_cast<typename BaseClock::duration>(m_currentTime));
}
-template<class BaseClock>
-boost::posix_time::time_duration
-UnitTestClock<BaseClock>::toPosixDuration(const typename BaseClock::duration& duration) const
+template<class BaseClock, class ClockTraits>
+typename BaseClock::duration
+UnitTestClock<BaseClock, ClockTraits>::toWaitDuration(typename BaseClock::duration) const
{
- return
-#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
- boost::posix_time::nanoseconds(1)
-#else
- boost::posix_time::microseconds(1)
-#endif
- ;
+ return typename BaseClock::duration(1);
}
-
-template<class BaseClock>
+template<class BaseClock, class ClockTraits>
void
-UnitTestClock<BaseClock>::advance(const nanoseconds& duration)
+UnitTestClock<BaseClock, ClockTraits>::advance(nanoseconds duration)
{
m_currentTime += duration;
- // On some platforms, boost::asio::io_service for deadline_timer (e.g., the one used in
- // Scheduler) will call time_traits<>::now() and will "sleep" for
- // time_traits<>::to_posix_time(duration) period before calling time_traits<>::now()
- // again. (Note that such "sleep" will occur even if there is no actual waiting and
- // program is calling io_service.poll().)
+ // When UnitTestClock is used with Asio timers (e.g. basic_waitable_timer), and
+ // an async wait operation on a timer is already in progress, Asio won't look
+ // at the clock again until the earliest timer has expired, so it won't know that
+ // the current time has changed.
//
- // As a result, in order for the clock advancement to be effective, we must sleep for a
- // period greater than time_traits<>::to_posix_time().
+ // Therefore, in order for the clock advancement to be effective, we must sleep
+ // for a period greater than wait_traits::to_wait_duration().
//
- // See also http://blog.think-async.com/2007/08/time-travel.html
- BOOST_ASSERT(boost::posix_time::microseconds(SLEEP_AFTER_TIME_CHANGE.count()) >
- boost::asio::time_traits<steady_clock>::to_posix_duration(duration));
- std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
+ // See also http://blog.think-async.com/2007/08/time-travel.html - "Jumping Through Time"
+ //
+ std::this_thread::sleep_for(std::chrono::nanoseconds(duration_cast<nanoseconds>(
+ boost::asio::wait_traits<steady_clock>::to_wait_duration(duration) +
+ typename BaseClock::duration(1)).count()));
}
-template<class BaseClock>
+template<class BaseClock, class ClockTraits>
void
-UnitTestClock<BaseClock>::setNow(const nanoseconds& timeSinceEpoch)
+UnitTestClock<BaseClock, ClockTraits>::setNow(nanoseconds timeSinceEpoch)
{
- BOOST_ASSERT(boost::posix_time::microseconds(SLEEP_AFTER_TIME_CHANGE.count()) >
- boost::asio::time_traits<steady_clock>::to_posix_duration(timeSinceEpoch -
- m_currentTime));
+ BOOST_ASSERT(!BaseClock::is_steady || timeSinceEpoch >= m_currentTime);
+
m_currentTime = timeSinceEpoch;
- std::this_thread::sleep_for(SLEEP_AFTER_TIME_CHANGE);
+
+ // See comment in advance()
+ auto delta = timeSinceEpoch - m_currentTime;
+ std::this_thread::sleep_for(std::chrono::nanoseconds(duration_cast<nanoseconds>(
+ boost::asio::wait_traits<steady_clock>::to_wait_duration(delta) +
+ typename BaseClock::duration(1)).count()));
}
template
diff --git a/src/util/time-unit-test-clock.hpp b/src/util/time-unit-test-clock.hpp
index 7d67507..0c6b90f 100644
--- a/src/util/time-unit-test-clock.hpp
+++ b/src/util/time-unit-test-clock.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -19,8 +19,8 @@
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
-#ifndef NDN_TIME_UNIT_TEST_CLOCK_HPP
-#define NDN_TIME_UNIT_TEST_CLOCK_HPP
+#ifndef NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP
+#define NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP
#include "time-custom-clock.hpp"
@@ -48,7 +48,7 @@
* @brief Specialization of UnitTestClockTraits for system_clock
*
* This specialization sets the default start time to 1415684132 seconds
- * (equivalent to Tue, 11 Nov 2014 05:35:32 UTC if unix epoch is assumed).
+ * (equivalent to Tue 11 Nov 2014 05:35:32 UTC, if Unix epoch is assumed).
*/
template<>
class UnitTestClockTraits<system_clock>
@@ -69,34 +69,34 @@
*
* @note Default start time is determined by UnitTestClockTraits
*/
-template<class BaseClock>
+template<class BaseClock, class ClockTraits = UnitTestClockTraits<BaseClock>>
class UnitTestClock : public CustomClock<BaseClock>
{
public:
explicit
- UnitTestClock(const nanoseconds& startTime = UnitTestClockTraits<BaseClock>::getDefaultStartTime());
+ UnitTestClock(nanoseconds startTime = ClockTraits::getDefaultStartTime());
/**
* @brief Advance unit test clock by @p duration
*/
void
- advance(const nanoseconds& duration);
+ advance(nanoseconds duration);
/**
* @brief Explicitly set clock to @p timeSinceEpoch
*/
void
- setNow(const nanoseconds& timeSinceEpoch);
+ setNow(nanoseconds timeSinceEpoch);
-public: // CustomClock<BaseClock>
+public:
std::string
getSince() const override;
typename BaseClock::time_point
getNow() const override;
- boost::posix_time::time_duration
- toPosixDuration(const typename BaseClock::duration& duration) const override;
+ typename BaseClock::duration
+ toWaitDuration(typename BaseClock::duration d) const override;
private:
nanoseconds m_currentTime;
@@ -105,10 +105,10 @@
extern template class UnitTestClock<system_clock>;
extern template class UnitTestClock<steady_clock>;
-typedef UnitTestClock<system_clock> UnitTestSystemClock;
-typedef UnitTestClock<steady_clock> UnitTestSteadyClock;
+using UnitTestSystemClock = UnitTestClock<system_clock>;
+using UnitTestSteadyClock = UnitTestClock<steady_clock>;
} // namespace time
} // namespace ndn
-#endif // NDN_TIME_UNIT_TEST_CLOCK_HPP
+#endif // NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP
diff --git a/src/util/time.cpp b/src/util/time.cpp
index 6f669cb..e9af51f 100644
--- a/src/util/time.cpp
+++ b/src/util/time.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -31,13 +31,13 @@
static shared_ptr<CustomSystemClock> g_systemClock;
static shared_ptr<CustomSteadyClock> g_steadyClock;
-// this method is defined in time-custom-clock.hpp
+// this function is declared in time-custom-clock.hpp
void
setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock,
shared_ptr<CustomSystemClock> systemClock)
{
- g_systemClock = systemClock;
- g_steadyClock = steadyClock;
+ g_systemClock = std::move(systemClock);
+ g_steadyClock = std::move(steadyClock);
}
/////////////////////////////////////////////////////////////////////////////////////////////
@@ -55,7 +55,7 @@
}
std::time_t
-system_clock::to_time_t(const time_point& t) noexcept
+system_clock::to_time_t(const system_clock::time_point& t) noexcept
{
return duration_cast<seconds>(t.time_since_epoch()).count();
}
@@ -88,21 +88,15 @@
}
}
-boost::posix_time::time_duration
-steady_clock::to_posix_duration(const duration& duration)
+steady_clock::duration
+steady_clock::to_wait_duration(steady_clock::duration d)
{
if (g_steadyClock == nullptr) {
// optimized default version
- return
-#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
- boost::posix_time::nanoseconds(duration_cast<nanoseconds>(duration).count())
-#else
- boost::posix_time::microseconds(duration_cast<microseconds>(duration).count())
-#endif
- ;
+ return d;
}
else {
- return g_steadyClock->toPosixDuration(duration);
+ return g_steadyClock->toWaitDuration(d);
}
}
@@ -111,7 +105,7 @@
const system_clock::TimePoint&
getUnixEpoch()
{
- static system_clock::TimePoint epoch = system_clock::from_time_t(0);
+ static auto epoch = system_clock::from_time_t(0);
return epoch;
}
@@ -122,7 +116,7 @@
}
system_clock::TimePoint
-fromUnixTimestamp(const milliseconds& duration)
+fromUnixTimestamp(milliseconds duration)
{
return getUnixEpoch() + duration;
}
@@ -141,28 +135,24 @@
constexpr auto unitsPerHour = duration_cast<BptResolutionUnit>(hours(1)).count();
auto sinceEpoch = duration_cast<BptResolutionUnit>(timePoint - getUnixEpoch()).count();
- bpt::ptime ptime = epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0,
- sinceEpoch % unitsPerHour);
+ bpt::ptime ptime = epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour);
return bpt::to_iso_string(ptime);
}
-
system_clock::TimePoint
fromIsoString(const std::string& isoString)
{
namespace bpt = boost::posix_time;
- static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
+ static bpt::ptime epoch = bpt::from_time_t(0);
bpt::ptime ptime = bpt::from_iso_string(isoString);
+ auto point = system_clock::from_time_t((ptime - epoch).total_seconds());
+ point += microseconds((ptime - epoch).total_microseconds() % 1000000);
- system_clock::TimePoint point =
- system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
- point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
return point;
}
-
std::string
toString(const system_clock::TimePoint& timePoint,
const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
@@ -182,25 +172,23 @@
return formattedTimePoint.str();
}
-
system_clock::TimePoint
-fromString(const std::string& formattedTimePoint,
+fromString(const std::string& timePointStr,
const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
const std::locale& locale/* = std::locale("C")*/)
{
namespace bpt = boost::posix_time;
- static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
+ static bpt::ptime epoch = bpt::from_time_t(0);
bpt::time_input_facet* facet = new bpt::time_input_facet(format);
- std::istringstream is(formattedTimePoint);
-
+ std::istringstream is(timePointStr);
is.imbue(std::locale(locale, facet));
bpt::ptime ptime;
is >> ptime;
- system_clock::TimePoint point =
- system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
- point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
+ auto point = system_clock::from_time_t((ptime - epoch).total_seconds());
+ point += microseconds((ptime - epoch).total_microseconds() % 1000000);
+
return point;
}
@@ -210,8 +198,6 @@
namespace boost {
namespace chrono {
-/////////////////////////////////////////////////////////////////////////////////////////////
-
template<class CharT>
std::basic_string<CharT>
clock_string<ndn::time::system_clock, CharT>::since()
@@ -225,11 +211,6 @@
}
}
-template
-struct clock_string<ndn::time::system_clock, char>;
-
-/////////////////////////////////////////////////////////////////////////////////////////////
-
template<class CharT>
std::basic_string<CharT>
clock_string<ndn::time::steady_clock, CharT>::since()
@@ -243,8 +224,8 @@
}
}
-template
-struct clock_string<ndn::time::steady_clock, char>;
+template struct clock_string<ndn::time::system_clock, char>;
+template struct clock_string<ndn::time::steady_clock, char>;
} // namespace chrono
} // namespace boost
diff --git a/src/util/time.hpp b/src/util/time.hpp
index e239ccd..29274a5 100644
--- a/src/util/time.hpp
+++ b/src/util/time.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -19,30 +19,28 @@
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
-#ifndef NDN_TIME_HPP
-#define NDN_TIME_HPP
+#ifndef NDN_UTIL_TIME_HPP
+#define NDN_UTIL_TIME_HPP
#include "../common.hpp"
+
+#include <boost/asio/wait_traits.hpp>
#include <boost/chrono.hpp>
-#include <boost/asio/time_traits.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
namespace ndn {
namespace time {
using boost::chrono::duration;
+using boost::chrono::duration_cast;
-typedef duration<boost::int_least32_t, boost::ratio<86400> > days;
+using days = duration<int_fast32_t, boost::ratio<86400>>;
using boost::chrono::hours;
using boost::chrono::minutes;
using boost::chrono::seconds;
-
using boost::chrono::milliseconds;
using boost::chrono::microseconds;
using boost::chrono::nanoseconds;
-using boost::chrono::duration_cast;
-
/** \return the absolute value of the duration d
* \note The function does not participate in the overload resolution
* unless std::numeric_limits<Rep>::is_signed is true.
@@ -64,13 +62,13 @@
* adjusted at any moment. It is the only clock that has the ability
* to be displayed and converted to/from UNIX timestamp.
*
- * To get current TimePoint:
+ * To get the current time:
*
* <code>
* system_clock::TimePoint now = system_clock::now();
* </code>
*
- * To convert TimePoint to/from UNIX timestamp:
+ * To convert a TimePoint to/from UNIX timestamp:
*
* <code>
* system_clock::TimePoint time = ...;
@@ -81,11 +79,11 @@
class system_clock
{
public:
- typedef BOOST_SYSTEM_CLOCK_DURATION duration;
- typedef duration::rep rep;
- typedef duration::period period;
- typedef boost::chrono::time_point<system_clock> time_point;
- static constexpr bool is_steady = false;
+ using duration = boost::chrono::system_clock::duration;
+ using rep = duration::rep;
+ using period = duration::period;
+ using time_point = boost::chrono::time_point<system_clock>;
+ static constexpr bool is_steady = boost::chrono::system_clock::is_steady;
typedef time_point TimePoint;
typedef duration Duration;
@@ -98,7 +96,7 @@
static time_point
from_time_t(std::time_t t) noexcept;
-}; // class system_clock
+};
/**
* \brief Steady clock
@@ -111,10 +109,10 @@
class steady_clock
{
public:
- typedef nanoseconds duration;
- typedef duration::rep rep;
- typedef duration::period period;
- typedef boost::chrono::time_point<steady_clock> time_point;
+ using duration = boost::chrono::steady_clock::duration;
+ using rep = duration::rep;
+ using period = duration::period;
+ using time_point = boost::chrono::time_point<steady_clock>;
static constexpr bool is_steady = true;
typedef time_point TimePoint;
@@ -125,19 +123,18 @@
private:
/**
- * \brief Method to be used in deadline timer to select proper waiting
+ * \brief Trait function used in detail::SteadyTimer to select proper waiting time
*
- * Mock time implementations should return minimum value to ensure Boost.Asio
- * is not enabling any waiting on mock timers.
+ * Mock time implementations should return the minimum value to ensure
+ * that Boost.Asio doesn't perform any waiting on mock timers.
*
- * @sa http://stackoverflow.com/questions/14191855/how-do-you-mock-the-time-for-boost-timers
+ * @sa http://blog.think-async.com/2007/08/time-travel.html
*/
- static boost::posix_time::time_duration
- to_posix_duration(const duration& duration);
+ static duration
+ to_wait_duration(duration d);
- friend struct boost::asio::time_traits<steady_clock>;
-}; // class steady_clock
-
+ friend struct boost::asio::wait_traits<steady_clock>; // see steady-timer.hpp
+};
/**
* \brief Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970)
@@ -155,13 +152,13 @@
* \brief Convert UNIX timestamp to system_clock::TimePoint
*/
system_clock::TimePoint
-fromUnixTimestamp(const milliseconds& duration);
+fromUnixTimestamp(milliseconds duration);
/**
* \brief Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
*
- * If timePoint contains doesn't contain fractional seconds the
- * output format is YYYYMMDDTHHMMSS
+ * If \p timePoint contains doesn't contain fractional seconds,
+ * the output format is YYYYMMDDTHHMMSS
*
* Examples:
*
@@ -199,7 +196,7 @@
* \param locale desired locale (default: "C" locale)
*
* \sa http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/date_time_io.html#date_time.format_flags
- * described possible formatting flags
+ * describes possible formatting flags
**/
std::string
toString(const system_clock::TimePoint& timePoint,
@@ -212,21 +209,18 @@
* By default, `%Y-%m-%d %H:%M:%S` is used, accepting dates like
* `2014-04-10 22:51:00`
*
- * \param formattedTimePoint string representing time point
- * \param format input output format (default: `%Y-%m-%d %H:%M:%S`)
- * \param locale input locale (default: "C" locale)
+ * \param timePointStr string representing time point
+ * \param format input output format (default: `%Y-%m-%d %H:%M:%S`)
+ * \param locale input locale (default: "C" locale)
*
* \sa http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/date_time_io.html#date_time.format_flags
- * described possible formatting flags
+ * describes possible formatting flags
*/
system_clock::TimePoint
-fromString(const std::string& formattedTimePoint,
+fromString(const std::string& timePointStr,
const std::string& format = "%Y-%m-%d %H:%M:%S",
const std::locale& locale = std::locale("C"));
-
-////////////////////////////////////////////////////////////////////////////////
-
} // namespace time
} // namespace ndn
@@ -247,7 +241,10 @@
since();
};
+extern template struct clock_string<ndn::time::system_clock, char>;
+extern template struct clock_string<ndn::time::steady_clock, char>;
+
} // namespace chrono
} // namespace boost
-#endif // NDN_TIME_HPP
+#endif // NDN_UTIL_TIME_HPP