Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2022, Regents of the University of California, |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Wentao Shang |
| 24 | * @author Steve DiBenedetto |
| 25 | * @author Andrea Tosatto |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 26 | * @author Davide Pesavento |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 27 | * @author Klaus Schneider |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include "core/version.hpp" |
| 31 | #include "producer.hpp" |
| 32 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 33 | #include <boost/program_options/options_description.hpp> |
| 34 | #include <boost/program_options/parsers.hpp> |
| 35 | #include <boost/program_options/variables_map.hpp> |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 37 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 38 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 39 | namespace po = boost::program_options; |
| 40 | |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 41 | static void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 42 | usage(std::ostream& os, std::string_view programName, const po::options_description& desc) |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 43 | { |
| 44 | os << "Usage: " << programName << " [options] ndn:/name\n" |
| 45 | << "\n" |
| 46 | << "Publish data under the specified prefix.\n" |
| 47 | << "Note: this tool expects data from the standard input.\n" |
| 48 | << "\n" |
| 49 | << desc; |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 52 | static int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 53 | main(int argc, char* argv[]) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 54 | { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 55 | const std::string programName(argv[0]); |
| 56 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 57 | Producer::Options opts; |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 58 | std::string prefix, nameConv, signingStr; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 59 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 60 | po::options_description visibleDesc("Options"); |
| 61 | visibleDesc.add_options() |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 62 | ("help,h", "print this help message and exit") |
| 63 | ("freshness,f", po::value<time::milliseconds::rep>()->default_value(opts.freshnessPeriod.count()), |
| 64 | "FreshnessPeriod of the published Data packets, in milliseconds") |
| 65 | ("size,s", po::value<size_t>(&opts.maxSegmentSize)->default_value(opts.maxSegmentSize), |
| 66 | "maximum chunk size, in bytes") |
| 67 | ("naming-convention,N", po::value<std::string>(&nameConv), |
| 68 | "encoding convention to use for name components, either 'marker' or 'typed'") |
| 69 | ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnputchunks' for usage") |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 70 | ("print-data-version,p", po::bool_switch(&opts.wantShowVersion), |
| 71 | "print Data version to the standard output") |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 72 | ("quiet,q", po::bool_switch(&opts.isQuiet), "turn off all non-error output") |
| 73 | ("verbose,v", po::bool_switch(&opts.isVerbose), "turn on verbose output (per Interest information)") |
| 74 | ("version,V", "print program version and exit") |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 75 | ; |
| 76 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 77 | po::options_description hiddenDesc; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 78 | hiddenDesc.add_options() |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 79 | ("name", po::value<std::string>(&prefix), "NDN name for the served content"); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 80 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 81 | po::options_description optDesc; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 82 | optDesc.add(visibleDesc).add(hiddenDesc); |
| 83 | |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 84 | po::positional_options_description p; |
| 85 | p.add("name", -1); |
| 86 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 87 | po::variables_map vm; |
| 88 | try { |
| 89 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm); |
| 90 | po::notify(vm); |
| 91 | } |
| 92 | catch (const po::error& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 93 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 94 | return 2; |
| 95 | } |
| 96 | catch (const boost::bad_any_cast& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 97 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 98 | return 2; |
| 99 | } |
| 100 | |
| 101 | if (vm.count("help") > 0) { |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 102 | usage(std::cout, programName, visibleDesc); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | if (vm.count("version") > 0) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 107 | std::cout << "ndnputchunks " << tools::VERSION << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | if (prefix.empty()) { |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 112 | usage(std::cerr, programName, visibleDesc); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 113 | return 2; |
| 114 | } |
| 115 | |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 116 | if (nameConv == "marker" || nameConv == "m" || nameConv == "1") { |
| 117 | name::setConventionEncoding(name::Convention::MARKER); |
| 118 | } |
| 119 | else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") { |
| 120 | name::setConventionEncoding(name::Convention::TYPED); |
| 121 | } |
| 122 | else if (!nameConv.empty()) { |
| 123 | std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n"; |
| 124 | return 2; |
| 125 | } |
| 126 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 127 | opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 128 | if (opts.freshnessPeriod < 0_ms) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 129 | std::cerr << "ERROR: --freshness cannot be negative\n"; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 130 | return 2; |
| 131 | } |
| 132 | |
| 133 | if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 134 | std::cerr << "ERROR: --size must be between 1 and " << MAX_NDN_PACKET_SIZE << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 135 | return 2; |
| 136 | } |
| 137 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 138 | try { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 139 | opts.signingInfo = security::SigningInfo(signingStr); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 140 | } |
| 141 | catch (const std::invalid_argument& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 142 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 143 | return 2; |
| 144 | } |
| 145 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 146 | if (opts.isQuiet && opts.isVerbose) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 147 | std::cerr << "ERROR: cannot be quiet and verbose at the same time\n"; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 148 | return 2; |
| 149 | } |
| 150 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 151 | try { |
| 152 | Face face; |
| 153 | KeyChain keyChain; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 154 | Producer producer(prefix, face, keyChain, std::cin, opts); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 155 | producer.run(); |
| 156 | } |
| 157 | catch (const std::exception& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 158 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 165 | } // namespace ndn::chunks |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 166 | |
| 167 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 168 | main(int argc, char* argv[]) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 169 | { |
| 170 | return ndn::chunks::main(argc, argv); |
| 171 | } |