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 | /* |
| 3 | * Copyright (c) 2016-2017, Regents of the University of California, |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
| 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 |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame^] | 26 | * @author Klaus Schneider |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "core/version.hpp" |
| 30 | #include "producer.hpp" |
| 31 | |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 32 | namespace po = boost::program_options; |
| 33 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 34 | namespace ndn { |
| 35 | namespace chunks { |
| 36 | |
Weiwei Liu | 85de842 | 2016-09-28 03:28:31 +0000 | [diff] [blame] | 37 | static void |
| 38 | usage(std::ostream& os, const std::string& programName, const po::options_description& visibleDesc) { |
| 39 | os << "Usage: " << programName << " [options] ndn:/name" << std::endl; |
| 40 | os << "\nPublish data under specified prefix. " |
| 41 | << "Note: this tool expects data from the standard input.\n" << std::endl; |
| 42 | os << visibleDesc; |
| 43 | } |
| 44 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | static int |
| 46 | main(int argc, char** argv) |
| 47 | { |
| 48 | std::string programName = argv[0]; |
| 49 | uint64_t freshnessPeriod = 10000; |
| 50 | bool printVersion = false; |
| 51 | size_t maxChunkSize = MAX_NDN_PACKET_SIZE >> 1; |
| 52 | std::string signingStr; |
| 53 | bool isVerbose = false; |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame^] | 54 | bool isQuiet = false; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 55 | std::string prefix; |
| 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") |
| 60 | ("freshness,f", po::value<uint64_t>(&freshnessPeriod)->default_value(freshnessPeriod), |
| 61 | "specify FreshnessPeriod, in milliseconds") |
| 62 | ("print-data-version,p", po::bool_switch(&printVersion), "print Data version to the standard output") |
| 63 | ("size,s", po::value<size_t>(&maxChunkSize)->default_value(maxChunkSize), |
| 64 | "maximum chunk size, in bytes") |
| 65 | ("signing-info,S", po::value<std::string>(&signingStr)->default_value(signingStr), |
| 66 | "set signing information") |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame^] | 67 | ("quiet,q", po::bool_switch(&isQuiet), "turn off all non-error output") |
| 68 | ("verbose,v", po::bool_switch(&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 | |
| 72 | po::options_description hiddenDesc("Hidden options"); |
| 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 | |
| 79 | po::options_description optDesc("Allowed options"); |
| 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 | |
| 111 | if (maxChunkSize < 1 || maxChunkSize > MAX_NDN_PACKET_SIZE) { |
| 112 | std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl; |
| 113 | return 2; |
| 114 | } |
| 115 | |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame^] | 116 | if (isQuiet && isVerbose) { |
| 117 | std::cerr << "ERROR: Cannot use flags -q and -v at the same time\n"; |
| 118 | return 2; |
| 119 | } |
| 120 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 121 | security::SigningInfo signingInfo; |
| 122 | try { |
| 123 | signingInfo = security::SigningInfo(signingStr); |
| 124 | } |
| 125 | catch (const std::invalid_argument& e) { |
| 126 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 127 | return 2; |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | Face face; |
| 132 | KeyChain keyChain; |
| 133 | Producer producer(prefix, face, keyChain, signingInfo, time::milliseconds(freshnessPeriod), |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame^] | 134 | maxChunkSize, isQuiet, isVerbose, printVersion, std::cin); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 135 | producer.run(); |
| 136 | } |
| 137 | catch (const std::exception& e) { |
| 138 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | } // namespace chunks |
| 146 | } // namespace ndn |
| 147 | |
| 148 | int |
| 149 | main(int argc, char** argv) |
| 150 | { |
| 151 | return ndn::chunks::main(argc, argv); |
| 152 | } |