blob: 1fbca4c32459dd181bef64da5f786d1d22e29389 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod2147442018-02-19 23:58:17 -05002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * 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 "strategy-choice-module.hpp"
27#include "format-helpers.hpp"
28
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::tools::nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000030
31void
Junxiao Shi5d3e4812017-04-05 16:52:59 +000032StrategyChoiceModule::registerCommands(CommandParser& parser)
33{
34 CommandDefinition defStrategyList("strategy", "list");
35 defStrategyList
36 .setTitle("print strategy choices");
37 parser.addCommand(defStrategyList, &StrategyChoiceModule::list);
Davide Pesaventod2147442018-02-19 23:58:17 -050038 parser.addAlias("strategy", "list", "");
Junxiao Shi5d3e4812017-04-05 16:52:59 +000039
40 CommandDefinition defStrategyShow("strategy", "show");
41 defStrategyShow
42 .setTitle("show strategy choice of an entry")
43 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
44 parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
Junxiao Shib283f522017-04-06 20:46:15 +000045
46 CommandDefinition defStrategySet("strategy", "set");
47 defStrategySet
48 .setTitle("set strategy choice for a name prefix")
49 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
50 .addArg("strategy", ArgValueType::NAME, Required::YES, Positional::YES);
51 parser.addCommand(defStrategySet, &StrategyChoiceModule::set);
52
53 CommandDefinition defStrategyUnset("strategy", "unset");
54 defStrategyUnset
55 .setTitle("clear strategy choice at a name prefix")
56 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
57 parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset);
Junxiao Shi5d3e4812017-04-05 16:52:59 +000058}
59
60void
61StrategyChoiceModule::list(ExecuteContext& ctx)
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
76void
77StrategyChoiceModule::show(ExecuteContext& ctx)
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
98void
Junxiao Shib283f522017-04-06 20:46:15 +000099StrategyChoiceModule::set(ExecuteContext& ctx)
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 ";
108 text::ItemAttributes ia;
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';
116 ///\todo #3887 list available strategies
117 return;
118 }
119 ctx.makeCommandFailureHandler("setting strategy")(resp); // invoke general error handler
120 },
121 ctx.makeCommandOptions());
122
123 ctx.face.processEvents();
124}
125
126void
127StrategyChoiceModule::unset(ExecuteContext& ctx)
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 ";
141 text::ItemAttributes ia;
142 ctx.out << ia("prefix") << resp.getName() << '\n';
143 },
144 ctx.makeCommandFailureHandler("unsetting strategy"),
145 ctx.makeCommandOptions());
146
147 ctx.face.processEvents();
148}
149
150void
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000151StrategyChoiceModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400152 const std::function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000153 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000154 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
164void
165StrategyChoiceModule::formatStatusXml(std::ostream& os) const
166{
167 os << "<strategyChoices>";
168 for (const StrategyChoice& item : m_status) {
169 this->formatItemXml(os, item);
170 }
171 os << "</strategyChoices>";
172}
173
174void
175StrategyChoiceModule::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
183void
184StrategyChoiceModule::formatStatusText(std::ostream& os) const
185{
186 os << "Strategy choices:\n";
187 for (const StrategyChoice& item : m_status) {
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000188 os << " ";
189 formatItemText(os, item);
190 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000191 }
192}
193
194void
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000195StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000196{
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000197 text::ItemAttributes ia(wantMultiLine, 8);
198 os << ia("prefix") << item.getName()
199 << ia("strategy") << item.getStrategy()
200 << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000201}
202
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400203} // namespace nfd::tools::nfdc