Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -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> |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 26 | * @author Davide Pesavento <davidepesa@gmail.com> |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "ndnpoke.hpp" |
| 30 | #include "core/version.hpp" |
| 31 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 32 | #include <boost/program_options/options_description.hpp> |
| 33 | #include <boost/program_options/parsers.hpp> |
| 34 | #include <boost/program_options/variables_map.hpp> |
| 35 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 36 | namespace ndn::peek { |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 37 | |
| 38 | namespace po = boost::program_options; |
| 39 | |
| 40 | static void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 41 | usage(std::ostream& os, std::string_view program, const po::options_description& options) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 42 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 43 | os << "Usage: " << program << " [options] /name\n" |
| 44 | << "\n" |
| 45 | << "Reads a payload from the standard input and sends it as a single Data packet.\n" |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 46 | << options; |
| 47 | } |
| 48 | |
| 49 | static int |
| 50 | main(int argc, char* argv[]) |
| 51 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 52 | std::string progName(argv[0]); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 53 | PokeOptions options; |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 54 | std::string signingStr; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 55 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 56 | po::options_description genericOptDesc("Generic options"); |
| 57 | genericOptDesc.add_options() |
| 58 | ("help,h", "print help and exit") |
| 59 | ("unsolicited,u", po::bool_switch(&options.wantUnsolicited), |
| 60 | "send the Data packet without waiting for an incoming Interest") |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 61 | ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds") |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 62 | ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output") |
| 63 | ("version,V", "print version and exit") |
| 64 | ; |
| 65 | |
| 66 | po::options_description dataOptDesc("Data construction"); |
| 67 | dataOptDesc.add_options() |
Davide Pesavento | 6929b43 | 2021-01-26 17:07:58 -0500 | [diff] [blame] | 68 | ("freshness,f", po::value<time::milliseconds::rep>()->default_value(options.freshnessPeriod.count()), |
| 69 | "set FreshnessPeriod, in milliseconds") |
| 70 | ("final,F", po::bool_switch(&options.wantFinalBlockId), |
| 71 | "set FinalBlockId to the last component of the Data name") |
| 72 | ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnpoke' for usage") |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 73 | ; |
| 74 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 75 | po::options_description visibleOptDesc; |
| 76 | visibleOptDesc.add(genericOptDesc).add(dataOptDesc); |
| 77 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 78 | po::options_description hiddenOptDesc; |
| 79 | hiddenOptDesc.add_options() |
| 80 | ("name", po::value<std::string>(), "Data name"); |
| 81 | |
| 82 | po::options_description optDesc; |
Davide Pesavento | 814ad34 | 2021-01-26 14:36:11 -0500 | [diff] [blame] | 83 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 84 | |
| 85 | po::positional_options_description optPos; |
| 86 | optPos.add("name", -1); |
| 87 | |
| 88 | po::variables_map vm; |
| 89 | try { |
| 90 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| 91 | po::notify(vm); |
| 92 | } |
| 93 | catch (const po::error& e) { |
| 94 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 95 | return 2; |
| 96 | } |
| 97 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 98 | if (vm.count("help") > 0) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 99 | usage(std::cout, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | if (vm.count("version") > 0) { |
| 104 | std::cout << "ndnpoke " << tools::VERSION << std::endl; |
| 105 | return 0; |
| 106 | } |
| 107 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 108 | if (vm.count("name") == 0) { |
| 109 | std::cerr << "ERROR: missing name\n\n"; |
| 110 | usage(std::cerr, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 111 | return 2; |
| 112 | } |
| 113 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 114 | try { |
| 115 | options.name = vm["name"].as<std::string>(); |
| 116 | } |
| 117 | catch (const Name::Error& e) { |
| 118 | std::cerr << "ERROR: invalid name: " << e.what() << std::endl; |
| 119 | return 2; |
| 120 | } |
| 121 | |
| 122 | if (options.name.empty()) { |
| 123 | std::cerr << "ERROR: name cannot have zero components" << std::endl; |
| 124 | return 2; |
| 125 | } |
| 126 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 127 | options.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
| 128 | if (options.freshnessPeriod < 0_ms) { |
| 129 | std::cerr << "ERROR: freshness cannot be negative" << std::endl; |
| 130 | return 2; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 133 | try { |
| 134 | options.signingInfo = security::SigningInfo(signingStr); |
| 135 | } |
| 136 | catch (const std::invalid_argument& e) { |
| 137 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 138 | return 2; |
| 139 | } |
| 140 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 141 | if (vm.count("timeout") > 0) { |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 142 | if (options.wantUnsolicited) { |
| 143 | std::cerr << "ERROR: conflicting '--unsolicited' and '--timeout' options specified" << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 144 | return 2; |
| 145 | } |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 146 | options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>()); |
| 147 | if (*options.timeout < 0_ms) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 148 | std::cerr << "ERROR: timeout cannot be negative" << std::endl; |
| 149 | return 2; |
| 150 | } |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 153 | try { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 154 | Face face; |
| 155 | KeyChain keyChain; |
| 156 | NdnPoke program(face, keyChain, std::cin, options); |
| 157 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 158 | program.start(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 159 | face.processEvents(); |
| 160 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 161 | return static_cast<int>(program.getResult()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 162 | } |
| 163 | catch (const std::exception& e) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 164 | std::cerr << "ERROR: " << e.what() << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | } |
| 168 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 169 | } // namespace ndn::peek |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 170 | |
| 171 | int |
| 172 | main(int argc, char* argv[]) |
| 173 | { |
| 174 | return ndn::peek::main(argc, argv); |
| 175 | } |