rib: remote prefix registration

Change-Id: I0ee01317c213380481eed5c3a13cc19fb0b897ee
Refs: #2056
diff --git a/rib/remote-registrator.cpp b/rib/remote-registrator.cpp
new file mode 100644
index 0000000..2a5a12d
--- /dev/null
+++ b/rib/remote-registrator.cpp
@@ -0,0 +1,423 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "remote-registrator.hpp"
+#include "core/logger.hpp"
+#include "core/scheduler.hpp"
+
+namespace nfd {
+namespace rib {
+
+NFD_LOG_INIT("RemoteRegistrator");
+
+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");
+
+RemoteRegistrator::RemoteRegistrator(ndn::nfd::Controller& controller,
+                                     ndn::KeyChain& keyChain,
+                                     Rib& rib)
+  : m_nfdController(controller)
+  , m_keyChain(keyChain)
+  , m_rib(rib)
+  , m_refreshInterval(time::seconds(25))
+  , m_hasConnectedHub(false)
+  , m_nRetries(0)
+{
+}
+
+RemoteRegistrator::~RemoteRegistrator()
+{
+  // cancel all periodically refresh events.
+  for (auto&& entry : m_regEntries)
+    {
+      scheduler::cancel(entry.second);
+    }
+}
+
+void
+RemoteRegistrator::loadConfig(const ConfigSection& configSection)
+{
+  size_t cost = 15, timeout = 10000;
+  size_t retry = 0;
+  size_t interval = 0;
+  const size_t intervalDef = 25, intervalMax = 600;
+
+  NFD_LOG_INFO("Load remote_register section in rib section");
+  for (auto&& i : configSection)
+    {
+      if (i.first == "cost")
+        {
+          cost = i.second.get_value<size_t>();
+        }
+      else if (i.first == "timeout")
+        {
+          timeout = i.second.get_value<size_t>();
+        }
+      else if (i.first == "retry")
+        {
+          retry = i.second.get_value<size_t>();
+        }
+      else if (i.first == "refresh_interval")
+        {
+          interval = i.second.get_value<size_t>();
+        }
+      else
+        {
+          throw ConfigFile::Error("Unrecognized option \"" + i.first +
+                                  "\" in \"remote-registrator\" section");
+        }
+    }
+
+   m_controlParameters
+     .setCost(cost)
+     .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client.
+     .setFaceId(0);// the remote hub will take the input face as the faceId.
+
+   m_commandOptions
+     .setPrefix(RM_HUB_PREFIX)
+     .setTimeout(time::milliseconds(timeout));
+
+   m_nRetries = retry;
+
+   if (interval == 0)
+     {
+       interval = intervalDef;
+     }
+
+   interval = std::min(interval, intervalMax);
+
+   m_refreshInterval = time::seconds(interval);
+}
+
+void
+RemoteRegistrator::registerPrefix(const Name& prefix)
+{
+  if (RM_LOCAL_PREFIX.isPrefixOf(prefix))
+    {
+      NFD_LOG_INFO("local registration only for " << prefix);
+      return;
+    }
+
+  bool isHubPrefix = prefix == RM_HUB_PREFIX;
+
+  if (isHubPrefix)
+    {
+      NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix);
+
+      m_hasConnectedHub = true;
+
+      redoRegistration();
+      return;
+    }
+
+  if (!m_hasConnectedHub)
+    {
+      NFD_LOG_INFO("no hub connected when registering " << prefix);
+      return;
+    }
+
+  std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
+
+  if (0 == identity.second)
+    {
+      NFD_LOG_INFO("no proper identity found for registering " << prefix);
+      return;
+    }
+
+  Name prefixForRegistration;
+  if (identity.first.size() == identity.second)
+    {
+      prefixForRegistration = identity.first;
+    }
+  else
+    {
+      prefixForRegistration = identity.first.getPrefix(-1);
+    }
+
+  if (m_regEntries.find(prefixForRegistration) != m_regEntries.end())
+    {
+      NFD_LOG_INFO("registration already in process for " << prefix);
+      return;
+    }
+
+  // make copies of m_controlParameters and m_commandOptions to
+  // avoid unreasonable overwriting during concurrent registration
+  // and unregistration.
+  ControlParameters parameters = m_controlParameters;
+  CommandOptions    options    = m_commandOptions;
+
+  startRegistration(parameters.setName(prefixForRegistration),
+                    options.setSigningIdentity(identity.first),
+                    m_nRetries);
+}
+
+void
+RemoteRegistrator::unregisterPrefix(const Name& prefix)
+{
+  if (prefix == RM_HUB_PREFIX)
+    {
+      NFD_LOG_INFO("disconnected to hub with prefix: " << prefix);
+
+      // for phase 1: suppose there is at most one hub connected.
+      // if the hub prefix has been unregistered locally, there may
+      // be no connected hub.
+      m_hasConnectedHub = false;
+
+      clearRefreshEvents();
+      return;
+    }
+
+  if (!m_hasConnectedHub)
+    {
+      NFD_LOG_INFO("no hub connected when unregistering " << prefix);
+      return;
+    }
+
+  std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
+
+  if (0 == identity.second)
+    {
+      NFD_LOG_INFO("no proper identity found for unregistering " << prefix);
+      return;
+    }
+
+  Name prefixForRegistration;
+  if (identity.first.size() == identity.second)
+    {
+      prefixForRegistration = identity.first;
+    }
+  else
+    {
+      prefixForRegistration = identity.first.getPrefix(-1);
+    }
+
+  RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration);
+  if (m_regEntries.end() == iRegEntry)
+    {
+      NFD_LOG_INFO("no existing entry found when unregistering " << prefix);
+      return;
+    }
+
+  for (auto&& entry : m_rib)
+    {
+      if (prefixForRegistration.isPrefixOf(entry.first) &&
+          findIdentityForRegistration(entry.first) == identity)
+        {
+          NFD_LOG_INFO("this identity should be kept for other rib entry: "
+                       << entry.first);
+          return;
+        }
+    }
+
+  scheduler::cancel(iRegEntry->second);
+  m_regEntries.erase(iRegEntry);
+
+  // make copies of m_controlParameters and m_commandOptions to
+  // avoid unreasonable overwriting during concurrent registration
+  // and unregistration.
+  ControlParameters parameters = m_controlParameters;
+  CommandOptions    options    = m_commandOptions;
+
+  startUnregistration(parameters.setName(prefixForRegistration).unsetCost(),
+                      options.setSigningIdentity(identity.first),
+                      m_nRetries);
+}
+
+std::pair<Name, size_t>
+RemoteRegistrator::findIdentityForRegistration(const Name& prefix)
+{
+  std::pair<Name, size_t> candidateIdentity;
+  std::vector<Name> identities;
+  bool isPrefix = false;
+  size_t maxLength = 0, curLength = 0;
+
+  // get all identies from the key-cahin except the default one.
+  m_keyChain.getAllIdentities(identities, false);
+
+  // get the default identity.
+  identities.push_back(m_keyChain.getDefaultIdentity());
+
+  // longest prefix matching to all indenties.
+  for (auto&& i : identities)
+    {
+      if (!i.empty() && RM_IGNORE_COMMPONENT == i.at(-1))
+        {
+          isPrefix = i.getPrefix(-1).isPrefixOf(prefix);
+          curLength = i.size() - 1;
+        }
+      else
+        {
+          isPrefix = i.isPrefixOf(prefix);
+          curLength = i.size();
+        }
+
+      if (isPrefix && curLength > maxLength)
+        {
+          candidateIdentity.first = i;
+          maxLength = curLength;
+        }
+    }
+
+  candidateIdentity.second = maxLength;
+
+  return candidateIdentity;
+}
+
+void
+RemoteRegistrator::startRegistration(const ControlParameters& parameters,
+                                     const CommandOptions& options,
+                                     int nRetries)
+{
+  NFD_LOG_INFO("start register " << parameters.getName());
+
+  m_nfdController.start<ndn::nfd::RibRegisterCommand>(
+     parameters,
+     bind(&RemoteRegistrator::onRegSuccess,
+          this, parameters, options),
+     bind(&RemoteRegistrator::onRegFailure,
+          this, _1, _2, parameters, options, nRetries),
+     options);
+}
+
+void
+RemoteRegistrator::startUnregistration(const ControlParameters& parameters,
+                                       const CommandOptions& options,
+                                       int nRetries)
+{
+  NFD_LOG_INFO("start unregister " << parameters.getName());
+
+  m_nfdController.start<ndn::nfd::RibUnregisterCommand>(
+     parameters,
+     bind(&RemoteRegistrator::onUnregSuccess,
+          this, parameters, options),
+     bind(&RemoteRegistrator::onUnregFailure,
+          this, _1, _2, parameters, options, nRetries),
+     options);
+}
+
+void
+RemoteRegistrator::onRegSuccess(const ControlParameters& parameters,
+                                const CommandOptions& options)
+{
+  NFD_LOG_INFO("success to register " << parameters.getName());
+
+  RegisteredEntryIt iRegEntry = m_regEntries.find(parameters.getName());
+
+  if (m_regEntries.end() != iRegEntry)
+    {
+      NFD_LOG_DEBUG("Existing Entry: (" << iRegEntry->first
+                                        << ", " << iRegEntry->second
+                                        << ")");
+
+      scheduler::cancel(iRegEntry->second);
+      iRegEntry->second = scheduler::schedule(
+                            m_refreshInterval,
+                            bind(&RemoteRegistrator::startRegistration,
+                                 this, parameters, options, m_nRetries));
+    }
+  else
+    {
+      NFD_LOG_DEBUG("New Entry");
+      m_regEntries.insert(RegisteredEntry(
+                              parameters.getName(),
+                              scheduler::schedule(
+                                m_refreshInterval,
+                                bind(&RemoteRegistrator::startRegistration,
+                                     this, parameters, options, m_nRetries))));
+    }
+}
+
+void
+RemoteRegistrator::onRegFailure(uint32_t code, const std::string& reason,
+                                const ControlParameters& parameters,
+                                const CommandOptions& options,
+                                int nRetries)
+{
+  NFD_LOG_INFO("fail to unregister " << parameters.getName()
+                                     << "\n\t reason:" << reason
+                                     << "\n\t remain retries:" << nRetries);
+
+  if (nRetries > 0)
+    {
+      startRegistration(parameters, options, nRetries - 1);
+    }
+}
+
+void
+RemoteRegistrator::onUnregSuccess(const ControlParameters& parameters,
+                                  const CommandOptions& options)
+{
+  NFD_LOG_INFO("success to unregister " << parameters.getName());
+}
+
+void
+RemoteRegistrator::onUnregFailure(uint32_t code, const std::string& reason,
+                                  const ControlParameters& parameters,
+                                  const CommandOptions& options,
+                                  int nRetries)
+{
+  NFD_LOG_INFO("fail to unregister " << parameters.getName()
+                                     << "\n\t reason:" << reason
+                                     << "\n\t remain retries:" << nRetries);
+
+  if (nRetries > 0)
+    {
+      startUnregistration(parameters, options, nRetries - 1);
+    }
+}
+
+void
+RemoteRegistrator::redoRegistration()
+{
+  NFD_LOG_INFO("redo " << m_regEntries.size()
+                       << " registration when new Hub connection is built.");
+
+  for (auto&& entry : m_regEntries)
+    {
+      // make copies to avoid unreasonable overwrite.
+      ControlParameters parameters = m_controlParameters;
+      CommandOptions    options    = m_commandOptions;
+      startRegistration(parameters.setName(entry.first),
+                        options.setSigningIdentity(entry.first),
+                        m_nRetries);
+    }
+}
+
+void
+RemoteRegistrator::clearRefreshEvents()
+{
+  for (auto&& entry : m_regEntries)
+    {
+      scheduler::cancel(entry.second);
+    }
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/rib/remote-registrator.hpp b/rib/remote-registrator.hpp
new file mode 100644
index 0000000..41be218
--- /dev/null
+++ b/rib/remote-registrator.hpp
@@ -0,0 +1,235 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_RIB_REMOTE_REGISTRATOR_HPP
+#define NFD_RIB_REMOTE_REGISTRATOR_HPP
+
+#include "rib.hpp"
+#include "core/config-file.hpp"
+#include "rib-status-publisher.hpp"
+
+#include <unordered_map>
+#include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/management/nfd-controller.hpp>
+#include <ndn-cxx/management/nfd-control-command.hpp>
+#include <ndn-cxx/management/nfd-control-parameters.hpp>
+#include <ndn-cxx/management/nfd-command-options.hpp>
+
+namespace nfd {
+namespace rib {
+
+/**
+ * @brief define the RemoteRegistrator class, which handles
+ *        the registration/unregistration to remote hub(s).
+ */
+class RemoteRegistrator : noncopyable
+{
+public:
+  class Error : public std::runtime_error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+    }
+  };
+
+  RemoteRegistrator(ndn::nfd::Controller& controller,
+                    ndn::KeyChain& keyChain,
+                    Rib& rib);
+
+  ~RemoteRegistrator();
+
+  /**
+   * @brief load the "remote_register" section from config file
+   *
+   * @param configSection the sub section in "rib" section.
+   */
+  void
+  loadConfig(const ConfigSection& configSection);
+
+  /**
+   * @brief register a prefix to remote hub(s).
+   *
+   * For the input prefix, we find the longest identity
+   * in the key-chain that can sign it, and then
+   * register this identity to remote hub(s).
+   *
+   * @param prefix the prefix being registered in local RIB.
+   */
+  void
+  registerPrefix(const Name& prefix);
+
+  /**
+   * @brief unregister a prefix from remote hub(s).
+   *
+   * For the input prefix, if the longest identity can sign it
+   * is already registered remotely, that identity should be
+   * unregistered from remote hub(s).
+   *
+   * @param prefix the prefix being unregistered in local RIB.
+   */
+  void
+  unregisterPrefix(const Name& prefix);
+
+private:
+  /**
+   * @brief find the most proper identity that can sign the
+   *        registration/unregistration command for the input prefix.
+   *
+   * @return the identity and the length of the longest match to the
+   *         input prefix.
+   *
+   * @retval { ignored, 0 } no matching identity
+   */
+  std::pair<Name, size_t>
+  findIdentityForRegistration(const Name& prefix);
+
+  /**
+   * @brief make and send the remote registration command.
+   *
+   * @param nRetries remaining number of retries.
+   */
+  void
+  startRegistration(const ndn::nfd::ControlParameters& parameters,
+                    const ndn::nfd::CommandOptions& options,
+                    int nRetries);
+
+  /**
+   * @brief make and send the remote unregistration command.
+   *
+   * @param nRetries remaining number of retries.
+   */
+  void
+  startUnregistration(const ndn::nfd::ControlParameters& parameters,
+                      const ndn::nfd::CommandOptions& options,
+                      int nRetries);
+  /**
+   * @brief refresh the remotely registered entry if registration
+   *        successes by re-sending the registration command.
+   *
+   * The interval of sending refresh command is defined in the
+   * "remote_register" section of the config file.
+   *
+   * @param parameters the same paremeters from startRegistration.
+   * @param options the same options as in startRegistration.
+   */
+  void
+  onRegSuccess(const ndn::nfd::ControlParameters& parameters,
+               const ndn::nfd::CommandOptions& options);
+
+  /**
+   * @brief retry to send registration command if registration fails.
+   *
+   * the number of retries is defined in the "remote_register"
+   * section of the config file.
+   *
+   * @param code error code.
+   * @param reason error reason in string.
+   * @param parameters the same paremeters from startRegistration.
+   * @param options the same options from startRegistration.
+   * @param nRetries remaining number of retries.
+   */
+  void
+  onRegFailure(uint32_t code, const std::string& reason,
+               const ndn::nfd::ControlParameters& parameters,
+               const ndn::nfd::CommandOptions& options,
+               int nRetries);
+
+  void
+  onUnregSuccess(const ndn::nfd::ControlParameters& parameters,
+                 const ndn::nfd::CommandOptions& options);
+
+  /**
+   * @brief retry to send unregistration command if registration fails.
+   *
+   * the number of retries is defined in the "remote_register"
+   * section of the config file.
+   *
+   * @param code error code.
+   * @param reason error reason in string.
+   * @param parameters the same paremeters as in startRegistration.
+   * @param options the same options as in startRegistration.
+   * @param nRetries remaining number of retries.
+   */
+  void
+  onUnregFailure(uint32_t code, const std::string& reason,
+                 const ndn::nfd::ControlParameters& parameters,
+                 const ndn::nfd::CommandOptions& options,
+                 int nRetries);
+
+  /**
+   * @brief re-register all prefixes
+   *
+   * This is called when a HUB connection is established.
+   */
+  void
+  redoRegistration();
+
+  /**
+   * @brief clear all refresh events
+   *
+   * This is called when all HUB connections are lost.
+   */
+  void
+  clearRefreshEvents();
+
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  /**
+   * When a locally registered prefix triggles remote
+   * registration, we actually register the longest
+   * identity that can sign this prefix to remote hub(s).
+   *
+   * Thus, the remotely reigstered prefix does not equal
+   * to Route Name. So it needs seperate sotrage instead
+   * of storing within the RIB.
+   */
+  typedef std::unordered_map<Name, EventId> RegisteredList;
+  typedef RegisteredList::iterator RegisteredEntryIt;
+  typedef RegisteredList::value_type RegisteredEntry;
+  RegisteredList m_regEntries;
+
+private:
+  ndn::nfd::Controller& m_nfdController;
+  ndn::KeyChain& m_keyChain;
+  Rib& m_rib;
+
+  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
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_RIB_REMOTE_REGISTRATOR_HPP
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index e0661ac..e7ea3e9 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.cpp
@@ -80,6 +80,7 @@
   , m_localhopValidator(m_face)
   , m_faceMonitor(m_face)
   , m_isLocalhopEnabled(false)
+  , m_remoteRegistrator(m_nfdController, m_keyChain, m_managedRib)
   , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
   , m_lastTransactionId(0)
   , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
@@ -153,6 +154,21 @@
           m_localhopValidator.load(i->second, filename);
           m_isLocalhopEnabled = true;
         }
+      else if (i->first == "remote_register")
+        {
+          m_remoteRegistrator.loadConfig(i->second);
+
+          // 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);
+          };
+        }
       else
         throw Error("Unrecognized rib property: " + i->first);
     }
diff --git a/rib/rib-manager.hpp b/rib/rib-manager.hpp
index 1ca86cf..181d210 100644
--- a/rib/rib-manager.hpp
+++ b/rib/rib-manager.hpp
@@ -29,6 +29,7 @@
 #include "rib.hpp"
 #include "core/config-file.hpp"
 #include "rib-status-publisher.hpp"
+#include "remote-registrator.hpp"
 
 #include <ndn-cxx/security/validator-config.hpp>
 #include <ndn-cxx/management/nfd-face-monitor.hpp>
@@ -239,6 +240,7 @@
   ndn::ValidatorConfig m_localhopValidator;
   ndn::nfd::FaceMonitor m_faceMonitor;
   bool m_isLocalhopEnabled;
+  RemoteRegistrator m_remoteRegistrator;
 
   RibStatusPublisher m_ribStatusPublisher;
 
diff --git a/rib/rib.cpp b/rib/rib.cpp
index 0d33f24..02df45e 100644
--- a/rib/rib.cpp
+++ b/rib/rib.cpp
@@ -67,11 +67,6 @@
 {
 }
 
-
-Rib::~Rib()
-{
-}
-
 Rib::const_iterator
 Rib::find(const Name& prefix) const
 {
@@ -193,6 +188,9 @@
       m_faceMap[face.faceId].push_back(entry);
 
       createFibUpdatesForNewRibEntry(*entry, face);
+
+      // do something after inserting an entry
+      afterInsertEntry(prefix);
     }
 }
 
@@ -385,6 +383,9 @@
 
   m_rib.erase(it);
 
+  // do something after erasing an entry.
+  afterEraseEntry(entry->getName());
+
   return nextIt;
 }
 
diff --git a/rib/rib.hpp b/rib/rib.hpp
index c555d21..ed9ca4e 100644
--- a/rib/rib.hpp
+++ b/rib/rib.hpp
@@ -29,7 +29,6 @@
 #include "rib-entry.hpp"
 #include "fib-update.hpp"
 #include "common.hpp"
-#include "rib-entry.hpp"
 #include <ndn-cxx/management/nfd-control-command.hpp>
 
 namespace nfd {
@@ -50,8 +49,6 @@
 
   Rib();
 
-  ~Rib();
-
   const_iterator
   find(const Name& prefix) const;
 
@@ -139,6 +136,10 @@
   void
   removeInheritedFacesFromEntry(RibEntry& entry, const Rib::FaceSet& facesToRemove);
 
+public:
+  ndn::util::EventEmitter<Name> afterInsertEntry;
+  ndn::util::EventEmitter<Name> afterEraseEntry;
+
 private:
   RibTable m_rib;
   FaceLookupTable m_faceMap;