blob: 428634a60f3ee019f78a2a17415fee46a55e2226 [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
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;
Zhuo Lib3558892016-08-12 15:51:12 -070053
54 po::options_description genericOptDesc("Generic options");
55 genericOptDesc.add_options()
56 ("help,h", "print help and exit")
57 ("payload,p", po::bool_switch(&options.wantPayloadOnly),
58 "print payload only, instead of full packet")
59 ("timeout,w", po::value<int>(),
60 "set timeout (in milliseconds)")
61 ("verbose,v", po::bool_switch(&options.isVerbose),
62 "turn on verbose output")
63 ("version,V", "print version and exit")
64 ;
65
66 po::options_description interestOptDesc("Interest construction");
67 interestOptDesc.add_options()
Junxiao Shib54aabd2018-04-16 19:36:24 +000068 ("prefix,P", po::bool_switch(&options.canBePrefix),
69 "set CanBePrefix")
Zhuo Lib3558892016-08-12 15:51:12 -070070 ("fresh,f", po::bool_switch(&options.mustBeFresh),
71 "set MustBeFresh")
Junxiao Shib54aabd2018-04-16 19:36:24 +000072 ("link-file", po::value<std::string>(),
73 "set ForwardingHint from a file")
Zhuo Lib3558892016-08-12 15:51:12 -070074 ("lifetime,l", po::value<int>(),
75 "set InterestLifetime (in milliseconds)")
Zhuo Lib3558892016-08-12 15:51:12 -070076 ;
77
78 po::options_description visibleOptDesc;
79 visibleOptDesc.add(genericOptDesc).add(interestOptDesc);
80
81 po::options_description hiddenOptDesc;
82 hiddenOptDesc.add_options()
Junxiao Shib54aabd2018-04-16 19:36:24 +000083 ("name", po::value<std::string>(), "Interest name");
Zhuo Lib3558892016-08-12 15:51:12 -070084
85 po::options_description optDesc;
86 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
87
88 po::positional_options_description optPos;
Junxiao Shib54aabd2018-04-16 19:36:24 +000089 optPos.add("name", -1);
Zhuo Lib3558892016-08-12 15:51:12 -070090
91 po::variables_map vm;
92 try {
93 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
94 po::notify(vm);
95 }
96 catch (const po::error& e) {
97 std::cerr << "ERROR: " << e.what() << std::endl;
98 return 2;
99 }
100
101 if (vm.count("help") > 0) {
102 usage(std::cout, visibleOptDesc);
103 return 0;
104 }
105
106 if (vm.count("version") > 0) {
107 std::cout << "ndnpeek " << tools::VERSION << std::endl;
108 return 0;
109 }
110
Junxiao Shib54aabd2018-04-16 19:36:24 +0000111 if (vm.count("name") > 0) {
112 options.name = vm["name"].as<std::string>();
Zhuo Lib3558892016-08-12 15:51:12 -0700113 }
114 else {
115 std::cerr << "ERROR: Interest name is missing" << std::endl;
116 usage(std::cerr, visibleOptDesc);
117 return 2;
118 }
119
Zhuo Lib3558892016-08-12 15:51:12 -0700120 if (vm.count("lifetime") > 0) {
121 if (vm["lifetime"].as<int>() >= 0) {
122 options.interestLifetime = time::milliseconds(vm["lifetime"].as<int>());
123 }
124 else {
125 std::cerr << "ERROR: InterestLifetime must be a non-negative integer" << std::endl;
126 usage(std::cerr, visibleOptDesc);
127 return 2;
128 }
129 }
130
131 if (vm.count("timeout") > 0) {
132 if (vm["timeout"].as<int>() > 0) {
133 options.timeout = time::milliseconds(vm["timeout"].as<int>());
134 }
135 else {
136 std::cerr << "ERROR: Timeout must be a positive integer" << std::endl;
137 usage(std::cerr, visibleOptDesc);
138 return 2;
139 }
140 }
141
142 if (vm.count("link-file") > 0) {
143 options.link = io::load<Link>(vm["link-file"].as<std::string>());
144 if (options.link == nullptr) {
145 std::cerr << "ERROR: Cannot read Link object from the specified file" << std::endl;
146 usage(std::cerr, visibleOptDesc);
147 return 2;
148 }
149 }
150
151 Face face;
152 NdnPeek program(face, options);
153
154 try {
155 program.start();
156 face.processEvents(program.getTimeout());
157 }
158 catch (const std::exception& e) {
159 std::cerr << "ERROR: " << e.what() << std::endl;
160 return 1;
161 }
162
163 ResultCode result = program.getResultCode();
164 if (result == ResultCode::TIMEOUT && options.isVerbose) {
165 std::cerr << "TIMEOUT" << std::endl;
166 }
167 return static_cast<int>(result);
168}
169
170} // namespace peek
171} // namespace ndn
172
173int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400174main(int argc, char* argv[])
Zhuo Lib3558892016-08-12 15:51:12 -0700175{
176 return ndn::peek::main(argc, argv);
177}