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;