Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 3 | * Copyright (c) 2016-2019, Regents of the University of California, |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Wentao Shang |
| 24 | * @author Steve DiBenedetto |
| 25 | * @author Andrea Tosatto |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 26 | * @author Davide Pesavento |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 27 | * @author Klaus Schneider |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include "core/version.hpp" |
| 31 | #include "producer.hpp" |
| 32 | |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 33 | namespace po = boost::program_options; |
| 34 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | namespace ndn { |
| 36 | namespace chunks { |
| 37 | |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 38 | static void |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 39 | usage(std::ostream& os, const std::string& programName, const po::options_description& desc) |
| 40 | { |
| 41 | os << "Usage: " << programName << " [options] ndn:/name\n" |
| 42 | << "\n" |
| 43 | << "Publish data under the specified prefix.\n" |
| 44 | << "Note: this tool expects data from the standard input.\n" |
| 45 | << "\n" |
| 46 | << desc; |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 49 | static int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 50 | main(int argc, char* argv[]) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 51 | { |
| 52 | std::string programName = argv[0]; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 53 | std::string prefix; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 54 | std::string signingStr; |
| 55 | Producer::Options opts; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 56 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 57 | po::options_description visibleDesc("Options"); |
| 58 | visibleDesc.add_options() |
| 59 | ("help,h", "print this help message and exit") |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 60 | ("freshness,f", po::value<time::milliseconds::rep>()->default_value(opts.freshnessPeriod.count()), |
| 61 | "FreshnessPeriod of the published Data packets, in milliseconds") |
| 62 | ("print-data-version,p", po::bool_switch(&opts.wantShowVersion), |
| 63 | "print Data version to the standard output") |
| 64 | ("size,s", po::value<size_t>(&opts.maxSegmentSize)->default_value(opts.maxSegmentSize), |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 65 | "maximum chunk size, in bytes") |
Klaus Schneider | e90a358 | 2019-04-03 17:22:39 -0700 | [diff] [blame] | 66 | ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnputchunks' for usage") |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 67 | ("quiet,q", po::bool_switch(&opts.isQuiet), "turn off all non-error output") |
| 68 | ("verbose,v", po::bool_switch(&opts.isVerbose), "turn on verbose output (per Interest information)") |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | ("version,V", "print program version and exit") |
| 70 | ; |
| 71 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 72 | po::options_description hiddenDesc; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 73 | hiddenDesc.add_options() |
| 74 | ("ndn-name,n", po::value<std::string>(&prefix), "NDN name for the served content"); |
| 75 | |
| 76 | po::positional_options_description p; |
| 77 | p.add("ndn-name", -1); |
| 78 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 79 | po::options_description optDesc; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 80 | optDesc.add(visibleDesc).add(hiddenDesc); |
| 81 | |
| 82 | po::variables_map vm; |
| 83 | try { |
| 84 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm); |
| 85 | po::notify(vm); |
| 86 | } |
| 87 | catch (const po::error& e) { |
| 88 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 89 | return 2; |
| 90 | } |
| 91 | catch (const boost::bad_any_cast& e) { |
| 92 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 93 | return 2; |
| 94 | } |
| 95 | |
| 96 | if (vm.count("help") > 0) { |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 97 | usage(std::cout, programName, visibleDesc); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | if (vm.count("version") > 0) { |
| 102 | std::cout << "ndnputchunks " << tools::VERSION << std::endl; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | if (prefix.empty()) { |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 107 | usage(std::cerr, programName, visibleDesc); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 108 | return 2; |
| 109 | } |
| 110 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 111 | opts.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame^] | 112 | if (opts.freshnessPeriod < 0_ms) { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 113 | std::cerr << "ERROR: FreshnessPeriod cannot be negative" << std::endl; |
| 114 | return 2; |
| 115 | } |
| 116 | |
| 117 | if (opts.maxSegmentSize < 1 || opts.maxSegmentSize > MAX_NDN_PACKET_SIZE) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 118 | std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl; |
| 119 | return 2; |
| 120 | } |
| 121 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 122 | try { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 123 | opts.signingInfo = security::SigningInfo(signingStr); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 124 | } |
| 125 | catch (const std::invalid_argument& e) { |
| 126 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 127 | return 2; |
| 128 | } |
| 129 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 130 | if (opts.isQuiet && opts.isVerbose) { |
| 131 | std::cerr << "ERROR: Cannot be quiet and verbose at the same time" << std::endl; |
| 132 | return 2; |
| 133 | } |
| 134 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 135 | try { |
| 136 | Face face; |
| 137 | KeyChain keyChain; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 138 | Producer producer(prefix, face, keyChain, std::cin, opts); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 139 | producer.run(); |
| 140 | } |
| 141 | catch (const std::exception& e) { |
| 142 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | } // namespace chunks |
| 150 | } // namespace ndn |
| 151 | |
| 152 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 153 | main(int argc, char* argv[]) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 154 | { |
| 155 | return ndn::chunks::main(argc, argv); |
| 156 | } |