blob: 195d2ca08a77b45496551fd59daa4a8ac8ff8f5b [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * 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
26 */
27
28#include "core/version.hpp"
29#include "producer.hpp"
30
Weiwei Liu85de8422016-09-28 03:28:31 +000031namespace po = boost::program_options;
32
Andrea Tosatto672b9a72016-01-05 16:18:20 +010033namespace ndn {
34namespace chunks {
35
Weiwei Liu85de8422016-09-28 03:28:31 +000036static void
37usage(std::ostream& os, const std::string& programName, const po::options_description& visibleDesc) {
38 os << "Usage: " << programName << " [options] ndn:/name" << std::endl;
39 os << "\nPublish data under specified prefix. "
40 << "Note: this tool expects data from the standard input.\n" << std::endl;
41 os << visibleDesc;
42}
43
Andrea Tosatto672b9a72016-01-05 16:18:20 +010044static int
45main(int argc, char** argv)
46{
47 std::string programName = argv[0];
48 uint64_t freshnessPeriod = 10000;
49 bool printVersion = false;
50 size_t maxChunkSize = MAX_NDN_PACKET_SIZE >> 1;
51 std::string signingStr;
52 bool isVerbose = false;
53 std::string prefix;
54
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055 po::options_description visibleDesc("Options");
56 visibleDesc.add_options()
57 ("help,h", "print this help message and exit")
58 ("freshness,f", po::value<uint64_t>(&freshnessPeriod)->default_value(freshnessPeriod),
59 "specify FreshnessPeriod, in milliseconds")
60 ("print-data-version,p", po::bool_switch(&printVersion), "print Data version to the standard output")
61 ("size,s", po::value<size_t>(&maxChunkSize)->default_value(maxChunkSize),
62 "maximum chunk size, in bytes")
63 ("signing-info,S", po::value<std::string>(&signingStr)->default_value(signingStr),
64 "set signing information")
65 ("verbose,v", po::bool_switch(&isVerbose), "turn on verbose output")
66 ("version,V", "print program version and exit")
67 ;
68
69 po::options_description hiddenDesc("Hidden options");
70 hiddenDesc.add_options()
71 ("ndn-name,n", po::value<std::string>(&prefix), "NDN name for the served content");
72
73 po::positional_options_description p;
74 p.add("ndn-name", -1);
75
76 po::options_description optDesc("Allowed options");
77 optDesc.add(visibleDesc).add(hiddenDesc);
78
79 po::variables_map vm;
80 try {
81 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
82 po::notify(vm);
83 }
84 catch (const po::error& e) {
85 std::cerr << "ERROR: " << e.what() << std::endl;
86 return 2;
87 }
88 catch (const boost::bad_any_cast& e) {
89 std::cerr << "ERROR: " << e.what() << std::endl;
90 return 2;
91 }
92
93 if (vm.count("help") > 0) {
Weiwei Liu85de8422016-09-28 03:28:31 +000094 usage(std::cout, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010095 return 0;
96 }
97
98 if (vm.count("version") > 0) {
99 std::cout << "ndnputchunks " << tools::VERSION << std::endl;
100 return 0;
101 }
102
103 if (prefix.empty()) {
Weiwei Liu85de8422016-09-28 03:28:31 +0000104 usage(std::cerr, programName, visibleDesc);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100105 return 2;
106 }
107
108 if (maxChunkSize < 1 || maxChunkSize > MAX_NDN_PACKET_SIZE) {
109 std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl;
110 return 2;
111 }
112
113 security::SigningInfo signingInfo;
114 try {
115 signingInfo = security::SigningInfo(signingStr);
116 }
117 catch (const std::invalid_argument& e) {
118 std::cerr << "ERROR: " << e.what() << std::endl;
119 return 2;
120 }
121
122 try {
123 Face face;
124 KeyChain keyChain;
125 Producer producer(prefix, face, keyChain, signingInfo, time::milliseconds(freshnessPeriod),
126 maxChunkSize, isVerbose, printVersion, std::cin);
127 producer.run();
128 }
129 catch (const std::exception& e) {
130 std::cerr << "ERROR: " << e.what() << std::endl;
131 return 1;
132 }
133
134 return 0;
135}
136
137} // namespace chunks
138} // namespace ndn
139
140int
141main(int argc, char** argv)
142{
143 return ndn::chunks::main(argc, argv);
144}