blob: cc9286c62d034c907a0c809fa625a5d17c8b856e [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/*
Jeff Thompson526ff432019-06-06 10:51:30 -07003 * Copyright (c) 2014-2019, 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
32namespace ndn {
33namespace peek {
34
35NdnPeek::NdnPeek(Face& face, const PeekOptions& options)
Davide Pesavento65d11552019-06-09 19:15:50 -040036 : m_options(options)
37 , m_face(face)
38 , m_scheduler(m_face.getIoService())
Zhuo Lib3558892016-08-12 15:51:12 -070039{
Zhuo Lib3558892016-08-12 15:51:12 -070040}
41
42void
43NdnPeek::start()
44{
Davide Pesavento65d11552019-06-09 19:15:50 -040045 m_pendingInterest = m_face.expressInterest(createInterest(),
46 [this] (auto&&, const auto& data) { this->onData(data); },
47 [this] (auto&&, const auto& nack) { this->onNack(nack); },
48 [this] (auto&&) { this->onTimeout(); });
49
50 if (m_options.timeout) {
51 m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
52 m_pendingInterest.cancel();
53 onTimeout();
54 });
55 }
56
57 m_sendTime = time::steady_clock::now();
Zhuo Lib3558892016-08-12 15:51:12 -070058}
59
60Interest
61NdnPeek::createInterest() const
62{
Junxiao Shib54aabd2018-04-16 19:36:24 +000063 Interest interest(m_options.name);
64 interest.setCanBePrefix(m_options.canBePrefix);
65 interest.setMustBeFresh(m_options.mustBeFresh);
Davide Pesavento65d11552019-06-09 19:15:50 -040066 if (m_options.link) {
Junxiao Shia5b60cc2017-07-26 01:35:48 +000067 interest.setForwardingHint(m_options.link->getDelegationList());
Junxiao Shib54aabd2018-04-16 19:36:24 +000068 }
Davide Pesaventoc214e072019-08-17 01:33:28 -040069 interest.setInterestLifetime(m_options.interestLifetime);
70 interest.setHopLimit(m_options.hopLimit);
Davide Pesavento96028232019-08-17 01:26:13 -040071 if (m_options.applicationParameters) {
72 interest.setApplicationParameters(m_options.applicationParameters);
73 }
Zhuo Lib3558892016-08-12 15:51:12 -070074
75 if (m_options.isVerbose) {
76 std::cerr << "INTEREST: " << interest << std::endl;
77 }
78
79 return interest;
80}
81
82void
83NdnPeek::onData(const Data& data)
84{
Davide Pesavento87434be2019-07-25 19:04:23 -040085 m_result = Result::DATA;
Davide Pesavento65d11552019-06-09 19:15:50 -040086 m_timeoutEvent.cancel();
Zhuo Lib3558892016-08-12 15:51:12 -070087
88 if (m_options.isVerbose) {
Jeff Thompson526ff432019-06-06 10:51:30 -070089 std::cerr << "DATA: " << data.getName() << "\nRTT: "
Davide Pesavento65d11552019-06-09 19:15:50 -040090 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
Jeff Thompson526ff432019-06-06 10:51:30 -070091 << " ms" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -070092 }
93
94 if (m_options.wantPayloadOnly) {
95 const Block& block = data.getContent();
96 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
97 }
98 else {
99 const Block& block = data.wireEncode();
100 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
101 }
102}
103
104void
105NdnPeek::onNack(const lp::Nack& nack)
106{
Davide Pesavento87434be2019-07-25 19:04:23 -0400107 m_result = Result::NACK;
Davide Pesavento65d11552019-06-09 19:15:50 -0400108 m_timeoutEvent.cancel();
Zhuo Lib3558892016-08-12 15:51:12 -0700109
Davide Pesavento65d11552019-06-09 19:15:50 -0400110 lp::NackHeader header = nack.getHeader();
Zhuo Lib3558892016-08-12 15:51:12 -0700111 if (m_options.isVerbose) {
Jeff Thompson526ff432019-06-06 10:51:30 -0700112 std::cerr << "NACK: " << header.getReason() << "\nRTT: "
Davide Pesavento65d11552019-06-09 19:15:50 -0400113 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
Jeff Thompson526ff432019-06-06 10:51:30 -0700114 << " ms" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700115 }
116
117 if (m_options.wantPayloadOnly) {
118 std::cout << header.getReason() << std::endl;
119 }
120 else {
121 const Block& block = header.wireEncode();
122 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
123 }
124}
125
Davide Pesavento65d11552019-06-09 19:15:50 -0400126void
127NdnPeek::onTimeout()
128{
Davide Pesavento87434be2019-07-25 19:04:23 -0400129 m_result = Result::TIMEOUT;
Davide Pesavento65d11552019-06-09 19:15:50 -0400130 m_timeoutEvent.cancel();
131
132 if (m_options.isVerbose) {
133 std::cerr << "TIMEOUT" << std::endl;
134 }
135}
136
Zhuo Lib3558892016-08-12 15:51:12 -0700137} // namespace peek
138} // namespace ndn