[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..7eb6a56 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()
+SimulatorIodispatch(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 177045f..725f81a 100644
--- a/core/scheduler.cpp
+++ b/core/scheduler.cpp
@@ -37,7 +37,7 @@
getGlobalScheduler()
{
if (g_scheduler.get() == nullptr) {
- g_scheduler.reset(new Scheduler(getGlobalIoService()));
+ g_scheduler.reset(new Scheduler(*static_cast<boost::asio::io_service*>(nullptr)));
}
return *g_scheduler;
diff --git a/core/scheduler.hpp b/core/scheduler.hpp
index b846c64..6e87199 100644
--- a/core/scheduler.hpp
+++ b/core/scheduler.hpp
@@ -49,6 +49,9 @@
void
cancel(const EventId& eventId);
+Scheduler&
+getGlobalScheduler();
+
/** \brief cancels an event automatically upon destruction
*/
class ScopedEventId : noncopyable