fw: FaceTable

refs #1317

Change-Id: Ief04fe572407120ca6777c087a519c1749f0cdd2
diff --git a/daemon/core/map-value-iterator.hpp b/daemon/core/map-value-iterator.hpp
new file mode 100644
index 0000000..c8d3df7
--- /dev/null
+++ b/daemon/core/map-value-iterator.hpp
@@ -0,0 +1,41 @@
+/* -*- 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_CORE_MAP_VALUE_ITERATOR_H
+#define NFD_CORE_MAP_VALUE_ITERATOR_H
+
+#include "common.hpp"
+#include <boost/iterator/transform_iterator.hpp>
+
+namespace nfd {
+
+/** \class MapValueIterator
+ *  \brief ForwardIterator to iterator over map values
+ */
+template<typename Map>
+class MapValueIterator
+  : public boost::transform_iterator<function<const typename Map::mapped_type&(const typename Map::value_type&)>,
+                                     typename Map::const_iterator>
+{
+public:
+  explicit
+  MapValueIterator(typename Map::const_iterator it)
+    : boost::transform_iterator<function<const typename Map::mapped_type&(const typename Map::value_type&)>,
+        typename Map::const_iterator>(it, &takeSecond)
+  {
+  }
+
+private:
+  static const typename Map::mapped_type&
+  takeSecond(const typename Map::value_type& pair)
+  {
+    return pair.second;
+  }
+};
+
+} // namespace nfd
+
+#endif // NFD_CORE_MAP_VALUE_ITERATOR_H
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index 325ad67..4c48860 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -122,7 +122,7 @@
   FaceCounters m_counters;
 
   // allow setting FaceId
-  friend class Forwarder;
+  friend class FaceTable;
 };
 
 
diff --git a/daemon/fw/face-table.cpp b/daemon/fw/face-table.cpp
new file mode 100644
index 0000000..6e06438
--- /dev/null
+++ b/daemon/fw/face-table.cpp
@@ -0,0 +1,50 @@
+/* -*- 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 "face-table.hpp"
+#include "forwarder.hpp"
+
+namespace nfd {
+
+NFD_LOG_INIT("FaceTable");
+
+FaceTable::FaceTable(Forwarder& forwarder)
+  : m_forwarder(forwarder)
+  , m_lastFaceId(0)
+{
+}
+
+void
+FaceTable::add(shared_ptr<Face> face)
+{
+  FaceId faceId = ++m_lastFaceId;
+  face->setId(faceId);
+  m_faces[faceId] = face;
+  NFD_LOG_INFO("addFace id=" << faceId);
+
+  face->onReceiveInterest += bind(&Forwarder::onInterest,
+                             &m_forwarder, boost::ref(*face), _1);
+  face->onReceiveData     += bind(&Forwarder::onData,
+                             &m_forwarder, boost::ref(*face), _1);
+}
+
+void
+FaceTable::remove(shared_ptr<Face> face)
+{
+  FaceId faceId = face->getId();
+  m_faces.erase(faceId);
+  face->setId(INVALID_FACEID);
+  NFD_LOG_INFO("removeFace id=" << faceId);
+
+  // XXX This clears all subscriptions, because EventEmitter
+  //     does not support only removing Forwarder's subscription
+  face->onReceiveInterest.clear();
+  face->onReceiveData    .clear();
+
+  m_forwarder.getFib().removeNextHopFromAllEntries(face);
+}
+
+} // namespace nfd
diff --git a/daemon/fw/face-table.hpp b/daemon/fw/face-table.hpp
new file mode 100644
index 0000000..28a4faa
--- /dev/null
+++ b/daemon/fw/face-table.hpp
@@ -0,0 +1,84 @@
+/* -*- 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_FW_FACE_TABLE_HPP
+#define NFD_FW_FACE_TABLE_HPP
+
+#include "face/face.hpp"
+#include "core/map-value-iterator.hpp"
+
+namespace nfd
+{
+
+class Forwarder;
+
+/** \brief container of all Faces
+ */
+class FaceTable
+{
+public:
+  explicit
+  FaceTable(Forwarder& forwarder);
+
+  void
+  add(shared_ptr<Face> face);
+
+  void
+  remove(shared_ptr<Face> face);
+
+  shared_ptr<Face>
+  get(FaceId id) const;
+
+  size_t
+  size() const;
+
+public: // enumeration
+  typedef std::map<FaceId, shared_ptr<Face> > FaceMap;
+
+  /** \brief ForwarderIterator for shared_ptr<Face>
+   */
+  typedef MapValueIterator<FaceMap> const_iterator;
+
+  const_iterator
+  begin() const;
+
+  const_iterator
+  end() const;
+
+private:
+  Forwarder& m_forwarder;
+  FaceId m_lastFaceId;
+  FaceMap m_faces;
+};
+
+inline shared_ptr<Face>
+FaceTable::get(FaceId id) const
+{
+  std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id);
+  return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
+}
+
+inline size_t
+FaceTable::size() const
+{
+  return m_faces.size();
+}
+
+inline FaceTable::const_iterator
+FaceTable::begin() const
+{
+  return const_iterator(m_faces.begin());
+}
+
+inline FaceTable::const_iterator
+FaceTable::end() const
+{
+  return const_iterator(m_faces.end());
+}
+
+} // namespace nfd
+
+#endif // NFD_FW_FACE_TABLE_HPP
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index a812198..b32041e 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -15,7 +15,7 @@
 const Name Forwarder::s_localhostName("ndn:/localhost");
 
 Forwarder::Forwarder()
-  : m_lastFaceId(0)
+  : m_faceTable(*this)
   , m_nameTree(1024) // "1024" could be made as one configurable parameter of the forwarder.
   , m_fib(m_nameTree)
   , m_pit(m_nameTree)
@@ -25,55 +25,6 @@
 }
 
 void
-Forwarder::addFace(shared_ptr<Face> face)
-{
-  FaceId faceId = ++m_lastFaceId;
-  face->setId(faceId);
-  m_faces[faceId] = face;
-  NFD_LOG_INFO("addFace id=" << faceId);
-
-  face->onReceiveInterest += bind(&Forwarder::onInterest,
-                             this, boost::ref(*face), _1);
-  face->onReceiveData     += bind(&Forwarder::onData,
-                             this, boost::ref(*face), _1);
-}
-
-void
-Forwarder::removeFace(shared_ptr<Face> face)
-{
-  FaceId faceId = face->getId();
-  m_faces.erase(faceId);
-  face->setId(INVALID_FACEID);
-  NFD_LOG_INFO("removeFace id=" << faceId);
-
-  // XXX This clears all subscriptions, because EventEmitter
-  //     does not support only removing Forwarder's subscription
-  face->onReceiveInterest.clear();
-  face->onReceiveData    .clear();
-
-  m_fib.removeNextHopFromAllEntries(face);
-}
-
-shared_ptr<Face>
-Forwarder::getFace(FaceId id)
-{
-  std::map<FaceId, shared_ptr<Face> >::iterator i = m_faces.find(id);
-  return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
-}
-
-void
-Forwarder::onInterest(Face& face, const Interest& interest)
-{
-  this->onIncomingInterest(face, interest);
-}
-
-void
-Forwarder::onData(Face& face, const Data& data)
-{
-  this->onIncomingData(face, data);
-}
-
-void
 Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
 {
   // receive Interest
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index 5130c76..726d8a0 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -9,7 +9,7 @@
 
 #include "common.hpp"
 #include "core/scheduler.hpp"
-#include "face/face.hpp"
+#include "face-table.hpp"
 #include "table/fib.hpp"
 #include "table/pit.hpp"
 #include "table/cs.hpp"
@@ -28,19 +28,38 @@
 public:
   Forwarder();
 
+public: // faces
+  FaceTable&
+  getFaceTable();
+
+  /** \brief get existing Face
+   *
+   *  shortcut to .getFaceTable().get(face)
+   */
+  shared_ptr<Face>
+  getFace(FaceId id) const;
+
+  /** \brief add new Face
+   *
+   *  shortcut to .getFaceTable().add(face)
+   */
   void
   addFace(shared_ptr<Face> face);
 
+  /** \brief remove existing Face
+   *
+   *  shortcut to .getFaceTable().remove(face)
+   */
   void
   removeFace(shared_ptr<Face> face);
 
+public: // forwarding entrypoints and tables
   void
   onInterest(Face& face, const Interest& interest);
 
   void
   onData(Face& face, const Data& data);
 
-public:
   Fib&
   getFib();
 
@@ -53,9 +72,6 @@
   Measurements&
   getMeasurements();
 
-  shared_ptr<Face>
-  getFace(FaceId id);
-
 PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
   /** \brief incoming Interest pipeline
    */
@@ -115,8 +131,7 @@
                      shared_ptr<pit::Entry> pitEntry);
 
 private:
-  FaceId m_lastFaceId;
-  std::map<FaceId, shared_ptr<Face> > m_faces;
+  FaceTable m_faceTable;
 
   NameTree     m_nameTree;
   Fib          m_fib;
@@ -132,6 +147,42 @@
   friend class fw::Strategy;
 };
 
+inline FaceTable&
+Forwarder::getFaceTable()
+{
+  return m_faceTable;
+}
+
+inline shared_ptr<Face>
+Forwarder::getFace(FaceId id) const
+{
+  return m_faceTable.get(id);
+}
+
+inline void
+Forwarder::addFace(shared_ptr<Face> face)
+{
+  m_faceTable.add(face);
+}
+
+inline void
+Forwarder::removeFace(shared_ptr<Face> face)
+{
+  m_faceTable.remove(face);
+}
+
+inline void
+Forwarder::onInterest(Face& face, const Interest& interest)
+{
+  this->onIncomingInterest(face, interest);
+}
+
+inline void
+Forwarder::onData(Face& face, const Data& data)
+{
+  this->onIncomingData(face, data);
+}
+
 inline Fib&
 Forwarder::getFib()
 {
@@ -156,6 +207,7 @@
   return m_measurements;
 }
 
+
 } // namespace nfd
 
 #endif // NFD_FW_FORWARDER_HPP
diff --git a/tests/core/map-value-iterator.cpp b/tests/core/map-value-iterator.cpp
new file mode 100644
index 0000000..c874b8f
--- /dev/null
+++ b/tests/core/map-value-iterator.cpp
@@ -0,0 +1,39 @@
+/* -*- 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 "core/map-value-iterator.hpp"
+#include <boost/concept_check.hpp>
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(CoreMapValueIterator, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+  typedef std::map<char, int> CharIntMap;
+  typedef MapValueIterator<CharIntMap> CharIntMapValueIterator;
+  BOOST_CONCEPT_ASSERT((boost::ForwardIterator<CharIntMapValueIterator>));
+
+  CharIntMap map;
+  map['a'] = 1918;
+  map['b'] = 2675;
+  map['c'] = 7783;
+  map['d'] = 2053;
+
+  CharIntMapValueIterator begin(map.begin());
+  CharIntMapValueIterator end  (map.end  ());
+
+  int expected[] = { 1918, 2675, 7783, 2053 };
+  BOOST_CHECK_EQUAL_COLLECTIONS(begin, end, expected, expected + 4);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/fw/face-table.cpp b/tests/fw/face-table.cpp
new file mode 100644
index 0000000..474a756
--- /dev/null
+++ b/tests/fw/face-table.cpp
@@ -0,0 +1,99 @@
+/* -*- 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 "fw/face-table.hpp"
+#include "fw/forwarder.hpp"
+
+#include "tests/test-common.hpp"
+#include "tests/face/dummy-face.hpp"
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(FwFaceTable, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(AddRemove)
+{
+  Forwarder forwarder;
+
+  shared_ptr<Face> face1 = make_shared<DummyFace>();
+  shared_ptr<Face> face2 = make_shared<DummyFace>();
+
+  BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
+  BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
+
+  forwarder.addFace(face1);
+  forwarder.addFace(face2);
+
+  BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
+  BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
+  BOOST_CHECK_NE(face1->getId(), face2->getId());
+
+  forwarder.removeFace(face1);
+  forwarder.removeFace(face2);
+
+  BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
+  BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
+}
+
+BOOST_AUTO_TEST_CASE(Enumerate)
+{
+  Forwarder forwarder;
+  FaceTable& faceTable = forwarder.getFaceTable();
+
+  shared_ptr<Face> face1 = make_shared<DummyFace>();
+  shared_ptr<Face> face2 = make_shared<DummyFace>();
+  bool hasFace1 = false, hasFace2 = false;
+
+  BOOST_CHECK_EQUAL(faceTable.size(), 0);
+  BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
+
+  faceTable.add(face1);
+  BOOST_CHECK_EQUAL(faceTable.size(), 1);
+  BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
+  hasFace1 = hasFace2 = false;
+  for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
+    if (*it == face1) {
+      hasFace1 = true;
+    }
+  }
+  BOOST_CHECK(hasFace1);
+
+  faceTable.add(face2);
+  BOOST_CHECK_EQUAL(faceTable.size(), 2);
+  BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
+  hasFace1 = hasFace2 = false;
+  for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
+    if (*it == face1) {
+      hasFace1 = true;
+    }
+    if (*it == face2) {
+      hasFace2 = true;
+    }
+  }
+  BOOST_CHECK(hasFace1);
+  BOOST_CHECK(hasFace2);
+
+  faceTable.remove(face1);
+  BOOST_CHECK_EQUAL(faceTable.size(), 1);
+  BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size());
+  hasFace1 = hasFace2 = false;
+  for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) {
+    if (*it == face1) {
+      hasFace1 = true;
+    }
+    if (*it == face2) {
+      hasFace2 = true;
+    }
+  }
+  BOOST_CHECK(!hasFace1);
+  BOOST_CHECK(hasFace2);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/fw/forwarder.cpp b/tests/fw/forwarder.cpp
index 0a30852..b9e460c 100644
--- a/tests/fw/forwarder.cpp
+++ b/tests/fw/forwarder.cpp
@@ -14,30 +14,6 @@
 
 BOOST_FIXTURE_TEST_SUITE(FwForwarder, BaseFixture)
 
-BOOST_AUTO_TEST_CASE(AddRemoveFace)
-{
-  Forwarder forwarder;
-
-  shared_ptr<Face> face1 = make_shared<DummyFace>();
-  shared_ptr<Face> face2 = make_shared<DummyFace>();
-
-  BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
-  BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
-
-  forwarder.addFace(face1);
-  forwarder.addFace(face2);
-
-  BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
-  BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
-  BOOST_CHECK_NE(face1->getId(), face2->getId());
-
-  forwarder.removeFace(face1);
-  forwarder.removeFace(face2);
-
-  BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
-  BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
-}
-
 BOOST_AUTO_TEST_CASE(SimpleExchange)
 {
   Forwarder forwarder;