[ndnSIM] util: Redirecting the scheduler of ndn-cxx to the scheduler of NS3
Change-Id: I82fdd94582a731c2238c6645e9ace21a2cacf2de
Refs: #4509
diff --git a/src/util/scheduler.hpp b/src/util/scheduler.hpp
index 79fbf23..2d669b1 100644
--- a/src/util/scheduler.hpp
+++ b/src/util/scheduler.hpp
@@ -26,6 +26,12 @@
#include "../net/asio-fwd.hpp"
#include <boost/system/error_code.hpp>
+#include "../common.hpp"
+
+#include "ns3/simulator.h"
+
+#include <set>
+#include <boost/asio/io_service.hpp>
#include <set>
namespace ndn {
@@ -37,85 +43,12 @@
namespace scheduler {
-/**
- * \brief Function to be invoked when a scheduled event expires
+typedef function<void()> EventCallback;
+
+/** \class EventId
+ * \brief Opaque type (shared_ptr) representing ID of a scheduled event
*/
-using EventCallback = std::function<void()>;
-
-/**
- * \brief Stores internal information about a scheduled event
- */
-class EventInfo;
-
-/**
- * \brief Identifies a scheduled event
- */
-class EventId
-{
-public:
- /**
- * \brief Constructs an empty EventId
- * \note EventId is implicitly convertible from nullptr.
- */
- EventId(std::nullptr_t = nullptr)
- {
- }
-
- /**
- * \retval true The event is valid.
- * \retval false This EventId is empty, or the event is expired or cancelled.
- */
- explicit
- operator bool() const;
-
- /**
- * \return whether this and other refer to the same event, or are both empty/expired/cancelled
- */
- bool
- operator==(const EventId& other) const;
-
- bool
- operator!=(const EventId& other) const
- {
- return !this->operator==(other);
- }
-
- /**
- * \brief clear this EventId
- * \note This does not cancel the event.
- * \post !(*this)
- */
- void
- reset()
- {
- m_info.reset();
- }
-
-private:
- explicit
- EventId(const weak_ptr<EventInfo>& info)
- : m_info(info)
- {
- }
-
-private:
- weak_ptr<EventInfo> m_info;
-
- friend class Scheduler;
- friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
-};
-
-std::ostream&
-operator<<(std::ostream& os, const EventId& eventId);
-
-class EventQueueCompare
-{
-public:
- bool
- operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const;
-};
-
-using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
+typedef std::shared_ptr<ns3::EventId> EventId;
/**
* \brief Generic scheduler
@@ -123,6 +56,11 @@
class Scheduler : noncopyable
{
public:
+ /**
+ * \deprecated use EventCallback
+ */
+ typedef EventCallback Event;
+
explicit
Scheduler(boost::asio::io_service& ioService);
@@ -133,7 +71,7 @@
* \return EventId that can be used to cancel the scheduled event
*/
EventId
- scheduleEvent(time::nanoseconds after, const EventCallback& callback);
+ scheduleEvent(const time::nanoseconds& after, const Event& event);
/**
* \brief Cancel a scheduled event
@@ -148,25 +86,36 @@
cancelAllEvents();
private:
- /**
- * \brief Schedule the next event on the deadline timer
- */
- void
- scheduleNext();
+ struct EventInfo
+ {
+ EventInfo(const time::nanoseconds& after, const Event& event);
- /**
- * \brief Execute expired events
- * \note 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 of this method.
- */
- void
- executeEvent(const boost::system::error_code& code);
+ EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
-private:
- unique_ptr<detail::SteadyTimer> m_timer;
- EventQueue m_queue;
- bool m_isEventExecuting;
+ bool
+ operator <=(const EventInfo& other) const
+ {
+ return this->m_scheduledTime <= other.m_scheduledTime;
+ }
+
+ bool
+ operator <(const EventInfo& other) const
+ {
+ return this->m_scheduledTime < other.m_scheduledTime;
+ }
+
+ time::nanoseconds
+ expiresFromNow() const;
+
+ time::steady_clock::TimePoint m_scheduledTime;
+ Event m_event;
+ mutable EventId m_eventId;
+ };
+
+ typedef std::multiset<EventId> EventQueue;
+
+ EventQueue m_events;
+ EventQueue::iterator m_scheduledEvent;
};
} // namespace scheduler