blob: 8ce23f6fe65eaf5c716c121fa1f8acfa442bb855 [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>
27 */
28
29#include "ndnpeek.hpp"
30
31namespace ndn {
32namespace peek {
33
34NdnPeek::NdnPeek(Face& face, const PeekOptions& options)
Davide Pesavento65d11552019-06-09 19:15:50 -040035 : m_options(options)
36 , m_face(face)
37 , m_scheduler(m_face.getIoService())
Zhuo Lib3558892016-08-12 15:51:12 -070038{
Zhuo Lib3558892016-08-12 15:51:12 -070039}
40
41void
42NdnPeek::start()
43{
Davide Pesavento65d11552019-06-09 19:15:50 -040044 m_pendingInterest = m_face.expressInterest(createInterest(),
45 [this] (auto&&, const auto& data) { this->onData(data); },
46 [this] (auto&&, const auto& nack) { this->onNack(nack); },
47 [this] (auto&&) { this->onTimeout(); });
48
49 if (m_options.timeout) {
50 m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
51 m_pendingInterest.cancel();
52 onTimeout();
53 });
54 }
55
56 m_sendTime = time::steady_clock::now();
Zhuo Lib3558892016-08-12 15:51:12 -070057}
58
59Interest
60NdnPeek::createInterest() const
61{
Junxiao Shib54aabd2018-04-16 19:36:24 +000062 Interest interest(m_options.name);
63 interest.setCanBePrefix(m_options.canBePrefix);
64 interest.setMustBeFresh(m_options.mustBeFresh);
Davide Pesavento65d11552019-06-09 19:15:50 -040065 if (m_options.link) {
Junxiao Shia5b60cc2017-07-26 01:35:48 +000066 interest.setForwardingHint(m_options.link->getDelegationList());
Junxiao Shib54aabd2018-04-16 19:36:24 +000067 }
Davide Pesavento65d11552019-06-09 19:15:50 -040068 if (m_options.interestLifetime) {
69 interest.setInterestLifetime(*m_options.interestLifetime);
Junxiao Shib54aabd2018-04-16 19:36:24 +000070 }
Zhuo Lib3558892016-08-12 15:51:12 -070071
72 if (m_options.isVerbose) {
73 std::cerr << "INTEREST: " << interest << std::endl;
74 }
75
76 return interest;
77}
78
79void
80NdnPeek::onData(const Data& data)
81{
82 m_resultCode = ResultCode::DATA;
Davide Pesavento65d11552019-06-09 19:15:50 -040083 m_timeoutEvent.cancel();
Zhuo Lib3558892016-08-12 15:51:12 -070084
85 if (m_options.isVerbose) {
Jeff Thompson526ff432019-06-06 10:51:30 -070086 std::cerr << "DATA: " << data.getName() << "\nRTT: "
Davide Pesavento65d11552019-06-09 19:15:50 -040087 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
Jeff Thompson526ff432019-06-06 10:51:30 -070088 << " ms" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -070089 }
90
91 if (m_options.wantPayloadOnly) {
92 const Block& block = data.getContent();
93 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
94 }
95 else {
96 const Block& block = data.wireEncode();
97 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
98 }
99}
100
101void
102NdnPeek::onNack(const lp::Nack& nack)
103{
104 m_resultCode = ResultCode::NACK;
Davide Pesavento65d11552019-06-09 19:15:50 -0400105 m_timeoutEvent.cancel();
Zhuo Lib3558892016-08-12 15:51:12 -0700106
Davide Pesavento65d11552019-06-09 19:15:50 -0400107 lp::NackHeader header = nack.getHeader();
Zhuo Lib3558892016-08-12 15:51:12 -0700108 if (m_options.isVerbose) {
Jeff Thompson526ff432019-06-06 10:51:30 -0700109 std::cerr << "NACK: " << header.getReason() << "\nRTT: "
Davide Pesavento65d11552019-06-09 19:15:50 -0400110 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
Jeff Thompson526ff432019-06-06 10:51:30 -0700111 << " ms" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700112 }
113
114 if (m_options.wantPayloadOnly) {
115 std::cout << header.getReason() << std::endl;
116 }
117 else {
118 const Block& block = header.wireEncode();
119 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
120 }
121}
122
Davide Pesavento65d11552019-06-09 19:15:50 -0400123void
124NdnPeek::onTimeout()
125{
126 m_resultCode = ResultCode::TIMEOUT;
127 m_timeoutEvent.cancel();
128
129 if (m_options.isVerbose) {
130 std::cerr << "TIMEOUT" << std::endl;
131 }
132}
133
Zhuo Lib3558892016-08-12 15:51:12 -0700134} // namespace peek
135} // namespace ndn