table: MeasurementsAccessor for strategy

refs #1276

Change-Id: Idec8f615c726f51612236a55a7532fabeb81a32d
diff --git a/daemon/table/fib-entry.cpp b/daemon/table/fib-entry.cpp
index 5b64e6b..61b9827 100644
--- a/daemon/table/fib-entry.cpp
+++ b/daemon/table/fib-entry.cpp
@@ -5,7 +5,6 @@
  */
 
 #include "fib-entry.hpp"
-#include <algorithm>
 
 namespace nfd {
 namespace fib {
@@ -62,6 +61,12 @@
   std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost);
 }
 
+void
+Entry::setStrategy(shared_ptr<fw::Strategy> strategy)
+{
+  m_strategy = strategy;
+}
+
 
 } // namespace fib
 } // namespace nfd
diff --git a/daemon/table/fib-entry.hpp b/daemon/table/fib-entry.hpp
index 40098ee..6adddd8 100644
--- a/daemon/table/fib-entry.hpp
+++ b/daemon/table/fib-entry.hpp
@@ -10,6 +10,11 @@
 #include "fib-nexthop.hpp"
 
 namespace nfd {
+
+namespace fw {
+class Strategy;
+}
+
 namespace fib {
 
 /** \class NextHopList
@@ -46,6 +51,12 @@
   /// removes a nexthop
   void
   removeNextHop(shared_ptr<Face> face);
+  
+  const fw::Strategy&
+  getStrategy() const;
+  
+  void
+  setStrategy(shared_ptr<fw::Strategy> strategy);
 
 private:
   /// sorts the nexthop list
@@ -55,6 +66,7 @@
 private:
   Name m_prefix;
   NextHopList m_nextHops;
+  shared_ptr<fw::Strategy> m_strategy;
 };
 
 
@@ -70,6 +82,12 @@
   return m_nextHops;
 }
 
+inline const fw::Strategy&
+Entry::getStrategy() const
+{
+  BOOST_ASSERT(static_cast<bool>(m_strategy));
+  return *m_strategy;
+}
 
 } // namespace fib
 } // namespace nfd
diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp
index 7a3295a..481df77 100644
--- a/daemon/table/fib.cpp
+++ b/daemon/table/fib.cpp
@@ -5,8 +5,8 @@
  */
 
 #include "fib.hpp"
-#include <algorithm>
-#include <numeric>
+#include "pit-entry.hpp"
+#include "measurements-entry.hpp"
 
 namespace nfd {
 
@@ -61,6 +61,18 @@
 }
 
 shared_ptr<fib::Entry>
+Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
+{
+  return this->findLongestPrefixMatch(pitEntry.getName());
+}
+
+shared_ptr<fib::Entry>
+Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
+{
+  return this->findLongestPrefixMatch(measurementsEntry.getName());
+}
+
+shared_ptr<fib::Entry>
 Fib::findExactMatch(const Name& prefix) const
 {
   std::list<shared_ptr<fib::Entry> >::const_iterator it =
diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp
index 22d5222..2661911 100644
--- a/daemon/table/fib.hpp
+++ b/daemon/table/fib.hpp
@@ -8,6 +8,9 @@
 #define NFD_TABLE_FIB_HPP
 
 #include "fib-entry.hpp"
+#include "pit-entry.hpp"
+#include "measurements-entry.hpp"
+
 namespace nfd {
 
 /** \class Fib
@@ -31,6 +34,14 @@
   shared_ptr<fib::Entry>
   findLongestPrefixMatch(const Name& prefix) const;
 
+  /// performs a longest prefix match
+  shared_ptr<fib::Entry>
+  findLongestPrefixMatch(const pit::Entry& pitEntry) const;
+
+  /// performs a longest prefix match
+  shared_ptr<fib::Entry>
+  findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
+
   shared_ptr<fib::Entry>
   findExactMatch(const Name& prefix) const;
 
diff --git a/daemon/table/measurements-accessor.cpp b/daemon/table/measurements-accessor.cpp
new file mode 100644
index 0000000..5a00c18
--- /dev/null
+++ b/daemon/table/measurements-accessor.cpp
@@ -0,0 +1,38 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "measurements-accessor.hpp"
+
+namespace nfd {
+
+MeasurementsAccessor::MeasurementsAccessor(Measurements& measurements,
+                                           Fib& fib, fw::Strategy* strategy)
+  : m_measurements(measurements)
+  , m_fib(fib)
+  , m_strategy(strategy)
+{
+}
+
+MeasurementsAccessor::~MeasurementsAccessor()
+{
+}
+
+shared_ptr<measurements::Entry>
+MeasurementsAccessor::filter(const shared_ptr<measurements::Entry>& entry)
+{
+  if (!static_cast<bool>(entry)) {
+    return entry;
+  }
+
+  shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*entry);
+  BOOST_ASSERT(static_cast<bool>(fibEntry));
+  if (&fibEntry->getStrategy() == m_strategy) {
+    return entry;
+  }
+  return shared_ptr<measurements::Entry>();
+}
+
+} // namespace nfd
diff --git a/daemon/table/measurements-accessor.hpp b/daemon/table/measurements-accessor.hpp
new file mode 100644
index 0000000..26a6d38
--- /dev/null
+++ b/daemon/table/measurements-accessor.hpp
@@ -0,0 +1,123 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_TABLE_MEASUREMENTS_ACCESSOR_HPP
+#define NFD_TABLE_MEASUREMENTS_ACCESSOR_HPP
+
+#include "measurements.hpp"
+#include "fib.hpp"
+
+namespace nfd {
+
+namespace fw {
+class Strategy;
+}
+
+/** \brief allows Strategy to access portion of Measurements table under its namespace
+ */
+class MeasurementsAccessor : noncopyable
+{
+public:
+  MeasurementsAccessor(Measurements& measurements, Fib& fib, fw::Strategy* strategy);
+
+  ~MeasurementsAccessor();
+
+  /// find or insert a Measurements entry for name
+  shared_ptr<measurements::Entry>
+  get(const Name& name);
+
+  /// find or insert a Measurements entry for fibEntry->getPrefix()
+  shared_ptr<measurements::Entry>
+  get(const fib::Entry& fibEntry);
+
+  /// find or insert a Measurements entry for pitEntry->getName()
+  shared_ptr<measurements::Entry>
+  get(const pit::Entry& pitEntry);
+
+  /** \brief find or insert a Measurements entry for child's parent
+   *
+   *  If child is the root entry, returns null.
+   */
+  shared_ptr<measurements::Entry>
+  getParent(shared_ptr<measurements::Entry> child);
+
+//  /// perform a longest prefix match
+//  shared_ptr<fib::Entry>
+//  findLongestPrefixMatch(const Name& name) const;
+//
+//  /// perform an exact match
+//  shared_ptr<fib::Entry>
+//  findExactMatch(const Name& name) const;
+
+  /** \brief extend lifetime of an entry
+   *
+   *  The entry will be kept until at least now()+lifetime.
+   */
+  void
+  extendLifetime(measurements::Entry& entry, const time::Duration& lifetime);
+
+private:
+  /** \brief perform access control to Measurements entry
+   *
+   *  \return entry if strategy has access to namespace, otherwise 0
+   */
+  shared_ptr<measurements::Entry>
+  filter(const shared_ptr<measurements::Entry>& entry);
+
+private:
+  Measurements& m_measurements;
+  Fib& m_fib;
+  fw::Strategy* m_strategy;
+};
+
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::get(const Name& name)
+{
+  return this->filter(m_measurements.get(name));
+}
+
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::get(const fib::Entry& fibEntry)
+{
+  if (&fibEntry.getStrategy() == m_strategy) {
+    return m_measurements.get(fibEntry);
+  }
+  return shared_ptr<measurements::Entry>();
+}
+
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::get(const pit::Entry& pitEntry)
+{
+  return this->filter(m_measurements.get(pitEntry));
+}
+
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::getParent(shared_ptr<measurements::Entry> child)
+{
+  return this->filter(m_measurements.getParent(child));
+}
+
+//inline shared_ptr<fib::Entry>
+//MeasurementsAccessor::findLongestPrefixMatch(const Name& name) const
+//{
+//  return this->filter(m_measurements.findLongestPrefixMatch(name));
+//}
+//
+//inline shared_ptr<fib::Entry>
+//MeasurementsAccessor::findExactMatch(const Name& name) const
+//{
+//  return this->filter(m_measurements.findExactMatch(name));
+//}
+
+inline void
+MeasurementsAccessor::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime)
+{
+  m_measurements.extendLifetime(entry, lifetime);
+}
+
+} // namespace nfd
+
+#endif // NFD_TABLE_MEASUREMENTS_ACCESSOR_HPP
diff --git a/daemon/table/measurements.cpp b/daemon/table/measurements.cpp
index bc5a9d2..705d3f1 100644
--- a/daemon/table/measurements.cpp
+++ b/daemon/table/measurements.cpp
@@ -71,7 +71,7 @@
 //}
 
 void
-Measurements::extendLifetime(measurements::Entry& entry, time::Duration lifetime)
+Measurements::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime)
 {
   std::map<Name, shared_ptr<measurements::Entry> >::iterator it =
       m_table.find(entry.getName());
@@ -83,7 +83,7 @@
 void
 Measurements::extendLifetimeInternal(
     std::map<Name, shared_ptr<measurements::Entry> >::iterator it,
-    time::Duration lifetime)
+    const time::Duration& lifetime)
 {
   shared_ptr<measurements::Entry>& entry = it->second;
 
diff --git a/daemon/table/measurements.hpp b/daemon/table/measurements.hpp
index 2e3a88c..0fa24e0 100644
--- a/daemon/table/measurements.hpp
+++ b/daemon/table/measurements.hpp
@@ -8,17 +8,12 @@
 #define NFD_TABLE_MEASUREMENTS_HPP
 
 #include "measurements-entry.hpp"
+#include "fib-entry.hpp"
+#include "pit-entry.hpp"
 #include "core/time.hpp"
 
 namespace nfd {
 
-namespace fib {
-class Entry;
-}
-namespace pit {
-class Entry;
-}
-
 /** \class Measurement
  *  \brief represents the Measurements table
  */
@@ -62,13 +57,13 @@
    *  The entry will be kept until at least now()+lifetime.
    */
   void
-  extendLifetime(measurements::Entry& entry, time::Duration lifetime);
+  extendLifetime(measurements::Entry& entry, const time::Duration& lifetime);
 
 private:
   void
   extendLifetimeInternal(
     std::map<Name, shared_ptr<measurements::Entry> >::iterator it,
-    time::Duration lifetime);
+    const time::Duration& lifetime);
 
   void
   cleanup(std::map<Name, shared_ptr<measurements::Entry> >::iterator it);