From fd2d1012c7294bfa5ba973f92a23338427075746 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Tue, 11 Jan 2022 18:20:38 +0000 Subject: [PATCH] ndnpeek: replace --link-file with --fwhint refs #4207, #5187 Change-Id: I8213e971520d3edab631634a03bf7950d265dc11 --- core/program-options-ext.cpp | 45 ++++++++++++++++++++++++++++++++++ core/program-options-ext.hpp | 42 +++++++++++++++++++++++++++++++ manpages/ndnpeek.rst | 5 ++-- tests/peek/ndnpeek.t.cpp | 30 +++++++++++++---------- tools/peek/ndnpeek/main.cpp | 29 ++++------------------ tools/peek/ndnpeek/ndnpeek.cpp | 6 ++--- tools/peek/ndnpeek/ndnpeek.hpp | 4 +-- 7 files changed, 116 insertions(+), 45 deletions(-) create mode 100644 core/program-options-ext.cpp create mode 100644 core/program-options-ext.hpp diff --git a/core/program-options-ext.cpp b/core/program-options-ext.cpp new file mode 100644 index 0000000..5667aab --- /dev/null +++ b/core/program-options-ext.cpp @@ -0,0 +1,45 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2014-2022, 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 . + */ + +#include "core/program-options-ext.hpp" + +namespace ndn { + +void +validate(boost::any& v, const std::vector& values, Name*, int) +{ + using namespace boost::program_options; + + validators::check_first_occurrence(v); + const std::string& s = validators::get_single_string(values); + + try { + v = Name(s); + } catch (const Name::Error&) { + throw invalid_option_value(s); + } +} + +} // namespace ndn diff --git a/core/program-options-ext.hpp b/core/program-options-ext.hpp new file mode 100644 index 0000000..16b4e41 --- /dev/null +++ b/core/program-options-ext.hpp @@ -0,0 +1,42 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2014-2022, 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 . + */ + +#ifndef NDN_TOOLS_CORE_PROGRAM_OPTIONS_EXT_HPP +#define NDN_TOOLS_CORE_PROGRAM_OPTIONS_EXT_HPP + +#include +#include + +namespace ndn { + +/** + * @brief Provide a Boost.Program_options custom validator for ndn::Name type. + */ +void +validate(boost::any& v, const std::vector& values, Name*, int); + +} // namespace ndn + +#endif // NDN_TOOLS_CORE_PROGRAM_OPTIONS_EXT_HPP diff --git a/manpages/ndnpeek.rst b/manpages/ndnpeek.rst index 2b50603..9d42a7a 100644 --- a/manpages/ndnpeek.rst +++ b/manpages/ndnpeek.rst @@ -28,8 +28,9 @@ Options ``-f, --fresh`` If specified, include ``MustBeFresh`` element in the Interest packet. -``--link-file `` - Read Link object from ``file`` and add it as ``ForwardingHint`` to the Interest packet. +``-F, --fwhint `` + Add a delegation name to the ``ForwardingHint`` of the Interest packet. + This option may be repeated to specify multiple delegation names. ``-l, --lifetime `` Set ``lifetime`` (in milliseconds) as the ``InterestLifetime``. diff --git a/tests/peek/ndnpeek.t.cpp b/tests/peek/ndnpeek.t.cpp index 9951e5c..336b1de 100644 --- a/tests/peek/ndnpeek.t.cpp +++ b/tests/peek/ndnpeek.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2020, Arizona Board of Regents. + * Copyright (c) 2014-2022, Arizona Board of Regents. * * This file is part of ndn-tools (Named Data Networking Essential Tools). * See AUTHORS.md for complete list of ndn-tools authors and contributors. @@ -155,12 +155,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks) OutputCheck::checkOutput(output, *data); BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); - BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false); - BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false); - BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true); - BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); - BOOST_CHECK(face.sentInterests.back().getHopLimit() == nullopt); - BOOST_CHECK(!face.sentInterests.back().hasApplicationParameters()); + const auto& interest = face.sentInterests.back(); + BOOST_CHECK_EQUAL(interest.getCanBePrefix(), false); + BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); + BOOST_CHECK_EQUAL(interest.getForwardingHint().empty(), true); + BOOST_CHECK_EQUAL(interest.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); + BOOST_CHECK(interest.getHopLimit() == nullopt); + BOOST_CHECK(!interest.hasApplicationParameters()); BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA); } @@ -169,6 +170,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks) auto options = OutputCheck::makeOptions(); options.canBePrefix = true; options.mustBeFresh = true; + options.forwardingHint.emplace_back("/fh"); options.interestLifetime = 200_ms; options.hopLimit = 64; initialize(options); @@ -187,12 +189,14 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks) OutputCheck::checkOutput(output, *data); BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); - BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true); - BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true); - BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true); - BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms); - BOOST_CHECK(face.sentInterests.back().getHopLimit() == 64); - BOOST_CHECK(!face.sentInterests.back().hasApplicationParameters()); + const auto& interest = face.sentInterests.back(); + BOOST_CHECK_EQUAL(interest.getCanBePrefix(), true); + BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); + BOOST_TEST(interest.getForwardingHint() == std::vector({"/fh"}), + boost::test_tools::per_element()); + BOOST_CHECK_EQUAL(interest.getInterestLifetime(), 200_ms); + BOOST_CHECK(interest.getHopLimit() == 64); + BOOST_CHECK(!interest.hasApplicationParameters()); BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA); } diff --git a/tools/peek/ndnpeek/main.cpp b/tools/peek/ndnpeek/main.cpp index ff9f2af..22523b7 100644 --- a/tools/peek/ndnpeek/main.cpp +++ b/tools/peek/ndnpeek/main.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2021, Regents of the University of California, + * Copyright (c) 2014-2022, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -28,6 +28,7 @@ */ #include "ndnpeek.hpp" +#include "core/program-options-ext.hpp" #include "core/version.hpp" #include @@ -85,7 +86,8 @@ main(int argc, char* argv[]) interestOptDesc.add_options() ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix") ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh") - ("link-file", po::value(), "set ForwardingHint from a raw binary file") + ("fwhint,F", po::value>(&options.forwardingHint)->composing(), + "add ForwardingHint delegation name") ("lifetime,l", po::value()->default_value(options.interestLifetime.count()), "set InterestLifetime, in milliseconds") ("hop-limit,H", po::value(), "set HopLimit") @@ -98,7 +100,7 @@ main(int argc, char* argv[]) po::options_description hiddenOptDesc; hiddenOptDesc.add_options() - ("name", po::value(), "Interest name"); + ("name", po::value(&options.name), "Interest name"); po::options_description optDesc; optDesc.add(visibleOptDesc).add(hiddenOptDesc); @@ -132,14 +134,6 @@ main(int argc, char* argv[]) return 2; } - try { - options.name = vm["name"].as(); - } - catch (const Name::Error& e) { - std::cerr << "ERROR: invalid name: " << e.what() << std::endl; - return 2; - } - if (vm.count("timeout") > 0) { options.timeout = time::milliseconds(vm["timeout"].as()); if (*options.timeout < 0_ms) { @@ -148,19 +142,6 @@ main(int argc, char* argv[]) } } - if (vm.count("link-file") > 0) { - auto filename = vm["link-file"].as(); - std::ifstream linkFile = openBinaryFile(filename); - if (!linkFile) { - return 2; - } - options.link = io::load(linkFile, io::NO_ENCODING); - if (!options.link) { - std::cerr << "ERROR: cannot parse a valid Link object from file '" << filename << "'" << std::endl; - return 2; - } - } - options.interestLifetime = time::milliseconds(vm["lifetime"].as()); if (options.interestLifetime < 0_ms) { std::cerr << "ERROR: InterestLifetime cannot be negative" << std::endl; diff --git a/tools/peek/ndnpeek/ndnpeek.cpp b/tools/peek/ndnpeek/ndnpeek.cpp index cc9286c..01fd6de 100644 --- a/tools/peek/ndnpeek/ndnpeek.cpp +++ b/tools/peek/ndnpeek/ndnpeek.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2019, Regents of the University of California, + * Copyright (c) 2014-2022, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -63,9 +63,7 @@ NdnPeek::createInterest() const Interest interest(m_options.name); interest.setCanBePrefix(m_options.canBePrefix); interest.setMustBeFresh(m_options.mustBeFresh); - if (m_options.link) { - interest.setForwardingHint(m_options.link->getDelegationList()); - } + interest.setForwardingHint(m_options.forwardingHint); interest.setInterestLifetime(m_options.interestLifetime); interest.setHopLimit(m_options.hopLimit); if (m_options.applicationParameters) { diff --git a/tools/peek/ndnpeek/ndnpeek.hpp b/tools/peek/ndnpeek/ndnpeek.hpp index afb0ea7..ce37712 100644 --- a/tools/peek/ndnpeek/ndnpeek.hpp +++ b/tools/peek/ndnpeek/ndnpeek.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2021, Regents of the University of California, + * Copyright (c) 2014-2022, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -47,7 +47,7 @@ struct PeekOptions Name name; bool canBePrefix = false; bool mustBeFresh = false; - shared_ptr link; + std::vector forwardingHint; time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME; optional hopLimit; shared_ptr applicationParameters;