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