[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 9cbe734..00b1a14 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>
 
@@ -83,11 +82,13 @@
 }
 
 Scheduler::Scheduler(boost::asio::io_service& ioService)
-  : m_timer(make_unique<util::detail::SteadyTimer>(ioService))
 {
 }
 
-Scheduler::~Scheduler() = default;
+Scheduler::~Scheduler()
+{
+  cancelAllEvents();
+}
 
 EventId
 Scheduler::schedule(time::nanoseconds after, EventCallback callback)
@@ -113,7 +114,12 @@
   }
 
   if (info->queueIt == m_queue.begin()) {
-    m_timer->cancel();
+    if (m_timerEvent) {
+      if (!m_timerEvent->IsExpired()) {
+        ns3::Simulator::Remove(*m_timerEvent);
+      }
+      m_timerEvent.reset();
+    }
   }
   m_queue.erase(info->queueIt);
 
@@ -126,27 +132,29 @@
 Scheduler::cancelAllEvents()
 {
   m_queue.clear();
-  m_timer->cancel();
+  if (m_timerEvent) {
+    if (!m_timerEvent->IsExpired()) {
+      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 887cf97..927856e 100644
--- a/ndn-cxx/util/scheduler.hpp
+++ b/ndn-cxx/util/scheduler.hpp
@@ -26,17 +26,13 @@
 #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 util
-
 namespace scheduler {
 
 class Scheduler;
@@ -160,12 +156,9 @@
   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:
   class EventQueueCompare
@@ -178,8 +171,8 @@
   using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
   EventQueue m_queue;
 
-  unique_ptr<util::detail::SteadyTimer> m_timer;
   bool m_isEventExecuting = false;
+  ndn::optional<ns3::EventId> m_timerEvent;
 
   friend EventId;
   friend EventInfo;