blob: ea19585c8b2e1114d2a0166758b60906e902d583 [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
12const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
13const time::Duration LimitedIo::UNLIMITED_TIME = time::nanoseconds(-1);
14
15LimitedIo::LimitedIo()
16 : m_isRunning(false)
17 , m_nOpsRemaining(0)
18{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070019}
20
21LimitedIo::StopReason
22LimitedIo::run(int nOpsLimit, time::Duration nTimeLimit)
23{
24 BOOST_ASSERT(!m_isRunning);
25 m_isRunning = true;
26
27 m_reason = NO_WORK;
28 m_nOpsRemaining = nOpsLimit;
29 if (nTimeLimit != UNLIMITED_TIME) {
30 m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this));
31 }
32
33 getGlobalIoService().run();
34
35 getGlobalIoService().reset();
36 scheduler::cancel(m_timeout);
37 m_isRunning = false;
38 return m_reason;
39}
40
41void
42LimitedIo::afterOp()
43{
44 --m_nOpsRemaining;
45 if (m_nOpsRemaining <= 0) {
46 m_reason = EXCEED_OPS;
47 getGlobalIoService().stop();
48 }
49}
50
51void
52LimitedIo::afterTimeout()
53{
54 m_reason = EXCEED_TIME;
55 getGlobalIoService().stop();
56}
57
Junxiao Shid9ee45c2014-02-27 15:38:11 -070058} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070059} // namespace nfd