blob: 004f3685066fbbf7813b7533af4050a9169e1d29 [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/*
3 * Copyright (c) 2014-2018, 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
29namespace nfd {
30namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000031namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
33void
Junxiao Shi5d3e4812017-04-05 16:52:59 +000034StrategyChoiceModule::registerCommands(CommandParser& parser)
35{
36 CommandDefinition defStrategyList("strategy", "list");
37 defStrategyList
38 .setTitle("print strategy choices");
39 parser.addCommand(defStrategyList, &StrategyChoiceModule::list);
Davide Pesaventod2147442018-02-19 23:58:17 -050040 parser.addAlias("strategy", "list", "");
Junxiao Shi5d3e4812017-04-05 16:52:59 +000041
42 CommandDefinition defStrategyShow("strategy", "show");
43 defStrategyShow
44 .setTitle("show strategy choice of an entry")
45 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
46 parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
Junxiao Shib283f522017-04-06 20:46:15 +000047
48 CommandDefinition defStrategySet("strategy", "set");
49 defStrategySet
50 .setTitle("set strategy choice for a name prefix")
51 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
52 .addArg("strategy", ArgValueType::NAME, Required::YES, Positional::YES);
53 parser.addCommand(defStrategySet, &StrategyChoiceModule::set);
54
55 CommandDefinition defStrategyUnset("strategy", "unset");
56 defStrategyUnset
57 .setTitle("clear strategy choice at a name prefix")
58 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
59 parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset);
Junxiao Shi5d3e4812017-04-05 16:52:59 +000060}
61
62void
63StrategyChoiceModule::list(ExecuteContext& ctx)
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
79StrategyChoiceModule::show(ExecuteContext& ctx)
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
Junxiao Shib283f522017-04-06 20:46:15 +0000101StrategyChoiceModule::set(ExecuteContext& ctx)
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 ";
110 text::ItemAttributes ia;
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';
118 ///\todo #3887 list available strategies
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
129StrategyChoiceModule::unset(ExecuteContext& ctx)
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 ";
143 text::ItemAttributes ia;
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
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000153StrategyChoiceModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400154 const std::function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000155 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000156 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
167StrategyChoiceModule::formatStatusXml(std::ostream& os) const
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
186StrategyChoiceModule::formatStatusText(std::ostream& os) const
187{
188 os << "Strategy choices:\n";
189 for (const StrategyChoice& item : m_status) {
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000190 os << " ";
191 formatItemText(os, item);
192 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000193 }
194}
195
196void
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000197StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000198{
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000199 text::ItemAttributes ia(wantMultiLine, 8);
200 os << ia("prefix") << item.getName()
201 << ia("strategy") << item.getStrategy()
202 << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000203}
204
Junxiao Shi331ade72016-08-19 14:07:19 +0000205} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000206} // namespace tools
207} // namespace nfd