blob: a7b1115ccf9f9583c2dc6c33775a766505131b1a [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "limited-io.hpp"
8
9namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070010namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070011
Junxiao Shi7e2413b2014-03-02 11:15:09 -070012NFD_LOG_INIT("LimitedIo");
13
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070014const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070015const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070016
17LimitedIo::LimitedIo()
18 : m_isRunning(false)
19 , m_nOpsRemaining(0)
20{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070021}
22
23LimitedIo::StopReason
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070024LimitedIo::run(int nOpsLimit, const time::nanoseconds& nTimeLimit)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070025{
26 BOOST_ASSERT(!m_isRunning);
27 m_isRunning = true;
Junxiao Shi98e29f42014-03-31 10:27:26 -070028
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070029 m_reason = NO_WORK;
30 m_nOpsRemaining = nOpsLimit;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070031 if (nTimeLimit >= time::nanoseconds::zero()) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070032 m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this));
33 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070034
Junxiao Shi7e2413b2014-03-02 11:15:09 -070035 try {
36 getGlobalIoService().run();
37 }
38 catch (std::exception& ex) {
39 m_reason = EXCEPTION;
40 NFD_LOG_ERROR("g_io.run() exception: " << ex.what());
41 m_lastException = ex;
42 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070043
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070044 getGlobalIoService().reset();
45 scheduler::cancel(m_timeout);
46 m_isRunning = false;
47 return m_reason;
48}
49
50void
51LimitedIo::afterOp()
52{
53 --m_nOpsRemaining;
54 if (m_nOpsRemaining <= 0) {
55 m_reason = EXCEED_OPS;
56 getGlobalIoService().stop();
57 }
58}
59
60void
61LimitedIo::afterTimeout()
62{
63 m_reason = EXCEED_TIME;
64 getGlobalIoService().stop();
65}
66
Junxiao Shi7e2413b2014-03-02 11:15:09 -070067const std::exception&
68LimitedIo::getLastException() const
69{
70 return m_lastException;
71}
72
Junxiao Shid9ee45c2014-02-27 15:38:11 -070073} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070074} // namespace nfd