[ndnSIM] util: Redirecting the scheduler of ndn-cxx to the scheduler of NS3

Compared to change in previous versions of ndnSIM, this commit includes
much less intrusive changes to the ndn-cxx scheduler, using
ns3::Scheduler only to scheduler a timer event (instead of boost timer
event).

Change-Id: Ie5b2cfe58a4da6d80f9843c922f9983ce332b33f
Refs: #4509
diff --git a/ndn-cxx/util/scheduler.cpp b/ndn-cxx/util/scheduler.cpp
index 36a662c..f2a8123 100644
--- a/ndn-cxx/util/scheduler.cpp
+++ b/ndn-cxx/util/scheduler.cpp
@@ -20,7 +20,6 @@
  */
 
 #include "ndn-cxx/util/scheduler.hpp"
-#include "ndn-cxx/util/impl/steady-timer.hpp"
 
 #include <boost/scope_exit.hpp>
 
@@ -91,12 +90,14 @@
 }
 
 Scheduler::Scheduler(boost::asio::io_service& ioService)
-  : m_timer(make_unique<detail::SteadyTimer>(ioService))
-  , m_isEventExecuting(false)
+  : m_isEventExecuting(false)
 {
 }
 
-Scheduler::~Scheduler() = default;
+Scheduler::~Scheduler()
+{
+  cancelAllEvents();
+}
 
 EventId
 Scheduler::scheduleEvent(time::nanoseconds after, const EventCallback& callback)
@@ -122,7 +123,10 @@
   }
 
   if (info->queueIt == m_queue.begin()) {
-    m_timer->cancel();
+    if (m_timerEvent) {
+      ns3::Simulator::Remove(*m_timerEvent);
+      m_timerEvent.reset();
+    }
   }
   m_queue.erase(info->queueIt);
 
@@ -135,27 +139,27 @@
 Scheduler::cancelAllEvents()
 {
   m_queue.clear();
-  m_timer->cancel();
+  if (m_timerEvent) {
+    ns3::Simulator::Remove(*m_timerEvent);
+    m_timerEvent.reset();
+  }
 }
 
 void
 Scheduler::scheduleNext()
 {
   if (!m_queue.empty()) {
-    m_timer->expires_from_now((*m_queue.begin())->expiresFromNow());
-    m_timer->async_wait([this] (const auto& error) { this->executeEvent(error); });
+    m_timerEvent = ns3::Simulator::Schedule(ns3::NanoSeconds((*m_queue.begin())->expiresFromNow().count()),
+                                            &Scheduler::executeEvent, this);
   }
 }
 
 void
-Scheduler::executeEvent(const boost::system::error_code& error)
+Scheduler::executeEvent()
 {
-  if (error) { // e.g., cancelled
-    return;
-  }
-
   m_isEventExecuting = true;
 
+  m_timerEvent.reset();
   BOOST_SCOPE_EXIT(this_) {
     this_->m_isEventExecuting = false;
     this_->scheduleNext();
diff --git a/ndn-cxx/util/scheduler.hpp b/ndn-cxx/util/scheduler.hpp
index 578d8ca..6163495 100644
--- a/ndn-cxx/util/scheduler.hpp
+++ b/ndn-cxx/util/scheduler.hpp
@@ -26,16 +26,12 @@
 #include "ndn-cxx/detail/cancel-handle.hpp"
 #include "ndn-cxx/util/time.hpp"
 
-#include <boost/system/error_code.hpp>
+#include "ns3/simulator.h"
+
 #include <set>
 
 namespace ndn {
 namespace util {
-
-namespace detail {
-class SteadyTimer;
-} // namespace detail
-
 namespace scheduler {
 
 class Scheduler;
@@ -190,17 +186,14 @@
   scheduleNext();
 
   /** \brief Execute expired events
-   *
-   *  If an event callback throws, the exception is propagated to the thread running the io_service.
-   *  In case there are other expired events, they will be processed in the next invocation.
    */
   void
-  executeEvent(const boost::system::error_code& code);
+  executeEvent();
 
 private:
-  unique_ptr<detail::SteadyTimer> m_timer;
   EventQueue m_queue;
   bool m_isEventExecuting;
+  ndn::optional<ns3::EventId> m_timerEvent;
 
   friend EventId;
 };