Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 2 | /* |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 4 | * 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 | |
| 31 | namespace ndn { |
| 32 | namespace peek { |
| 33 | |
| 34 | NdnPeek::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 Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 40 | if (m_timeout < 0_ms) { |
| 41 | m_timeout = m_options.interestLifetime < 0_ms ? |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 42 | DEFAULT_INTEREST_LIFETIME : m_options.interestLifetime; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | time::milliseconds |
| 47 | NdnPeek::getTimeout() const |
| 48 | { |
| 49 | return m_timeout; |
| 50 | } |
| 51 | |
| 52 | ResultCode |
| 53 | NdnPeek::getResultCode() const |
| 54 | { |
| 55 | return m_resultCode; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | NdnPeek::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 | |
| 68 | Interest |
| 69 | NdnPeek::createInterest() const |
| 70 | { |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 71 | Interest interest(m_options.name); |
| 72 | interest.setCanBePrefix(m_options.canBePrefix); |
| 73 | interest.setMustBeFresh(m_options.mustBeFresh); |
| 74 | if (m_options.link != nullptr) { |
Junxiao Shi | a5b60cc | 2017-07-26 01:35:48 +0000 | [diff] [blame] | 75 | interest.setForwardingHint(m_options.link->getDelegationList()); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 76 | } |
| 77 | if (m_options.interestLifetime >= 0_ms) { |
| 78 | interest.setInterestLifetime(m_options.interestLifetime); |
| 79 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 80 | |
| 81 | if (m_options.isVerbose) { |
| 82 | std::cerr << "INTEREST: " << interest << std::endl; |
| 83 | } |
| 84 | |
| 85 | return interest; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | NdnPeek::onData(const Data& data) |
| 90 | { |
| 91 | m_resultCode = ResultCode::DATA; |
| 92 | |
| 93 | if (m_options.isVerbose) { |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame^] | 94 | std::cerr << "DATA: " << data.getName() << "\nRTT: " |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 95 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame^] | 96 | << " ms" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 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 | |
| 109 | void |
| 110 | NdnPeek::onNack(const lp::Nack& nack) |
| 111 | { |
| 112 | m_resultCode = ResultCode::NACK; |
| 113 | lp::NackHeader header = nack.getHeader(); |
| 114 | |
| 115 | if (m_options.isVerbose) { |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame^] | 116 | std::cerr << "NACK: " << header.getReason() << "\nRTT: " |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 117 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame^] | 118 | << " ms" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 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 |