blob: 8969e068acfece2472602593cf09373d07633db1 [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, 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 Pesaventob3570c62022-02-19 19:19:00 -050037namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050039namespace po = boost::program_options;
40
Weiwei Liu85de8422016-09-28 03:28:31 +000041static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050042usage(std::ostream& os, std::string_view programName, const po::options_description& desc)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040043{
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 Liu85de8422016-09-28 03:28:31 +000050}
51
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040053main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054{
Davide Pesaventof8a14d82021-03-12 00:42:03 -050055 const std::string programName(argv[0]);
56
Davide Pesavento6f76afc2017-09-19 18:43:51 -040057 Producer::Options opts;
Davide Pesaventof8a14d82021-03-12 00:42:03 -050058 std::string prefix, nameConv, signingStr;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060 po::options_description visibleDesc("Options");
61 visibleDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050062 ("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 Pesavento6f76afc2017-09-19 18:43:51 -040070 ("print-data-version,p", po::bool_switch(&opts.wantShowVersion),
71 "print Data version to the standard output")
Davide Pesaventof8a14d82021-03-12 00:42:03 -050072 ("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 Tosatto672b9a72016-01-05 16:18:20 +010075 ;
76
Davide Pesavento6f76afc2017-09-19 18:43:51 -040077 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078 hiddenDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050079 ("name", po::value<std::string>(&prefix), "NDN name for the served content");
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080
Davide Pesavento6f76afc2017-09-19 18:43:51 -040081 po::options_description optDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010082 optDesc.add(visibleDesc).add(hiddenDesc);
83
Davide Pesaventof8a14d82021-03-12 00:42:03 -050084 po::positional_options_description p;
85 p.add("name", -1);
86
Andrea Tosatto672b9a72016-01-05 16:18:20 +010087 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 Pesaventof8a14d82021-03-12 00:42:03 -050093 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010094 return 2;
95 }
96 catch (const boost::bad_any_cast& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -050097 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010098 return 2;
99 }
100
101 if (vm.count("help") > 0) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000102 usage(std::cout, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100103 return 0;
104 }
105
106 if (vm.count("version") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500107 std::cout << "ndnputchunks " << tools::VERSION << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100108 return 0;
109 }
110
111 if (prefix.empty()) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000112 usage(std::cerr, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100113 return 2;
114 }
115
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500116 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 Pesavento6f76afc2017-09-19 18:43:51 -0400127 opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
Davide Pesavento0da1f442019-07-26 17:38:13 -0400128 if (opts.freshnessPeriod < 0_ms) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500129 std::cerr << "ERROR: --freshness cannot be negative\n";
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400130 return 2;
131 }
132
133 if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500134 std::cerr << "ERROR: --size must be between 1 and " << MAX_NDN_PACKET_SIZE << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135 return 2;
136 }
137
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100138 try {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400139 opts.signingInfo = security::SigningInfo(signingStr);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140 }
141 catch (const std::invalid_argument& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500142 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100143 return 2;
144 }
145
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400146 if (opts.isQuiet && opts.isVerbose) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500147 std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400148 return 2;
149 }
150
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100151 try {
152 Face face;
153 KeyChain keyChain;
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400154 Producer producer(prefix, face, keyChain, std::cin, opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100155 producer.run();
156 }
157 catch (const std::exception& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500158 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100159 return 1;
160 }
161
162 return 0;
163}
164
Davide Pesaventob3570c62022-02-19 19:19:00 -0500165} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100166
167int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400168main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100169{
170 return ndn::chunks::main(argc, argv);
171}