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 | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, 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 | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 37 | #include <iostream> |
| 38 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 39 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 40 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 41 | namespace po = boost::program_options; |
| 42 | |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 43 | static void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 44 | 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] | 45 | { |
| 46 | os << "Usage: " << programName << " [options] ndn:/name\n" |
| 47 | << "\n" |
| 48 | << "Publish data under the specified prefix.\n" |
| 49 | << "Note: this tool expects data from the standard input.\n" |
| 50 | << "\n" |
| 51 | << desc; |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 54 | static int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 55 | main(int argc, char* argv[]) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 56 | { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 57 | const std::string programName(argv[0]); |
| 58 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 59 | Producer::Options opts; |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 60 | std::string prefix, nameConv, signingStr; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 61 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 62 | po::options_description visibleDesc("Options"); |
| 63 | visibleDesc.add_options() |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 64 | ("help,h", "print this help message and exit") |
| 65 | ("freshness,f", po::value<time::milliseconds::rep>()->default_value(opts.freshnessPeriod.count()), |
| 66 | "FreshnessPeriod of the published Data packets, in milliseconds") |
| 67 | ("size,s", po::value<size_t>(&opts.maxSegmentSize)->default_value(opts.maxSegmentSize), |
| 68 | "maximum chunk size, in bytes") |
| 69 | ("naming-convention,N", po::value<std::string>(&nameConv), |
| 70 | "encoding convention to use for name components, either 'marker' or 'typed'") |
| 71 | ("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] | 72 | ("print-data-version,p", po::bool_switch(&opts.wantShowVersion), |
| 73 | "print Data version to the standard output") |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 74 | ("quiet,q", po::bool_switch(&opts.isQuiet), "turn off all non-error output") |
| 75 | ("verbose,v", po::bool_switch(&opts.isVerbose), "turn on verbose output (per Interest information)") |
| 76 | ("version,V", "print program version and exit") |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 77 | ; |
| 78 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 79 | po::options_description hiddenDesc; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 80 | hiddenDesc.add_options() |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 81 | ("name", po::value<std::string>(&prefix), "NDN name for the served content"); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 82 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 83 | po::options_description optDesc; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 84 | optDesc.add(visibleDesc).add(hiddenDesc); |
| 85 | |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 86 | po::positional_options_description p; |
| 87 | p.add("name", -1); |
| 88 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 89 | po::variables_map vm; |
| 90 | try { |
| 91 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm); |
| 92 | po::notify(vm); |
| 93 | } |
| 94 | catch (const po::error& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 95 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 96 | return 2; |
| 97 | } |
| 98 | catch (const boost::bad_any_cast& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 99 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 100 | return 2; |
| 101 | } |
| 102 | |
| 103 | if (vm.count("help") > 0) { |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 104 | usage(std::cout, programName, visibleDesc); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | if (vm.count("version") > 0) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 109 | std::cout << "ndnputchunks " << tools::VERSION << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | if (prefix.empty()) { |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 114 | usage(std::cerr, programName, visibleDesc); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 115 | return 2; |
| 116 | } |
| 117 | |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 118 | if (nameConv == "marker" || nameConv == "m" || nameConv == "1") { |
| 119 | name::setConventionEncoding(name::Convention::MARKER); |
| 120 | } |
| 121 | else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") { |
| 122 | name::setConventionEncoding(name::Convention::TYPED); |
| 123 | } |
| 124 | else if (!nameConv.empty()) { |
| 125 | std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n"; |
| 126 | return 2; |
| 127 | } |
| 128 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 129 | opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 130 | if (opts.freshnessPeriod < 0_ms) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 131 | std::cerr << "ERROR: --freshness cannot be negative\n"; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 132 | return 2; |
| 133 | } |
| 134 | |
| 135 | if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 136 | 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] | 137 | return 2; |
| 138 | } |
| 139 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 140 | try { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 141 | opts.signingInfo = security::SigningInfo(signingStr); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 142 | } |
| 143 | catch (const std::invalid_argument& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 144 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 145 | return 2; |
| 146 | } |
| 147 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 148 | if (opts.isQuiet && opts.isVerbose) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 149 | 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] | 150 | return 2; |
| 151 | } |
| 152 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 153 | try { |
| 154 | Face face; |
| 155 | KeyChain keyChain; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 156 | Producer producer(prefix, face, keyChain, std::cin, opts); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 157 | producer.run(); |
| 158 | } |
| 159 | catch (const std::exception& e) { |
Davide Pesavento | f8a14d8 | 2021-03-12 00:42:03 -0500 | [diff] [blame] | 160 | std::cerr << "ERROR: " << e.what() << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 167 | } // namespace ndn::chunks |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 168 | |
| 169 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 170 | main(int argc, char* argv[]) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 171 | { |
| 172 | return ndn::chunks::main(argc, argv); |
| 173 | } |