Fixing bugs with forwarding hints and registration/deregistration

We forgot to handle the case when forwarding hint is /, which should not
be ever de-registered.

Change-Id: I8aa45256151a4d5b1e0dc0ca3136aa6fb2e6e4a2
diff --git a/src/dispatcher.cc b/src/dispatcher.cc
index 6beb7c3..f4cf57d 100644
--- a/src/dispatcher.cc
+++ b/src/dispatcher.cc
@@ -125,13 +125,46 @@
 }
 
 void
-Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
+Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &forwardingHint)
 {
-  Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
-  _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
+  Name effectiveForwardingHint;
+  if (m_localUserName.size () >= forwardingHint.size () &&
+      m_localUserName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
+    {
+      effectiveForwardingHint = Name ("/"); // "directly" accesible
+    }
+  else
+    {
+      effectiveForwardingHint = forwardingHint;
+    }
 
-  m_server->registerPrefix(prefix);
-  m_syncLog->UpdateLocalLocator (prefix);
+  Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
+
+  if (oldLocalPrefix == effectiveForwardingHint)
+    {
+      _LOG_DEBUG ("Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint << ", but effective prefix didn't change");
+      return;
+    }
+
+  if (effectiveForwardingHint == Name ("/") ||
+      effectiveForwardingHint == Name (BROADCAST_DOMAIN))
+    {
+      _LOG_DEBUG ("Basic effective prefix [" << effectiveForwardingHint << "]. Updating local prefix, but don't reregister");
+      m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
+      return;
+    }
+
+  _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
+
+  m_server->registerPrefix(effectiveForwardingHint);
+  m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
+
+  if (oldLocalPrefix == Name ("/") ||
+      oldLocalPrefix == Name (BROADCAST_DOMAIN))
+    {
+      _LOG_DEBUG ("Don't deregister basic prefix: " << oldLocalPrefix);
+      return;
+    }
   m_server->deregisterPrefix(oldLocalPrefix);
 }
 
diff --git a/src/sync-log.cc b/src/sync-log.cc
index 5bd5703..47b1adf 100644
--- a/src/sync-log.cc
+++ b/src/sync-log.cc
@@ -369,15 +369,7 @@
 void
 SyncLog::UpdateLocalLocator (const Ccnx::Name &forwardingHint)
 {
-  if (m_localName.size () >= forwardingHint.size () &&
-      m_localName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
-    {
-      return UpdateLocator (m_localName, Name ("/")); // "directly" accesible
-    }
-  else
-    {
-      return UpdateLocator (m_localName, forwardingHint);
-    }
+  return UpdateLocator (m_localName, forwardingHint);
 }
 
 SyncStateMsgPtr