Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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 NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD 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 | * NFD 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 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 26 | #include "status-main.hpp" |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 27 | #include "core/version.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 28 | #include "status-report.hpp" |
| 29 | #include "forwarder-general-module.hpp" |
| 30 | #include "channel-module.hpp" |
| 31 | #include "face-module.hpp" |
| 32 | #include "fib-module.hpp" |
| 33 | #include "rib-module.hpp" |
| 34 | #include "strategy-choice-module.hpp" |
| 35 | |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 36 | #include <ndn-cxx/security/validator-null.hpp> |
| 37 | #include <boost/program_options.hpp> |
| 38 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 39 | namespace nfd { |
| 40 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 41 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 42 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 43 | struct Options |
| 44 | { |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 45 | ReportFormat output = ReportFormat::TEXT; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 46 | bool wantForwarderGeneral = false; |
| 47 | bool wantChannels = false; |
| 48 | bool wantFaces = false; |
| 49 | bool wantFib = false; |
| 50 | bool wantRib = false; |
| 51 | bool wantStrategyChoice = false; |
| 52 | }; |
| 53 | |
| 54 | static void |
| 55 | showUsage(std::ostream& os, const boost::program_options::options_description& cmdOptions) |
| 56 | { |
| 57 | os << "Usage: nfd-status [options]\n\n" |
| 58 | << "Show NFD version and status information.\n\n" |
| 59 | << cmdOptions; |
| 60 | } |
| 61 | |
| 62 | /** \brief parse command line, and show usage if necessary |
| 63 | * \return if first item is -1, caller should retrieve and display StatusReport; |
| 64 | * otherwise, caller should immediately exit with the specified exit code |
| 65 | */ |
| 66 | static std::tuple<int, Options> |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 67 | parseCommandLine(const std::vector<std::string>& args) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 68 | { |
| 69 | Options options; |
| 70 | |
| 71 | namespace po = boost::program_options; |
| 72 | po::options_description cmdOptions("Options"); |
| 73 | cmdOptions.add_options() |
| 74 | ("help,h", "print this help message") |
| 75 | ("version,V", "show program version") |
| 76 | ("general,v", po::bool_switch(&options.wantForwarderGeneral), "show general status") |
| 77 | ("channels,c", po::bool_switch(&options.wantChannels), "show channels") |
| 78 | ("faces,f", po::bool_switch(&options.wantFaces), "show faces") |
| 79 | ("fib,b", po::bool_switch(&options.wantFib), "show FIB entries") |
| 80 | ("rib,r", po::bool_switch(&options.wantRib), "show RIB routes") |
| 81 | ("sc,s", po::bool_switch(&options.wantStrategyChoice), "show strategy choice entries") |
| 82 | ("xml,x", "output as XML instead of text (implies -vcfbrs)"); |
| 83 | po::variables_map vm; |
| 84 | try { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 85 | po::store(po::command_line_parser(args).options(cmdOptions).run(), vm); |
| 86 | po::notify(vm); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 87 | } |
| 88 | catch (const po::error& e) { |
| 89 | std::cerr << e.what() << "\n"; |
| 90 | showUsage(std::cerr, cmdOptions); |
| 91 | return std::make_tuple(2, options); |
| 92 | } |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 93 | |
| 94 | if (vm.count("help") > 0) { |
| 95 | showUsage(std::cout, cmdOptions); |
| 96 | return std::make_tuple(0, options); |
| 97 | } |
| 98 | if (vm.count("version") > 0) { |
| 99 | std::cout << "nfd-status " << NFD_VERSION_BUILD_STRING << "\n"; |
| 100 | return std::make_tuple(0, options); |
| 101 | } |
| 102 | |
| 103 | if (vm.count("xml") > 0) { |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 104 | options.output = ReportFormat::XML; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 105 | } |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 106 | if (options.output == ReportFormat::XML || |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 107 | (!options.wantForwarderGeneral && !options.wantChannels && !options.wantFaces && |
| 108 | !options.wantFib && !options.wantRib && !options.wantStrategyChoice)) { |
| 109 | options.wantForwarderGeneral = options.wantChannels = options.wantFaces = |
| 110 | options.wantFib = options.wantRib = options.wantStrategyChoice = true; |
| 111 | } |
| 112 | |
| 113 | return std::make_tuple(-1, options); |
| 114 | } |
| 115 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 116 | int |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 117 | statusMain(const std::vector<std::string>& args) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 118 | { |
| 119 | int exitCode = -1; |
| 120 | Options options; |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 121 | std::tie(exitCode, options) = parseCommandLine(args); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 122 | if (exitCode >= 0) { |
| 123 | return exitCode; |
| 124 | } |
| 125 | |
| 126 | Face face; |
| 127 | KeyChain keyChain; |
| 128 | unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>(); |
| 129 | CommandOptions ctrlOptions; |
| 130 | |
| 131 | StatusReport report; |
| 132 | |
| 133 | if (options.wantForwarderGeneral) { |
| 134 | auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator)); |
| 135 | auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>(); |
| 136 | forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector); |
| 137 | report.sections.push_back(std::move(forwarderGeneralModule)); |
| 138 | validator = std::move(nfdIdCollector); |
| 139 | } |
| 140 | |
| 141 | if (options.wantChannels) { |
| 142 | report.sections.push_back(make_unique<ChannelModule>()); |
| 143 | } |
| 144 | |
| 145 | if (options.wantFaces) { |
| 146 | report.sections.push_back(make_unique<FaceModule>()); |
| 147 | } |
| 148 | |
| 149 | if (options.wantFib) { |
| 150 | report.sections.push_back(make_unique<FibModule>()); |
| 151 | } |
| 152 | |
| 153 | if (options.wantRib) { |
| 154 | report.sections.push_back(make_unique<RibModule>()); |
| 155 | } |
| 156 | |
| 157 | if (options.wantStrategyChoice) { |
| 158 | report.sections.push_back(make_unique<StrategyChoiceModule>()); |
| 159 | } |
| 160 | |
| 161 | uint32_t code = report.collect(face, keyChain, *validator, ctrlOptions); |
| 162 | if (code != 0) { |
| 163 | // Give a simple error code for end user. |
| 164 | // Technical support personnel: |
| 165 | // 1. get the exact command from end user |
| 166 | // 2. code div 1000000 is zero-based section index |
| 167 | // 3. code mod 1000000 is a Controller.fetch error code |
| 168 | std::cerr << "Error while collecting status report (" << code << ").\n"; |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | switch (options.output) { |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 173 | case ReportFormat::XML: |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 174 | report.formatXml(std::cout); |
| 175 | break; |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 176 | case ReportFormat::TEXT: |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 177 | report.formatText(std::cout); |
| 178 | break; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 183 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 184 | } // namespace tools |
| 185 | } // namespace nfd |