status-main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "status-main.hpp"
27 #include "core/version.hpp"
28 #include "status-report.hpp"
30 #include "channel-module.hpp"
31 #include "face-module.hpp"
32 #include "fib-module.hpp"
33 #include "rib-module.hpp"
35 
36 #include <ndn-cxx/security/validator-null.hpp>
37 #include <boost/program_options.hpp>
38 
39 namespace nfd {
40 namespace tools {
41 namespace nfdc {
42 
43 struct Options
44 {
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 
66 static std::tuple<int, Options>
67 parseCommandLine(const std::vector<std::string>& args)
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 {
85  po::store(po::command_line_parser(args).options(cmdOptions).run(), vm);
86  po::notify(vm);
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  }
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) {
104  options.output = ReportFormat::XML;
105  }
106  if (options.output == ReportFormat::XML ||
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 
116 int
117 statusMain(const std::vector<std::string>& args, Face& face, KeyChain& keyChain)
118 {
119  int exitCode = -1;
120  Options options;
121  std::tie(exitCode, options) = parseCommandLine(args);
122  if (exitCode >= 0) {
123  return exitCode;
124  }
125 
126  unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>();
127  CommandOptions ctrlOptions;
128 
129  StatusReport report;
130 
131  if (options.wantForwarderGeneral) {
132  auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator));
133  auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>();
134  forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector);
135  report.sections.push_back(std::move(forwarderGeneralModule));
136  validator = std::move(nfdIdCollector);
137  }
138 
139  if (options.wantChannels) {
140  report.sections.push_back(make_unique<ChannelModule>());
141  }
142 
143  if (options.wantFaces) {
144  report.sections.push_back(make_unique<FaceModule>());
145  }
146 
147  if (options.wantFib) {
148  report.sections.push_back(make_unique<FibModule>());
149  }
150 
151  if (options.wantRib) {
152  report.sections.push_back(make_unique<RibModule>());
153  }
154 
155  if (options.wantStrategyChoice) {
156  report.sections.push_back(make_unique<StrategyChoiceModule>());
157  }
158 
159  uint32_t code = report.collect(face, keyChain, *validator, ctrlOptions);
160  if (code != 0) {
161  // Give a simple error code for end user.
162  // Technical support personnel:
163  // 1. get the exact command from end user
164  // 2. code div 1000000 is zero-based section index
165  // 3. code mod 1000000 is a Controller.fetch error code
166  std::cerr << "Error while collecting status report (" << code << ").\n";
167  return 1;
168  }
169 
170  switch (options.output) {
171  case ReportFormat::XML:
172  report.formatXml(std::cout);
173  break;
174  case ReportFormat::TEXT:
175  report.formatText(std::cout);
176  break;
177  }
178  return 0;
179 }
180 
181 } // namespace nfdc
182 } // namespace tools
183 } // namespace nfd
std::vector< unique_ptr< Module > > sections
modules through which status is collected
void formatXml(std::ostream &os) const
print an XML report
uint32_t collect(Face &face, KeyChain &keyChain, Validator &validator, const CommandOptions &options)
collect status via chosen sections
int statusMain(const std::vector< std::string > &args, Face &face, KeyChain &keyChain)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
collects and prints NFD status report
static void showUsage(std::ostream &os, const boost::program_options::options_description &cmdOptions)
Definition: status-main.cpp:55
void formatText(std::ostream &os) const
print a text report
static std::tuple< int, Options > parseCommandLine(const std::vector< std::string > &args)
parse command line, and show usage if necessary
Definition: status-main.cpp:67