| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /** |
| * Copyright (c) 2014-2016, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| * |
| * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| /** |
| * Copyright (c) 2014, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| * |
| * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| */ |
| |
| #include "core/version.hpp" |
| #include "core/common.hpp" |
| |
| #include <ndn-cxx/util/io.hpp> |
| |
| namespace ndn { |
| namespace peek { |
| |
| class NdnPeek : boost::noncopyable |
| { |
| public: |
| explicit |
| NdnPeek(char* programName) |
| : isVerbose(false) |
| , mustBeFresh(false) |
| , wantRightmostChild(false) |
| , wantPayloadOnly(false) |
| , m_programName(programName) |
| , m_minSuffixComponents(-1) |
| , m_maxSuffixComponents(-1) |
| , m_interestLifetime(-1) |
| , m_timeout(-1) |
| , m_prefixName("") |
| , m_didReceiveData(false) |
| { |
| } |
| |
| void |
| usage(std::ostream& os ,const boost::program_options::options_description& options) const |
| { |
| os << "Usage: " << m_programName << " [options] ndn:/name\n" |
| " Fetch one data item matching the name prefix and write it to standard output\n" |
| "\n" |
| << options; |
| } |
| |
| void |
| setMinSuffixComponents(int minSuffixComponents) |
| { |
| if (minSuffixComponents < 0) |
| throw std::out_of_range("'minSuffixComponents' must be a non-negative integer"); |
| |
| m_minSuffixComponents = minSuffixComponents; |
| } |
| |
| void |
| setMaxSuffixComponents(int maxSuffixComponents) |
| { |
| if (maxSuffixComponents < 0) |
| throw std::out_of_range("'maxSuffixComponents' must be a non-negative integer"); |
| |
| m_maxSuffixComponents = maxSuffixComponents; |
| } |
| |
| void |
| setInterestLifetime(int interestLifetime) |
| { |
| if (interestLifetime < 0) |
| throw std::out_of_range("'lifetime' must be a non-negative integer"); |
| |
| m_interestLifetime = time::milliseconds(interestLifetime); |
| } |
| |
| void |
| setTimeout(int timeout) |
| { |
| if (timeout < 0) |
| throw std::out_of_range("'timeout' must be a non-negative integer"); |
| |
| m_timeout = time::milliseconds(timeout); |
| } |
| |
| void |
| setPrefixName(const std::string& prefixName) |
| { |
| m_prefixName = prefixName; |
| } |
| |
| time::milliseconds |
| getDefaultInterestLifetime() |
| { |
| return time::seconds(4); |
| } |
| |
| Interest |
| createInterestPacket() |
| { |
| Name interestName(m_prefixName); |
| Interest interestPacket(interestName); |
| |
| if (mustBeFresh) |
| interestPacket.setMustBeFresh(true); |
| |
| if (wantRightmostChild) |
| interestPacket.setChildSelector(1); |
| |
| if (m_minSuffixComponents >= 0) |
| interestPacket.setMinSuffixComponents(m_minSuffixComponents); |
| |
| if (m_maxSuffixComponents >= 0) |
| interestPacket.setMaxSuffixComponents(m_maxSuffixComponents); |
| |
| if (m_interestLifetime < time::milliseconds::zero()) |
| interestPacket.setInterestLifetime(getDefaultInterestLifetime()); |
| else |
| interestPacket.setInterestLifetime(m_interestLifetime); |
| |
| if (isVerbose) { |
| std::cerr << "INTEREST: " << interestPacket << std::endl; |
| } |
| |
| return interestPacket; |
| } |
| |
| void |
| onData(const Interest& interest, Data& data) |
| { |
| m_didReceiveData = true; |
| |
| if (isVerbose) { |
| std::cerr << "DATA, RTT: " |
| << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count() |
| << "ms" << std::endl; |
| } |
| |
| if (wantPayloadOnly) { |
| const Block& block = data.getContent(); |
| std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| } |
| else { |
| const Block& block = data.wireEncode(); |
| std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size()); |
| } |
| } |
| |
| void |
| onTimeout(const Interest& interest) |
| { |
| } |
| |
| int |
| run() |
| { |
| try { |
| m_face.expressInterest(createInterestPacket(), |
| bind(&NdnPeek::onData, this, _1, _2), |
| bind(&NdnPeek::onTimeout, this, _1)); |
| m_expressInterestTime = time::steady_clock::now(); |
| if (m_timeout < time::milliseconds::zero()) { |
| m_timeout = m_interestLifetime < time::milliseconds::zero() ? |
| getDefaultInterestLifetime() : m_interestLifetime; |
| } |
| m_face.processEvents(m_timeout); |
| } |
| catch (const std::exception& e) { |
| std::cerr << "ERROR: " << e.what() << std::endl; |
| return 1; |
| } |
| |
| if (isVerbose && !m_didReceiveData) { |
| std::cerr << "TIMEOUT" << std::endl; |
| return 3; |
| } |
| |
| return 0; |
| } |
| |
| public: |
| bool isVerbose; |
| bool mustBeFresh; |
| bool wantRightmostChild; |
| bool wantPayloadOnly; |
| |
| private: |
| std::string m_programName; |
| int m_minSuffixComponents; |
| int m_maxSuffixComponents; |
| time::milliseconds m_interestLifetime; |
| time::milliseconds m_timeout; |
| std::string m_prefixName; |
| time::steady_clock::TimePoint m_expressInterestTime; |
| bool m_didReceiveData; |
| Face m_face; |
| }; |
| |
| int |
| main(int argc, char* argv[]) |
| { |
| NdnPeek program(argv[0]); |
| |
| namespace po = boost::program_options; |
| |
| po::options_description visibleOptDesc("Allowed options"); |
| visibleOptDesc.add_options() |
| ("help,h", "print help and exit") |
| ("version,V", "print version and exit") |
| ("fresh,f", po::bool_switch(&program.mustBeFresh), |
| "set MustBeFresh") |
| ("rightmost,r", po::bool_switch(&program.wantRightmostChild), |
| "set ChildSelector to rightmost") |
| ("minsuffix,m", po::value<int>()->notifier(bind(&NdnPeek::setMinSuffixComponents, &program, _1)), |
| "set MinSuffixComponents") |
| ("maxsuffix,M", po::value<int>()->notifier(bind(&NdnPeek::setMaxSuffixComponents, &program, _1)), |
| "set MaxSuffixComponents") |
| ("lifetime,l", po::value<int>()->notifier(bind(&NdnPeek::setInterestLifetime, &program, _1)), |
| "set InterestLifetime (in milliseconds)") |
| ("payload,p", po::bool_switch(&program.wantPayloadOnly), |
| "print payload only, instead of full packet") |
| ("timeout,w", po::value<int>()->notifier(bind(&NdnPeek::setTimeout, &program, _1)), |
| "set timeout (in milliseconds)") |
| ("verbose,v", po::bool_switch(&program.isVerbose), |
| "turn on verbose output") |
| ; |
| |
| po::options_description hiddenOptDesc("Hidden options"); |
| hiddenOptDesc.add_options() |
| ("prefix", po::value<std::string>(), "Interest name"); |
| |
| po::options_description optDesc("Allowed options"); |
| optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| |
| po::positional_options_description optPos; |
| optPos.add("prefix", -1); |
| |
| try { |
| po::variables_map vm; |
| po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| po::notify(vm); |
| |
| if (vm.count("help") > 0) { |
| program.usage(std::cout, visibleOptDesc); |
| return 0; |
| } |
| |
| if (vm.count("version") > 0) { |
| std::cout << "ndnpeek " << tools::VERSION << std::endl; |
| return 0; |
| } |
| |
| if (vm.count("prefix") > 0) { |
| std::string prefixName = vm["prefix"].as<std::string>(); |
| program.setPrefixName(prefixName); |
| } |
| else { |
| throw std::runtime_error("Required argument 'prefix' is missing"); |
| } |
| } |
| catch (const std::exception& e) { |
| std::cerr << "ERROR: " << e.what() << std::endl; |
| program.usage(std::cerr, visibleOptDesc); |
| return 2; |
| } |
| |
| return program.run(); |
| } |
| |
| } // namespace peek |
| } // namespace ndn |
| |
| int |
| main(int argc, char** argv) |
| { |
| return ndn::peek::main(argc, argv); |
| } |