blob: d6280c9c79e7ae930488e0c2734ba3d2bb01df8d [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi82e7f582014-09-07 15:15:40 -07003 * 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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070025
26#include "limited-io.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070028
29namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070031
Junxiao Shi7e2413b2014-03-02 11:15:09 -070032NFD_LOG_INIT("LimitedIo");
33
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070034const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070035const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036
37LimitedIo::LimitedIo()
38 : m_isRunning(false)
39 , m_nOpsRemaining(0)
40{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070041}
42
43LimitedIo::StopReason
Junxiao Shi455581d2014-11-17 18:38:40 -070044LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045{
46 BOOST_ASSERT(!m_isRunning);
Junxiao Shi82e7f582014-09-07 15:15:40 -070047
48 if (nOpsLimit <= 0) {
49 return EXCEED_OPS;
50 }
51
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070052 m_isRunning = true;
Junxiao Shi98e29f42014-03-31 10:27:26 -070053
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070054 m_reason = NO_WORK;
55 m_nOpsRemaining = nOpsLimit;
Junxiao Shi455581d2014-11-17 18:38:40 -070056 if (timeLimit >= time::nanoseconds::zero()) {
57 m_timeout = scheduler::schedule(timeLimit, bind(&LimitedIo::afterTimeout, this));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070058 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070059
Junxiao Shi7e2413b2014-03-02 11:15:09 -070060 try {
61 getGlobalIoService().run();
62 }
63 catch (std::exception& ex) {
64 m_reason = EXCEPTION;
65 NFD_LOG_ERROR("g_io.run() exception: " << ex.what());
66 m_lastException = ex;
67 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070068
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070069 getGlobalIoService().reset();
70 scheduler::cancel(m_timeout);
71 m_isRunning = false;
72 return m_reason;
73}
74
75void
76LimitedIo::afterOp()
77{
Junxiao Shi82e7f582014-09-07 15:15:40 -070078 if (!m_isRunning) {
79 // Do not proceed further if .afterOp() is invoked out of .run(),
80 // because io_service.stop() without io_service.reset() would leave it unusable.
81 return;
82 }
83
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070084 --m_nOpsRemaining;
85 if (m_nOpsRemaining <= 0) {
86 m_reason = EXCEED_OPS;
87 getGlobalIoService().stop();
88 }
89}
90
91void
92LimitedIo::afterTimeout()
93{
94 m_reason = EXCEED_TIME;
95 getGlobalIoService().stop();
96}
97
Junxiao Shi7e2413b2014-03-02 11:15:09 -070098const std::exception&
99LimitedIo::getLastException() const
100{
101 return m_lastException;
102}
103
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700104} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700105} // namespace nfd