blob: f763b964112f0c02c4fbc12bfdf0a2c6e66b5b03 [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>
27 */
28
29#include "ndnpeek.hpp"
30#include "core/version.hpp"
31
32#include <ndn-cxx/util/io.hpp>
33
34namespace ndn {
35namespace peek {
36
37namespace po = boost::program_options;
38
39static void
Davide Pesavento65d11552019-06-09 19:15:50 -040040usage(std::ostream& os, const std::string& program, const po::options_description& options)
Zhuo Lib3558892016-08-12 15:51:12 -070041{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040042 os << "Usage: " << program << " [options] /name\n"
Davide Pesavento65d11552019-06-09 19:15:50 -040043 << "\n"
44 << "Fetch one data item matching the specified name and write it to the standard output.\n"
Zhuo Lib3558892016-08-12 15:51:12 -070045 << options;
46}
47
48static int
49main(int argc, char* argv[])
50{
Davide Pesavento65d11552019-06-09 19:15:50 -040051 std::string progName(argv[0]);
Zhuo Lib3558892016-08-12 15:51:12 -070052 PeekOptions options;
Zhuo Lib3558892016-08-12 15:51:12 -070053
54 po::options_description genericOptDesc("Generic options");
55 genericOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040056 ("help,h", "print help and exit")
57 ("payload,p", po::bool_switch(&options.wantPayloadOnly), "print payload only, instead of full packet")
58 ("timeout,w", po::value<int>(), "set timeout (in milliseconds)")
59 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output")
Zhuo Lib3558892016-08-12 15:51:12 -070060 ("version,V", "print version and exit")
61 ;
62
63 po::options_description interestOptDesc("Interest construction");
64 interestOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040065 ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix")
66 ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh")
67 ("link-file", po::value<std::string>(), "set ForwardingHint from a file")
68 ("lifetime,l", po::value<int>(), "set InterestLifetime (in milliseconds)")
Zhuo Lib3558892016-08-12 15:51:12 -070069 ;
70
71 po::options_description visibleOptDesc;
72 visibleOptDesc.add(genericOptDesc).add(interestOptDesc);
73
74 po::options_description hiddenOptDesc;
75 hiddenOptDesc.add_options()
Junxiao Shib54aabd2018-04-16 19:36:24 +000076 ("name", po::value<std::string>(), "Interest name");
Zhuo Lib3558892016-08-12 15:51:12 -070077
78 po::options_description optDesc;
79 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
80
81 po::positional_options_description optPos;
Junxiao Shib54aabd2018-04-16 19:36:24 +000082 optPos.add("name", -1);
Zhuo Lib3558892016-08-12 15:51:12 -070083
84 po::variables_map vm;
85 try {
86 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
87 po::notify(vm);
88 }
89 catch (const po::error& e) {
90 std::cerr << "ERROR: " << e.what() << std::endl;
91 return 2;
92 }
93
94 if (vm.count("help") > 0) {
Davide Pesavento65d11552019-06-09 19:15:50 -040095 usage(std::cout, progName, visibleOptDesc);
Zhuo Lib3558892016-08-12 15:51:12 -070096 return 0;
97 }
98
99 if (vm.count("version") > 0) {
100 std::cout << "ndnpeek " << tools::VERSION << std::endl;
101 return 0;
102 }
103
Davide Pesavento65d11552019-06-09 19:15:50 -0400104 if (vm.count("name") == 0) {
105 std::cerr << "ERROR: missing name\n\n";
106 usage(std::cerr, progName, visibleOptDesc);
107 return 2;
108 }
109
110 try {
Junxiao Shib54aabd2018-04-16 19:36:24 +0000111 options.name = vm["name"].as<std::string>();
Zhuo Lib3558892016-08-12 15:51:12 -0700112 }
Davide Pesavento65d11552019-06-09 19:15:50 -0400113 catch (const Name::Error& e) {
114 std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700115 return 2;
116 }
117
Zhuo Lib3558892016-08-12 15:51:12 -0700118 if (vm.count("lifetime") > 0) {
119 if (vm["lifetime"].as<int>() >= 0) {
120 options.interestLifetime = time::milliseconds(vm["lifetime"].as<int>());
121 }
122 else {
Davide Pesavento65d11552019-06-09 19:15:50 -0400123 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700124 return 2;
125 }
126 }
127
128 if (vm.count("timeout") > 0) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400129 if (vm["timeout"].as<int>() >= 0) {
Zhuo Lib3558892016-08-12 15:51:12 -0700130 options.timeout = time::milliseconds(vm["timeout"].as<int>());
131 }
132 else {
Davide Pesavento65d11552019-06-09 19:15:50 -0400133 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700134 return 2;
135 }
136 }
137
138 if (vm.count("link-file") > 0) {
139 options.link = io::load<Link>(vm["link-file"].as<std::string>());
140 if (options.link == nullptr) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400141 std::cerr << "ERROR: cannot read Link object from the specified file" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700142 return 2;
143 }
144 }
145
Zhuo Lib3558892016-08-12 15:51:12 -0700146 try {
Davide Pesavento65d11552019-06-09 19:15:50 -0400147 Face face;
148 NdnPeek program(face, options);
149
Zhuo Lib3558892016-08-12 15:51:12 -0700150 program.start();
Davide Pesavento65d11552019-06-09 19:15:50 -0400151 face.processEvents();
152
153 return static_cast<int>(program.getResultCode());
Zhuo Lib3558892016-08-12 15:51:12 -0700154 }
155 catch (const std::exception& e) {
156 std::cerr << "ERROR: " << e.what() << std::endl;
157 return 1;
158 }
Zhuo Lib3558892016-08-12 15:51:12 -0700159}
160
161} // namespace peek
162} // namespace ndn
163
164int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400165main(int argc, char* argv[])
Zhuo Lib3558892016-08-12 15:51:12 -0700166{
167 return ndn::peek::main(argc, argv);
168}