fw: NccStrategy

refs #1242

Change-Id: I21f870728b7b361547adbde9d64357c93aeb1ed9
diff --git a/tests/core/limited-io.cpp b/tests/core/limited-io.cpp
new file mode 100644
index 0000000..5752685
--- /dev/null
+++ b/tests/core/limited-io.cpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "limited-io.hpp"
+
+namespace nfd {
+
+const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
+const time::Duration LimitedIo::UNLIMITED_TIME = time::nanoseconds(-1);
+
+LimitedIo::LimitedIo()
+  : m_isRunning(false)
+  , m_nOpsRemaining(0)
+{
+  resetGlobalIoService();
+}
+
+LimitedIo::StopReason
+LimitedIo::run(int nOpsLimit, time::Duration nTimeLimit)
+{
+  BOOST_ASSERT(!m_isRunning);
+  m_isRunning = true;
+  
+  m_reason = NO_WORK;
+  m_nOpsRemaining = nOpsLimit;
+  if (nTimeLimit != UNLIMITED_TIME) {
+    m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this));
+  }
+  
+  getGlobalIoService().run();
+  
+  getGlobalIoService().reset();
+  scheduler::cancel(m_timeout);
+  m_isRunning = false;
+  return m_reason;
+}
+
+void
+LimitedIo::afterOp()
+{
+  --m_nOpsRemaining;
+  if (m_nOpsRemaining <= 0) {
+    m_reason = EXCEED_OPS;
+    getGlobalIoService().stop();
+  }
+}
+
+void
+LimitedIo::afterTimeout()
+{
+  m_reason = EXCEED_TIME;
+  getGlobalIoService().stop();
+}
+
+} // namespace nfd
diff --git a/tests/core/limited-io.hpp b/tests/core/limited-io.hpp
new file mode 100644
index 0000000..14dadd7
--- /dev/null
+++ b/tests/core/limited-io.hpp
@@ -0,0 +1,49 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_TEST_CORE_LIMITED_IO_HPP
+#define NFD_TEST_CORE_LIMITED_IO_HPP
+
+#include "core/scheduler.hpp"
+
+namespace nfd {
+
+class LimitedIo
+{
+public:
+  LimitedIo();
+  
+  enum StopReason
+  {
+    NO_WORK,
+    EXCEED_OPS,
+    EXCEED_TIME
+  };
+  
+  StopReason
+  run(int nOpsLimit, time::Duration nTimeLimit);
+  
+  void
+  afterOp();
+  
+private:
+  void
+  afterTimeout();
+
+public:
+  static const int UNLIMITED_OPS;
+  static const time::Duration UNLIMITED_TIME;
+
+private:
+  bool m_isRunning;
+  int m_nOpsRemaining;
+  EventId m_timeout;
+  StopReason m_reason;
+};
+
+} // namespace nfd
+
+#endif // NFD_TEST_CORE_LIMITED_IO_HPP