table: Mock implementation of FIB

refs #1127

Change-Id: Ie0bc1fc2ddcc61dd1f1cf10fb1935edde4aff6c5
diff --git a/daemon/common.hpp b/daemon/common.hpp
index 0dd2e7e..d87a067 100644
--- a/daemon/common.hpp
+++ b/daemon/common.hpp
@@ -16,8 +16,10 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/function.hpp>
 #include <boost/bind.hpp>
+#include <boost/ref.hpp>
 
 #include <vector>
+#include <list>
 
 namespace ndn {
 
diff --git a/daemon/table/fib-entry.cpp b/daemon/table/fib-entry.cpp
new file mode 100644
index 0000000..a00a582
--- /dev/null
+++ b/daemon/table/fib-entry.cpp
@@ -0,0 +1,67 @@
+/* -*- 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 "fib-entry.hpp"
+#include <algorithm>
+
+namespace ndn {
+namespace fib {
+
+Entry::Entry(const Name& prefix)
+  : m_prefix(prefix)
+{
+}
+
+static inline bool
+predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face)
+{
+  return nexthop.getFace() == face;
+}
+
+void
+Entry::addNextHop(shared_ptr<Face> face, int32_t cost)
+{
+  NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
+    bind(&predicate_NextHop_eq_Face, _1, face));
+  if (it == m_nextHops.end()) {
+    m_nextHops.push_back(fib::NextHop(face));
+    it = m_nextHops.end() - 1;
+  }
+  // now it refers to the NextHop for face
+  
+  it->setCost(cost);
+  
+  this->sortNextHops();
+
+}
+
+void
+Entry::removeNextHop(shared_ptr<Face> face)
+{
+  NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
+    bind(&predicate_NextHop_eq_Face, _1, face));
+  if (it == m_nextHops.end()) {
+    return;
+  }
+  
+  m_nextHops.erase(it);
+}
+
+static inline bool
+compare_NextHop_cost(const NextHop& a, const NextHop& b)
+{
+  return a.getCost() < b.getCost();
+}
+
+void
+Entry::sortNextHops()
+{
+  std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost);
+}
+
+
+} // namespace fib
+} // namespace ndn
diff --git a/daemon/table/fib-entry.hpp b/daemon/table/fib-entry.hpp
new file mode 100644
index 0000000..67f5056
--- /dev/null
+++ b/daemon/table/fib-entry.hpp
@@ -0,0 +1,76 @@
+/* -*- 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_FIB_ENTRY_HPP
+#define NFD_TABLE_FIB_ENTRY_HPP
+
+#include "fib-nexthop.hpp"
+
+namespace ndn {
+namespace fib {
+
+/** \class NextHopList
+ *  \brief represents a collection of nexthops
+ *  This type has these methods:
+ *    iterator<NextHop> begin()
+ *    iterator<NextHop> end()
+ *    size_t size()
+ */
+typedef std::vector<fib::NextHop> NextHopList;
+
+/** \class Entry
+ *  \brief represents a FIB entry
+ */
+class Entry : noncopyable
+{
+public:
+  Entry(const Name& prefix);
+  
+  const Name&
+  getPrefix() const;
+  
+  /** \brief gives the nexthops explicitly defined on this entry
+   *  This list does not include inherited nexthops.
+   */
+  const NextHopList&
+  getNextHops() const;
+  
+  /// adds a nexthop
+  void
+  addNextHop(shared_ptr<Face> face, int32_t cost);
+  
+  /// removes a nexthop
+  void
+  removeNextHop(shared_ptr<Face> face);
+
+private:
+  /// sorts the nexthop list
+  void
+  sortNextHops();
+
+private:
+  Name m_prefix;
+  NextHopList m_nextHops;
+};
+
+
+inline const Name&
+Entry::getPrefix() const
+{
+  return m_prefix;
+}
+
+inline const NextHopList&
+Entry::getNextHops() const
+{
+  return m_nextHops;
+}
+
+
+} // namespace fib
+} // namespace ndn
+
+#endif // NFD_TABLE_FIB_ENTRY_HPP
diff --git a/daemon/table/fib-nexthop.cpp b/daemon/table/fib-nexthop.cpp
new file mode 100644
index 0000000..06bbdcb
--- /dev/null
+++ b/daemon/table/fib-nexthop.cpp
@@ -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.
+ */
+
+#include "fib-nexthop.hpp"
+
+namespace ndn {
+namespace fib {
+
+NextHop::NextHop(shared_ptr<Face> face)
+  : m_face(face), m_cost(0)
+{
+}
+
+NextHop::NextHop(const NextHop& other)
+  : m_face(other.m_face), m_cost(other.m_cost)
+{
+}
+
+shared_ptr<Face>
+NextHop::getFace() const
+{
+  return m_face;
+}
+
+void
+NextHop::setCost(int32_t cost)
+{
+  m_cost = cost;
+}
+
+int32_t
+NextHop::getCost() const
+{
+  return m_cost;
+}
+
+} // namespace fib
+} // namespace ndn
diff --git a/daemon/table/fib-nexthop.hpp b/daemon/table/fib-nexthop.hpp
new file mode 100644
index 0000000..c3e2cf0
--- /dev/null
+++ b/daemon/table/fib-nexthop.hpp
@@ -0,0 +1,43 @@
+/* -*- 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_FIB_NEXTHOP_HPP
+#define NFD_TABLE_FIB_NEXTHOP_HPP
+
+#include "common.hpp"
+#include "face/face.hpp"
+
+namespace ndn {
+namespace fib {
+
+/** \class NextHop
+ *  \brief represents a nexthop record in FIB entry
+ */
+class NextHop
+{
+public:
+  NextHop(shared_ptr<Face> face);
+  
+  NextHop(const NextHop& other);
+  
+  shared_ptr<Face>
+  getFace() const;
+  
+  void
+  setCost(int32_t cost);
+  
+  int32_t
+  getCost() const;
+
+private:
+  shared_ptr<Face> m_face;
+  int32_t m_cost;
+};
+
+} // namespace fib
+} // namespace ndn
+
+#endif // NFD_TABLE_FIB_NEXTHOP_HPP
diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp
new file mode 100644
index 0000000..1d8fe3a
--- /dev/null
+++ b/daemon/table/fib.cpp
@@ -0,0 +1,78 @@
+/* -*- 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 "fib.hpp"
+#include <algorithm>
+#include <numeric>
+
+namespace ndn {
+
+Fib::Fib()
+{
+  std::pair<shared_ptr<fib::Entry>, bool> pair_rootEntry_dummy =
+    this->insert(Name());
+  m_rootEntry = pair_rootEntry_dummy.first;
+}
+
+Fib::~Fib()
+{
+}
+
+static inline bool
+predicate_FibEntry_eq_Name(const shared_ptr<fib::Entry>& entry,
+  const Name& name)
+{
+  return entry->getPrefix().equals(name);
+}
+
+std::pair<shared_ptr<fib::Entry>, bool>
+Fib::insert(const Name& prefix)
+{
+  std::list<shared_ptr<fib::Entry> >::iterator it = std::find_if(
+    m_table.begin(), m_table.end(),
+    bind(&predicate_FibEntry_eq_Name, _1, prefix));
+  if (it != m_table.end()) return std::make_pair(*it, false);
+  
+  shared_ptr<fib::Entry> entry = make_shared<fib::Entry>(prefix);
+  m_table.push_back(entry);
+  return std::make_pair(entry, true);
+}
+
+static inline const shared_ptr<fib::Entry>&
+accumulate_FibEntry_longestPrefixMatch(
+  const shared_ptr<fib::Entry>& bestMatch,
+  const shared_ptr<fib::Entry>& entry, const Name& name)
+{
+  if (!entry->getPrefix().isPrefixOf(name)) return bestMatch;
+  if (bestMatch->getPrefix().size() < entry->getPrefix().size()) return entry;
+  return bestMatch;
+}
+
+shared_ptr<fib::Entry>
+Fib::findLongestPrefixMatch(const Name& prefix) const
+{
+  shared_ptr<fib::Entry> bestMatch = 
+    std::accumulate(m_table.begin(), m_table.end(), m_rootEntry,
+    bind(&accumulate_FibEntry_longestPrefixMatch, _1, _2, prefix));
+  return bestMatch;
+}
+
+static inline void
+FibEntry_removeNextHop(shared_ptr<fib::Entry> entry,
+  shared_ptr<Face> face)
+{
+  entry->removeNextHop(face);
+}
+
+void
+Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
+{
+  std::for_each(m_table.begin(), m_table.end(),
+    bind(&FibEntry_removeNextHop, _1, face));
+}
+
+
+} // namespace ndn
diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp
new file mode 100644
index 0000000..0c88eaa
--- /dev/null
+++ b/daemon/table/fib.hpp
@@ -0,0 +1,48 @@
+/* -*- 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_FIB_HPP
+#define NFD_TABLE_FIB_HPP
+
+#include "fib-entry.hpp"
+namespace ndn {
+
+/** \class Fib
+ *  \brief represents the FIB
+ */
+class Fib : noncopyable
+{
+public:
+  Fib();
+  
+  ~Fib();
+  
+  /** \brief inserts a FIB entry for prefix
+   *  If an entry for exact same prefix exists, that entry is returned.
+   *  \return{ the entry, and true for new entry, false for existing entry }
+   */
+  std::pair<shared_ptr<fib::Entry>, bool>
+  insert(const Name& prefix);
+  
+  /// performs a longest prefix match
+  shared_ptr<fib::Entry>
+  findLongestPrefixMatch(const Name& prefix) const;
+  
+  /** \brief removes the NextHop record for face in all entrites
+   *  This is usually invoked when face goes away.
+   *  Removing all NextHops in a FIB entry will not remove the FIB entry.
+   */
+  void
+  removeNextHopFromAllEntries(shared_ptr<Face> face);
+
+private:
+  shared_ptr<fib::Entry> m_rootEntry;
+  std::list<shared_ptr<fib::Entry> > m_table;
+};
+
+} // namespace ndn
+
+#endif // NFD_TABLE_FIB_HPP