Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 24 | |
| 25 | #include "limited-io.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | #include "core/logger.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 27 | |
| 28 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 31 | NFD_LOG_INIT("LimitedIo"); |
| 32 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 33 | const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max(); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 34 | const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 35 | |
| 36 | LimitedIo::LimitedIo() |
| 37 | : m_isRunning(false) |
| 38 | , m_nOpsRemaining(0) |
| 39 | { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | LimitedIo::StopReason |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 43 | LimitedIo::run(int nOpsLimit, const time::nanoseconds& nTimeLimit) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 44 | { |
| 45 | BOOST_ASSERT(!m_isRunning); |
| 46 | m_isRunning = true; |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 48 | m_reason = NO_WORK; |
| 49 | m_nOpsRemaining = nOpsLimit; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 50 | if (nTimeLimit >= time::nanoseconds::zero()) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 51 | m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this)); |
| 52 | } |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 54 | try { |
| 55 | getGlobalIoService().run(); |
| 56 | } |
| 57 | catch (std::exception& ex) { |
| 58 | m_reason = EXCEPTION; |
| 59 | NFD_LOG_ERROR("g_io.run() exception: " << ex.what()); |
| 60 | m_lastException = ex; |
| 61 | } |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 62 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 63 | getGlobalIoService().reset(); |
| 64 | scheduler::cancel(m_timeout); |
| 65 | m_isRunning = false; |
| 66 | return m_reason; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | LimitedIo::afterOp() |
| 71 | { |
| 72 | --m_nOpsRemaining; |
| 73 | if (m_nOpsRemaining <= 0) { |
| 74 | m_reason = EXCEED_OPS; |
| 75 | getGlobalIoService().stop(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | LimitedIo::afterTimeout() |
| 81 | { |
| 82 | m_reason = EXCEED_TIME; |
| 83 | getGlobalIoService().stop(); |
| 84 | } |
| 85 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 86 | const std::exception& |
| 87 | LimitedIo::getLastException() const |
| 88 | { |
| 89 | return m_lastException; |
| 90 | } |
| 91 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 92 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 93 | } // namespace nfd |