blob: b3816969313381a5591cc5603320f9fa84825d7f [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();
16const time::Duration LimitedIo::UNLIMITED_TIME = time::nanoseconds(-1);
17
18LimitedIo::LimitedIo()
19 : m_isRunning(false)
20 , m_nOpsRemaining(0)
21{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070022}
23
24LimitedIo::StopReason
25LimitedIo::run(int nOpsLimit, time::Duration nTimeLimit)
26{
27 BOOST_ASSERT(!m_isRunning);
28 m_isRunning = true;
29
30 m_reason = NO_WORK;
31 m_nOpsRemaining = nOpsLimit;
32 if (nTimeLimit != UNLIMITED_TIME) {
33 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