rib-manager: deleting expired entries from the rib and fib.

refs #1326
Change-Id: I865bff19da766560f920b1818bdf3eaa91e7dbb1
diff --git a/rib/rib.cpp b/rib/rib.cpp
index c3fc399..9901b77 100644
--- a/rib/rib.cpp
+++ b/rib/rib.cpp
@@ -74,7 +74,7 @@
   return m_rib.find(prefix);
 }
 
-shared_ptr<FaceEntry>
+FaceEntry*
 Rib::find(const Name& prefix, const FaceEntry& face) const
 {
   RibTable::const_iterator ribIt = m_rib.find(prefix);
@@ -84,16 +84,15 @@
     {
       shared_ptr<RibEntry> entry(ribIt->second);
 
-      RibEntry::const_iterator faceIt = std::find_if(entry->begin(), entry->end(),
+      RibEntry::iterator faceIt = std::find_if(entry->begin(), entry->end(),
                                                      bind(&compareFaceIdAndOrigin, _1, face));
 
       if (faceIt != entry->end())
         {
-          return make_shared<FaceEntry>(*faceIt);
+          return &((*faceIt));
         }
     }
-
-  return shared_ptr<FaceEntry>();
+  return 0;
 }
 
 void
@@ -126,6 +125,13 @@
         }
       else // Entry exists, update fields
         {
+          // First cancel old scheduled event, if any, then set the EventId to new one
+          if (static_cast<bool>(faceIt->getExpirationEvent()))
+              scheduler::cancel(faceIt->getExpirationEvent());
+
+          // No checks are required here as the iterator needs to be updated in all cases.
+          faceIt->setExpirationEvent(face.getExpirationEvent());
+
           // Save flags for update processing
           uint64_t previousFlags = faceIt->flags;
 
@@ -171,7 +177,6 @@
                 {
                   parent->removeChild((*child));
                 }
-
               entry->addChild((*child));
             }
         }
@@ -183,7 +188,6 @@
     }
 }
 
-
 void
 Rib::erase(const Name& prefix, const FaceEntry& face)
 {
@@ -199,12 +203,13 @@
       // Need to copy face to do FIB updates with correct cost and flags since nfdc does not
       // pass flags or cost
       RibEntry::iterator faceIt = entry->findFace(face);
-      FaceEntry faceToErase = *faceIt;
-      faceToErase.flags = faceIt->flags;
-      faceToErase.cost = faceIt->cost;
 
       if (faceIt != entry->end())
         {
+          FaceEntry faceToErase = *faceIt;
+          faceToErase.flags = faceIt->flags;
+          faceToErase.cost = faceIt->cost;
+
           entry->eraseFace(faceIt);
           m_nItems--;