blob: 23833f1aa1ecd8fe32d428e70cc2a7e328e4e95e [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/*
3 * Copyright (c) 2014-2018, 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)
35 : m_face(face)
36 , m_options(options)
37 , m_timeout(options.timeout)
38 , m_resultCode(ResultCode::TIMEOUT)
39{
Junxiao Shib54aabd2018-04-16 19:36:24 +000040 if (m_timeout < 0_ms) {
41 m_timeout = m_options.interestLifetime < 0_ms ?
Zhuo Lib3558892016-08-12 15:51:12 -070042 DEFAULT_INTEREST_LIFETIME : m_options.interestLifetime;
43 }
44}
45
46time::milliseconds
47NdnPeek::getTimeout() const
48{
49 return m_timeout;
50}
51
52ResultCode
53NdnPeek::getResultCode() const
54{
55 return m_resultCode;
56}
57
58void
59NdnPeek::start()
60{
61 m_face.expressInterest(createInterest(),
62 bind(&NdnPeek::onData, this, _2),
63 bind(&NdnPeek::onNack, this, _2),
64 nullptr);
65 m_expressInterestTime = time::steady_clock::now();
66}
67
68Interest
69NdnPeek::createInterest() const
70{
Junxiao Shib54aabd2018-04-16 19:36:24 +000071 Interest interest(m_options.name);
72 interest.setCanBePrefix(m_options.canBePrefix);
73 interest.setMustBeFresh(m_options.mustBeFresh);
74 if (m_options.link != nullptr) {
Junxiao Shia5b60cc2017-07-26 01:35:48 +000075 interest.setForwardingHint(m_options.link->getDelegationList());
Junxiao Shib54aabd2018-04-16 19:36:24 +000076 }
77 if (m_options.interestLifetime >= 0_ms) {
78 interest.setInterestLifetime(m_options.interestLifetime);
79 }
Zhuo Lib3558892016-08-12 15:51:12 -070080
81 if (m_options.isVerbose) {
82 std::cerr << "INTEREST: " << interest << std::endl;
83 }
84
85 return interest;
86}
87
88void
89NdnPeek::onData(const Data& data)
90{
91 m_resultCode = ResultCode::DATA;
92
93 if (m_options.isVerbose) {
94 std::cerr << "DATA, RTT: "
95 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
96 << "ms" << std::endl;
97 }
98
99 if (m_options.wantPayloadOnly) {
100 const Block& block = data.getContent();
101 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
102 }
103 else {
104 const Block& block = data.wireEncode();
105 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
106 }
107}
108
109void
110NdnPeek::onNack(const lp::Nack& nack)
111{
112 m_resultCode = ResultCode::NACK;
113 lp::NackHeader header = nack.getHeader();
114
115 if (m_options.isVerbose) {
116 std::cerr << "NACK, RTT: "
117 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
118 << "ms" << std::endl;
119 }
120
121 if (m_options.wantPayloadOnly) {
122 std::cout << header.getReason() << std::endl;
123 }
124 else {
125 const Block& block = header.wireEncode();
126 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
127 }
128}
129
130} // namespace peek
131} // namespace ndn