fw: FaceTable

refs #1317

Change-Id: Ief04fe572407120ca6777c087a519c1749f0cdd2
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