blob: 774d7593b498818e17f65620e0dca3b3f5244af0 [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California,
4 * 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
40usage(std::ostream& os, const po::options_description& options)
41{
42 os << "Usage: ndnpeek [options] ndn:/name\n"
43 "\n"
44 "Fetch one data item matching the name prefix and write it to standard output.\n"
45 "\n"
46 << options;
47}
48
49static int
50main(int argc, char* argv[])
51{
52 PeekOptions options;
53 options.isVerbose = false;
54 options.mustBeFresh = false;
55 options.wantRightmostChild = false;
56 options.wantPayloadOnly = false;
57 options.minSuffixComponents = -1;
58 options.maxSuffixComponents = -1;
59 options.interestLifetime = time::milliseconds(-1);
60 options.timeout = time::milliseconds(-1);
61
62 po::options_description genericOptDesc("Generic options");
63 genericOptDesc.add_options()
64 ("help,h", "print help and exit")
65 ("payload,p", po::bool_switch(&options.wantPayloadOnly),
66 "print payload only, instead of full packet")
67 ("timeout,w", po::value<int>(),
68 "set timeout (in milliseconds)")
69 ("verbose,v", po::bool_switch(&options.isVerbose),
70 "turn on verbose output")
71 ("version,V", "print version and exit")
72 ;
73
74 po::options_description interestOptDesc("Interest construction");
75 interestOptDesc.add_options()
76 ("fresh,f", po::bool_switch(&options.mustBeFresh),
77 "set MustBeFresh")
78 ("rightmost,r", po::bool_switch(&options.wantRightmostChild),
79 "set ChildSelector to rightmost")
80 ("minsuffix,m", po::value<int>(&options.minSuffixComponents),
81 "set MinSuffixComponents")
82 ("maxsuffix,M", po::value<int>(&options.maxSuffixComponents),
83 "set MaxSuffixComponents")
84 ("lifetime,l", po::value<int>(),
85 "set InterestLifetime (in milliseconds)")
86 ("link-file", po::value<std::string>(),
87 "set Link from a file")
88 ;
89
90 po::options_description visibleOptDesc;
91 visibleOptDesc.add(genericOptDesc).add(interestOptDesc);
92
93 po::options_description hiddenOptDesc;
94 hiddenOptDesc.add_options()
95 ("prefix", po::value<std::string>(), "Interest name");
96
97 po::options_description optDesc;
98 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
99
100 po::positional_options_description optPos;
101 optPos.add("prefix", -1);
102
103 po::variables_map vm;
104 try {
105 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
106 po::notify(vm);
107 }
108 catch (const po::error& e) {
109 std::cerr << "ERROR: " << e.what() << std::endl;
110 return 2;
111 }
112
113 if (vm.count("help") > 0) {
114 usage(std::cout, visibleOptDesc);
115 return 0;
116 }
117
118 if (vm.count("version") > 0) {
119 std::cout << "ndnpeek " << tools::VERSION << std::endl;
120 return 0;
121 }
122
123 if (vm.count("prefix") > 0) {
124 options.prefix = vm["prefix"].as<std::string>();
125 }
126 else {
127 std::cerr << "ERROR: Interest name is missing" << std::endl;
128 usage(std::cerr, visibleOptDesc);
129 return 2;
130 }
131
132 if (vm.count("minsuffix") > 0 && options.minSuffixComponents < 0) {
133 std::cerr << "ERROR: MinSuffixComponents must be a non-negative integer" << std::endl;
134 usage(std::cerr, visibleOptDesc);
135 return 2;
136 }
137
138 if (vm.count("maxsuffix") > 0 && options.maxSuffixComponents < 0) {
139 std::cerr << "ERROR: MaxSuffixComponents must be a non-negative integer" << std::endl;
140 usage(std::cerr, visibleOptDesc);
141 return 2;
142 }
143
144 if (vm.count("lifetime") > 0) {
145 if (vm["lifetime"].as<int>() >= 0) {
146 options.interestLifetime = time::milliseconds(vm["lifetime"].as<int>());
147 }
148 else {
149 std::cerr << "ERROR: InterestLifetime must be a non-negative integer" << std::endl;
150 usage(std::cerr, visibleOptDesc);
151 return 2;
152 }
153 }
154
155 if (vm.count("timeout") > 0) {
156 if (vm["timeout"].as<int>() > 0) {
157 options.timeout = time::milliseconds(vm["timeout"].as<int>());
158 }
159 else {
160 std::cerr << "ERROR: Timeout must be a positive integer" << std::endl;
161 usage(std::cerr, visibleOptDesc);
162 return 2;
163 }
164 }
165
166 if (vm.count("link-file") > 0) {
167 options.link = io::load<Link>(vm["link-file"].as<std::string>());
168 if (options.link == nullptr) {
169 std::cerr << "ERROR: Cannot read Link object from the specified file" << std::endl;
170 usage(std::cerr, visibleOptDesc);
171 return 2;
172 }
173 }
174
175 Face face;
176 NdnPeek program(face, options);
177
178 try {
179 program.start();
180 face.processEvents(program.getTimeout());
181 }
182 catch (const std::exception& e) {
183 std::cerr << "ERROR: " << e.what() << std::endl;
184 return 1;
185 }
186
187 ResultCode result = program.getResultCode();
188 if (result == ResultCode::TIMEOUT && options.isVerbose) {
189 std::cerr << "TIMEOUT" << std::endl;
190 }
191 return static_cast<int>(result);
192}
193
194} // namespace peek
195} // namespace ndn
196
197int
198main(int argc, char** argv)
199{
200 return ndn::peek::main(argc, argv);
201}