Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 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 | |
| 34 | namespace ndn { |
| 35 | namespace peek { |
| 36 | |
| 37 | namespace po = boost::program_options; |
| 38 | |
| 39 | static void |
| 40 | usage(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 | |
| 49 | static int |
| 50 | main(int argc, char* argv[]) |
| 51 | { |
| 52 | PeekOptions options; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 53 | |
| 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 Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 68 | ("prefix,P", po::bool_switch(&options.canBePrefix), |
| 69 | "set CanBePrefix") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 70 | ("fresh,f", po::bool_switch(&options.mustBeFresh), |
| 71 | "set MustBeFresh") |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 72 | ("link-file", po::value<std::string>(), |
| 73 | "set ForwardingHint from a file") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 74 | ("lifetime,l", po::value<int>(), |
| 75 | "set InterestLifetime (in milliseconds)") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 76 | ; |
| 77 | |
| 78 | po::options_description visibleOptDesc; |
| 79 | visibleOptDesc.add(genericOptDesc).add(interestOptDesc); |
| 80 | |
| 81 | po::options_description hiddenOptDesc; |
| 82 | hiddenOptDesc.add_options() |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 83 | ("name", po::value<std::string>(), "Interest name"); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 84 | |
| 85 | po::options_description optDesc; |
| 86 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 87 | |
| 88 | po::positional_options_description optPos; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 89 | optPos.add("name", -1); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 90 | |
| 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 Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 111 | if (vm.count("name") > 0) { |
| 112 | options.name = vm["name"].as<std::string>(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 113 | } |
| 114 | else { |
| 115 | std::cerr << "ERROR: Interest name is missing" << std::endl; |
| 116 | usage(std::cerr, visibleOptDesc); |
| 117 | return 2; |
| 118 | } |
| 119 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 120 | 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 | |
| 173 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame^] | 174 | main(int argc, char* argv[]) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 175 | { |
| 176 | return ndn::peek::main(argc, argv); |
| 177 | } |