table: Mock implementation of FIB
refs #1127
Change-Id: Ie0bc1fc2ddcc61dd1f1cf10fb1935edde4aff6c5
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