core: use ndn-cxx scheduler::ScopedEventId

refs #4698

Change-Id: I5ab56855fb22824e129fd7e3ccfb53174cf34f82
diff --git a/core/scheduler.cpp b/core/scheduler.cpp
index d0b0f2b..094c188 100644
--- a/core/scheduler.cpp
+++ b/core/scheduler.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  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,
@@ -50,58 +50,10 @@
 }
 
 void
-cancel(const EventId& eventId)
-{
-  getGlobalScheduler().cancelEvent(eventId);
-}
-
-void
 resetGlobalScheduler()
 {
   g_scheduler.reset();
 }
 
-ScopedEventId::ScopedEventId()
-{
-}
-
-ScopedEventId::ScopedEventId(const EventId& event)
-  : m_event(event)
-{
-}
-
-ScopedEventId::ScopedEventId(ScopedEventId&& other)
-  : m_event(other.m_event)
-{
-  other.release();
-}
-
-ScopedEventId&
-ScopedEventId::operator=(const EventId& event)
-{
-  if (m_event != event) {
-    scheduler::cancel(m_event);
-    m_event = event;
-  }
-  return *this;
-}
-
-ScopedEventId::~ScopedEventId()
-{
-  scheduler::cancel(m_event);
-}
-
-void
-ScopedEventId::cancel()
-{
-  scheduler::cancel(m_event);
-}
-
-void
-ScopedEventId::release()
-{
-  m_event.reset();
-}
-
 } // namespace scheduler
 } // namespace nfd
diff --git a/core/scheduler.hpp b/core/scheduler.hpp
index 491408a..3ad448b 100644
--- a/core/scheduler.hpp
+++ b/core/scheduler.hpp
@@ -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,
@@ -33,13 +33,9 @@
 namespace nfd {
 namespace scheduler {
 
-using ndn::Scheduler;
-
-/** \class nfd::scheduler::EventId
- *  \brief Opaque handle for a scheduled event.
- */
-using ndn::EventId;
-
+using ndn::util::scheduler::Scheduler;
+using ndn::util::scheduler::EventId;
+using ndn::util::scheduler::ScopedEventId;
 using ndn::util::scheduler::EventCallback;
 
 /** \brief Schedule an event.
@@ -49,50 +45,11 @@
 
 /** \brief Cancel a scheduled event.
  */
-void
-cancel(const EventId& eventId);
-
-/** \brief Cancels an event automatically upon destruction.
- */
-class ScopedEventId : noncopyable
+inline void
+cancel(EventId eventId)
 {
-public:
-  ScopedEventId();
-
-  /** \brief Implicit constructor from EventId.
-   *  \param event the event to be cancelled upon destruction
-   */
-  ScopedEventId(const EventId& event);
-
-  /** \brief Move constructor.
-   */
-  ScopedEventId(ScopedEventId&& other);
-
-  /** \brief Assigns an event.
-   *
-   *  If a different event has been assigned to this instance previously,
-   *  that event will be cancelled immediately.
-   */
-  ScopedEventId&
-  operator=(const EventId& event);
-
-  /** \brief Cancels the event.
-   */
-  ~ScopedEventId();
-
-  /** \brief Cancels the event manually.
-   */
-  void
-  cancel();
-
-  /** \brief Releases the event so that it won't be cancelled when this ScopedEventId is destructed.
-   */
-  void
-  release();
-
-private:
-  EventId m_event;
-};
+  eventId.cancel();
+}
 
 } // namespace scheduler
 } // namespace nfd
diff --git a/rib/readvertise/readvertise.cpp b/rib/readvertise/readvertise.cpp
index 09715eb..4c51a03 100644
--- a/rib/readvertise/readvertise.cpp
+++ b/rib/readvertise/readvertise.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,
@@ -75,7 +75,7 @@
 
   ReadvertisedRouteContainer::iterator rrIt;
   bool isNew = false;
-  std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix, m_scheduler);
+  std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix);
 
   if (!isNew && rrIt->signer != action->signer) {
     NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
diff --git a/rib/readvertise/readvertised-route.hpp b/rib/readvertise/readvertised-route.hpp
index c2ca848..50be0c4 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-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,
@@ -29,7 +29,7 @@
 #include "core/common.hpp"
 
 #include <ndn-cxx/security/signing-info.hpp>
-#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
 
 namespace nfd {
 namespace rib {
@@ -39,11 +39,11 @@
 class ReadvertisedRoute : noncopyable
 {
 public:
-  ReadvertisedRoute(const Name& prefix, ndn::util::Scheduler& scheduler)
+  explicit
+  ReadvertisedRoute(const Name& prefix)
     : prefix(prefix)
     , nRibRoutes(0)
     , retryDelay(0)
-    , retryEvt(scheduler)
   {
   }
 
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index 35bc87f..d09420e 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.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,
@@ -59,7 +59,6 @@
   , m_localhostValidator(face)
   , m_localhopValidator(face)
   , m_isLocalhopEnabled(false)
-  , m_activeFaceFetchEvent(m_scheduler)
 {
   registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
     bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
@@ -139,7 +138,7 @@
 
   if (expires) {
     auto event = m_scheduler.scheduleEvent(*expires, [=] { m_rib.onRouteExpiration(name, route); });
-    route.setExpirationEvent(event, m_scheduler);
+    route.setExpirationEvent(event);
     NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
   }
 
diff --git a/rib/rib-manager.hpp b/rib/rib-manager.hpp
index 98d29f6..0b4eb6f 100644
--- a/rib/rib-manager.hpp
+++ b/rib/rib-manager.hpp
@@ -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,
@@ -36,7 +36,7 @@
 #include <ndn-cxx/mgmt/nfd/controller.hpp>
 #include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
 #include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
-#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
 
 namespace nfd {
 namespace rib {
diff --git a/rib/route.hpp b/rib/route.hpp
index cc4ff02..05449e2 100644
--- a/rib/route.hpp
+++ b/rib/route.hpp
@@ -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,
@@ -60,17 +60,15 @@
   }
 
   void
-  setExpirationEvent(const ndn::util::scheduler::EventId& eid, ndn::util::Scheduler& scheduler)
+  setExpirationEvent(const ndn::util::scheduler::EventId& eid)
   {
     m_expirationEvent = eid;
-    m_scheduler = &scheduler;
   }
 
   void
-  cancelExpirationEvent() const
+  cancelExpirationEvent()
   {
-    if (m_scheduler)
-      m_scheduler->cancelEvent(m_expirationEvent);
+    m_expirationEvent.cancel();
   }
 
   std::underlying_type<ndn::nfd::RouteFlags>::type
@@ -104,7 +102,6 @@
 
 private:
   ndn::util::scheduler::EventId m_expirationEvent;
-  ndn::util::Scheduler* m_scheduler;
 };
 
 bool
diff --git a/tests/core/scheduler.t.cpp b/tests/core/scheduler.t.cpp
index a2ad752..88adb03 100644
--- a/tests/core/scheduler.t.cpp
+++ b/tests/core/scheduler.t.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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -30,7 +30,6 @@
 #include <boost/thread.hpp>
 
 namespace nfd {
-
 namespace scheduler {
 // defined in scheduler.cpp
 Scheduler&
@@ -39,58 +38,28 @@
 
 namespace tests {
 
-using scheduler::EventId;
-using scheduler::ScopedEventId;
-
 BOOST_FIXTURE_TEST_SUITE(TestScheduler, BaseFixture)
 
-class SchedulerFixture : protected BaseFixture
+BOOST_AUTO_TEST_CASE(ScheduleCancel)
 {
-public:
-  SchedulerFixture()
-    : count1(0)
-    , count2(0)
-    , count3(0)
-  {
-  }
+  int count1 = 0, count2 = 0, count3 = 0;
 
-  void
-  event1()
-  {
+  scheduler::schedule(500_ms, [&] {
     BOOST_CHECK_EQUAL(count3, 1);
     ++count1;
-  }
+  });
 
-  void
-  event2()
-  {
-    ++count2;
-  }
+  scheduler::EventId eid = scheduler::schedule(1_s, [&] { ++count2; });
+  scheduler::cancel(eid);
 
-  void
-  event3()
-  {
+  scheduler::schedule(250_ms, [&] {
     BOOST_CHECK_EQUAL(count1, 0);
     ++count3;
-  }
+  });
 
-public:
-  int count1;
-  int count2;
-  int count3;
-};
-
-BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
-{
-  scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
-
-  EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
-  scheduler::cancel(i);
-
-  scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
-
-  i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
-  scheduler::cancel(i);
+  {
+    scheduler::ScopedEventId se = scheduler::schedule(50_ms, [&] { ++count2; });
+  } // se goes out of scope, canceling the event
 
   g_io.run();
 
@@ -99,87 +68,11 @@
   BOOST_CHECK_EQUAL(count3, 1);
 }
 
-BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
-{
-  EventId i;
-  scheduler::cancel(i);
-
-  // Trivial check to avoid "test case did not check any assertions" message from Boost.Test
-  BOOST_CHECK(true);
-}
-
-class SelfCancelFixture : protected BaseFixture
-{
-public:
-  void
-  cancelSelf() const
-  {
-    scheduler::cancel(m_selfEventId);
-  }
-
-public:
-  EventId m_selfEventId;
-};
-
-BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
-{
-  m_selfEventId = scheduler::schedule(time::milliseconds(100),
-                                      bind(&SelfCancelFixture::cancelSelf, this));
-
-  BOOST_REQUIRE_NO_THROW(g_io.run());
-}
-
-BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture)
-{
-  int hit = 0;
-  {
-    ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
-  } // se goes out of scope
-  this->advanceClocks(time::milliseconds(1), 15);
-  BOOST_CHECK_EQUAL(hit, 0);
-}
-
-BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture)
-{
-  int hit1 = 0, hit2 = 0;
-  ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; });
-  se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; });
-  this->advanceClocks(time::milliseconds(1), 15);
-  BOOST_CHECK_EQUAL(hit1, 0);
-  BOOST_CHECK_EQUAL(hit2, 1);
-}
-
-BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture)
-{
-  int hit = 0;
-  {
-    ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
-    se.release();
-  } // se goes out of scope
-  this->advanceClocks(time::milliseconds(1), 15);
-  BOOST_CHECK_EQUAL(hit, 1);
-}
-
-BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture)
-{
-  int hit = 0;
-  unique_ptr<scheduler::ScopedEventId> se2;
-  {
-    ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
-    se2.reset(new ScopedEventId(std::move(se)));
-  } // se goes out of scope
-  this->advanceClocks(time::milliseconds(1), 15);
-  BOOST_CHECK_EQUAL(hit, 1);
-}
-
 BOOST_AUTO_TEST_CASE(ThreadLocalScheduler)
 {
   scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler();
   scheduler::Scheduler* s2 = nullptr;
-  boost::thread t([&s2] {
-      s2 = &scheduler::getGlobalScheduler();
-    });
-
+  boost::thread t([&s2] { s2 = &scheduler::getGlobalScheduler(); });
   t.join();
 
   BOOST_CHECK(s1 != nullptr);
diff --git a/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp b/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
index 48164bd..4967788 100644
--- a/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp
+++ b/tests/rib/readvertise/nfd-rib-readvertise-destination.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,
@@ -122,7 +122,7 @@
 {
   Scenario scenario;
   Name prefix("/ndn/memphis/test");
-  ReadvertisedRoute rr(prefix, scheduler);
+  ReadvertisedRoute rr(prefix);
   const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
 
   dest.advertise(rr, successCallback, failureCallback);
@@ -200,7 +200,7 @@
 {
   Scenario scenario;
   Name prefix("/ndn/memphis/test");
-  ReadvertisedRoute rr(prefix, scheduler);
+  ReadvertisedRoute rr(prefix);
   const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
 
   dest.withdraw(rr, successCallback, failureCallback);
diff --git a/tools/ndn-autoconfig/main.cpp b/tools/ndn-autoconfig/main.cpp
index 6a2a03f..a8b2c45 100644
--- a/tools/ndn-autoconfig/main.cpp
+++ b/tools/ndn-autoconfig/main.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2017,  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,
@@ -35,7 +35,6 @@
 #include <boost/program_options/variables_map.hpp>
 #include <ndn-cxx/net/network-monitor.hpp>
 #include <ndn-cxx/util/scheduler.hpp>
-#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
 #include <ndn-cxx/util/time.hpp>
 
 // suppress warning caused by boost::program_options::parse_config_file
@@ -88,7 +87,7 @@
   });
 
   util::Scheduler sched(proc.getIoService());
-  util::scheduler::ScopedEventId runEvt(sched);
+  util::scheduler::ScopedEventId runEvt;
   auto scheduleRerun = [&] (time::nanoseconds delay) {
     runEvt = sched.scheduleEvent(delay, [&] { proc.runOnce(); });
   };