blob: 70977aa54a824d1de95776d48fb6dcec165ac81c [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")
Davide Pesavento0da1f442019-07-26 17:38:13 -040057 ("payload,p", po::bool_switch(&options.wantPayloadOnly),
58 "print payload only, instead of full packet")
59 ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds")
Davide Pesavento65d11552019-06-09 19:15:50 -040060 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output")
Zhuo Lib3558892016-08-12 15:51:12 -070061 ("version,V", "print version and exit")
62 ;
63
64 po::options_description interestOptDesc("Interest construction");
65 interestOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040066 ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix")
67 ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh")
68 ("link-file", po::value<std::string>(), "set ForwardingHint from a file")
Davide Pesavento0da1f442019-07-26 17:38:13 -040069 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
70 "set InterestLifetime, in milliseconds")
Zhuo Lib3558892016-08-12 15:51:12 -070071 ;
72
73 po::options_description visibleOptDesc;
74 visibleOptDesc.add(genericOptDesc).add(interestOptDesc);
75
76 po::options_description hiddenOptDesc;
77 hiddenOptDesc.add_options()
Junxiao Shib54aabd2018-04-16 19:36:24 +000078 ("name", po::value<std::string>(), "Interest name");
Zhuo Lib3558892016-08-12 15:51:12 -070079
80 po::options_description optDesc;
81 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
82
83 po::positional_options_description optPos;
Junxiao Shib54aabd2018-04-16 19:36:24 +000084 optPos.add("name", -1);
Zhuo Lib3558892016-08-12 15:51:12 -070085
86 po::variables_map vm;
87 try {
88 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
89 po::notify(vm);
90 }
91 catch (const po::error& e) {
92 std::cerr << "ERROR: " << e.what() << std::endl;
93 return 2;
94 }
95
96 if (vm.count("help") > 0) {
Davide Pesavento65d11552019-06-09 19:15:50 -040097 usage(std::cout, progName, visibleOptDesc);
Zhuo Lib3558892016-08-12 15:51:12 -070098 return 0;
99 }
100
101 if (vm.count("version") > 0) {
102 std::cout << "ndnpeek " << tools::VERSION << std::endl;
103 return 0;
104 }
105
Davide Pesavento65d11552019-06-09 19:15:50 -0400106 if (vm.count("name") == 0) {
107 std::cerr << "ERROR: missing name\n\n";
108 usage(std::cerr, progName, visibleOptDesc);
109 return 2;
110 }
111
112 try {
Junxiao Shib54aabd2018-04-16 19:36:24 +0000113 options.name = vm["name"].as<std::string>();
Zhuo Lib3558892016-08-12 15:51:12 -0700114 }
Davide Pesavento65d11552019-06-09 19:15:50 -0400115 catch (const Name::Error& e) {
116 std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700117 return 2;
118 }
119
Davide Pesavento0da1f442019-07-26 17:38:13 -0400120 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
121 if (options.interestLifetime < 0_ms) {
122 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
123 return 2;
Zhuo Lib3558892016-08-12 15:51:12 -0700124 }
125
126 if (vm.count("timeout") > 0) {
Davide Pesavento0da1f442019-07-26 17:38:13 -0400127 options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>());
128 if (*options.timeout < 0_ms) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400129 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700130 return 2;
131 }
132 }
133
134 if (vm.count("link-file") > 0) {
135 options.link = io::load<Link>(vm["link-file"].as<std::string>());
136 if (options.link == nullptr) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400137 std::cerr << "ERROR: cannot read Link object from the specified file" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700138 return 2;
139 }
140 }
141
Zhuo Lib3558892016-08-12 15:51:12 -0700142 try {
Davide Pesavento65d11552019-06-09 19:15:50 -0400143 Face face;
144 NdnPeek program(face, options);
145
Zhuo Lib3558892016-08-12 15:51:12 -0700146 program.start();
Davide Pesavento65d11552019-06-09 19:15:50 -0400147 face.processEvents();
148
Davide Pesavento87434be2019-07-25 19:04:23 -0400149 return static_cast<int>(program.getResult());
Zhuo Lib3558892016-08-12 15:51:12 -0700150 }
151 catch (const std::exception& e) {
152 std::cerr << "ERROR: " << e.what() << std::endl;
153 return 1;
154 }
Zhuo Lib3558892016-08-12 15:51:12 -0700155}
156
157} // namespace peek
158} // namespace ndn
159
160int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400161main(int argc, char* argv[])
Zhuo Lib3558892016-08-12 15:51:12 -0700162{
163 return ndn::peek::main(argc, argv);
164}