strategy-choice-module.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 
27 #include "format-helpers.hpp"
28 
29 namespace nfd::tools::nfdc {
30 
31 void
33 {
34  CommandDefinition defStrategyList("strategy", "list");
35  defStrategyList
36  .setTitle("print strategy choices");
37  parser.addCommand(defStrategyList, &StrategyChoiceModule::list);
38  parser.addAlias("strategy", "list", "");
39 
40  CommandDefinition defStrategyShow("strategy", "show");
41  defStrategyShow
42  .setTitle("show strategy choice of an entry")
44  parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
45 
46  CommandDefinition defStrategySet("strategy", "set");
47  defStrategySet
48  .setTitle("set strategy choice for a name prefix")
51  parser.addCommand(defStrategySet, &StrategyChoiceModule::set);
52 
53  CommandDefinition defStrategyUnset("strategy", "unset");
54  defStrategyUnset
55  .setTitle("clear strategy choice at a name prefix")
57  parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset);
58 }
59 
60 void
62 {
63  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
64  [&] (const std::vector<StrategyChoice>& dataset) {
65  for (const StrategyChoice& entry : dataset) {
66  formatItemText(ctx.out, entry);
67  ctx.out << '\n';
68  }
69  },
70  ctx.makeDatasetFailureHandler("strategy choice dataset"),
71  ctx.makeCommandOptions());
72 
73  ctx.face.processEvents();
74 }
75 
76 void
78 {
79  auto prefix = ctx.args.get<Name>("prefix");
80 
81  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
82  [&] (const std::vector<StrategyChoice>& dataset) {
83  StrategyChoice match; // longest prefix match
84  for (const StrategyChoice& entry : dataset) {
85  if (entry.getName().isPrefixOf(prefix) &&
86  entry.getName().size() >= match.getName().size()) {
87  match = entry;
88  }
89  }
90  formatItemText(ctx.out, match, true);
91  },
92  ctx.makeDatasetFailureHandler("strategy choice dataset"),
93  ctx.makeCommandOptions());
94 
95  ctx.face.processEvents();
96 }
97 
98 void
100 {
101  auto prefix = ctx.args.get<Name>("prefix");
102  auto strategy = ctx.args.get<Name>("strategy");
103 
104  ctx.controller.start<ndn::nfd::StrategyChoiceSetCommand>(
105  ControlParameters().setName(prefix).setStrategy(strategy),
106  [&] (const ControlParameters& resp) {
107  ctx.out << "strategy-set ";
109  ctx.out << ia("prefix") << resp.getName()
110  << ia("strategy") << resp.getStrategy() << '\n';
111  },
112  [&] (const ControlResponse& resp) {
113  if (resp.getCode() == 404) {
114  ctx.exitCode = 7;
115  ctx.err << "Unknown strategy: " << strategy << '\n';
117  return;
118  }
119  ctx.makeCommandFailureHandler("setting strategy")(resp); // invoke general error handler
120  },
121  ctx.makeCommandOptions());
122 
123  ctx.face.processEvents();
124 }
125 
126 void
128 {
129  auto prefix = ctx.args.get<Name>("prefix");
130 
131  if (prefix.empty()) {
132  ctx.exitCode = 2;
133  ctx.err << "Unsetting default strategy is prohibited\n";
134  return;
135  }
136 
137  ctx.controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(
138  ControlParameters().setName(prefix),
139  [&] (const ControlParameters& resp) {
140  ctx.out << "strategy-unset ";
142  ctx.out << ia("prefix") << resp.getName() << '\n';
143  },
144  ctx.makeCommandFailureHandler("unsetting strategy"),
145  ctx.makeCommandOptions());
146 
147  ctx.face.processEvents();
148 }
149 
150 void
151 StrategyChoiceModule::fetchStatus(Controller& controller,
152  const std::function<void()>& onSuccess,
153  const Controller::DatasetFailCallback& onFailure,
154  const CommandOptions& options)
155 {
156  controller.fetch<ndn::nfd::StrategyChoiceDataset>(
157  [this, onSuccess] (const std::vector<StrategyChoice>& result) {
158  m_status = result;
159  onSuccess();
160  },
161  onFailure, options);
162 }
163 
164 void
166 {
167  os << "<strategyChoices>";
168  for (const StrategyChoice& item : m_status) {
169  this->formatItemXml(os, item);
170  }
171  os << "</strategyChoices>";
172 }
173 
174 void
175 StrategyChoiceModule::formatItemXml(std::ostream& os, const StrategyChoice& item) const
176 {
177  os << "<strategyChoice>";
178  os << "<namespace>" << xml::Text{item.getName().toUri()} << "</namespace>";
179  os << "<strategy><name>" << xml::Text{item.getStrategy().toUri()} << "</name></strategy>";
180  os << "</strategyChoice>";
181 }
182 
183 void
185 {
186  os << "Strategy choices:\n";
187  for (const StrategyChoice& item : m_status) {
188  os << " ";
189  formatItemText(os, item);
190  os << '\n';
191  }
192 }
193 
194 void
195 StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
196 {
197  text::ItemAttributes ia(wantMultiLine, 8);
198  os << ia("prefix") << item.getName()
199  << ia("strategy") << item.getStrategy()
200  << ia.end();
201 }
202 
203 } // namespace nfd::tools::nfdc
T get(std::string_view key, const T &defaultValue=T()) const
CommandDefinition & setTitle(std::string_view title)
Set one-line description.
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
Declare an argument.
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type_t< AvailableIn > modes=AVAILABLE_IN_ALL)
Add an available command.
CommandParser & addAlias(const std::string &noun, const std::string &verb, const std::string &verb2)
Add an alias "noun verb2" to existing command "noun verb".
Context for command execution.
std::ostream & out
output stream
Controller::DatasetFailCallback makeDatasetFailureHandler(const std::string &datasetName)
const CommandArguments & args
Controller::CommandFailCallback makeCommandFailureHandler(const std::string &commandName)
ndn::nfd::CommandOptions makeCommandOptions() const
std::ostream & err
error stream
static void registerCommands(CommandParser &parser)
Register 'strategy list', 'strategy show', 'strategy set', 'strategy unset' commands.
static void set(ExecuteContext &ctx)
The 'strategy set' command.
void formatStatusXml(std::ostream &os) const override
Format collected status as XML.
void fetchStatus(Controller &controller, const std::function< void()> &onSuccess, const Controller::DatasetFailCallback &onFailure, const CommandOptions &options) override
Collect status from NFD.
static void unset(ExecuteContext &ctx)
The 'strategy unset' command.
void formatItemXml(std::ostream &os, const StrategyChoice &item) const
Format a single status item as XML.
static void list(ExecuteContext &ctx)
The 'strategy list' command.
static void show(ExecuteContext &ctx)
The 'strategy show' command.
static void formatItemText(std::ostream &os, const StrategyChoice &item, bool wantMultiLine=false)
Format a single status item as text.
void formatStatusText(std::ostream &os) const override
Format collected status as text.
Print attributes of an item.
@ YES
argument is required
@ YES
argument can be specified as positional