/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2019,  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 ndn-tools (Named Data Networking Essential Tools).
 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
 *
 * ndn-tools 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.
 *
 * ndn-tools 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
 * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
 * @author Zhuo Li <zhuoli@email.arizona.edu>
 */

#include "ndnpeek.hpp"

namespace ndn {
namespace peek {

NdnPeek::NdnPeek(Face& face, const PeekOptions& options)
  : m_options(options)
  , m_face(face)
  , m_scheduler(m_face.getIoService())
{
}

void
NdnPeek::start()
{
  m_pendingInterest = m_face.expressInterest(createInterest(),
                                             [this] (auto&&, const auto& data) { this->onData(data); },
                                             [this] (auto&&, const auto& nack) { this->onNack(nack); },
                                             [this] (auto&&) { this->onTimeout(); });

  if (m_options.timeout) {
    m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
      m_pendingInterest.cancel();
      onTimeout();
    });
  }

  m_sendTime = time::steady_clock::now();
}

Interest
NdnPeek::createInterest() const
{
  Interest interest(m_options.name);
  interest.setCanBePrefix(m_options.canBePrefix);
  interest.setMustBeFresh(m_options.mustBeFresh);
  if (m_options.link) {
    interest.setForwardingHint(m_options.link->getDelegationList());
  }
  if (m_options.interestLifetime) {
    interest.setInterestLifetime(*m_options.interestLifetime);
  }

  if (m_options.isVerbose) {
    std::cerr << "INTEREST: " << interest << std::endl;
  }

  return interest;
}

void
NdnPeek::onData(const Data& data)
{
  m_result = Result::DATA;
  m_timeoutEvent.cancel();

  if (m_options.isVerbose) {
    std::cerr << "DATA: " << data.getName() << "\nRTT: "
              << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
              << " ms" << std::endl;
  }

  if (m_options.wantPayloadOnly) {
    const Block& block = data.getContent();
    std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
  }
  else {
    const Block& block = data.wireEncode();
    std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
  }
}

void
NdnPeek::onNack(const lp::Nack& nack)
{
  m_result = Result::NACK;
  m_timeoutEvent.cancel();

  lp::NackHeader header = nack.getHeader();
  if (m_options.isVerbose) {
    std::cerr << "NACK: " << header.getReason() << "\nRTT: "
              << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
              << " ms" << std::endl;
  }

  if (m_options.wantPayloadOnly) {
    std::cout << header.getReason() << std::endl;
  }
  else {
    const Block& block = header.wireEncode();
    std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
  }
}

void
NdnPeek::onTimeout()
{
  m_result = Result::TIMEOUT;
  m_timeoutEvent.cancel();

  if (m_options.isVerbose) {
    std::cerr << "TIMEOUT" << std::endl;
  }
}

} // namespace peek
} // namespace ndn
