blob: de338f3a792b93247bd0f252058d4962defbfc32 [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 Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2016-2021, 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
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037namespace ndn {
38namespace chunks {
39
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050040namespace po = boost::program_options;
41
Weiwei Liu85de8422016-09-28 03:28:31 +000042static void
Davide Pesavento6f76afc2017-09-19 18:43:51 -040043usage(std::ostream& os, const std::string& programName, const po::options_description& desc)
44{
45 os << "Usage: " << programName << " [options] ndn:/name\n"
46 << "\n"
47 << "Publish data under the specified prefix.\n"
48 << "Note: this tool expects data from the standard input.\n"
49 << "\n"
50 << desc;
Weiwei Liu85de8422016-09-28 03:28:31 +000051}
52
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040054main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055{
Davide Pesaventof8a14d82021-03-12 00:42:03 -050056 const std::string programName(argv[0]);
57
Davide Pesavento6f76afc2017-09-19 18:43:51 -040058 Producer::Options opts;
Davide Pesaventof8a14d82021-03-12 00:42:03 -050059 std::string prefix, nameConv, signingStr;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 po::options_description visibleDesc("Options");
62 visibleDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050063 ("help,h", "print this help message and exit")
64 ("freshness,f", po::value<time::milliseconds::rep>()->default_value(opts.freshnessPeriod.count()),
65 "FreshnessPeriod of the published Data packets, in milliseconds")
66 ("size,s", po::value<size_t>(&opts.maxSegmentSize)->default_value(opts.maxSegmentSize),
67 "maximum chunk size, in bytes")
68 ("naming-convention,N", po::value<std::string>(&nameConv),
69 "encoding convention to use for name components, either 'marker' or 'typed'")
70 ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnputchunks' for usage")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040071 ("print-data-version,p", po::bool_switch(&opts.wantShowVersion),
72 "print Data version to the standard output")
Davide Pesaventof8a14d82021-03-12 00:42:03 -050073 ("quiet,q", po::bool_switch(&opts.isQuiet), "turn off all non-error output")
74 ("verbose,v", po::bool_switch(&opts.isVerbose), "turn on verbose output (per Interest information)")
75 ("version,V", "print program version and exit")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010076 ;
77
Davide Pesavento6f76afc2017-09-19 18:43:51 -040078 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010079 hiddenDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050080 ("name", po::value<std::string>(&prefix), "NDN name for the served content");
Andrea Tosatto672b9a72016-01-05 16:18:20 +010081
Davide Pesavento6f76afc2017-09-19 18:43:51 -040082 po::options_description optDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010083 optDesc.add(visibleDesc).add(hiddenDesc);
84
Davide Pesaventof8a14d82021-03-12 00:42:03 -050085 po::positional_options_description p;
86 p.add("name", -1);
87
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088 po::variables_map vm;
89 try {
90 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
91 po::notify(vm);
92 }
93 catch (const po::error& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -050094 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010095 return 2;
96 }
97 catch (const boost::bad_any_cast& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -050098 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010099 return 2;
100 }
101
102 if (vm.count("help") > 0) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000103 usage(std::cout, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100104 return 0;
105 }
106
107 if (vm.count("version") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500108 std::cout << "ndnputchunks " << tools::VERSION << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100109 return 0;
110 }
111
112 if (prefix.empty()) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000113 usage(std::cerr, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100114 return 2;
115 }
116
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500117 if (nameConv == "marker" || nameConv == "m" || nameConv == "1") {
118 name::setConventionEncoding(name::Convention::MARKER);
119 }
120 else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") {
121 name::setConventionEncoding(name::Convention::TYPED);
122 }
123 else if (!nameConv.empty()) {
124 std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n";
125 return 2;
126 }
127
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400128 opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
Davide Pesavento0da1f442019-07-26 17:38:13 -0400129 if (opts.freshnessPeriod < 0_ms) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500130 std::cerr << "ERROR: --freshness cannot be negative\n";
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400131 return 2;
132 }
133
134 if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500135 std::cerr << "ERROR: --size must be between 1 and " << MAX_NDN_PACKET_SIZE << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100136 return 2;
137 }
138
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100139 try {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400140 opts.signingInfo = security::SigningInfo(signingStr);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141 }
142 catch (const std::invalid_argument& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500143 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100144 return 2;
145 }
146
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400147 if (opts.isQuiet && opts.isVerbose) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500148 std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400149 return 2;
150 }
151
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100152 try {
153 Face face;
154 KeyChain keyChain;
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400155 Producer producer(prefix, face, keyChain, std::cin, opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100156 producer.run();
157 }
158 catch (const std::exception& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500159 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100160 return 1;
161 }
162
163 return 0;
164}
165
166} // namespace chunks
167} // namespace ndn
168
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}