blob: 1eba63da3914d5cde1c5c2c60008942a1ee1c286 [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"
Junxiao Shi7e2413b2014-03-02 11:15:09 -07008#include "core/logger.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07009
10namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070011namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070012
Junxiao Shi7e2413b2014-03-02 11:15:09 -070013NFD_LOG_INIT("LimitedIo");
14
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070015const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070016const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070017
18LimitedIo::LimitedIo()
19 : m_isRunning(false)
20 , m_nOpsRemaining(0)
21{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070022}
23
24LimitedIo::StopReason
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070025LimitedIo::run(int nOpsLimit, const time::nanoseconds& nTimeLimit)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070026{
27 BOOST_ASSERT(!m_isRunning);
28 m_isRunning = true;
29
30 m_reason = NO_WORK;
31 m_nOpsRemaining = nOpsLimit;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070032 if (nTimeLimit >= time::nanoseconds::zero()) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070033 m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this));
34 }
35
Junxiao Shi7e2413b2014-03-02 11:15:09 -070036 try {
37 getGlobalIoService().run();
38 }
39 catch (std::exception& ex) {
40 m_reason = EXCEPTION;
41 NFD_LOG_ERROR("g_io.run() exception: " << ex.what());
42 m_lastException = ex;
43 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070044
45 getGlobalIoService().reset();
46 scheduler::cancel(m_timeout);
47 m_isRunning = false;
48 return m_reason;
49}
50
51void
52LimitedIo::afterOp()
53{
54 --m_nOpsRemaining;
55 if (m_nOpsRemaining <= 0) {
56 m_reason = EXCEED_OPS;
57 getGlobalIoService().stop();
58 }
59}
60
61void
62LimitedIo::afterTimeout()
63{
64 m_reason = EXCEED_TIME;
65 getGlobalIoService().stop();
66}
67
Junxiao Shi7e2413b2014-03-02 11:15:09 -070068const std::exception&
69LimitedIo::getLastException() const
70{
71 return m_lastException;
72}
73
Junxiao Shid9ee45c2014-02-27 15:38:11 -070074} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070075} // namespace nfd