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 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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" |
Junxiao Shi | fd2d101 | 2022-01-11 18:20:38 +0000 | [diff] [blame] | 31 | #include "core/program-options-ext.hpp" |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 32 | #include "core/version.hpp" |
| 33 | |
| 34 | #include <ndn-cxx/util/io.hpp> |
| 35 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 36 | #include <boost/program_options/options_description.hpp> |
| 37 | #include <boost/program_options/parsers.hpp> |
| 38 | #include <boost/program_options/variables_map.hpp> |
| 39 | |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame] | 40 | #include <cerrno> |
| 41 | #include <cstring> |
| 42 | #include <fstream> |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 43 | #include <iostream> |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame] | 44 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 45 | namespace ndn::peek { |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 46 | |
| 47 | namespace po = boost::program_options; |
| 48 | |
| 49 | static void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 50 | usage(std::ostream& os, std::string_view program, const po::options_description& options) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 51 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 52 | os << "Usage: " << program << " [options] /name\n" |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 53 | << "\n" |
| 54 | << "Fetch one data item matching the specified name and write it to the standard output.\n" |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 55 | << options; |
| 56 | } |
| 57 | |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame] | 58 | static std::ifstream |
| 59 | openBinaryFile(const std::string& filename) |
| 60 | { |
| 61 | std::ifstream file(filename, std::ios::binary); |
| 62 | if (!file) { |
| 63 | std::cerr << "ERROR: cannot open '" << filename << "' for reading: " |
| 64 | << std::strerror(errno) << std::endl; |
| 65 | } |
| 66 | return file; |
| 67 | } |
| 68 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 69 | static int |
| 70 | main(int argc, char* argv[]) |
| 71 | { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 72 | std::string progName(argv[0]); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 73 | PeekOptions options; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 74 | |
| 75 | po::options_description genericOptDesc("Generic options"); |
| 76 | genericOptDesc.add_options() |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 77 | ("help,h", "print help and exit") |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 78 | ("payload,p", po::bool_switch(&options.wantPayloadOnly), |
| 79 | "print payload only, instead of full packet") |
| 80 | ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds") |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 81 | ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 82 | ("version,V", "print version and exit") |
| 83 | ; |
| 84 | |
| 85 | po::options_description interestOptDesc("Interest construction"); |
| 86 | interestOptDesc.add_options() |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 87 | ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix") |
| 88 | ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh") |
Junxiao Shi | fd2d101 | 2022-01-11 18:20:38 +0000 | [diff] [blame] | 89 | ("fwhint,F", po::value<std::vector<Name>>(&options.forwardingHint)->composing(), |
| 90 | "add ForwardingHint delegation name") |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 91 | ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()), |
| 92 | "set InterestLifetime, in milliseconds") |
Davide Pesavento | c214e07 | 2019-08-17 01:33:28 -0400 | [diff] [blame] | 93 | ("hop-limit,H", po::value<int>(), "set HopLimit") |
Davide Pesavento | 9602823 | 2019-08-17 01:26:13 -0400 | [diff] [blame] | 94 | ("app-params,A", po::value<std::string>(), "set ApplicationParameters from a base64-encoded string") |
| 95 | ("app-params-file", po::value<std::string>(), "set ApplicationParameters from a file") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 96 | ; |
| 97 | |
| 98 | po::options_description visibleOptDesc; |
| 99 | visibleOptDesc.add(genericOptDesc).add(interestOptDesc); |
| 100 | |
| 101 | po::options_description hiddenOptDesc; |
| 102 | hiddenOptDesc.add_options() |
Junxiao Shi | fd2d101 | 2022-01-11 18:20:38 +0000 | [diff] [blame] | 103 | ("name", po::value<Name>(&options.name), "Interest name"); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 104 | |
| 105 | po::options_description optDesc; |
| 106 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 107 | |
| 108 | po::positional_options_description optPos; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 109 | optPos.add("name", -1); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 110 | |
| 111 | po::variables_map vm; |
| 112 | try { |
| 113 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| 114 | po::notify(vm); |
| 115 | } |
| 116 | catch (const po::error& e) { |
| 117 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 118 | return 2; |
| 119 | } |
| 120 | |
| 121 | if (vm.count("help") > 0) { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 122 | usage(std::cout, progName, visibleOptDesc); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | if (vm.count("version") > 0) { |
| 127 | std::cout << "ndnpeek " << tools::VERSION << std::endl; |
| 128 | return 0; |
| 129 | } |
| 130 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 131 | if (vm.count("name") == 0) { |
| 132 | std::cerr << "ERROR: missing name\n\n"; |
| 133 | usage(std::cerr, progName, visibleOptDesc); |
| 134 | return 2; |
| 135 | } |
| 136 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 137 | if (vm.count("timeout") > 0) { |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 138 | options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>()); |
| 139 | if (*options.timeout < 0_ms) { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 140 | std::cerr << "ERROR: timeout cannot be negative" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 141 | return 2; |
| 142 | } |
| 143 | } |
| 144 | |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame] | 145 | options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>()); |
| 146 | if (options.interestLifetime < 0_ms) { |
Davide Pesavento | c214e07 | 2019-08-17 01:33:28 -0400 | [diff] [blame] | 147 | std::cerr << "ERROR: InterestLifetime cannot be negative" << std::endl; |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame] | 148 | return 2; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Davide Pesavento | c214e07 | 2019-08-17 01:33:28 -0400 | [diff] [blame] | 151 | if (vm.count("hop-limit") > 0) { |
| 152 | auto hopLimit = vm["hop-limit"].as<int>(); |
| 153 | if (hopLimit < 0 || hopLimit > 255) { |
| 154 | std::cerr << "ERROR: HopLimit must be between 0 and 255" << std::endl; |
| 155 | return 2; |
| 156 | } |
| 157 | options.hopLimit = static_cast<uint8_t>(hopLimit); |
| 158 | } |
| 159 | |
Davide Pesavento | 9602823 | 2019-08-17 01:26:13 -0400 | [diff] [blame] | 160 | if (vm.count("app-params") > 0) { |
| 161 | if (vm.count("app-params-file") > 0) { |
| 162 | std::cerr << "ERROR: cannot specify both '--app-params' and '--app-params-file'" << std::endl; |
| 163 | return 2; |
| 164 | } |
| 165 | std::istringstream is(vm["app-params"].as<std::string>()); |
| 166 | try { |
| 167 | options.applicationParameters = io::loadBuffer(is, io::BASE64); |
| 168 | } |
| 169 | catch (const io::Error& e) { |
| 170 | std::cerr << "ERROR: invalid ApplicationParameters string: " << e.what() << std::endl; |
| 171 | return 2; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (vm.count("app-params-file") > 0) { |
| 176 | auto filename = vm["app-params-file"].as<std::string>(); |
| 177 | std::ifstream paramsFile = openBinaryFile(filename); |
| 178 | if (!paramsFile) { |
| 179 | return 2; |
| 180 | } |
| 181 | try { |
| 182 | options.applicationParameters = io::loadBuffer(paramsFile, io::NO_ENCODING); |
| 183 | } |
| 184 | catch (const io::Error& e) { |
| 185 | std::cerr << "ERROR: cannot read ApplicationParameters from file '" << filename |
| 186 | << "': " << e.what() << std::endl; |
| 187 | return 2; |
| 188 | } |
| 189 | } |
| 190 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 191 | try { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 192 | Face face; |
| 193 | NdnPeek program(face, options); |
| 194 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 195 | program.start(); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 196 | face.processEvents(); |
| 197 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 198 | return static_cast<int>(program.getResult()); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 199 | } |
| 200 | catch (const std::exception& e) { |
| 201 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 202 | return 1; |
| 203 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 206 | } // namespace ndn::peek |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 207 | |
| 208 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 209 | main(int argc, char* argv[]) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 210 | { |
| 211 | return ndn::peek::main(argc, argv); |
| 212 | } |