blob: 9e0031f3a8ac5bcba230c4dbc1e74e067f2b30a6 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5d3e4812017-04-05 16:52:59 +00003 * Copyright (c) 2014-2017, 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);
40
41 CommandDefinition defStrategyShow("strategy", "show");
42 defStrategyShow
43 .setTitle("show strategy choice of an entry")
44 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
45 parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
Junxiao Shib283f522017-04-06 20:46:15 +000046
47 CommandDefinition defStrategySet("strategy", "set");
48 defStrategySet
49 .setTitle("set strategy choice for a name prefix")
50 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
51 .addArg("strategy", ArgValueType::NAME, Required::YES, Positional::YES);
52 parser.addCommand(defStrategySet, &StrategyChoiceModule::set);
53
54 CommandDefinition defStrategyUnset("strategy", "unset");
55 defStrategyUnset
56 .setTitle("clear strategy choice at a name prefix")
57 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
58 parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset);
Junxiao Shi5d3e4812017-04-05 16:52:59 +000059}
60
61void
62StrategyChoiceModule::list(ExecuteContext& ctx)
63{
64 ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
65 [&] (const std::vector<StrategyChoice>& dataset) {
66 for (const StrategyChoice& entry : dataset) {
67 formatItemText(ctx.out, entry);
68 ctx.out << '\n';
69 }
70 },
71 ctx.makeDatasetFailureHandler("strategy choice dataset"),
72 ctx.makeCommandOptions());
73
74 ctx.face.processEvents();
75}
76
77void
78StrategyChoiceModule::show(ExecuteContext& ctx)
79{
80 auto prefix = ctx.args.get<Name>("prefix");
81
82 ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
83 [&] (const std::vector<StrategyChoice>& dataset) {
84 StrategyChoice match; // longest prefix match
85 for (const StrategyChoice& entry : dataset) {
86 if (entry.getName().isPrefixOf(prefix) &&
87 entry.getName().size() >= match.getName().size()) {
88 match = entry;
89 }
90 }
91 formatItemText(ctx.out, match, true);
92 },
93 ctx.makeDatasetFailureHandler("strategy choice dataset"),
94 ctx.makeCommandOptions());
95
96 ctx.face.processEvents();
97}
98
99void
Junxiao Shib283f522017-04-06 20:46:15 +0000100StrategyChoiceModule::set(ExecuteContext& ctx)
101{
102 auto prefix = ctx.args.get<Name>("prefix");
103 auto strategy = ctx.args.get<Name>("strategy");
104
105 ctx.controller.start<ndn::nfd::StrategyChoiceSetCommand>(
106 ControlParameters().setName(prefix).setStrategy(strategy),
107 [&] (const ControlParameters& resp) {
108 ctx.out << "strategy-set ";
109 text::ItemAttributes ia;
110 ctx.out << ia("prefix") << resp.getName()
111 << ia("strategy") << resp.getStrategy() << '\n';
112 },
113 [&] (const ControlResponse& resp) {
114 if (resp.getCode() == 404) {
115 ctx.exitCode = 7;
116 ctx.err << "Unknown strategy: " << strategy << '\n';
117 ///\todo #3887 list available strategies
118 return;
119 }
120 ctx.makeCommandFailureHandler("setting strategy")(resp); // invoke general error handler
121 },
122 ctx.makeCommandOptions());
123
124 ctx.face.processEvents();
125}
126
127void
128StrategyChoiceModule::unset(ExecuteContext& ctx)
129{
130 auto prefix = ctx.args.get<Name>("prefix");
131
132 if (prefix.empty()) {
133 ctx.exitCode = 2;
134 ctx.err << "Unsetting default strategy is prohibited\n";
135 return;
136 }
137
138 ctx.controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(
139 ControlParameters().setName(prefix),
140 [&] (const ControlParameters& resp) {
141 ctx.out << "strategy-unset ";
142 text::ItemAttributes ia;
143 ctx.out << ia("prefix") << resp.getName() << '\n';
144 },
145 ctx.makeCommandFailureHandler("unsetting strategy"),
146 ctx.makeCommandOptions());
147
148 ctx.face.processEvents();
149}
150
151void
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000152StrategyChoiceModule::fetchStatus(Controller& controller,
153 const function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000154 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000155 const CommandOptions& options)
156{
157 controller.fetch<ndn::nfd::StrategyChoiceDataset>(
158 [this, onSuccess] (const std::vector<StrategyChoice>& result) {
159 m_status = result;
160 onSuccess();
161 },
162 onFailure, options);
163}
164
165void
166StrategyChoiceModule::formatStatusXml(std::ostream& os) const
167{
168 os << "<strategyChoices>";
169 for (const StrategyChoice& item : m_status) {
170 this->formatItemXml(os, item);
171 }
172 os << "</strategyChoices>";
173}
174
175void
176StrategyChoiceModule::formatItemXml(std::ostream& os, const StrategyChoice& item) const
177{
178 os << "<strategyChoice>";
179 os << "<namespace>" << xml::Text{item.getName().toUri()} << "</namespace>";
180 os << "<strategy><name>" << xml::Text{item.getStrategy().toUri()} << "</name></strategy>";
181 os << "</strategyChoice>";
182}
183
184void
185StrategyChoiceModule::formatStatusText(std::ostream& os) const
186{
187 os << "Strategy choices:\n";
188 for (const StrategyChoice& item : m_status) {
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000189 os << " ";
190 formatItemText(os, item);
191 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000192 }
193}
194
195void
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000196StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000197{
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000198 text::ItemAttributes ia(wantMultiLine, 8);
199 os << ia("prefix") << item.getName()
200 << ia("strategy") << item.getStrategy()
201 << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000202}
203
Junxiao Shi331ade72016-08-19 14:07:19 +0000204} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000205} // namespace tools
206} // namespace nfd