Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 9 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 10 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 11 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 12 | NFD_LOG_INIT("LimitedIo"); |
| 13 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 14 | const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max(); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 15 | const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 16 | |
| 17 | LimitedIo::LimitedIo() |
| 18 | : m_isRunning(false) |
| 19 | , m_nOpsRemaining(0) |
| 20 | { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | LimitedIo::StopReason |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 24 | LimitedIo::run(int nOpsLimit, const time::nanoseconds& nTimeLimit) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 25 | { |
| 26 | BOOST_ASSERT(!m_isRunning); |
| 27 | m_isRunning = true; |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame^] | 28 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 29 | m_reason = NO_WORK; |
| 30 | m_nOpsRemaining = nOpsLimit; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 31 | if (nTimeLimit >= time::nanoseconds::zero()) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 32 | m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this)); |
| 33 | } |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame^] | 34 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 35 | 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 Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame^] | 43 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 44 | getGlobalIoService().reset(); |
| 45 | scheduler::cancel(m_timeout); |
| 46 | m_isRunning = false; |
| 47 | return m_reason; |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | LimitedIo::afterOp() |
| 52 | { |
| 53 | --m_nOpsRemaining; |
| 54 | if (m_nOpsRemaining <= 0) { |
| 55 | m_reason = EXCEED_OPS; |
| 56 | getGlobalIoService().stop(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | LimitedIo::afterTimeout() |
| 62 | { |
| 63 | m_reason = EXCEED_TIME; |
| 64 | getGlobalIoService().stop(); |
| 65 | } |
| 66 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 67 | const std::exception& |
| 68 | LimitedIo::getLastException() const |
| 69 | { |
| 70 | return m_lastException; |
| 71 | } |
| 72 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 73 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 74 | } // namespace nfd |