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 | /* |
Junxiao Shi | fd2d101 | 2022-01-11 18:20:38 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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> |
Davide Pesavento | 9602823 | 2019-08-17 01:26:13 -0400 | [diff] [blame] | 27 | * @author Davide Pesavento <davidepesa@gmail.com> |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include "ndnpeek.hpp" |
| 31 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 32 | namespace ndn::peek { |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 33 | |
| 34 | NdnPeek::NdnPeek(Face& face, const PeekOptions& options) |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 35 | : m_options(options) |
| 36 | , m_face(face) |
| 37 | , m_scheduler(m_face.getIoService()) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 38 | { |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
| 42 | NdnPeek::start() |
| 43 | { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 44 | 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 Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | Interest |
| 60 | NdnPeek::createInterest() const |
| 61 | { |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 62 | Interest interest(m_options.name); |
| 63 | interest.setCanBePrefix(m_options.canBePrefix); |
| 64 | interest.setMustBeFresh(m_options.mustBeFresh); |
Junxiao Shi | fd2d101 | 2022-01-11 18:20:38 +0000 | [diff] [blame] | 65 | interest.setForwardingHint(m_options.forwardingHint); |
Davide Pesavento | c214e07 | 2019-08-17 01:33:28 -0400 | [diff] [blame] | 66 | interest.setInterestLifetime(m_options.interestLifetime); |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame^] | 67 | if (m_options.hopLimit) { |
| 68 | interest.setHopLimit(*m_options.hopLimit); |
| 69 | } |
Davide Pesavento | 9602823 | 2019-08-17 01:26:13 -0400 | [diff] [blame] | 70 | if (m_options.applicationParameters) { |
| 71 | interest.setApplicationParameters(m_options.applicationParameters); |
| 72 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 73 | |
| 74 | if (m_options.isVerbose) { |
| 75 | std::cerr << "INTEREST: " << interest << std::endl; |
| 76 | } |
| 77 | |
| 78 | return interest; |
| 79 | } |
| 80 | |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame^] | 81 | static void |
| 82 | writeToCout(span<const uint8_t> bytes) |
| 83 | { |
| 84 | std::cout.write(reinterpret_cast<const char*>(bytes.data()), bytes.size()); |
| 85 | } |
| 86 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 87 | void |
| 88 | NdnPeek::onData(const Data& data) |
| 89 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 90 | m_result = Result::DATA; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 91 | m_timeoutEvent.cancel(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 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: " |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 95 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).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) { |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame^] | 100 | writeToCout(data.getContent().value_bytes()); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 101 | } |
| 102 | else { |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame^] | 103 | writeToCout(data.wireEncode()); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | NdnPeek::onNack(const lp::Nack& nack) |
| 109 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 110 | m_result = Result::NACK; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 111 | m_timeoutEvent.cancel(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 112 | |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame^] | 113 | const auto& header = nack.getHeader(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 114 | if (m_options.isVerbose) { |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame] | 115 | std::cerr << "NACK: " << header.getReason() << "\nRTT: " |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 116 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count() |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame] | 117 | << " ms" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (m_options.wantPayloadOnly) { |
| 121 | std::cout << header.getReason() << std::endl; |
| 122 | } |
| 123 | else { |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame^] | 124 | writeToCout(header.wireEncode()); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 128 | void |
| 129 | NdnPeek::onTimeout() |
| 130 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 131 | m_result = Result::TIMEOUT; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 132 | m_timeoutEvent.cancel(); |
| 133 | |
| 134 | if (m_options.isVerbose) { |
| 135 | std::cerr << "TIMEOUT" << std::endl; |
| 136 | } |
| 137 | } |
| 138 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 139 | } // namespace ndn::peek |