/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2023,  Regents of the University of California,
 *                           Arizona Board of Regents,
 *                           Colorado State University,
 *                           University Pierre & Marie Curie, Sorbonne University,
 *                           Washington University in St. Louis,
 *                           Beijing Institute of Technology,
 *                           The University of Memphis.
 *
 * This file is part of NFD (Named Data Networking Forwarding Daemon).
 * See AUTHORS.md for complete list of NFD authors and contributors.
 *
 * NFD is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tests/daemon/limited-io.hpp"
#include "tests/test-common.hpp"
#include "common/global.hpp"

#include <boost/exception/diagnostic_information.hpp>

namespace nfd::tests {

LimitedIo::LimitedIo(GlobalIoTimeFixture* fixture)
  : m_fixture(fixture)
{
}

LimitedIo::StopReason
LimitedIo::run(int nOpsLimit, time::nanoseconds timeLimit, time::nanoseconds tick)
{
  BOOST_ASSERT(!m_isRunning);

  if (nOpsLimit <= 0) {
    return EXCEED_OPS;
  }

  m_isRunning = true;

  m_reason = NO_WORK;
  m_nOpsRemaining = nOpsLimit;
  if (timeLimit >= 0_ns) {
    m_timeout = getScheduler().schedule(timeLimit, [this] { afterTimeout(); });
  }

  try {
    if (m_fixture == nullptr) {
      getGlobalIoService().run();
    }
    else {
      // timeLimit is enforced by afterTimeout
      m_fixture->advanceClocks(tick, time::nanoseconds::max());
    }
  }
  catch (const StopException&) {
  }
  catch (...) {
    BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information());
    m_reason = EXCEPTION;
    m_lastException = std::current_exception();
  }

  getGlobalIoService().restart();
  m_timeout.cancel();
  m_isRunning = false;

  return m_reason;
}

void
LimitedIo::afterOp()
{
  if (!m_isRunning) {
    // Do not proceed further if .afterOp() is invoked out of .run(),
    return;
  }

  --m_nOpsRemaining;

  if (m_nOpsRemaining <= 0) {
    m_reason = EXCEED_OPS;
    getGlobalIoService().stop();
    if (m_fixture != nullptr) {
      NDN_THROW(StopException());
    }
  }
}

void
LimitedIo::afterTimeout()
{
  m_reason = EXCEED_TIME;
  getGlobalIoService().stop();
  if (m_fixture != nullptr) {
    NDN_THROW(StopException());
  }
}

} // namespace nfd::tests
