NFD: Named Data Networking Forwarding Daemon 24.07-28-gdcc0e6e0
Loading...
Searching...
No Matches
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-2023, 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#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
30
31namespace nfd::tools::nfdc {
32
33void
35{
36 CommandDefinition defStrategyList("strategy", "list");
37 defStrategyList
38 .setTitle("print strategy choices");
39 parser.addCommand(defStrategyList, &StrategyChoiceModule::list);
40 parser.addAlias("strategy", "list", "");
41
42 CommandDefinition defStrategyShow("strategy", "show");
43 defStrategyShow
44 .setTitle("show strategy choice of an entry")
46 parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
47
48 CommandDefinition defStrategySet("strategy", "set");
49 defStrategySet
50 .setTitle("set strategy choice for a name prefix")
53 parser.addCommand(defStrategySet, &StrategyChoiceModule::set);
54
55 CommandDefinition defStrategyUnset("strategy", "unset");
56 defStrategyUnset
57 .setTitle("clear strategy choice at a name prefix")
59 parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset);
60}
61
62void
64{
65 ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
66 [&] (const std::vector<StrategyChoice>& dataset) {
67 for (const StrategyChoice& entry : dataset) {
68 formatItemText(ctx.out, entry);
69 ctx.out << '\n';
70 }
71 },
72 ctx.makeDatasetFailureHandler("strategy choice dataset"),
73 ctx.makeCommandOptions());
74
75 ctx.face.processEvents();
76}
77
78void
80{
81 auto prefix = ctx.args.get<Name>("prefix");
82
83 ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
84 [&] (const std::vector<StrategyChoice>& dataset) {
85 StrategyChoice match; // longest prefix match
86 for (const StrategyChoice& entry : dataset) {
87 if (entry.getName().isPrefixOf(prefix) &&
88 entry.getName().size() >= match.getName().size()) {
89 match = entry;
90 }
91 }
92 formatItemText(ctx.out, match, true);
93 },
94 ctx.makeDatasetFailureHandler("strategy choice dataset"),
95 ctx.makeCommandOptions());
96
97 ctx.face.processEvents();
98}
99
100void
102{
103 auto prefix = ctx.args.get<Name>("prefix");
104 auto strategy = ctx.args.get<Name>("strategy");
105
106 ctx.controller.start<ndn::nfd::StrategyChoiceSetCommand>(
107 ControlParameters().setName(prefix).setStrategy(strategy),
108 [&] (const ControlParameters& resp) {
109 ctx.out << "strategy-set ";
111 ctx.out << ia("prefix") << resp.getName()
112 << ia("strategy") << resp.getStrategy() << '\n';
113 },
114 [&] (const ControlResponse& resp) {
115 if (resp.getCode() == 404) {
116 ctx.exitCode = 7;
117 ctx.err << "Unknown strategy: " << strategy << '\n';
119 return;
120 }
121 ctx.makeCommandFailureHandler("setting strategy")(resp); // invoke general error handler
122 },
123 ctx.makeCommandOptions());
124
125 ctx.face.processEvents();
126}
127
128void
130{
131 auto prefix = ctx.args.get<Name>("prefix");
132
133 if (prefix.empty()) {
134 ctx.exitCode = 2;
135 ctx.err << "Unsetting default strategy is prohibited\n";
136 return;
137 }
138
139 ctx.controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(
140 ControlParameters().setName(prefix),
141 [&] (const ControlParameters& resp) {
142 ctx.out << "strategy-unset ";
144 ctx.out << ia("prefix") << resp.getName() << '\n';
145 },
146 ctx.makeCommandFailureHandler("unsetting strategy"),
147 ctx.makeCommandOptions());
148
149 ctx.face.processEvents();
150}
151
152void
153StrategyChoiceModule::fetchStatus(ndn::nfd::Controller& controller,
154 const std::function<void()>& onSuccess,
155 const ndn::nfd::DatasetFailureCallback& onFailure,
156 const CommandOptions& options)
157{
158 controller.fetch<ndn::nfd::StrategyChoiceDataset>(
159 [this, onSuccess] (const std::vector<StrategyChoice>& result) {
160 m_status = result;
161 onSuccess();
162 },
163 onFailure, options);
164}
165
166void
168{
169 os << "<strategyChoices>";
170 for (const StrategyChoice& item : m_status) {
171 this->formatItemXml(os, item);
172 }
173 os << "</strategyChoices>";
174}
175
176void
177StrategyChoiceModule::formatItemXml(std::ostream& os, const StrategyChoice& item) const
178{
179 os << "<strategyChoice>";
180 os << "<namespace>" << xml::Text{item.getName().toUri()} << "</namespace>";
181 os << "<strategy><name>" << xml::Text{item.getStrategy().toUri()} << "</name></strategy>";
182 os << "</strategyChoice>";
183}
184
185void
187{
188 os << "Strategy choices:\n";
189 for (const StrategyChoice& item : m_status) {
190 os << " ";
191 formatItemText(os, item);
192 os << '\n';
193 }
194}
195
196void
197StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
198{
199 text::ItemAttributes ia(wantMultiLine, 8);
200 os << ia("prefix") << item.getName()
201 << ia("strategy") << item.getStrategy()
202 << ia.end();
203}
204
205} // 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
ndn::nfd::DatasetFailureCallback makeDatasetFailureHandler(const std::string &datasetName)
ndn::nfd::Controller & controller
const CommandArguments & args
ndn::nfd::CommandFailureCallback 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 fetchStatus(ndn::nfd::Controller &controller, const std::function< void()> &onSuccess, const ndn::nfd::DatasetFailureCallback &onFailure, const CommandOptions &options) override
Collect status from NFD.
void formatStatusXml(std::ostream &os) const override
Format collected status as XML.
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.
@ YES
argument is required
@ YES
argument can be specified as positional