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
diff --git a/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp b/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
index 61cfb62..75e8cfa 100644
--- a/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
+++ b/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
@@ -23,15 +23,13 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "tests/test-common.hpp"
-
 #include "rib/readvertise/nfd-rib-readvertise-destination.hpp"
 
+#include "tests/test-common.hpp"
+#include "tests/identity-management-fixture.hpp"
 #include <ndn-cxx/util/dummy-client-face.hpp>
 #include <ndn-cxx/security/signing-info.hpp>
 
-#include "tests/identity-management-fixture.hpp"
-
 namespace nfd {
 namespace rib {
 namespace tests {
@@ -46,8 +44,8 @@
     , nFailureCallbacks(0)
     , face(getGlobalIoService(), m_keyChain, {true, false})
     , controller(face, m_keyChain)
-    , dest(controller, Name("/localhop/nfd/rib/readvertise"), rib)
-    , successCallback([this] () {
+    , dest(controller, Name("/localhost/nlsr"), rib)
+    , successCallback([this] {
         nSuccessCallbacks++;
       })
     , failureCallback([this] (const std::string& str) {
@@ -69,6 +67,7 @@
   std::function<void(const std::string&)> failureCallback;
 };
 
+BOOST_AUTO_TEST_SUITE(Readvertise)
 BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
 
 class AdvertiseSuccessScenario
@@ -118,16 +117,14 @@
   }
 };
 
-typedef boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario> AdvertiseScenarios;
+using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
 
 BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
 {
   Scenario scenario;
   Name prefix("/ndn/memphis/test");
-  ndn::security::SigningInfo secInfo;
-  std::vector<RibRouteRef> routes;
-  ReadvertisedRoute rr(prefix, secInfo, routes);
-  const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nfd/rib/register");
+  ReadvertisedRoute rr(prefix);
+  const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
 
   dest.advertise(rr, successCallback, failureCallback);
   advanceClocks(time::milliseconds(100));
@@ -198,16 +195,14 @@
   }
 };
 
-typedef boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario> WithdrawScenarios;
+using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>;
 
 BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
 {
   Scenario scenario;
   Name prefix("/ndn/memphis/test");
-  ndn::security::SigningInfo secInfo;
-  std::vector<RibRouteRef> routes;
-  ReadvertisedRoute rr(prefix, secInfo, routes);
-  const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nfd/rib/unregister");
+  ReadvertisedRoute rr(prefix);
+  const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
 
   dest.withdraw(rr, successCallback, failureCallback);
   this->advanceClocks(time::milliseconds(10));
@@ -235,28 +230,28 @@
 BOOST_AUTO_TEST_CASE(DestinationAvailability)
 {
   std::vector<bool> availabilityChangeHistory;
-  Name prefix("/localhop/nfd/rib/readvertise");
+  Name commandPrefix("/localhost/nlsr");
   Route route;
 
   dest.afterAvailabilityChange.connect(
     std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
   BOOST_CHECK_EQUAL(dest.isAvailable(), false);
 
-  rib.insert(prefix, route);
-  this->advanceClocks(time::milliseconds(100), 1);
+  rib.insert(commandPrefix, route);
+  this->advanceClocks(time::milliseconds(100));
   BOOST_CHECK_EQUAL(dest.isAvailable(), true);
-  BOOST_CHECK_EQUAL(availabilityChangeHistory.size(), 1);
+  BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
   BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
 
-  rib.erase(prefix, route);
-
-  this->advanceClocks(time::milliseconds(100), 1);
+  rib.erase(commandPrefix, route);
+  this->advanceClocks(time::milliseconds(100));
   BOOST_CHECK_EQUAL(dest.isAvailable(), false);
-  BOOST_CHECK_EQUAL(availabilityChangeHistory.size(), 2);
+  BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
   BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
+BOOST_AUTO_TEST_SUITE_END() // Readvertise
 
 } // namespace tests
 } // namespace rib