blob: 7dbb5a889a843041714afa7d85442f0698fb8729 [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/*
3 * Copyright (c) 2016-2017, Regents of the University of California,
Andrea Tosatto672b9a72016-01-05 16:18:20 +01004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
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
Klaus Schneider59ec95a2017-09-14 17:50:00 -070026 * @author Klaus Schneider
Andrea Tosatto672b9a72016-01-05 16:18:20 +010027 */
28
29#include "core/version.hpp"
30#include "producer.hpp"
31
Weiwei Liu85de8422016-09-28 03:28:31 +000032namespace po = boost::program_options;
33
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034namespace ndn {
35namespace chunks {
36
Weiwei Liu85de8422016-09-28 03:28:31 +000037static void
38usage(std::ostream& os, const std::string& programName, const po::options_description& visibleDesc) {
39 os << "Usage: " << programName << " [options] ndn:/name" << std::endl;
40 os << "\nPublish data under specified prefix. "
41 << "Note: this tool expects data from the standard input.\n" << std::endl;
42 os << visibleDesc;
43}
44
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045static int
46main(int argc, char** argv)
47{
48 std::string programName = argv[0];
49 uint64_t freshnessPeriod = 10000;
50 bool printVersion = false;
51 size_t maxChunkSize = MAX_NDN_PACKET_SIZE >> 1;
52 std::string signingStr;
53 bool isVerbose = false;
Klaus Schneider59ec95a2017-09-14 17:50:00 -070054 bool isQuiet = false;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055 std::string prefix;
56
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")
60 ("freshness,f", po::value<uint64_t>(&freshnessPeriod)->default_value(freshnessPeriod),
61 "specify FreshnessPeriod, in milliseconds")
62 ("print-data-version,p", po::bool_switch(&printVersion), "print Data version to the standard output")
63 ("size,s", po::value<size_t>(&maxChunkSize)->default_value(maxChunkSize),
64 "maximum chunk size, in bytes")
65 ("signing-info,S", po::value<std::string>(&signingStr)->default_value(signingStr),
66 "set signing information")
Klaus Schneider59ec95a2017-09-14 17:50:00 -070067 ("quiet,q", po::bool_switch(&isQuiet), "turn off all non-error output")
68 ("verbose,v", po::bool_switch(&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
72 po::options_description hiddenDesc("Hidden options");
73 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
79 po::options_description optDesc("Allowed options");
80 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
111 if (maxChunkSize < 1 || maxChunkSize > MAX_NDN_PACKET_SIZE) {
112 std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl;
113 return 2;
114 }
115
Klaus Schneider59ec95a2017-09-14 17:50:00 -0700116 if (isQuiet && isVerbose) {
117 std::cerr << "ERROR: Cannot use flags -q and -v at the same time\n";
118 return 2;
119 }
120
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100121 security::SigningInfo signingInfo;
122 try {
123 signingInfo = security::SigningInfo(signingStr);
124 }
125 catch (const std::invalid_argument& e) {
126 std::cerr << "ERROR: " << e.what() << std::endl;
127 return 2;
128 }
129
130 try {
131 Face face;
132 KeyChain keyChain;
133 Producer producer(prefix, face, keyChain, signingInfo, time::milliseconds(freshnessPeriod),
Klaus Schneider59ec95a2017-09-14 17:50:00 -0700134 maxChunkSize, isQuiet, isVerbose, printVersion, std::cin);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135 producer.run();
136 }
137 catch (const std::exception& e) {
138 std::cerr << "ERROR: " << e.what() << std::endl;
139 return 1;
140 }
141
142 return 0;
143}
144
145} // namespace chunks
146} // namespace ndn
147
148int
149main(int argc, char** argv)
150{
151 return ndn::chunks::main(argc, argv);
152}