blob: 89b22228fdb635debf191678273c3fd70a321fd4 [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib54aabd2018-04-16 19:36:24 +00002/*
Davide Pesaventoda85e252019-03-18 11:42:01 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Zhuo Lib3558892016-08-12 15:51:12 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of ndn-tools (Named Data Networking Essential Tools).
12 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
13 *
14 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
26 * @author Zhuo Li <zhuoli@email.arizona.edu>
Davide Pesavento96028232019-08-17 01:26:13 -040027 * @author Davide Pesavento <davidepesa@gmail.com>
Zhuo Lib3558892016-08-12 15:51:12 -070028 */
29
30#include "ndnpeek.hpp"
31#include "core/version.hpp"
32
33#include <ndn-cxx/util/io.hpp>
34
Davide Pesavento50cf6db2019-07-29 20:40:10 -040035#include <cerrno>
36#include <cstring>
37#include <fstream>
38
Zhuo Lib3558892016-08-12 15:51:12 -070039namespace ndn {
40namespace peek {
41
42namespace po = boost::program_options;
43
44static void
Davide Pesavento65d11552019-06-09 19:15:50 -040045usage(std::ostream& os, const std::string& program, const po::options_description& options)
Zhuo Lib3558892016-08-12 15:51:12 -070046{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040047 os << "Usage: " << program << " [options] /name\n"
Davide Pesavento65d11552019-06-09 19:15:50 -040048 << "\n"
49 << "Fetch one data item matching the specified name and write it to the standard output.\n"
Zhuo Lib3558892016-08-12 15:51:12 -070050 << options;
51}
52
Davide Pesavento50cf6db2019-07-29 20:40:10 -040053static std::ifstream
54openBinaryFile(const std::string& filename)
55{
56 std::ifstream file(filename, std::ios::binary);
57 if (!file) {
58 std::cerr << "ERROR: cannot open '" << filename << "' for reading: "
59 << std::strerror(errno) << std::endl;
60 }
61 return file;
62}
63
Zhuo Lib3558892016-08-12 15:51:12 -070064static int
65main(int argc, char* argv[])
66{
Davide Pesavento65d11552019-06-09 19:15:50 -040067 std::string progName(argv[0]);
Zhuo Lib3558892016-08-12 15:51:12 -070068 PeekOptions options;
Zhuo Lib3558892016-08-12 15:51:12 -070069
70 po::options_description genericOptDesc("Generic options");
71 genericOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040072 ("help,h", "print help and exit")
Davide Pesavento0da1f442019-07-26 17:38:13 -040073 ("payload,p", po::bool_switch(&options.wantPayloadOnly),
74 "print payload only, instead of full packet")
75 ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds")
Davide Pesavento65d11552019-06-09 19:15:50 -040076 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output")
Zhuo Lib3558892016-08-12 15:51:12 -070077 ("version,V", "print version and exit")
78 ;
79
80 po::options_description interestOptDesc("Interest construction");
81 interestOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040082 ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix")
83 ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh")
Davide Pesavento50cf6db2019-07-29 20:40:10 -040084 ("link-file", po::value<std::string>(), "set ForwardingHint from a raw binary file")
Davide Pesavento0da1f442019-07-26 17:38:13 -040085 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
86 "set InterestLifetime, in milliseconds")
Davide Pesavento96028232019-08-17 01:26:13 -040087 ("app-params,A", po::value<std::string>(), "set ApplicationParameters from a base64-encoded string")
88 ("app-params-file", po::value<std::string>(), "set ApplicationParameters from a file")
Zhuo Lib3558892016-08-12 15:51:12 -070089 ;
90
91 po::options_description visibleOptDesc;
92 visibleOptDesc.add(genericOptDesc).add(interestOptDesc);
93
94 po::options_description hiddenOptDesc;
95 hiddenOptDesc.add_options()
Junxiao Shib54aabd2018-04-16 19:36:24 +000096 ("name", po::value<std::string>(), "Interest name");
Zhuo Lib3558892016-08-12 15:51:12 -070097
98 po::options_description optDesc;
99 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
100
101 po::positional_options_description optPos;
Junxiao Shib54aabd2018-04-16 19:36:24 +0000102 optPos.add("name", -1);
Zhuo Lib3558892016-08-12 15:51:12 -0700103
104 po::variables_map vm;
105 try {
106 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
107 po::notify(vm);
108 }
109 catch (const po::error& e) {
110 std::cerr << "ERROR: " << e.what() << std::endl;
111 return 2;
112 }
113
114 if (vm.count("help") > 0) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400115 usage(std::cout, progName, visibleOptDesc);
Zhuo Lib3558892016-08-12 15:51:12 -0700116 return 0;
117 }
118
119 if (vm.count("version") > 0) {
120 std::cout << "ndnpeek " << tools::VERSION << std::endl;
121 return 0;
122 }
123
Davide Pesavento65d11552019-06-09 19:15:50 -0400124 if (vm.count("name") == 0) {
125 std::cerr << "ERROR: missing name\n\n";
126 usage(std::cerr, progName, visibleOptDesc);
127 return 2;
128 }
129
130 try {
Junxiao Shib54aabd2018-04-16 19:36:24 +0000131 options.name = vm["name"].as<std::string>();
Zhuo Lib3558892016-08-12 15:51:12 -0700132 }
Davide Pesavento65d11552019-06-09 19:15:50 -0400133 catch (const Name::Error& e) {
134 std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700135 return 2;
136 }
137
Zhuo Lib3558892016-08-12 15:51:12 -0700138 if (vm.count("timeout") > 0) {
Davide Pesavento0da1f442019-07-26 17:38:13 -0400139 options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>());
140 if (*options.timeout < 0_ms) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400141 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700142 return 2;
143 }
144 }
145
146 if (vm.count("link-file") > 0) {
Davide Pesavento50cf6db2019-07-29 20:40:10 -0400147 auto filename = vm["link-file"].as<std::string>();
148 std::ifstream linkFile = openBinaryFile(filename);
149 if (!linkFile) {
Zhuo Lib3558892016-08-12 15:51:12 -0700150 return 2;
151 }
Davide Pesavento50cf6db2019-07-29 20:40:10 -0400152 options.link = io::load<Link>(linkFile, io::NO_ENCODING);
153 if (!options.link) {
154 std::cerr << "ERROR: cannot parse a valid Link object from file '" << filename << "'" << std::endl;
155 return 2;
156 }
157 }
158
159 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
160 if (options.interestLifetime < 0_ms) {
161 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
162 return 2;
Zhuo Lib3558892016-08-12 15:51:12 -0700163 }
164
Davide Pesavento96028232019-08-17 01:26:13 -0400165 if (vm.count("app-params") > 0) {
166 if (vm.count("app-params-file") > 0) {
167 std::cerr << "ERROR: cannot specify both '--app-params' and '--app-params-file'" << std::endl;
168 return 2;
169 }
170 std::istringstream is(vm["app-params"].as<std::string>());
171 try {
172 options.applicationParameters = io::loadBuffer(is, io::BASE64);
173 }
174 catch (const io::Error& e) {
175 std::cerr << "ERROR: invalid ApplicationParameters string: " << e.what() << std::endl;
176 return 2;
177 }
178 }
179
180 if (vm.count("app-params-file") > 0) {
181 auto filename = vm["app-params-file"].as<std::string>();
182 std::ifstream paramsFile = openBinaryFile(filename);
183 if (!paramsFile) {
184 return 2;
185 }
186 try {
187 options.applicationParameters = io::loadBuffer(paramsFile, io::NO_ENCODING);
188 }
189 catch (const io::Error& e) {
190 std::cerr << "ERROR: cannot read ApplicationParameters from file '" << filename
191 << "': " << e.what() << std::endl;
192 return 2;
193 }
194 }
195
Zhuo Lib3558892016-08-12 15:51:12 -0700196 try {
Davide Pesavento65d11552019-06-09 19:15:50 -0400197 Face face;
198 NdnPeek program(face, options);
199
Zhuo Lib3558892016-08-12 15:51:12 -0700200 program.start();
Davide Pesavento65d11552019-06-09 19:15:50 -0400201 face.processEvents();
202
Davide Pesavento87434be2019-07-25 19:04:23 -0400203 return static_cast<int>(program.getResult());
Zhuo Lib3558892016-08-12 15:51:12 -0700204 }
205 catch (const std::exception& e) {
206 std::cerr << "ERROR: " << e.what() << std::endl;
207 return 1;
208 }
Zhuo Lib3558892016-08-12 15:51:12 -0700209}
210
211} // namespace peek
212} // namespace ndn
213
214int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400215main(int argc, char* argv[])
Zhuo Lib3558892016-08-12 15:51:12 -0700216{
217 return ndn::peek::main(argc, argv);
218}