src: Adding retry in case of prefix registration fails

Change-Id: Ia124ab393c665b29f03216903128de98bd68de9d
diff --git a/src/route/fib.cpp b/src/route/fib.cpp
index ade99d5..b99d7a2 100644
--- a/src/route/fib.cpp
+++ b/src/route/fib.cpp
@@ -74,7 +74,6 @@
                                                   m_table.end(),
                                                   bind(&fibEntryNameCompare, _1, name));
   if (it != m_table.end()) {
-    cancelScheduledExpiringEvent((*it).getExpiringEventId());
     _LOG_DEBUG("Refreshing the FIB entry. Name: " <<  name);
     for (std::list<NextHop>::iterator nhit =
            (*it).getNexthopList().getNextHops().begin();
@@ -83,7 +82,7 @@
       if (isPrefixUpdatable(it->getName())) {
         registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
                        std::ceil(nhit->getRouteCost()),
-                       ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
+                       ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
       }
     }
     // increase sequence number and schedule refresh again
@@ -139,7 +138,7 @@
         if (isPrefixUpdatable(name)) {
           registerPrefix(name, nhit->getConnectingFaceUri(),
                          std::ceil(nhit->getRouteCost()),
-                         ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
+                         ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
         }
       }
       newEntry.getNexthopList().sort();
@@ -163,7 +162,7 @@
         if (isPrefixUpdatable(name)) {
           registerPrefix(name, nhit->getConnectingFaceUri(),
                          std::ceil(nhit->getRouteCost()),
-                         ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
+                         ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
         }
         removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
         it->getNexthopList().reset();
@@ -176,7 +175,7 @@
           if (isPrefixUpdatable(name)) {
             registerPrefix(name, nhit->getConnectingFaceUri(),
                            std::ceil(nhit->getRouteCost()),
-                           ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
+                           ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
           }
         }
       }
@@ -292,12 +291,19 @@
 
 void
 Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
-                    uint64_t faceCost, const ndn::time::milliseconds& timeout)
+                    uint64_t faceCost, const ndn::time::milliseconds& timeout,
+                    uint8_t times)
 {
-  createFace(faceUri,
-             ndn::bind(&Fib::registerPrefixInNfd, this,_1, namePrefix, faceCost, timeout,
-                       faceUri),
-             ndn::bind(&Fib::onFailure, this, _1, _2,"Failed in name registration"));
+  uint64_t faceId = m_nlsr.getAdjacencyList().getFaceId(faceUri);
+  if (faceId != 0) {
+    _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
+               << " Face Id: " << faceId);
+    registerPrefixInNfd(namePrefix, faceId,
+                        faceCost, timeout, faceUri, times);
+  }
+  else {
+    _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
+  }
 }
 
 void
@@ -305,26 +311,30 @@
                     const std::string& faceUri,
                     uint64_t faceCost,
                     const ndn::time::milliseconds& timeout,
+                    uint8_t times,
                     const CommandSucceedCallback& onSuccess,
                     const CommandFailCallback& onFailure)
 
 {
  createFace(faceUri,
             ndn::bind(&Fib::registerPrefixInNfd, this,_1,
-                      namePrefix, faceCost, timeout, onSuccess, onFailure),
+                      namePrefix, faceCost,
+                      timeout, times, onSuccess, onFailure),
             onFailure);
 }
 
 void
-Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, 
-                         const ndn::Name& namePrefix, uint64_t faceCost,
+Fib::registerPrefixInNfd(const ndn::Name& namePrefix,
+                         uint64_t faceId,
+                         uint64_t faceCost,
                          const ndn::time::milliseconds& timeout,
-                         const std::string& faceUri)
+                         const std::string& faceUri,
+                         uint8_t times)
 {
   ndn::nfd::ControlParameters controlParameters;
   controlParameters
     .setName(namePrefix)
-    .setFaceId(faceCreateResult.getFaceId())
+    .setFaceId(faceId)
     .setCost(faceCost)
     .setExpirationPeriod(timeout)
     .setOrigin(128);
@@ -332,14 +342,18 @@
                                                    ndn::bind(&Fib::onRegistration, this, _1,
                                                              "Successful in name registration",
                                                              faceUri),
-                                                   ndn::bind(&Fib::onFailure, this, _1, _2,
-                                                             "Failed in name registration"));
+                                                   ndn::bind(&Fib::onRegistrationFailure,
+                                                             this, _1, _2,
+                                                             "Failed in name registration",
+                                                             namePrefix, faceUri, faceCost,
+                                                             timeout, times));
 }
 
 void
 Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
                          const ndn::Name& namePrefix, uint64_t faceCost,
                          const ndn::time::milliseconds& timeout,
+                         uint8_t times,
                          const CommandSucceedCallback& onSuccess,
                          const CommandFailCallback& onFailure)
 {
@@ -369,7 +383,8 @@
     m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
                                                      ndn::bind(&Fib::onUnregistration, this, _1,
                                                                "Successful in unregistering name"),
-                                                     ndn::bind(&Fib::onFailure, this, _1, _2,
+                                                     ndn::bind(&Fib::onUnregistrationFailure,
+                                                               this, _1, _2,
                                                                "Failed in unregistering name"));
   }
 }
@@ -411,8 +426,26 @@
 }
 
 void
-Fib::onFailure(uint32_t code, const std::string& error,
-               const std::string& message)
+Fib::onRegistrationFailure(uint32_t code, const std::string& error,
+                           const std::string& message,
+                           const ndn::Name& namePrefix, const std::string& faceUri,
+                           uint64_t faceCost, const ndn::time::milliseconds& timeout,
+                           uint8_t times)
+{
+  _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
+  _LOG_DEBUG("Prefix: " << namePrefix << " failed for: " << times);
+  if (times < 3) {
+    _LOG_DEBUG("Trying to register again...");
+    registerPrefix(namePrefix, faceUri, faceCost, timeout, times+1);
+  }
+  else {
+    _LOG_DEBUG("Registration trial given up");
+  }
+}
+
+void
+Fib::onUnregistrationFailure(uint32_t code, const std::string& error,
+                            const std::string& message)
 {
   _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
 }
diff --git a/src/route/fib.hpp b/src/route/fib.hpp
index c593a5b..4c54a79 100644
--- a/src/route/fib.hpp
+++ b/src/route/fib.hpp
@@ -94,13 +94,14 @@
   void
   registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
                  uint64_t faceCost,
-                 const ndn::time::milliseconds& timeout);
+                 const ndn::time::milliseconds& timeout, uint8_t times);
 
   void
   registerPrefix(const ndn::Name& namePrefix,
                  const std::string& faceUri,
                  uint64_t faceCost,
                  const ndn::time::milliseconds& timeout,
+                 uint8_t times,
                  const CommandSucceedCallback& onSuccess,
                  const CommandFailCallback& onFailure);
   
@@ -122,15 +123,18 @@
              const CommandFailCallback& onFailure);
 
   void
-  registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
-                      const ndn::Name& namePrefix, uint64_t faceCost,
+  registerPrefixInNfd(const ndn::Name& namePrefix,
+                      uint64_t faceId,
+                      uint64_t faceCost,
                       const ndn::time::milliseconds& timeout,
-                      const std::string& faceUri);
+                      const std::string& faceUri,
+                      uint8_t times);
 
   void
   registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
                       const ndn::Name& namePrefix, uint64_t faceCost,
                       const ndn::time::milliseconds& timeout,
+                      uint8_t times,
                       const CommandSucceedCallback& onSuccess,
                       const CommandFailCallback& onFailure);
 
@@ -151,7 +155,15 @@
                    const std::string& message);
 
   void
-  onFailure(uint32_t code, const std::string& error, const std::string& message);
+  onRegistrationFailure(uint32_t code, const std::string& error,
+                        const std::string& message,
+                        const ndn::Name& namePrefix, const std::string& faceUri,
+                        uint64_t faceCost, const ndn::time::milliseconds& timeout,
+                        uint8_t times);
+
+  void
+  onUnregistrationFailure(uint32_t code, const std::string& error,
+                        const std::string& message);
 
   void
   onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
diff --git a/src/route/nexthop.hpp b/src/route/nexthop.hpp
index ecd84fa..a354752 100644
--- a/src/route/nexthop.hpp
+++ b/src/route/nexthop.hpp
@@ -24,6 +24,7 @@
 #define NLSR_NEXTHOP_HPP
 
 #include <iostream>
+#include <cmath>
 #include <boost/cstdint.hpp>
 
 namespace nlsr {
@@ -54,10 +55,11 @@
     m_connectingFaceUri = cfu;
   }
 
-  double
+  uint64_t
   getRouteCost() const
   {
-    return m_routeCost;
+    uint64_t routeCost = static_cast<uint64_t>(ceil(m_routeCost));
+    return routeCost;
   }
 
   void