rib: simplify ReadvertisedRoute

refs #3818

Change-Id: Ied644d825458f387b6f886bfcd425cffd2c2c7aa
diff --git a/rib/readvertise/nfd-rib-readvertise-destination.cpp b/rib/readvertise/nfd-rib-readvertise-destination.cpp
index dcf3d93..3d19004 100644
--- a/rib/readvertise/nfd-rib-readvertise-destination.cpp
+++ b/rib/readvertise/nfd-rib-readvertise-destination.cpp
@@ -24,61 +24,74 @@
  */
 
 #include "nfd-rib-readvertise-destination.hpp"
+#include "core/logger.hpp"
+
+#include <ndn-cxx/mgmt/nfd/command-options.hpp>
+#include <ndn-cxx/mgmt/nfd/control-command.hpp>
+#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
+#include <ndn-cxx/mgmt/nfd/control-response.hpp>
 
 namespace nfd {
 namespace rib {
 
+NFD_LOG_INIT("NfdRibReadvertiseDestination");
+
+using ndn::nfd::CommandOptions;
 using ndn::nfd::ControlParameters;
 using ndn::nfd::ControlResponse;
 
 NfdRibReadvertiseDestination::NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
-                                                           const ndn::Name& commandPrefix,
+                                                           const Name& commandPrefix,
                                                            Rib& rib)
   : m_controller(controller)
   , m_commandPrefix(commandPrefix)
 {
-  m_ribAddConn = rib.afterInsertEntry.connect(
-    std::bind(&NfdRibReadvertiseDestination::handleRibAdd, this, _1));
-  m_ribRemoveConn = rib.afterEraseEntry.connect(
-    std::bind(&NfdRibReadvertiseDestination::handleRibRemove, this, _1));
+  m_ribInsertConn = rib.afterInsertEntry.connect(
+    std::bind(&NfdRibReadvertiseDestination::handleRibInsert, this, _1));
+  m_ribEraseConn = rib.afterEraseEntry.connect(
+    std::bind(&NfdRibReadvertiseDestination::handleRibErase, this, _1));
 }
 
 void
-NfdRibReadvertiseDestination::advertise(nfd::rib::ReadvertisedRoute& rr,
+NfdRibReadvertiseDestination::advertise(const nfd::rib::ReadvertisedRoute& rr,
                                         std::function<void()> successCb,
                                         std::function<void(const std::string&)> failureCb)
 {
-  m_controller.start<ndn::nfd::RibRegisterCommand>(ControlParameters()
-                                                   .setName(rr.getPrefix())
-                                                   .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
+  NFD_LOG_DEBUG("advertise " << rr.prefix << " on " << m_commandPrefix);
+
+  m_controller.start<ndn::nfd::RibRegisterCommand>(
+    ControlParameters().setName(rr.prefix).setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
     [=] (const ControlParameters& cp) { successCb(); },
-    [=] (const ControlResponse& cr) { failureCb(cr.getText()); });
+    [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
+    CommandOptions().setPrefix(m_commandPrefix).setSigningInfo(rr.signer));
 }
 
 void
-NfdRibReadvertiseDestination::withdraw(nfd::rib::ReadvertisedRoute& rr,
+NfdRibReadvertiseDestination::withdraw(const nfd::rib::ReadvertisedRoute& rr,
                                        std::function<void()> successCb,
                                        std::function<void(const std::string&)> failureCb)
 {
-  m_controller.start<ndn::nfd::RibUnregisterCommand>(ControlParameters()
-                                                     .setName(rr.getPrefix())
-                                                     .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
+  NFD_LOG_DEBUG("withdraw " << rr.prefix << " on " << m_commandPrefix);
+
+  m_controller.start<ndn::nfd::RibUnregisterCommand>(
+    ControlParameters().setName(rr.prefix).setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
     [=] (const ControlParameters& cp) { successCb(); },
-    [=] (const ControlResponse& cr) { failureCb(cr.getText()); });
+    [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
+    CommandOptions().setPrefix(m_commandPrefix).setSigningInfo(rr.signer));
 }
 
 void
-NfdRibReadvertiseDestination::handleRibAdd(const ndn::Name& name)
+NfdRibReadvertiseDestination::handleRibInsert(const ndn::Name& name)
 {
-  if (name == m_commandPrefix) {
+  if (name.isPrefixOf(m_commandPrefix)) {
     setAvailability(true);
   }
 }
 
 void
-NfdRibReadvertiseDestination::handleRibRemove(const ndn::Name& name)
+NfdRibReadvertiseDestination::handleRibErase(const ndn::Name& name)
 {
-  if (name == m_commandPrefix) {
+  if (name.isPrefixOf(m_commandPrefix)) {
     setAvailability(false);
   }
 }
diff --git a/rib/readvertise/nfd-rib-readvertise-destination.hpp b/rib/readvertise/nfd-rib-readvertise-destination.hpp
index b737d3a..ec71d98 100644
--- a/rib/readvertise/nfd-rib-readvertise-destination.hpp
+++ b/rib/readvertise/nfd-rib-readvertise-destination.hpp
@@ -26,14 +26,10 @@
 #ifndef NFD_RIB_READVERTISE_NFD_RIB_READVERTISE_DESTINATION_HPP
 #define NFD_RIB_READVERTISE_NFD_RIB_READVERTISE_DESTINATION_HPP
 
-#include "readvertised-route.hpp"
 #include "readvertise-destination.hpp"
+#include "../rib.hpp"
 
-#include <ndn-cxx/mgmt/nfd/command-options.hpp>
 #include <ndn-cxx/mgmt/nfd/controller.hpp>
-#include <ndn-cxx/mgmt/nfd/control-command.hpp>
-#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
-#include <ndn-cxx/mgmt/nfd/control-response.hpp>
 
 namespace nfd {
 namespace rib {
@@ -44,36 +40,36 @@
 {
 public:
   NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
-                               const ndn::Name& commandPrefix,
+                               const Name& commandPrefix,
                                Rib& rib);
 
   /** \brief add a name prefix into NFD RIB
    */
   void
-  advertise(nfd::rib::ReadvertisedRoute& rr,
+  advertise(const ReadvertisedRoute& rr,
             std::function<void()> successCb,
             std::function<void(const std::string&)> failureCb) override;
 
   /** \brief remove a name prefix from NFD RIB
    */
   void
-  withdraw(nfd::rib::ReadvertisedRoute& rr,
+  withdraw(const ReadvertisedRoute& rr,
            std::function<void()> successCb,
            std::function<void(const std::string&)> failureCb) override;
 
 private:
   void
-  handleRibAdd(const ndn::Name& name);
+  handleRibInsert(const Name& name);
 
   void
-  handleRibRemove(const ndn::Name& name);
+  handleRibErase(const Name& name);
 
 private:
   ndn::nfd::Controller& m_controller;
   Name m_commandPrefix;
 
-  signal::ScopedConnection m_ribAddConn;
-  signal::ScopedConnection m_ribRemoveConn;
+  signal::ScopedConnection m_ribInsertConn;
+  signal::ScopedConnection m_ribEraseConn;
 };
 
 } // namespace rib
diff --git a/rib/readvertise/readvertise-destination.cpp b/rib/readvertise/readvertise-destination.cpp
index c064b23..3c577c7 100644
--- a/rib/readvertise/readvertise-destination.cpp
+++ b/rib/readvertise/readvertise-destination.cpp
@@ -24,14 +24,23 @@
  */
 
 #include "readvertise-destination.hpp"
+#include "core/logger.hpp"
 
 namespace nfd {
 namespace rib {
 
+NFD_LOG_INIT("ReadvertiseDestination");
+
 void
 ReadvertiseDestination::setAvailability(bool isAvailable)
 {
   if (m_isAvailable != isAvailable) {
+    if (isAvailable) {
+      NFD_LOG_DEBUG("Destination has become available.");
+    }
+    else {
+      NFD_LOG_DEBUG("Destinatino has become unavailable.");
+    }
     m_isAvailable = isAvailable;
     afterAvailabilityChange(isAvailable);
   }
diff --git a/rib/readvertise/readvertise-destination.hpp b/rib/readvertise/readvertise-destination.hpp
index 204d2b6..1bfaeb7 100644
--- a/rib/readvertise/readvertise-destination.hpp
+++ b/rib/readvertise/readvertise-destination.hpp
@@ -28,8 +28,6 @@
 
 #include "readvertised-route.hpp"
 
-#include <ndn-cxx/mgmt/nfd/controller.hpp>
-
 namespace nfd {
 namespace rib {
 
@@ -42,12 +40,12 @@
   ~ReadvertiseDestination() = default;
 
   virtual void
-  advertise(nfd::rib::ReadvertisedRoute& rr,
+  advertise(const ReadvertisedRoute& rr,
             std::function<void()> successCb,
             std::function<void(const std::string&)> failureCb) = 0;
 
   virtual void
-  withdraw(nfd::rib::ReadvertisedRoute& rr,
+  withdraw(const ReadvertisedRoute& rr,
            std::function<void()> successCb,
            std::function<void(const std::string&)> failureCb) = 0;
 
@@ -64,8 +62,7 @@
 public:
   /** \brief signals when the destination becomes available or unavailable
    */
-  ndn::util::signal::Signal<ReadvertiseDestination, bool>
-  afterAvailabilityChange;
+  signal::Signal<ReadvertiseDestination, bool> afterAvailabilityChange;
 
 private:
   bool m_isAvailable = false;
diff --git a/rib/readvertise/readvertised-route.cpp b/rib/readvertise/readvertised-route.cpp
index 719ab46..7028697 100644
--- a/rib/readvertise/readvertised-route.cpp
+++ b/rib/readvertise/readvertised-route.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -24,23 +24,16 @@
  */
 
 #include "readvertised-route.hpp"
+#include "core/random.hpp"
 
 namespace nfd {
 namespace rib {
 
-ReadvertisedRoute::ReadvertisedRoute(const ndn::Name& prefix, const ndn::security::SigningInfo& signer,
-                                     const std::vector<RibRouteRef>& routes)
-  : m_prefix(prefix)
-  , m_signer(signer)
-  , m_ribRoutes(routes)
+ReadvertisedRoute::ReadvertisedRoute(const Name& prefix)
+  : prefix(prefix)
+  , nRibRoutes(0)
 {
 }
 
-bool
-operator<(const ReadvertisedRoute& lhs, const ReadvertisedRoute& rhs)
-{
-  return lhs.getPrefix() < rhs.getPrefix();
-}
-
 } // namespace rib
 } // namespace nfd
diff --git a/rib/readvertise/readvertised-route.hpp b/rib/readvertise/readvertised-route.hpp
index 702974c..1a896b1 100644
--- a/rib/readvertise/readvertised-route.hpp
+++ b/rib/readvertise/readvertised-route.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -26,55 +26,41 @@
 #ifndef NFD_RIB_READVERTISE_READVERTISED_ROUTE_HPP
 #define NFD_RIB_READVERTISE_READVERTISED_ROUTE_HPP
 
-#include "../rib.hpp"
+#include "core/scheduler.hpp"
 #include <ndn-cxx/security/signing-info.hpp>
 
 namespace nfd {
 namespace rib {
 
-/** \brief a readvertised route
+/** \brief state of a readvertised route
  */
-class ReadvertisedRoute
+class ReadvertisedRoute : noncopyable
 {
 public:
-  /** \brief standard constructor
-   */
-  ReadvertisedRoute(const ndn::Name& prefix, const ndn::security::SigningInfo& signer,
-                    const std::vector<RibRouteRef>& routes);
+  explicit
+  ReadvertisedRoute(const Name& prefix);
 
-  /** \return name prefix being advertised
-   */
-  const Name&
-  getPrefix() const
-  {
-    return m_prefix;
-  }
-
-  /** \return signer
-   */
-  const ndn::security::SigningInfo&
-  getSigner() const
-  {
-    return m_signer;
-  }
-
-  /** \return routes that caused the creation of this readvertised route
-   */
-  const std::vector<RibRouteRef>&
-  getRibRoutes() const
-  {
-    return m_ribRoutes;
-  }
-
-
-private:
-  Name m_prefix;
-  ndn::security::SigningInfo m_signer;
-  std::vector<RibRouteRef> m_ribRoutes;
+public:
+  Name prefix; ///< readvertised prefix
+  mutable ndn::security::SigningInfo signer; ///< signer for commands
+  mutable size_t nRibRoutes; ///< number of RIB routes that cause the readvertisement
+  mutable time::milliseconds retryDelay; ///< retry interval (not used for refresh)
+  mutable scheduler::ScopedEventId retryEvt; ///< retry or refresh event
 };
 
-bool
-operator<(const ReadvertisedRoute& lhs, const ReadvertisedRoute& rhs);
+/** \brief a less than comparison functor for ReadvertisedRoute prefix
+ */
+class ReadvertisedRoutePrefixCompare
+{
+public:
+  bool
+  operator()(const ReadvertisedRoute& lhs, const ReadvertisedRoute& rhs)
+  {
+    return lhs.prefix < rhs.prefix;
+  }
+};
+
+using ReadvertisedRouteContainer = std::set<ReadvertisedRoute, ReadvertisedRoutePrefixCompare>;
 
 } // namespace rib
 } // namespace nfd