mgmt+rib: use getScheduler() in RibManager and Readvertise

Refs: #4528
Change-Id: Ie6dd2b0fbe5d57cd7878d86b0185ae5d9bd92cf0
diff --git a/daemon/mgmt/rib-manager.cpp b/daemon/mgmt/rib-manager.cpp
index 8db60ab..a790775 100644
--- a/daemon/mgmt/rib-manager.cpp
+++ b/daemon/mgmt/rib-manager.cpp
@@ -27,6 +27,7 @@
 
 #include "core/fib-max-depth.hpp"
 #include "core/logger.hpp"
+#include "daemon/global.hpp"
 #include "rib/rib.hpp"
 
 #include <ndn-cxx/lp/tags.hpp>
@@ -47,13 +48,12 @@
 const Name RibManager::LOCALHOP_TOP_PREFIX = "/localhop/nfd";
 
 RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
-                       ndn::nfd::Controller& nfdController, Dispatcher& dispatcher, Scheduler& scheduler)
+                       ndn::nfd::Controller& nfdController, Dispatcher& dispatcher)
   : ManagerBase(MGMT_MODULE_NAME, dispatcher)
   , m_rib(rib)
   , m_keyChain(keyChain)
   , m_nfdController(nfdController)
   , m_dispatcher(dispatcher)
-  , m_scheduler(scheduler)
   , m_faceMonitor(face)
   , m_localhostValidator(face)
   , m_localhopValidator(face)
@@ -136,7 +136,7 @@
                " origin=" << route.origin << " cost=" << route.cost);
 
   if (expires) {
-    auto event = m_scheduler.schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
+    auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
     route.setExpirationEvent(event);
     NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
   }
@@ -458,7 +458,7 @@
 void
 RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
 {
-  m_activeFaceFetchEvent = m_scheduler.schedule(timeToWait, [this] { fetchActiveFaces(); });
+  m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
 }
 
 void
@@ -475,8 +475,8 @@
   // face destroyed events
   for (auto faceId : m_registeredFaces) {
     if (activeFaceIds.count(faceId) == 0) {
-      NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
-      m_scheduler.schedule(0_ns, [this, faceId] { this->onFaceDestroyedEvent(faceId); });
+      NFD_LOG_DEBUG("Removing invalid FaceId " << faceId);
+      getGlobalIoService().post([this, faceId] { onFaceDestroyedEvent(faceId); });
     }
   }
 
@@ -490,8 +490,8 @@
   NFD_LOG_TRACE("onNotification: " << notification);
 
   if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
-    NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
-    m_scheduler.schedule(0_ns, [this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); });
+    NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId());
+    getGlobalIoService().post([this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); });
   }
 }
 
diff --git a/daemon/mgmt/rib-manager.hpp b/daemon/mgmt/rib-manager.hpp
index 788a1d0..6888024 100644
--- a/daemon/mgmt/rib-manager.hpp
+++ b/daemon/mgmt/rib-manager.hpp
@@ -51,7 +51,7 @@
 {
 public:
   RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
-             ndn::nfd::Controller& nfdController, Dispatcher& dispatcher, Scheduler& scheduler);
+             ndn::nfd::Controller& nfdController, Dispatcher& dispatcher);
 
   /**
    * @brief Apply localhost_security configuration.
@@ -245,7 +245,6 @@
   ndn::KeyChain& m_keyChain;
   ndn::nfd::Controller& m_nfdController;
   Dispatcher& m_dispatcher;
-  Scheduler& m_scheduler;
 
   ndn::nfd::FaceMonitor m_faceMonitor;
   ndn::ValidatorConfig m_localhostValidator;
diff --git a/daemon/rib/readvertise/readvertise.cpp b/daemon/rib/readvertise/readvertise.cpp
index 49c3400..d5bedf8 100644
--- a/daemon/rib/readvertise/readvertise.cpp
+++ b/daemon/rib/readvertise/readvertise.cpp
@@ -25,6 +25,7 @@
 
 #include "readvertise.hpp"
 #include "core/logger.hpp"
+#include "daemon/global.hpp"
 
 #include <ndn-cxx/util/random.hpp>
 
@@ -44,11 +45,10 @@
   return std::max(newTime, 0_ms);
 }
 
-Readvertise::Readvertise(Rib& rib, Scheduler& scheduler,
+Readvertise::Readvertise(Rib& rib,
                          unique_ptr<ReadvertisePolicy> policy,
                          unique_ptr<ReadvertiseDestination> destination)
-  : m_scheduler(scheduler)
-  , m_policy(std::move(policy))
+  : m_policy(std::move(policy))
   , m_destination(std::move(destination))
 {
   m_addRouteConn = rib.afterAddRoute.connect([this] (const auto& r) { this->afterAddRoute(r); });
@@ -162,14 +162,14 @@
     [=] {
       NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success");
       rrIt->retryDelay = RETRY_DELAY_MIN;
-      rrIt->retryEvt = m_scheduler.schedule(randomizeTimer(m_policy->getRefreshInterval()),
-                                            [=] { advertise(rrIt); });
+      rrIt->retryEvt = getScheduler().schedule(randomizeTimer(m_policy->getRefreshInterval()),
+                                               [=] { advertise(rrIt); });
     },
     [=] (const std::string& msg) {
       NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg);
       rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
-      rrIt->retryEvt = m_scheduler.schedule(randomizeTimer(rrIt->retryDelay),
-                                            [=] { advertise(rrIt); });
+      rrIt->retryEvt = getScheduler().schedule(randomizeTimer(rrIt->retryDelay),
+                                               [=] { advertise(rrIt); });
     });
 }
 
@@ -192,8 +192,8 @@
     [=] (const std::string& msg) {
       NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg);
       rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
-      rrIt->retryEvt = m_scheduler.schedule(randomizeTimer(rrIt->retryDelay),
-                                            [=] { withdraw(rrIt); });
+      rrIt->retryEvt = getScheduler().schedule(randomizeTimer(rrIt->retryDelay),
+                                               [=] { withdraw(rrIt); });
     });
 }
 
diff --git a/daemon/rib/readvertise/readvertise.hpp b/daemon/rib/readvertise/readvertise.hpp
index a14d438..871702f 100644
--- a/daemon/rib/readvertise/readvertise.hpp
+++ b/daemon/rib/readvertise/readvertise.hpp
@@ -43,9 +43,8 @@
  */
 class Readvertise : noncopyable
 {
-
 public:
-  Readvertise(Rib& rib, Scheduler& scheduler,
+  Readvertise(Rib& rib,
               unique_ptr<ReadvertisePolicy> policy,
               unique_ptr<ReadvertiseDestination> destination);
 
@@ -76,7 +75,6 @@
   static const time::milliseconds RETRY_DELAY_MIN;
   static const time::milliseconds RETRY_DELAY_MAX;
 
-  Scheduler& m_scheduler;
   unique_ptr<ReadvertisePolicy> m_policy;
   unique_ptr<ReadvertiseDestination> m_destination;
 
diff --git a/daemon/rib/service.cpp b/daemon/rib/service.cpp
index 8405c8e..043b980 100644
--- a/daemon/rib/service.cpp
+++ b/daemon/rib/service.cpp
@@ -107,11 +107,10 @@
                  const ConfigParseFunc& configParse)
   : m_keyChain(keyChain)
   , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain)
-  , m_scheduler(m_face.getIoService())
   , m_nfdController(m_face, m_keyChain)
   , m_fibUpdater(m_rib, m_nfdController)
   , m_dispatcher(m_face, m_keyChain)
-  , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
+  , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
 {
   if (s_instance != nullptr) {
     NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once"));
@@ -219,7 +218,6 @@
 
         m_readvertisePropagation = make_unique<Readvertise>(
           m_rib,
-          m_scheduler,
           make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
           make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
       }
@@ -242,7 +240,6 @@
     auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
     m_readvertiseNlsr = make_unique<Readvertise>(
       m_rib,
-      m_scheduler,
       make_unique<ClientToNlsrReadvertisePolicy>(),
       make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
   }
diff --git a/daemon/rib/service.hpp b/daemon/rib/service.hpp
index 1c07d7e..d1c2226 100644
--- a/daemon/rib/service.hpp
+++ b/daemon/rib/service.hpp
@@ -111,7 +111,6 @@
 
   ndn::KeyChain& m_keyChain;
   ndn::Face m_face;
-  Scheduler m_scheduler;
   ndn::nfd::Controller m_nfdController;
 
   Rib m_rib;
diff --git a/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp b/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp
index 5576878..ea3ca10 100644
--- a/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp
+++ b/tests/daemon/mgmt/rib-manager-sl-announce.t.cpp
@@ -42,7 +42,6 @@
 
   RibManagerSlAnnounceFixture()
     : m_face(g_io, m_keyChain)
-    , m_scheduler(g_io)
     , m_nfdController(m_face, m_keyChain)
     , m_dispatcher(m_face, m_keyChain)
     , m_fibUpdater(rib, m_nfdController)
@@ -55,7 +54,7 @@
     // Face, Controller, Dispatcher are irrelevant to SlAnnounce functions but required by
     // RibManager construction, so they are private. RibManager is a pointer to avoid code style
     // rule 1.4 violation.
-    manager = make_unique<RibManager>(rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler);
+    manager = make_unique<RibManager>(rib, m_face, m_keyChain, m_nfdController, m_dispatcher);
 
     loadTrustSchema();
   }
@@ -163,7 +162,6 @@
 
 private:
   ndn::util::DummyClientFace m_face;
-  Scheduler m_scheduler;
   ndn::nfd::Controller m_nfdController;
   Dispatcher m_dispatcher;
   rib::FibUpdater m_fibUpdater;
diff --git a/tests/daemon/mgmt/rib-manager.t.cpp b/tests/daemon/mgmt/rib-manager.t.cpp
index 9756a36..04d9dca 100644
--- a/tests/daemon/mgmt/rib-manager.t.cpp
+++ b/tests/daemon/mgmt/rib-manager.t.cpp
@@ -53,14 +53,12 @@
 class RibManagerFixture : public ManagerCommonFixture
 {
 public:
-  explicit
   RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
     : m_commands(m_face.sentInterests)
     , m_status(status)
-    , m_scheduler(g_io)
     , m_nfdController(m_face, m_keyChain)
     , m_fibUpdater(m_rib, m_nfdController)
-    , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
+    , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
   {
     m_rib.mockFibResponse = [] (const rib::RibUpdateBatch& batch) {
       BOOST_CHECK(batch.begin() != batch.end());
@@ -205,7 +203,6 @@
   std::vector<Interest>& m_commands;
   ConfigurationStatus m_status;
 
-  Scheduler m_scheduler;
   ndn::nfd::Controller m_nfdController;
   rib::Rib m_rib;
   rib::FibUpdater m_fibUpdater;
diff --git a/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp b/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp
index 1b357cb..c455065 100644
--- a/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp
+++ b/tests/daemon/rib/readvertise/host-to-gateway-readvertise-policy.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -38,8 +38,9 @@
 class HostToGatewayReadvertisePolicyFixture : public IdentityManagementFixture
 {
 public:
-  RibRouteRef
-  makeNewRoute(const Name& prefix) {
+  static RibRouteRef
+  makeNewRoute(const Name& prefix)
+  {
     auto entry = make_shared<RibEntry>();
     entry->setName(prefix);
 
@@ -56,8 +57,7 @@
 };
 
 BOOST_AUTO_TEST_SUITE(Readvertise)
-BOOST_FIXTURE_TEST_SUITE(TestHostToGatewayReadvertisePolicy,
-                         HostToGatewayReadvertisePolicyFixture)
+BOOST_FIXTURE_TEST_SUITE(TestHostToGatewayReadvertisePolicy, HostToGatewayReadvertisePolicyFixture)
 
 BOOST_AUTO_TEST_CASE(PrefixToAdvertise)
 {
diff --git a/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp b/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
index d3ec057..e3afcbf 100644
--- a/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
+++ b/tests/daemon/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
@@ -44,11 +44,10 @@
     : nSuccessCallbacks(0)
     , nFailureCallbacks(0)
     , face(g_io, m_keyChain, {true, false})
-    , scheduler(g_io)
     , controller(face, m_keyChain)
     , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr"))
     , successCallback([this] { nSuccessCallbacks++; })
-    , failureCallback([this] (const std::string& str) { nFailureCallbacks++; })
+    , failureCallback([this] (const std::string&) { nFailureCallbacks++; })
   {
   }
 
@@ -58,7 +57,6 @@
 
 protected:
   ndn::util::DummyClientFace face;
-  Scheduler scheduler;
   ndn::nfd::Controller controller;
   Rib rib;
   NfdRibReadvertiseDestination dest;
diff --git a/tests/daemon/rib/readvertise/readvertise.t.cpp b/tests/daemon/rib/readvertise/readvertise.t.cpp
index f73efbd..d2d8bb2 100644
--- a/tests/daemon/rib/readvertise/readvertise.t.cpp
+++ b/tests/daemon/rib/readvertise/readvertise.t.cpp
@@ -41,7 +41,7 @@
 {
 public:
   optional<ReadvertiseAction>
-  handleNewRoute(const RibRouteRef& route) const override
+  handleNewRoute(const RibRouteRef&) const override
   {
     return this->decision;
   }
@@ -49,7 +49,7 @@
   time::milliseconds
   getRefreshInterval() const override
   {
-    return time::seconds(60);
+    return 1_min;
   }
 
 public:
@@ -115,14 +115,12 @@
 public:
   ReadvertiseFixture()
     : m_face(g_io, m_keyChain, {false, false})
-    , m_scheduler(g_io)
   {
-    auto policyUnique = make_unique<DummyReadvertisePolicy>();
-    policy = policyUnique.get();
-    auto destinationUnique = make_unique<DummyReadvertiseDestination>();
-    destination = destinationUnique.get();
-    readvertise = make_unique<Readvertise>(m_rib, m_scheduler,
-                                           std::move(policyUnique), std::move(destinationUnique));
+    auto policyPtr = make_unique<DummyReadvertisePolicy>();
+    policy = policyPtr.get();
+    auto destinationPtr = make_unique<DummyReadvertiseDestination>();
+    destination = destinationPtr.get();
+    readvertise = make_unique<Readvertise>(m_rib, std::move(policyPtr), std::move(destinationPtr));
   }
 
   void
@@ -159,7 +157,6 @@
 
 private:
   ndn::util::DummyClientFace m_face;
-  Scheduler m_scheduler;
   Rib m_rib;
 };