blob: 04bfec89aec76b935d14ca560080252b2c795848 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider59ec95a2017-09-14 17:50:00 -07002/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2016-2024, Regents of the University of California,
Davide Pesavento6f76afc2017-09-19 18:43:51 -04004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
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 Pesavento6f76afc2017-09-19 18:43:51 -040026 * @author Davide Pesavento
Klaus Schneider59ec95a2017-09-14 17:50:00 -070027 * @author Klaus Schneider
Andrea Tosatto672b9a72016-01-05 16:18:20 +010028 */
29
30#include "core/version.hpp"
31#include "producer.hpp"
32
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050033#include <boost/program_options/options_description.hpp>
34#include <boost/program_options/parsers.hpp>
35#include <boost/program_options/variables_map.hpp>
Weiwei Liu85de8422016-09-28 03:28:31 +000036
Davide Pesavento5748e822024-01-26 18:40:22 -050037#include <iostream>
38
Davide Pesaventob3570c62022-02-19 19:19:00 -050039namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050041namespace po = boost::program_options;
42
Weiwei Liu85de8422016-09-28 03:28:31 +000043static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050044usage(std::ostream& os, std::string_view programName, const po::options_description& desc)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040045{
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 Liu85de8422016-09-28 03:28:31 +000052}
53
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040055main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056{
Davide Pesaventof8a14d82021-03-12 00:42:03 -050057 const std::string programName(argv[0]);
58
Davide Pesavento6f76afc2017-09-19 18:43:51 -040059 Producer::Options opts;
Davide Pesaventof8a14d82021-03-12 00:42:03 -050060 std::string prefix, nameConv, signingStr;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061
Andrea Tosatto672b9a72016-01-05 16:18:20 +010062 po::options_description visibleDesc("Options");
63 visibleDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050064 ("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 Pesavento6f76afc2017-09-19 18:43:51 -040072 ("print-data-version,p", po::bool_switch(&opts.wantShowVersion),
73 "print Data version to the standard output")
Davide Pesaventof8a14d82021-03-12 00:42:03 -050074 ("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 Tosatto672b9a72016-01-05 16:18:20 +010077 ;
78
Davide Pesavento6f76afc2017-09-19 18:43:51 -040079 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080 hiddenDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050081 ("name", po::value<std::string>(&prefix), "NDN name for the served content");
Andrea Tosatto672b9a72016-01-05 16:18:20 +010082
Davide Pesavento6f76afc2017-09-19 18:43:51 -040083 po::options_description optDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010084 optDesc.add(visibleDesc).add(hiddenDesc);
85
Davide Pesaventof8a14d82021-03-12 00:42:03 -050086 po::positional_options_description p;
87 p.add("name", -1);
88
Andrea Tosatto672b9a72016-01-05 16:18:20 +010089 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 Pesaventof8a14d82021-03-12 00:42:03 -050095 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010096 return 2;
97 }
98 catch (const boost::bad_any_cast& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -050099 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100100 return 2;
101 }
102
103 if (vm.count("help") > 0) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000104 usage(std::cout, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100105 return 0;
106 }
107
108 if (vm.count("version") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500109 std::cout << "ndnputchunks " << tools::VERSION << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100110 return 0;
111 }
112
113 if (prefix.empty()) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000114 usage(std::cerr, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100115 return 2;
116 }
117
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500118 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 Pesavento6f76afc2017-09-19 18:43:51 -0400129 opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
Davide Pesavento0da1f442019-07-26 17:38:13 -0400130 if (opts.freshnessPeriod < 0_ms) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500131 std::cerr << "ERROR: --freshness cannot be negative\n";
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400132 return 2;
133 }
134
135 if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500136 std::cerr << "ERROR: --size must be between 1 and " << MAX_NDN_PACKET_SIZE << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100137 return 2;
138 }
139
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140 try {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400141 opts.signingInfo = security::SigningInfo(signingStr);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100142 }
143 catch (const std::invalid_argument& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500144 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100145 return 2;
146 }
147
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400148 if (opts.isQuiet && opts.isVerbose) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500149 std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400150 return 2;
151 }
152
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100153 try {
154 Face face;
155 KeyChain keyChain;
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400156 Producer producer(prefix, face, keyChain, std::cin, opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100157 producer.run();
158 }
159 catch (const std::exception& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500160 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100161 return 1;
162 }
163
164 return 0;
165}
166
Davide Pesaventob3570c62022-02-19 19:19:00 -0500167} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100168
169int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400170main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100171{
172 return ndn::chunks::main(argc, argv);
173}