blob: 5e1dd9fd4bf94f12798b665455dc0ab10fb0a5fb [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 Pesaventoda85e252019-03-18 11:42:01 -04003 * Copyright (c) 2016-2019, 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
Weiwei Liu85de8422016-09-28 03:28:31 +000033namespace po = boost::program_options;
34
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035namespace ndn {
36namespace chunks {
37
Weiwei Liu85de8422016-09-28 03:28:31 +000038static void
Davide Pesavento6f76afc2017-09-19 18:43:51 -040039usage(std::ostream& os, const std::string& programName, const po::options_description& desc)
40{
41 os << "Usage: " << programName << " [options] ndn:/name\n"
42 << "\n"
43 << "Publish data under the specified prefix.\n"
44 << "Note: this tool expects data from the standard input.\n"
45 << "\n"
46 << desc;
Weiwei Liu85de8422016-09-28 03:28:31 +000047}
48
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040050main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051{
52 std::string programName = argv[0];
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053 std::string prefix;
Davide Pesavento6f76afc2017-09-19 18:43:51 -040054 std::string signingStr;
55 Producer::Options opts;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 po::options_description visibleDesc("Options");
58 visibleDesc.add_options()
59 ("help,h", "print this help message and exit")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040060 ("freshness,f", po::value<time::milliseconds::rep>()->default_value(opts.freshnessPeriod.count()),
61 "FreshnessPeriod of the published Data packets, in milliseconds")
62 ("print-data-version,p", po::bool_switch(&opts.wantShowVersion),
63 "print Data version to the standard output")
64 ("size,s", po::value<size_t>(&opts.maxSegmentSize)->default_value(opts.maxSegmentSize),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010065 "maximum chunk size, in bytes")
Klaus Schneidere90a3582019-04-03 17:22:39 -070066 ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnputchunks' for usage")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040067 ("quiet,q", po::bool_switch(&opts.isQuiet), "turn off all non-error output")
68 ("verbose,v", po::bool_switch(&opts.isVerbose), "turn on verbose output (per Interest information)")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069 ("version,V", "print program version and exit")
70 ;
71
Davide Pesavento6f76afc2017-09-19 18:43:51 -040072 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073 hiddenDesc.add_options()
74 ("ndn-name,n", po::value<std::string>(&prefix), "NDN name for the served content");
75
76 po::positional_options_description p;
77 p.add("ndn-name", -1);
78
Davide Pesavento6f76afc2017-09-19 18:43:51 -040079 po::options_description optDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080 optDesc.add(visibleDesc).add(hiddenDesc);
81
82 po::variables_map vm;
83 try {
84 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
85 po::notify(vm);
86 }
87 catch (const po::error& e) {
88 std::cerr << "ERROR: " << e.what() << std::endl;
89 return 2;
90 }
91 catch (const boost::bad_any_cast& e) {
92 std::cerr << "ERROR: " << e.what() << std::endl;
93 return 2;
94 }
95
96 if (vm.count("help") > 0) {
Weiwei Liu85de8422016-09-28 03:28:31 +000097 usage(std::cout, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010098 return 0;
99 }
100
101 if (vm.count("version") > 0) {
102 std::cout << "ndnputchunks " << tools::VERSION << std::endl;
103 return 0;
104 }
105
106 if (prefix.empty()) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000107 usage(std::cerr, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100108 return 2;
109 }
110
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400111 opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
Davide Pesavento0da1f442019-07-26 17:38:13 -0400112 if (opts.freshnessPeriod < 0_ms) {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400113 std::cerr << "ERROR: FreshnessPeriod cannot be negative" << std::endl;
114 return 2;
115 }
116
117 if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100118 std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl;
119 return 2;
120 }
121
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100122 try {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400123 opts.signingInfo = security::SigningInfo(signingStr);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100124 }
125 catch (const std::invalid_argument& e) {
126 std::cerr << "ERROR: " << e.what() << std::endl;
127 return 2;
128 }
129
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400130 if (opts.isQuiet && opts.isVerbose) {
131 std::cerr << "ERROR: Cannot be quiet and verbose at the same time" << std::endl;
132 return 2;
133 }
134
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135 try {
136 Face face;
137 KeyChain keyChain;
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400138 Producer producer(prefix, face, keyChain, std::cin, opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100139 producer.run();
140 }
141 catch (const std::exception& e) {
142 std::cerr << "ERROR: " << e.what() << std::endl;
143 return 1;
144 }
145
146 return 0;
147}
148
149} // namespace chunks
150} // namespace ndn
151
152int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400153main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100154{
155 return ndn::chunks::main(argc, argv);
156}