fw: Use ndn-cxx's RttEstimator in AsfStrategy

Refs: #4887
Change-Id: Ice718be99f2b96f183bb35c5df84930be54a9f8e
diff --git a/daemon/fw/asf-measurements.cpp b/daemon/fw/asf-measurements.cpp
index 404fc85..cf3a317 100644
--- a/daemon/fw/asf-measurements.cpp
+++ b/daemon/fw/asf-measurements.cpp
@@ -32,8 +32,8 @@
 
 NFD_LOG_INIT(AsfMeasurements);
 
-const RttStats::Rtt RttStats::RTT_TIMEOUT(-1.0);
-const RttStats::Rtt RttStats::RTT_NO_MEASUREMENT(0.0);
+const time::nanoseconds RttStats::RTT_TIMEOUT(-1);
+const time::nanoseconds RttStats::RTT_NO_MEASUREMENT(0);
 const double RttStats::ALPHA = 0.125;
 
 RttStats::RttStats()
@@ -43,23 +43,11 @@
 }
 
 void
-RttStats::addRttMeasurement(RttEstimator::Duration& durationRtt)
+RttStats::addRttMeasurement(time::nanoseconds durationRtt)
 {
-  m_rtt = static_cast<RttStats::Rtt>(durationRtt.count());
-
-  m_rttEstimator.addMeasurement(durationRtt);
-
-  m_srtt = computeSrtt(m_srtt, m_rtt);
-}
-
-RttStats::Rtt
-RttStats::computeSrtt(Rtt previousSrtt, Rtt currentRtt)
-{
-  if (previousSrtt == RTT_NO_MEASUREMENT) {
-    return currentRtt;
-  }
-
-  return Rtt(ALPHA * currentRtt + (1 - ALPHA) * previousSrtt);
+  m_rtt = durationRtt;
+  m_rttEstimator.addMeasurement(durationRtt, 1);
+  m_srtt = m_rttEstimator.getSmoothedRtt();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -122,14 +110,9 @@
     return;
   }
 
-  time::steady_clock::Duration steadyRtt = time::steady_clock::now() - outRecord->getLastRenewed();
-  auto durationRtt = time::duration_cast<RttEstimator::Duration>(steadyRtt);
-
-  m_rttStats.addRttMeasurement(durationRtt);
-
+  m_rttStats.addRttMeasurement(time::steady_clock::now() - outRecord->getLastRenewed());
   NFD_LOG_TRACE("Recording RTT for FaceId: " << inFace.getId()
-                                             << " RTT: "    << m_rttStats.getRtt()
-                                             << " SRTT: "   << m_rttStats.getSrtt());
+                << " RTT: " << m_rttStats.getRtt() << " SRTT: " << m_rttStats.getSrtt());
 }
 
 void
@@ -167,9 +150,10 @@
   FaceInfo* info = nullptr;
 
   if (it == m_fit.end()) {
-    const auto& pair = m_fit.emplace(faceId, FaceInfo());
+    const auto& pair = m_fit.emplace(std::piecewise_construct,
+                                     std::forward_as_tuple(faceId),
+                                     std::forward_as_tuple());
     info = &pair.first->second;
-
     extendFaceInfoLifetime(*info, faceId);
   }
   else {
diff --git a/daemon/fw/asf-measurements.hpp b/daemon/fw/asf-measurements.hpp
index a3775b0..0032f14 100644
--- a/daemon/fw/asf-measurements.hpp
+++ b/daemon/fw/asf-measurements.hpp
@@ -26,10 +26,11 @@
 #ifndef NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
 #define NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
 
-#include "core/rtt-estimator.hpp"
 #include "fw/strategy-info.hpp"
 #include "table/measurements-accessor.hpp"
 
+#include <ndn-cxx/util/rtt-estimator.hpp>
+
 namespace nfd {
 namespace fw {
 namespace asf {
@@ -37,12 +38,10 @@
 class RttStats
 {
 public:
-  typedef time::duration<double, boost::micro> Rtt;
-
   RttStats();
 
   void
-  addRttMeasurement(RttEstimator::Duration& durationRtt);
+  addRttMeasurement(time::nanoseconds rtt);
 
   void
   recordTimeout()
@@ -50,36 +49,32 @@
     m_rtt = RTT_TIMEOUT;
   }
 
-  Rtt
+  time::nanoseconds
   getRtt() const
   {
     return m_rtt;
   }
 
-  Rtt
+  time::nanoseconds
   getSrtt() const
   {
     return m_srtt;
   }
 
-  RttEstimator::Duration
+  time::nanoseconds
   computeRto() const
   {
-    return m_rttEstimator.computeRto();
+    return m_rttEstimator.getEstimatedRto();
   }
 
-private:
-  static Rtt
-  computeSrtt(Rtt previousSrtt, Rtt currentRtt);
-
 public:
-  static const Rtt RTT_TIMEOUT;
-  static const Rtt RTT_NO_MEASUREMENT;
+  static const time::nanoseconds RTT_TIMEOUT;
+  static const time::nanoseconds RTT_NO_MEASUREMENT;
 
 private:
-  Rtt m_srtt;
-  Rtt m_rtt;
-  RttEstimator m_rttEstimator;
+  time::nanoseconds m_srtt;
+  time::nanoseconds m_rtt;
+  ndn::util::RttEstimator m_rttEstimator;
 
   static const double ALPHA;
 };
@@ -142,19 +137,19 @@
     return getRtt() == RttStats::RTT_TIMEOUT;
   }
 
-  RttEstimator::Duration
+  time::nanoseconds
   computeRto() const
   {
     return m_rttStats.computeRto();
   }
 
-  RttStats::Rtt
+  time::nanoseconds
   getRtt() const
   {
     return m_rttStats.getRtt();
   }
 
-  RttStats::Rtt
+  time::nanoseconds
   getSrtt() const
   {
     return m_rttStats.getSrtt();
@@ -203,7 +198,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
-/** \brief stores stategy information about each face in this namespace
+/** \brief stores strategy information about each face in this namespace
  */
 class NamespaceInfo : public StrategyInfo
 {
@@ -251,10 +246,12 @@
     return m_fit.end();
   }
 
-  const FaceInfoTable::iterator
+  FaceInfoTable::iterator
   insert(FaceId faceId)
   {
-    return m_fit.emplace(faceId, FaceInfo()).first;
+    return m_fit.emplace(std::piecewise_construct,
+                         std::forward_as_tuple(faceId),
+                         std::forward_as_tuple()).first;
   }
 
   bool
diff --git a/daemon/fw/asf-strategy.cpp b/daemon/fw/asf-strategy.cpp
index ce689bc..4e26817 100644
--- a/daemon/fw/asf-strategy.cpp
+++ b/daemon/fw/asf-strategy.cpp
@@ -247,7 +247,7 @@
 
   if (!faceInfo.isTimeoutScheduled()) {
     // Estimate and schedule timeout
-    RttEstimator::Duration timeout = faceInfo.computeRto();
+    auto timeout = faceInfo.computeRto();
 
     NFD_LOG_TRACE("Scheduling timeout for " << fibEntry.getPrefix() << " to: " << egress
                   << " in " << time::duration_cast<time::milliseconds>(timeout) << " ms");
@@ -261,27 +261,27 @@
 struct FaceStats
 {
   Face* face;
-  RttStats::Rtt rtt;
-  RttStats::Rtt srtt;
+  time::nanoseconds rtt;
+  time::nanoseconds srtt;
   uint64_t cost;
 };
 
-double
+time::nanoseconds
 getValueForSorting(const FaceStats& stats)
 {
   // These values allow faces with no measurements to be ranked better than timeouts
   // srtt < RTT_NO_MEASUREMENT < RTT_TIMEOUT
-  static const RttStats::Rtt SORTING_RTT_TIMEOUT = time::microseconds::max();
-  static const RttStats::Rtt SORTING_RTT_NO_MEASUREMENT = SORTING_RTT_TIMEOUT / 2;
+  static const time::nanoseconds SORTING_RTT_TIMEOUT = time::nanoseconds::max();
+  static const time::nanoseconds SORTING_RTT_NO_MEASUREMENT = SORTING_RTT_TIMEOUT / 2;
 
   if (stats.rtt == RttStats::RTT_TIMEOUT) {
-    return SORTING_RTT_TIMEOUT.count();
+    return SORTING_RTT_TIMEOUT;
   }
   else if (stats.rtt == RttStats::RTT_NO_MEASUREMENT) {
-    return SORTING_RTT_NO_MEASUREMENT.count();
+    return SORTING_RTT_NO_MEASUREMENT;
   }
   else {
-    return stats.srtt.count();
+    return stats.srtt;
   }
 }
 
@@ -298,8 +298,8 @@
   FaceStatsSet rankedFaces(
     [] (const FaceStats& lhs, const FaceStats& rhs) -> bool {
       // Sort by RTT and then by cost
-      double lhsValue = getValueForSorting(lhs);
-      double rhsValue = getValueForSorting(rhs);
+      time::nanoseconds lhsValue = getValueForSorting(lhs);
+      time::nanoseconds rhsValue = getValueForSorting(rhs);
 
       if (lhsValue < rhsValue) {
         return true;
diff --git a/tests/daemon/fw/asf-measurements.t.cpp b/tests/daemon/fw/asf-measurements.t.cpp
index a6d8c02..1590e7c 100644
--- a/tests/daemon/fw/asf-measurements.t.cpp
+++ b/tests/daemon/fw/asf-measurements.t.cpp
@@ -50,7 +50,7 @@
   BOOST_CHECK_EQUAL(stats.getSrtt(), RttStats::RTT_NO_MEASUREMENT);
 
   // Receive Data back in 50ms
-  RttEstimator::Duration rtt(50_ms);
+  time::nanoseconds rtt(50_ms);
   stats.addRttMeasurement(rtt);
 
   BOOST_CHECK_EQUAL(stats.getRtt(), rtt);
@@ -63,7 +63,7 @@
   BOOST_CHECK_EQUAL(stats.getSrtt(), rtt);
 
   // Receive Data back with a higher RTT
-  RttEstimator::Duration higherRtt(100_ms);
+  time::nanoseconds higherRtt(100_ms);
   stats.addRttMeasurement(higherRtt);
 
   BOOST_CHECK_EQUAL(stats.getRtt(), higherRtt);
@@ -71,7 +71,7 @@
   BOOST_CHECK_LT(stats.getSrtt(), higherRtt);
 
   // Simulate timeout
-  RttStats::Rtt previousSrtt = stats.getSrtt();
+  auto previousSrtt = stats.getSrtt();
 
   stats.recordTimeout();
 
@@ -104,8 +104,8 @@
 
   pitEntry->insertOrUpdateOutRecord(*face, 0, *interest);
 
-  RttEstimator::Duration rtt(50);
-  this->advanceClocks(5_ms, rtt);
+  time::nanoseconds rtt(5_ms);
+  this->advanceClocks(5_ms);
 
   info.recordRtt(pitEntry, *face);
   info.cancelTimeoutEvent(interestName);