blob: 919ce1eee2f14193aec823457b5671029c613398 [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib54aabd2018-04-16 19:36:24 +00002/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Zhuo Lib3558892016-08-12 15:51:12 -07004 * 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.
10 *
11 * This file is part of ndn-tools (Named Data Networking Essential Tools).
12 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
13 *
14 * ndn-tools 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 * ndn-tools 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 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
26 * @author Zhuo Li <zhuoli@email.arizona.edu>
Davide Pesavento96028232019-08-17 01:26:13 -040027 * @author Davide Pesavento <davidepesa@gmail.com>
Zhuo Lib3558892016-08-12 15:51:12 -070028 */
29
30#include "ndnpeek.hpp"
31
Davide Pesavento5748e822024-01-26 18:40:22 -050032#include <iostream>
33
Davide Pesaventob3570c62022-02-19 19:19:00 -050034namespace ndn::peek {
Zhuo Lib3558892016-08-12 15:51:12 -070035
36NdnPeek::NdnPeek(Face& face, const PeekOptions& options)
Davide Pesavento65d11552019-06-09 19:15:50 -040037 : m_options(options)
38 , m_face(face)
Davide Pesavento7e9d7e42023-11-11 15:00:03 -050039 , m_scheduler(m_face.getIoContext())
Zhuo Lib3558892016-08-12 15:51:12 -070040{
Zhuo Lib3558892016-08-12 15:51:12 -070041}
42
43void
44NdnPeek::start()
45{
Davide Pesavento65d11552019-06-09 19:15:50 -040046 m_pendingInterest = m_face.expressInterest(createInterest(),
47 [this] (auto&&, const auto& data) { this->onData(data); },
48 [this] (auto&&, const auto& nack) { this->onNack(nack); },
49 [this] (auto&&) { this->onTimeout(); });
50
51 if (m_options.timeout) {
52 m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
53 m_pendingInterest.cancel();
54 onTimeout();
55 });
56 }
57
58 m_sendTime = time::steady_clock::now();
Zhuo Lib3558892016-08-12 15:51:12 -070059}
60
61Interest
62NdnPeek::createInterest() const
63{
Junxiao Shib54aabd2018-04-16 19:36:24 +000064 Interest interest(m_options.name);
65 interest.setCanBePrefix(m_options.canBePrefix);
66 interest.setMustBeFresh(m_options.mustBeFresh);
Junxiao Shifd2d1012022-01-11 18:20:38 +000067 interest.setForwardingHint(m_options.forwardingHint);
Davide Pesaventoc214e072019-08-17 01:33:28 -040068 interest.setInterestLifetime(m_options.interestLifetime);
Davide Pesavento242d5062022-03-11 16:34:23 -050069 if (m_options.hopLimit) {
70 interest.setHopLimit(*m_options.hopLimit);
71 }
Davide Pesavento96028232019-08-17 01:26:13 -040072 if (m_options.applicationParameters) {
73 interest.setApplicationParameters(m_options.applicationParameters);
74 }
Zhuo Lib3558892016-08-12 15:51:12 -070075
76 if (m_options.isVerbose) {
77 std::cerr << "INTEREST: " << interest << std::endl;
78 }
79
80 return interest;
81}
82
Davide Pesavento242d5062022-03-11 16:34:23 -050083static void
84writeToCout(span<const uint8_t> bytes)
85{
86 std::cout.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
87}
88
Zhuo Lib3558892016-08-12 15:51:12 -070089void
90NdnPeek::onData(const Data& data)
91{
Davide Pesavento87434be2019-07-25 19:04:23 -040092 m_result = Result::DATA;
Davide Pesavento65d11552019-06-09 19:15:50 -040093 m_timeoutEvent.cancel();
Zhuo Lib3558892016-08-12 15:51:12 -070094
95 if (m_options.isVerbose) {
Jeff Thompson526ff432019-06-06 10:51:30 -070096 std::cerr << "DATA: " << data.getName() << "\nRTT: "
Davide Pesavento65d11552019-06-09 19:15:50 -040097 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
Jeff Thompson526ff432019-06-06 10:51:30 -070098 << " ms" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -070099 }
100
101 if (m_options.wantPayloadOnly) {
Davide Pesavento242d5062022-03-11 16:34:23 -0500102 writeToCout(data.getContent().value_bytes());
Zhuo Lib3558892016-08-12 15:51:12 -0700103 }
104 else {
Davide Pesavento242d5062022-03-11 16:34:23 -0500105 writeToCout(data.wireEncode());
Zhuo Lib3558892016-08-12 15:51:12 -0700106 }
107}
108
109void
110NdnPeek::onNack(const lp::Nack& nack)
111{
Davide Pesavento87434be2019-07-25 19:04:23 -0400112 m_result = Result::NACK;
Davide Pesavento65d11552019-06-09 19:15:50 -0400113 m_timeoutEvent.cancel();
Zhuo Lib3558892016-08-12 15:51:12 -0700114
Davide Pesavento242d5062022-03-11 16:34:23 -0500115 const auto& header = nack.getHeader();
Zhuo Lib3558892016-08-12 15:51:12 -0700116 if (m_options.isVerbose) {
Jeff Thompson526ff432019-06-06 10:51:30 -0700117 std::cerr << "NACK: " << header.getReason() << "\nRTT: "
Davide Pesavento65d11552019-06-09 19:15:50 -0400118 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
Jeff Thompson526ff432019-06-06 10:51:30 -0700119 << " ms" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700120 }
121
122 if (m_options.wantPayloadOnly) {
123 std::cout << header.getReason() << std::endl;
124 }
125 else {
Davide Pesavento242d5062022-03-11 16:34:23 -0500126 writeToCout(header.wireEncode());
Zhuo Lib3558892016-08-12 15:51:12 -0700127 }
128}
129
Davide Pesavento65d11552019-06-09 19:15:50 -0400130void
131NdnPeek::onTimeout()
132{
Davide Pesavento87434be2019-07-25 19:04:23 -0400133 m_result = Result::TIMEOUT;
Davide Pesavento65d11552019-06-09 19:15:50 -0400134 m_timeoutEvent.cancel();
135
136 if (m_options.isVerbose) {
137 std::cerr << "TIMEOUT" << std::endl;
138 }
139}
140
Davide Pesaventob3570c62022-02-19 19:19:00 -0500141} // namespace ndn::peek