ccnx: Restoring functionality to reconnecting to ccnd after failure (e.g., ccnd restart)

Change-Id: If978c4384cde6093f75f4a2a8587940f954eee80
diff --git a/ccnx/ccnx-wrapper.cpp b/ccnx/ccnx-wrapper.cpp
index c498515..54f93af 100644
--- a/ccnx/ccnx-wrapper.cpp
+++ b/ccnx/ccnx-wrapper.cpp
@@ -117,27 +117,30 @@
 void
 CcnxWrapper::connectCcnd()
 {
-  //if (m_handle != 0) {
-    //ccn_disconnect (m_handle);
+  if (m_handle != 0) {
+    ccn_disconnect (m_handle);
     //ccn_destroy (&m_handle);
-  //}
+  }
+  else
+    {
+      m_handle = ccn_create ();
+    }
 
-  m_handle = ccn_create ();
-  //UniqueRecLock lock(m_mutex);
+  UniqueRecLock lock(m_mutex);
   if (ccn_connect(m_handle, NULL) < 0)
   {
     BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
   }
   m_connected = true;
 
-  //if (!m_registeredInterests.empty())
-  //{
-   // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
-    //{
-      // clearInterestFilter(it->first);
-     // setInterestFilter(it->first, it->second);
-    //}
-  //}
+  if (!m_registeredInterests.empty())
+  {
+   for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
+    {
+      clearInterestFilter(it->first, false);
+      setInterestFilter(it->first, it->second, false);
+    }
+  }
 }
 
 CcnxWrapper::~CcnxWrapper()
@@ -194,10 +197,9 @@
 
           if (res < 0) {
             _LOG_ERROR ("ccn_run returned negative status: " << res);
-            usleep (100000);
-            continue;
-            // BOOST_THROW_EXCEPTION (CcnxOperationException()
-            //                     << errmsg_info_str("ccn_run returned error"));
+
+            BOOST_THROW_EXCEPTION (CcnxOperationException()
+                                   << errmsg_info_str("ccn_run returned error"));
           }
 
 
@@ -219,10 +221,6 @@
         }
         catch (CcnxOperationException &e)
         {
-          // do not try reconnect for now
-          cout << *get_error_info<errmsg_info_str> (e) << endl;
-          throw e;
-          /*
           m_connected = false;
           // probably ccnd has been stopped
           // try reconnect with sleep
@@ -232,15 +230,15 @@
           {
             try
             {
-              this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
+              this_thread::sleep (boost::get_system_time () +  boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ()));
 
-              connectCcnd();
+              connectCcnd ();
               _LOG_DEBUG("reconnect to ccnd succeeded");
               break;
             }
             catch (CcnxOperationException &e)
             {
-              this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
+              this_thread::sleep (boost::get_system_time () +  boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ()));
 
               // do exponential backup for reconnect interval
               if (interval < maxInterval)
@@ -249,7 +247,6 @@
               }
             }
           }
-          */
         }
         catch (const std::exception &exc)
           {
@@ -531,7 +528,7 @@
   return 0;
 }
 
-int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
+int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record/* = true*/)
 {
   _LOG_TRACE (">> setInterestFilter");
   UniqueRecLock lock(m_mutex);
@@ -555,7 +552,10 @@
     _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
   }
 
-  m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
+  if (record)
+    {
+      m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
+    }
 
   _LOG_TRACE ("<< setInterestFilter");
 
@@ -563,7 +563,7 @@
 }
 
 void
-CcnxWrapper::clearInterestFilter (const Name &prefix)
+CcnxWrapper::clearInterestFilter (const Name &prefix, bool record/* = true*/)
 {
   _LOG_TRACE (">> clearInterestFilter");
   UniqueRecLock lock(m_mutex);
@@ -578,7 +578,10 @@
   {
   }
 
-  m_registeredInterests.erase(prefix);
+  if (record)
+    {
+      m_registeredInterests.erase(prefix);
+    }
 
   _LOG_TRACE ("<< clearInterestFilter");
 }
diff --git a/ccnx/ccnx-wrapper.h b/ccnx/ccnx-wrapper.h
index 8ad6e34..42b3d91 100644
--- a/ccnx/ccnx-wrapper.h
+++ b/ccnx/ccnx-wrapper.h
@@ -57,10 +57,10 @@
   shutdown (); // called in destructor, but can called manually
 
   virtual int
-  setInterestFilter (const Name &prefix, const InterestCallback &interestCallback);
+  setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record = true);
 
   virtual void
-  clearInterestFilter (const Name &prefix);
+  clearInterestFilter (const Name &prefix, bool record = true);
 
   virtual int
   sendInterest (const Name &interest, const Closure &closure, const Selectors &selector = Selectors());