rib: remote prefix registration

resolve 3 bugs:

There are redundant registrations/unregistrations if loading the
config file multiple times.

Remote registration/unregistration will fail if localhop_security
is enabled.

Unstable RemoteRegistrator/UnregisterAdvanced test case.

Change-Id: I4437292e9f6c0e340c761ef7556a9bdc703ac06c
refs: #2294
diff --git a/rib/remote-registrator.cpp b/rib/remote-registrator.cpp
index 2a5a12d..a587653 100644
--- a/rib/remote-registrator.cpp
+++ b/rib/remote-registrator.cpp
@@ -35,10 +35,9 @@
 using ndn::nfd::ControlParameters;
 using ndn::nfd::CommandOptions;
 
-
-const Name RemoteRegistrator::RM_LOCAL_PREFIX = "/localhost";
-const Name RemoteRegistrator::RM_HUB_PREFIX = "/localhop/nfd";
-const name::Component RemoteRegistrator::RM_IGNORE_COMMPONENT("rib");
+const Name RemoteRegistrator::LOCAL_REGISTRATION_PREFIX = "/localhost";
+const Name RemoteRegistrator::REMOTE_HUB_PREFIX = "/localhop/nfd/rib";
+const name::Component RemoteRegistrator::IGNORE_COMMPONENT("rib");
 
 RemoteRegistrator::RemoteRegistrator(ndn::nfd::Controller& controller,
                                      ndn::KeyChain& keyChain,
@@ -100,8 +99,14 @@
      .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client.
      .setFaceId(0);// the remote hub will take the input face as the faceId.
 
+   Name commandPrefix = REMOTE_HUB_PREFIX;
+   if (IGNORE_COMMPONENT == commandPrefix.at(-1))
+     {
+       commandPrefix = commandPrefix.getPrefix(-1);
+     }
+
    m_commandOptions
-     .setPrefix(RM_HUB_PREFIX)
+     .setPrefix(commandPrefix)
      .setTimeout(time::milliseconds(timeout));
 
    m_nRetries = retry;
@@ -117,15 +122,38 @@
 }
 
 void
+RemoteRegistrator::enable()
+{
+  // do remote registration after an entry is inserted into the RIB.
+  m_afterInsertConnection =
+    m_rib.afterInsertEntry.connect([this] (const Name& prefix) {
+        registerPrefix(prefix);
+      });
+
+  // do remote unregistration after an entry is erased from the RIB.
+  m_afterEraseConnection =
+    m_rib.afterEraseEntry.connect([this] (const Name& prefix) {
+        unregisterPrefix(prefix);
+      });
+}
+
+void
+RemoteRegistrator::disable()
+{
+  m_afterInsertConnection.disconnect();
+  m_afterEraseConnection.disconnect();
+}
+
+void
 RemoteRegistrator::registerPrefix(const Name& prefix)
 {
-  if (RM_LOCAL_PREFIX.isPrefixOf(prefix))
+  if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix))
     {
       NFD_LOG_INFO("local registration only for " << prefix);
       return;
     }
 
-  bool isHubPrefix = prefix == RM_HUB_PREFIX;
+  bool isHubPrefix = prefix == REMOTE_HUB_PREFIX;
 
   if (isHubPrefix)
     {
@@ -181,7 +209,7 @@
 void
 RemoteRegistrator::unregisterPrefix(const Name& prefix)
 {
-  if (prefix == RM_HUB_PREFIX)
+  if (prefix == REMOTE_HUB_PREFIX)
     {
       NFD_LOG_INFO("disconnected to hub with prefix: " << prefix);
 
@@ -267,7 +295,7 @@
   // longest prefix matching to all indenties.
   for (auto&& i : identities)
     {
-      if (!i.empty() && RM_IGNORE_COMMPONENT == i.at(-1))
+      if (!i.empty() && IGNORE_COMMPONENT == i.at(-1))
         {
           isPrefix = i.getPrefix(-1).isPrefixOf(prefix);
           curLength = i.size() - 1;
@@ -360,9 +388,9 @@
                                 const CommandOptions& options,
                                 int nRetries)
 {
-  NFD_LOG_INFO("fail to unregister " << parameters.getName()
-                                     << "\n\t reason:" << reason
-                                     << "\n\t remain retries:" << nRetries);
+  NFD_LOG_INFO("fail to register " << parameters.getName()
+                                   << "\n\t reason:" << reason
+                                   << "\n\t remain retries:" << nRetries);
 
   if (nRetries > 0)
     {
diff --git a/rib/remote-registrator.hpp b/rib/remote-registrator.hpp
index 41be218..0d9eea3 100644
--- a/rib/remote-registrator.hpp
+++ b/rib/remote-registrator.hpp
@@ -36,6 +36,7 @@
 #include <ndn-cxx/management/nfd-control-command.hpp>
 #include <ndn-cxx/management/nfd-control-parameters.hpp>
 #include <ndn-cxx/management/nfd-command-options.hpp>
+#include <ndn-cxx/util/signal.hpp>
 
 namespace nfd {
 namespace rib {
@@ -72,6 +73,20 @@
   loadConfig(const ConfigSection& configSection);
 
   /**
+   * @brief enable remote registration/unregistration.
+   *
+   */
+  void
+  enable();
+
+  /**
+   * @brief disable remote registration/unregistration.
+   *
+   */
+  void
+  disable();
+
+  /**
    * @brief register a prefix to remote hub(s).
    *
    * For the input prefix, we find the longest identity
@@ -197,7 +212,6 @@
   void
   clearRefreshEvents();
 
-
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * When a locally registered prefix triggles remote
@@ -217,16 +231,17 @@
   ndn::nfd::Controller& m_nfdController;
   ndn::KeyChain& m_keyChain;
   Rib& m_rib;
-
+  ndn::util::signal::ScopedConnection m_afterInsertConnection;
+  ndn::util::signal::ScopedConnection m_afterEraseConnection;
   ndn::nfd::ControlParameters m_controlParameters;
   ndn::nfd::CommandOptions m_commandOptions;
   time::seconds m_refreshInterval;
   bool m_hasConnectedHub;
   int m_nRetries;
 
-  static const Name RM_LOCAL_PREFIX; // /localhost
-  static const Name RM_HUB_PREFIX; // /localhop/nfd
-  static const name::Component RM_IGNORE_COMMPONENT; // rib
+  static const Name LOCAL_REGISTRATION_PREFIX; // /localhost
+  static const Name REMOTE_HUB_PREFIX; // /localhop/nfd/rib
+  static const name::Component IGNORE_COMMPONENT; // rib
 };
 
 } // namespace rib
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index 491739a..7d0f5b3 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.cpp
@@ -144,6 +144,8 @@
                      bool isDryRun,
                      const std::string& filename)
 {
+  bool isRemoteRegisterEnabled = false;
+
   for (ConfigSection::const_iterator i = configSection.begin();
        i != configSection.end(); ++i)
     {
@@ -157,21 +159,23 @@
       else if (i->first == "remote_register")
         {
           m_remoteRegistrator.loadConfig(i->second);
+          isRemoteRegisterEnabled = true;
+          // avoid other actions when isDryRun == true
+          if (isDryRun)
+            {
+              continue;
+            }
 
-          // register callback to the RIB.
-          // do remote registration after an entry is inserted into the RIB.
-          // do remote unregistration after an entry is erased from the RIB.
-          m_managedRib.afterInsertEntry += [this] (const Name& prefix) {
-            m_remoteRegistrator.registerPrefix(prefix);
-          };
-
-          m_managedRib.afterEraseEntry += [this] (const Name& prefix) {
-            m_remoteRegistrator.unregisterPrefix(prefix);
-          };
+          m_remoteRegistrator.enable();
         }
       else
         throw Error("Unrecognized rib property: " + i->first);
     }
+
+  if (!isRemoteRegisterEnabled)
+    {
+      m_remoteRegistrator.disable();
+    }
 }
 
 void
@@ -200,16 +204,7 @@
 RibManager::onLocalhostRequest(const Interest& request)
 {
   const Name& command = request.getName();
-
-  if (command.size() <= COMMAND_PREFIX.size())
-    {
-      // command is too short to have a verb
-      NFD_LOG_DEBUG("command result: malformed");
-      sendResponse(command, 400, "Malformed command");
-      return;
-    }
-
-  const Name::Component& verb = command.at(COMMAND_PREFIX.size());
+  const Name::Component& verb = command.get(COMMAND_PREFIX.size());
 
   UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
 
diff --git a/rib/rib.hpp b/rib/rib.hpp
index ed9ca4e..2568b1a 100644
--- a/rib/rib.hpp
+++ b/rib/rib.hpp
@@ -30,6 +30,7 @@
 #include "fib-update.hpp"
 #include "common.hpp"
 #include <ndn-cxx/management/nfd-control-command.hpp>
+#include <ndn-cxx/util/signal.hpp>
 
 namespace nfd {
 namespace rib {
@@ -137,8 +138,8 @@
   removeInheritedFacesFromEntry(RibEntry& entry, const Rib::FaceSet& facesToRemove);
 
 public:
-  ndn::util::EventEmitter<Name> afterInsertEntry;
-  ndn::util::EventEmitter<Name> afterEraseEntry;
+  ndn::util::signal::Signal<Rib, Name> afterInsertEntry;
+  ndn::util::signal::Signal<Rib, Name> afterEraseEntry;
 
 private:
   RibTable m_rib;