[ndnSIM] core: Partial emulation of boost::asio::io_service for NS-3
This commit assumes that ndn::Scheduler is redirected to ns3::Simulator
routines inside the ndn-cxx library.
Change-Id: Ib987f09f76a5d2103365312261fc5a2eba7024d2
diff --git a/core/global-io.cpp b/core/global-io.cpp
index 48566f6..ddd9ff9 100644
--- a/core/global-io.cpp
+++ b/core/global-io.cpp
@@ -24,33 +24,31 @@
*/
#include "global-io.hpp"
-#include <boost/thread/tss.hpp>
+#include "scheduler.hpp"
namespace nfd {
-namespace scheduler {
-// defined in scheduler.cpp
+namespace detail {
+
void
-resetGlobalScheduler();
-} // namespace scheduler
-
-
-static boost::thread_specific_ptr<boost::asio::io_service> g_ioService;
-
-boost::asio::io_service&
-getGlobalIoService()
+SimulatorIo::post(const std::function<void()>& callback)
{
- if (g_ioService.get() == nullptr) {
- g_ioService.reset(new boost::asio::io_service());
- }
- return *g_ioService;
+ scheduler::schedule(time::seconds(0), callback);
}
void
-resetGlobalIoService()
+SimulatorIo::dispatch(const std::function<void()>& callback)
{
- scheduler::resetGlobalScheduler();
- g_ioService.reset();
+ scheduler::schedule(time::seconds(0), callback);
+}
+
+} // namespace detail
+
+detail::SimulatorIo&
+getGlobalIoService()
+{
+ static detail::SimulatorIo io;
+ return io;
}
} // namespace nfd
diff --git a/core/global-io.hpp b/core/global-io.hpp
index d5030ab..6a76c57 100644
--- a/core/global-io.hpp
+++ b/core/global-io.hpp
@@ -29,19 +29,27 @@
namespace nfd {
-/** \return the global io_service instance
- */
-boost::asio::io_service&
-getGlobalIoService();
+namespace detail {
-#ifdef WITH_TESTS
-/** \brief delete the global io_service instance
- *
- * It will be recreated at the next invocation of getGlobalIoService.
+/**
+ * @brief Simulator-based IO that implements a few interfaces from boost::asio::io_service
*/
-void
-resetGlobalIoService();
-#endif
+class SimulatorIo
+{
+public:
+ void
+ post(const std::function<void()>& callback);
+
+ void
+ dispatch(const std::function<void()>& callback);
+};
+
+} // namespace detail
+
+/** \return Simulator-based IO object
+ */
+detail::SimulatorIo&
+getGlobalIoService();
} // namespace nfd
diff --git a/core/scheduler.cpp b/core/scheduler.cpp
index d0b0f2b..fd310eb 100644
--- a/core/scheduler.cpp
+++ b/core/scheduler.cpp
@@ -36,8 +36,9 @@
Scheduler&
getGlobalScheduler()
{
+ static ndn::DummyIoService io;
if (g_scheduler.get() == nullptr) {
- g_scheduler.reset(new Scheduler(getGlobalIoService()));
+ g_scheduler.reset(new Scheduler(io));
}
return *g_scheduler;
diff --git a/core/scheduler.hpp b/core/scheduler.hpp
index 491408a..5bdcf22 100644
--- a/core/scheduler.hpp
+++ b/core/scheduler.hpp
@@ -52,6 +52,9 @@
void
cancel(const EventId& eventId);
+Scheduler&
+getGlobalScheduler();
+
/** \brief Cancels an event automatically upon destruction.
*/
class ScopedEventId : noncopyable