Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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 | { |
| 40 | if (m_timeout < time::milliseconds::zero()) { |
| 41 | m_timeout = m_options.interestLifetime < time::milliseconds::zero() ? |
| 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 | { |
| 71 | Interest interest(m_options.prefix); |
| 72 | |
| 73 | if (m_options.minSuffixComponents >= 0) |
| 74 | interest.setMinSuffixComponents(m_options.minSuffixComponents); |
| 75 | |
| 76 | if (m_options.maxSuffixComponents >= 0) |
| 77 | interest.setMaxSuffixComponents(m_options.maxSuffixComponents); |
| 78 | |
| 79 | if (m_options.interestLifetime >= time::milliseconds::zero()) |
| 80 | interest.setInterestLifetime(m_options.interestLifetime); |
| 81 | |
| 82 | if (m_options.link != nullptr) |
| 83 | interest.setLink(m_options.link->wireEncode()); |
| 84 | |
| 85 | if (m_options.mustBeFresh) |
| 86 | interest.setMustBeFresh(true); |
| 87 | |
| 88 | if (m_options.wantRightmostChild) |
| 89 | interest.setChildSelector(1); |
| 90 | |
| 91 | if (m_options.isVerbose) { |
| 92 | std::cerr << "INTEREST: " << interest << std::endl; |
| 93 | } |
| 94 | |
| 95 | return interest; |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | NdnPeek::onData(const Data& data) |
| 100 | { |
| 101 | m_resultCode = ResultCode::DATA; |
| 102 | |
| 103 | if (m_options.isVerbose) { |
| 104 | std::cerr << "DATA, RTT: " |
| 105 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
| 106 | << "ms" << std::endl; |
| 107 | } |
| 108 | |
| 109 | if (m_options.wantPayloadOnly) { |
| 110 | const Block& block = data.getContent(); |
| 111 | std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 112 | } |
| 113 | else { |
| 114 | const Block& block = data.wireEncode(); |
| 115 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | NdnPeek::onNack(const lp::Nack& nack) |
| 121 | { |
| 122 | m_resultCode = ResultCode::NACK; |
| 123 | lp::NackHeader header = nack.getHeader(); |
| 124 | |
| 125 | if (m_options.isVerbose) { |
| 126 | std::cerr << "NACK, RTT: " |
| 127 | << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
| 128 | << "ms" << std::endl; |
| 129 | } |
| 130 | |
| 131 | if (m_options.wantPayloadOnly) { |
| 132 | std::cout << header.getReason() << std::endl; |
| 133 | } |
| 134 | else { |
| 135 | const Block& block = header.wireEncode(); |
| 136 | std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } // namespace peek |
| 141 | } // namespace ndn |