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 | |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame^] | 34 | #include <cerrno> |
| 35 | #include <cstring> |
| 36 | #include <fstream> |
| 37 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 38 | namespace ndn { |
| 39 | namespace peek { |
| 40 | |
| 41 | namespace po = boost::program_options; |
| 42 | |
| 43 | static void |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 44 | usage(std::ostream& os, const std::string& program, const po::options_description& options) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 45 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 46 | os << "Usage: " << program << " [options] /name\n" |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 47 | << "\n" |
| 48 | << "Fetch one data item matching the specified name and write it to the standard output.\n" |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 49 | << options; |
| 50 | } |
| 51 | |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame^] | 52 | static std::ifstream |
| 53 | openBinaryFile(const std::string& filename) |
| 54 | { |
| 55 | std::ifstream file(filename, std::ios::binary); |
| 56 | if (!file) { |
| 57 | std::cerr << "ERROR: cannot open '" << filename << "' for reading: " |
| 58 | << std::strerror(errno) << std::endl; |
| 59 | } |
| 60 | return file; |
| 61 | } |
| 62 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 63 | static int |
| 64 | main(int argc, char* argv[]) |
| 65 | { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 66 | std::string progName(argv[0]); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 67 | PeekOptions options; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 68 | |
| 69 | po::options_description genericOptDesc("Generic options"); |
| 70 | genericOptDesc.add_options() |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 71 | ("help,h", "print help and exit") |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 72 | ("payload,p", po::bool_switch(&options.wantPayloadOnly), |
| 73 | "print payload only, instead of full packet") |
| 74 | ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds") |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 75 | ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 76 | ("version,V", "print version and exit") |
| 77 | ; |
| 78 | |
| 79 | po::options_description interestOptDesc("Interest construction"); |
| 80 | interestOptDesc.add_options() |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 81 | ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix") |
| 82 | ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh") |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame^] | 83 | ("link-file", po::value<std::string>(), "set ForwardingHint from a raw binary file") |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 84 | ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()), |
| 85 | "set InterestLifetime, in milliseconds") |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 86 | ; |
| 87 | |
| 88 | po::options_description visibleOptDesc; |
| 89 | visibleOptDesc.add(genericOptDesc).add(interestOptDesc); |
| 90 | |
| 91 | po::options_description hiddenOptDesc; |
| 92 | hiddenOptDesc.add_options() |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 93 | ("name", po::value<std::string>(), "Interest name"); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 94 | |
| 95 | po::options_description optDesc; |
| 96 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 97 | |
| 98 | po::positional_options_description optPos; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 99 | optPos.add("name", -1); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 100 | |
| 101 | po::variables_map vm; |
| 102 | try { |
| 103 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| 104 | po::notify(vm); |
| 105 | } |
| 106 | catch (const po::error& e) { |
| 107 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 108 | return 2; |
| 109 | } |
| 110 | |
| 111 | if (vm.count("help") > 0) { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 112 | usage(std::cout, progName, visibleOptDesc); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | if (vm.count("version") > 0) { |
| 117 | std::cout << "ndnpeek " << tools::VERSION << std::endl; |
| 118 | return 0; |
| 119 | } |
| 120 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 121 | if (vm.count("name") == 0) { |
| 122 | std::cerr << "ERROR: missing name\n\n"; |
| 123 | usage(std::cerr, progName, visibleOptDesc); |
| 124 | return 2; |
| 125 | } |
| 126 | |
| 127 | try { |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 128 | options.name = vm["name"].as<std::string>(); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 129 | } |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 130 | catch (const Name::Error& e) { |
| 131 | std::cerr << "ERROR: invalid name: " << e.what() << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 132 | return 2; |
| 133 | } |
| 134 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 135 | if (vm.count("timeout") > 0) { |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 136 | options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>()); |
| 137 | if (*options.timeout < 0_ms) { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 138 | std::cerr << "ERROR: timeout cannot be negative" << std::endl; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 139 | return 2; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (vm.count("link-file") > 0) { |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame^] | 144 | auto filename = vm["link-file"].as<std::string>(); |
| 145 | std::ifstream linkFile = openBinaryFile(filename); |
| 146 | if (!linkFile) { |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 147 | return 2; |
| 148 | } |
Davide Pesavento | 50cf6db | 2019-07-29 20:40:10 -0400 | [diff] [blame^] | 149 | options.link = io::load<Link>(linkFile, io::NO_ENCODING); |
| 150 | if (!options.link) { |
| 151 | std::cerr << "ERROR: cannot parse a valid Link object from file '" << filename << "'" << std::endl; |
| 152 | return 2; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>()); |
| 157 | if (options.interestLifetime < 0_ms) { |
| 158 | std::cerr << "ERROR: lifetime cannot be negative" << std::endl; |
| 159 | return 2; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 162 | try { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 163 | Face face; |
| 164 | NdnPeek program(face, options); |
| 165 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 166 | program.start(); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 167 | face.processEvents(); |
| 168 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 169 | return static_cast<int>(program.getResult()); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 170 | } |
| 171 | catch (const std::exception& e) { |
| 172 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 173 | return 1; |
| 174 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | } // namespace peek |
| 178 | } // namespace ndn |
| 179 | |
| 180 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 181 | main(int argc, char* argv[]) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 182 | { |
| 183 | return ndn::peek::main(argc, argv); |
| 184 | } |