rib: route addition and removal signals
refs: #3818
Change-Id: Ic47afeba4b4133a2092b26ecd49adbaac0505781
diff --git a/rib/rib-entry.cpp b/rib/rib-entry.cpp
index 7d4cff8..a33e113 100644
--- a/rib/rib-entry.cpp
+++ b/rib/rib-entry.cpp
@@ -46,7 +46,7 @@
return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
}
-bool
+std::pair<RibEntry::iterator, bool>
RibEntry::insertRoute(const Route& route)
{
iterator it = findRoute(route);
@@ -58,10 +58,10 @@
m_routes.push_back(route);
- return true;
+ return std::make_pair(std::prev(m_routes.end()), true);
}
else {
- return false;
+ return std::make_pair(it, false);
}
}
diff --git a/rib/rib-entry.hpp b/rib/rib-entry.hpp
index c3c5b4c..d55d3f1 100644
--- a/rib/rib-entry.hpp
+++ b/rib/rib-entry.hpp
@@ -72,9 +72,12 @@
/** \brief inserts a new route into the entry's route list
* If another route already exists with the same faceId and origin,
* the new route is not inserted.
- * \return{ true if the route is inserted, false otherwise }
+ * \return a pair, whose first element is the iterator to the newly
+ * inserted element if the insert succeeds and to the
+ * previously-existing element otherwise, and whose second element
+ * is true if the insert succeeds and false otherwise.
*/
- bool
+ std::pair<RibEntry::iterator, bool>
insertRoute(const Route& route);
/** \brief erases a Route with the same faceId and origin
diff --git a/rib/rib.cpp b/rib/rib.cpp
index eb577bb..97492b2 100644
--- a/rib/rib.cpp
+++ b/rib/rib.cpp
@@ -89,42 +89,45 @@
if (ribIt != m_rib.end()) {
shared_ptr<RibEntry> entry(ribIt->second);
- RibEntry::iterator routeIt = entry->findRoute(route);
+ RibEntry::iterator entryIt;
+ bool didInsert = false;
+ std::tie(entryIt, didInsert) = entry->insertRoute(route);
- if (routeIt == entry->end()) {
- // New route
- entry->insertRoute(route);
+ if (didInsert) {
+ // The route was new and we successfully inserted it.
m_nItems++;
+ afterAddRoute(RibRouteRef{entry, entryIt});
+
// Register with face lookup table
m_faceMap[route.faceId].push_back(entry);
}
else {
// Route exists, update fields
// First cancel old scheduled event, if any, then set the EventId to new one
- if (static_cast<bool>(routeIt->getExpirationEvent())) {
+ if (static_cast<bool>(entryIt->getExpirationEvent())) {
NFD_LOG_TRACE("Cancelling expiration event for " << entry->getName() << " "
- << *routeIt);
- scheduler::cancel(routeIt->getExpirationEvent());
+ << (*entryIt));
+ scheduler::cancel(entryIt->getExpirationEvent());
}
// No checks are required here as the iterator needs to be updated in all cases.
- routeIt->setExpirationEvent(route.getExpirationEvent());
+ entryIt->setExpirationEvent(route.getExpirationEvent());
- routeIt->flags = route.flags;
- routeIt->cost = route.cost;
- routeIt->expires = route.expires;
+ entryIt->flags = route.flags;
+ entryIt->cost = route.cost;
+ entryIt->expires = route.expires;
}
}
else {
// New name prefix
- shared_ptr<RibEntry> entry(make_shared<RibEntry>(RibEntry()));
+ shared_ptr<RibEntry> entry = make_shared<RibEntry>();
m_rib[prefix] = entry;
m_nItems++;
entry->setName(prefix);
- entry->insertRoute(route);
+ RibEntry::iterator routeIt = entry->insertRoute(route).first;
// Find prefix's parent
shared_ptr<RibEntry> parent = findParent(prefix);
@@ -152,6 +155,7 @@
// do something after inserting an entry
afterInsertEntry(prefix);
+ afterAddRoute(RibRouteRef{entry, routeIt});
}
}
@@ -166,6 +170,8 @@
RibEntry::iterator routeIt = entry->findRoute(route);
if (routeIt != entry->end()) {
+ beforeRemoveRoute(RibRouteRef{entry, routeIt});
+
auto faceId = route.faceId;
entry->eraseRoute(routeIt);
m_nItems--;
diff --git a/rib/rib.hpp b/rib/rib.hpp
index 4c9e608..70554e6 100644
--- a/rib/rib.hpp
+++ b/rib/rib.hpp
@@ -38,7 +38,20 @@
class FibUpdater;
-/** \brief represents the RIB
+ /** \brief references a route
+ */
+struct RibRouteRef
+{
+ shared_ptr<RibEntry> entry;
+ RibEntry::const_iterator route;
+};
+
+/** \brief represents the Routing Information Base
+
+ The Routing Information Base contains a collection of Routes, each
+ represents a piece of static or dynamic routing information
+ registered by applications, operators, or NFD itself. Routes
+ associated with the same namespace are collected into a RIB entry.
*/
class Rib : noncopyable
{
@@ -201,9 +214,29 @@
findRoutesWithFaceId(uint64_t faceId);
public:
+ /** \brief signals after a RIB entry is inserted
+ *
+ * A RIB entry is inserted when the first route associated with a
+ * certain namespace is added.
+ */
ndn::util::signal::Signal<Rib, Name> afterInsertEntry;
+
+ /** \brief signals after a RIB entry is erased
+ *
+ * A RIB entry is erased when the last route associated with a
+ * certain namespace is removed.
+ */
+
ndn::util::signal::Signal<Rib, Name> afterEraseEntry;
+ /** \brief signals after a Route is added
+ */
+ ndn::util::signal::Signal<Rib, RibRouteRef> afterAddRoute;
+
+ /** \brief signals before a route is removed
+ */
+ ndn::util::signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
+
private:
RibTable m_rib;
FaceLookupTable m_faceMap;
diff --git a/tests/rib/rib-entry.t.cpp b/tests/rib/rib-entry.t.cpp
new file mode 100644
index 0000000..03cd8b9
--- /dev/null
+++ b/tests/rib/rib-entry.t.cpp
@@ -0,0 +1,79 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "rib/rib-entry.hpp"
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace rib {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(TestRibEntry, nfd::tests::BaseFixture)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ rib::RibEntry entry;
+ rib::RibEntry::iterator entryIt;
+ bool didInsert = false;
+
+ rib::Route route1;
+ route1.faceId = 1;
+ route1.origin = 0;
+
+ std::tie(entryIt, didInsert) = entry.insertRoute(route1);
+ BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
+ BOOST_CHECK(entryIt == entry.findRoute(route1));
+ BOOST_CHECK(didInsert);
+
+ Route route2;
+ route2.faceId = 1;
+ route2.origin = 128;
+
+ std::tie(entryIt, didInsert) = entry.insertRoute(route2);
+ BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
+ BOOST_CHECK(entryIt == entry.findRoute(route2));
+ BOOST_CHECK(didInsert);
+
+ entry.eraseRoute(route1);
+ BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
+ BOOST_CHECK(entry.findRoute(route1) == entry.end());
+
+ BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
+ BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
+
+ std::tie(entryIt, didInsert) = entry.insertRoute(route2);
+ BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
+ BOOST_CHECK(!didInsert);
+
+ entry.eraseRoute(route1);
+ BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
+ BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
+
+} // namespace tests
+} // namespace rib
+} // namespace nfd
diff --git a/tests/rib/rib.t.cpp b/tests/rib/rib.t.cpp
index eed09f9..9a73ac7 100644
--- a/tests/rib/rib.t.cpp
+++ b/tests/rib/rib.t.cpp
@@ -1,4 +1,4 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
@@ -35,38 +35,6 @@
BOOST_FIXTURE_TEST_SUITE(TestRib, nfd::tests::BaseFixture)
-BOOST_AUTO_TEST_CASE(RibEntry)
-{
- rib::RibEntry entry;
-
- rib::Route route1;
- route1.faceId = 1;
- route1.origin = 0;
-
- entry.insertRoute(route1);
- BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
-
- Route route2;
- route2.faceId = 1;
- route2.origin = 128;
-
- entry.insertRoute(route2);
- BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
-
- entry.eraseRoute(route1);
- BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
-
- BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
- BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
-
- entry.insertRoute(route2);
- BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
-
- entry.eraseRoute(route1);
- BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
- BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
-}
-
BOOST_AUTO_TEST_CASE(Parent)
{
rib::Rib rib;
@@ -295,6 +263,79 @@
BOOST_CHECK_EQUAL(rib.size(), 1);
}
+BOOST_AUTO_TEST_CASE(RibSignals)
+{
+ rib::Rib rib;
+
+ Route route;
+ Name name("/hello/world");
+
+ Route route1;
+ route1.faceId = 1;
+ route1.origin = 20;
+ route1.cost = 10;
+
+ Route route2;
+ route2.faceId = 2;
+ route2.origin = 30;
+ route2.cost = 20;
+
+ RibRouteRef routeInfo;
+
+ int nAfterInsertEntryInvocations = 0;
+ int nAfterAddRouteInvocations = 0;
+ int nBeforeRemoveRouteInvocations = 0;
+ int nAfterEraseEntryInvocations = 0;
+ rib.afterInsertEntry.connect([&] (const Name& inName) {
+ BOOST_CHECK_EQUAL(nAfterInsertEntryInvocations, 0);
+ BOOST_CHECK_EQUAL(nAfterAddRouteInvocations, 0);
+ BOOST_CHECK(rib.find(name) != rib.end());
+ nAfterInsertEntryInvocations++;
+ });
+
+ rib.afterAddRoute.connect([&] (const RibRouteRef& rrr) {
+ BOOST_CHECK_EQUAL(nAfterInsertEntryInvocations, 1);
+ BOOST_CHECK(rib.find(name) != rib.end());
+ BOOST_CHECK(rib.find(name, route) != nullptr);
+ nAfterAddRouteInvocations++;
+ });
+
+ rib.beforeRemoveRoute.connect([&] (const RibRouteRef& rrr) {
+ BOOST_CHECK_EQUAL(nAfterEraseEntryInvocations, 0);
+ BOOST_CHECK(rib.find(name) != rib.end());
+ BOOST_CHECK(rib.find(name, route) != nullptr);
+ nBeforeRemoveRouteInvocations++;
+ });
+
+ rib.afterEraseEntry.connect([&] (const Name& inName) {
+ BOOST_CHECK_EQUAL(nBeforeRemoveRouteInvocations, 2);
+ BOOST_CHECK_EQUAL(nAfterEraseEntryInvocations, 0);
+ BOOST_CHECK(rib.find(name) == rib.end());
+ nAfterEraseEntryInvocations++;
+ });
+
+ route = route1;
+ rib.insert(name, route);
+ BOOST_CHECK_EQUAL(nAfterInsertEntryInvocations, 1);
+ BOOST_CHECK_EQUAL(nAfterAddRouteInvocations, 1);
+
+ route = route2;
+ rib.insert(name, route);
+ BOOST_CHECK_EQUAL(nAfterInsertEntryInvocations, 1);
+ BOOST_CHECK_EQUAL(nAfterAddRouteInvocations, 2);
+
+ route = route1;
+ rib.erase(name, route);
+ BOOST_CHECK_EQUAL(nBeforeRemoveRouteInvocations, 1);
+ BOOST_CHECK_EQUAL(nAfterEraseEntryInvocations, 0);
+
+ route = route2;
+ rib.erase(name, route);
+ BOOST_CHECK_EQUAL(nBeforeRemoveRouteInvocations, 2);
+ BOOST_CHECK_EQUAL(nAfterEraseEntryInvocations, 1);
+
+}
+
BOOST_AUTO_TEST_SUITE_END() // TestRib
} // namespace tests