status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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 
26 #include "status.hpp"
28 #include "channel-module.hpp"
29 #include "face-module.hpp"
30 #include "fib-module.hpp"
31 #include "rib-module.hpp"
33 
34 #include <ndn-cxx/security/validator-null.hpp>
35 
36 namespace nfd {
37 namespace tools {
38 namespace nfdc {
39 
40 void
42 {
43  StatusReport report;
44 
45  if (options.wantForwarderGeneral) {
46  report.sections.push_back(make_unique<ForwarderGeneralModule>());
47  }
48 
49  if (options.wantChannels) {
50  report.sections.push_back(make_unique<ChannelModule>());
51  }
52 
53  if (options.wantFaces) {
54  report.sections.push_back(make_unique<FaceModule>());
55  }
56 
57  if (options.wantFib) {
58  report.sections.push_back(make_unique<FibModule>());
59  }
60 
61  if (options.wantRib) {
62  report.sections.push_back(make_unique<RibModule>());
63  }
64 
65  if (options.wantStrategyChoice) {
66  report.sections.push_back(make_unique<StrategyChoiceModule>());
67  }
68 
69  uint32_t code = report.collect(ctx.face, ctx.keyChain,
70  ndn::security::v2::getAcceptAllValidator(),
71  CommandOptions());
72  if (code != 0) {
73  ctx.exitCode = 1;
74  // Give a simple error code for end user.
75  // Technical support personnel:
76  // 1. get the exact command from end user
77  // 2. code div 1000000 is zero-based section index
78  // 3. code mod 1000000 is a Controller.fetch error code
79  ctx.err << "Error while collecting status report (" << code << ").\n";
80  }
81 
82  switch (options.output) {
83  case ReportFormat::XML:
84  report.formatXml(ctx.out);
85  break;
86  case ReportFormat::TEXT:
87  report.formatText(ctx.out);
88  break;
89  }
90 }
91 
94 static void
96 {
97  StatusReportOptions options;
98  options.*wantSection = true;
99  reportStatus(ctx, options);
100 }
101 
104 static void
106 {
107  StatusReportOptions options;
108  options.output = ctx.args.get<ReportFormat>("format", ReportFormat::TEXT);
109  options.wantForwarderGeneral = options.wantChannels = options.wantFaces =
110  options.wantFib = options.wantRib = options.wantStrategyChoice = true;
111  reportStatus(ctx, options);
112 }
113 
114 void
116 {
117  CommandDefinition defStatusReport("status", "report");
118  defStatusReport
119  .setTitle("print NFD status report")
121  parser.addCommand(defStatusReport, &reportStatusComprehensive);
122 
123  CommandDefinition defStatusShow("status", "show");
124  defStatusShow
125  .setTitle("print general status");
127  parser.addAlias("status", "show", "list");
128 
129  CommandDefinition defChannelList("channel", "list");
130  defChannelList
131  .setTitle("print channel list");
132  parser.addCommand(defChannelList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantChannels));
133 
134  CommandDefinition defFibList("fib", "list");
135  defFibList
136  .setTitle("print FIB entries");
137  parser.addCommand(defFibList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFib));
138 }
139 
140 } // namespace nfdc
141 } // namespace tools
142 } // namespace nfd
const CommandArguments & args
void registerStatusCommands(CommandParser &parser)
registers status commands
Definition: status.cpp:115
std::vector< unique_ptr< Module > > sections
modules through which status is collected
declares semantics of a command
std::ostream & out
output stream
context for command execution
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
void reportStatus(ExecuteContext &ctx, const StatusReportOptions &options)
collect a status report and write to stdout
Definition: status.cpp:41
T get(const std::string &key, const T &defaultValue=T()) const
CommandDefinition & setTitle(const std::string &title)
set one-line description
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
collects and prints NFD status report
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
declare an argument
static void reportStatusComprehensive(ExecuteContext &ctx)
the &#39;status report&#39; command
Definition: status.cpp:105
static void reportStatusSingleSection(ExecuteContext &ctx, bool StatusReportOptions::*wantSection)
single-section status command
Definition: status.cpp:95
argument is required
std::ostream & err
error stream
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type< AvailableIn >::type modes=AVAILABLE_IN_ALL)
add an available command
argument is optional
CommandParser & addAlias(const std::string &noun, const std::string &verb, const std::string &verb2)
add an alias "noun verb2" to existing command "noun verb"
void formatText(std::ostream &os) const
print a text report
report format &#39;xml&#39; or &#39;text&#39;