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) |
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); |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame^] | 65 | interest.setInterestLifetime(m_options.interestLifetime); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 66 | if (m_options.link) { |
Junxiao Shi | a5b60cc | 2017-07-26 01:35:48 +0000 | [diff] [blame] | 67 | interest.setForwardingHint(m_options.link->getDelegationList()); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 68 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 69 | |
| 70 | if (m_options.isVerbose) { |
| 71 | std::cerr << "INTEREST: " << interest << std::endl; |
| 72 | } |
| 73 | |
| 74 | return interest; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | NdnPeek::onData(const Data& data) |
| 79 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 80 | m_result = Result::DATA; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 81 | m_timeoutEvent.cancel(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 82 | |
| 83 | if (m_options.isVerbose) { |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame] | 84 | std::cerr << "DATA: " << data.getName() << "\nRTT: " |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 85 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count() |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame] | 86 | << " ms" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (m_options.wantPayloadOnly) { |
| 90 | const Block& block = data.getContent(); |
| 91 | std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 92 | } |
| 93 | else { |
| 94 | const Block& block = data.wireEncode(); |
| 95 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | NdnPeek::onNack(const lp::Nack& nack) |
| 101 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 102 | m_result = Result::NACK; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 103 | m_timeoutEvent.cancel(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 104 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 105 | lp::NackHeader header = nack.getHeader(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 106 | if (m_options.isVerbose) { |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame] | 107 | std::cerr << "NACK: " << header.getReason() << "\nRTT: " |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 108 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count() |
Jeff Thompson | 526ff43 | 2019-06-06 10:51:30 -0700 | [diff] [blame] | 109 | << " ms" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (m_options.wantPayloadOnly) { |
| 113 | std::cout << header.getReason() << std::endl; |
| 114 | } |
| 115 | else { |
| 116 | const Block& block = header.wireEncode(); |
| 117 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 118 | } |
| 119 | } |
| 120 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 121 | void |
| 122 | NdnPeek::onTimeout() |
| 123 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 124 | m_result = Result::TIMEOUT; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 125 | m_timeoutEvent.cancel(); |
| 126 | |
| 127 | if (m_options.isVerbose) { |
| 128 | std::cerr << "TIMEOUT" << std::endl; |
| 129 | } |
| 130 | } |
| 131 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 132 | } // namespace peek |
| 133 | } // namespace ndn |