blob: e1b0ea520391676deef4114ac4bd71cab1762046 [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{
56 std::string programName = argv[0];
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 std::string prefix;
Davide Pesavento6f76afc2017-09-19 18:43:51 -040058 std::string signingStr;
59 Producer::Options opts;
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()
63 ("help,h", "print this help message and exit")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040064 ("freshness,f", po::value<time::milliseconds::rep>()->default_value(opts.freshnessPeriod.count()),
65 "FreshnessPeriod of the published Data packets, in milliseconds")
66 ("print-data-version,p", po::bool_switch(&opts.wantShowVersion),
67 "print Data version to the standard output")
68 ("size,s", po::value<size_t>(&opts.maxSegmentSize)->default_value(opts.maxSegmentSize),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069 "maximum chunk size, in bytes")
Klaus Schneidere90a3582019-04-03 17:22:39 -070070 ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnputchunks' for usage")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040071 ("quiet,q", po::bool_switch(&opts.isQuiet), "turn off all non-error output")
72 ("verbose,v", po::bool_switch(&opts.isVerbose), "turn on verbose output (per Interest information)")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073 ("version,V", "print program version and exit")
74 ;
75
Davide Pesavento6f76afc2017-09-19 18:43:51 -040076 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010077 hiddenDesc.add_options()
78 ("ndn-name,n", po::value<std::string>(&prefix), "NDN name for the served content");
79
80 po::positional_options_description p;
81 p.add("ndn-name", -1);
82
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
86 po::variables_map vm;
87 try {
88 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
89 po::notify(vm);
90 }
91 catch (const po::error& e) {
92 std::cerr << "ERROR: " << e.what() << std::endl;
93 return 2;
94 }
95 catch (const boost::bad_any_cast& e) {
96 std::cerr << "ERROR: " << e.what() << std::endl;
97 return 2;
98 }
99
100 if (vm.count("help") > 0) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000101 usage(std::cout, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100102 return 0;
103 }
104
105 if (vm.count("version") > 0) {
106 std::cout << "ndnputchunks " << tools::VERSION << std::endl;
107 return 0;
108 }
109
110 if (prefix.empty()) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000111 usage(std::cerr, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100112 return 2;
113 }
114
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400115 opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
Davide Pesavento0da1f442019-07-26 17:38:13 -0400116 if (opts.freshnessPeriod < 0_ms) {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400117 std::cerr << "ERROR: FreshnessPeriod cannot be negative" << std::endl;
118 return 2;
119 }
120
121 if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100122 std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl;
123 return 2;
124 }
125
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100126 try {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400127 opts.signingInfo = security::SigningInfo(signingStr);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100128 }
129 catch (const std::invalid_argument& e) {
130 std::cerr << "ERROR: " << e.what() << std::endl;
131 return 2;
132 }
133
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400134 if (opts.isQuiet && opts.isVerbose) {
135 std::cerr << "ERROR: Cannot be quiet and verbose at the same time" << std::endl;
136 return 2;
137 }
138
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100139 try {
140 Face face;
141 KeyChain keyChain;
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400142 Producer producer(prefix, face, keyChain, std::cin, opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100143 producer.run();
144 }
145 catch (const std::exception& e) {
146 std::cerr << "ERROR: " << e.what() << std::endl;
147 return 1;
148 }
149
150 return 0;
151}
152
153} // namespace chunks
154} // namespace ndn
155
156int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400157main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100158{
159 return ndn::chunks::main(argc, argv);
160}