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-2018, 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 {
30 namespace tools {
31 namespace nfdc {
32 
33 void
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 
62 void
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 
78 void
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 
100 void
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 
128 void
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 
152 void
153 StrategyChoiceModule::fetchStatus(Controller& controller,
154  const std::function<void()>& onSuccess,
155  const Controller::DatasetFailCallback& 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 
166 void
168 {
169  os << "<strategyChoices>";
170  for (const StrategyChoice& item : m_status) {
171  this->formatItemXml(os, item);
172  }
173  os << "</strategyChoices>";
174 }
175 
176 void
177 StrategyChoiceModule::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 
185 void
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 
196 void
197 StrategyChoiceModule::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 nfdc
206 } // namespace tools
207 } // namespace nfd
const CommandArguments & args
ndn::nfd::CommandOptions makeCommandOptions() const
Controller::CommandFailCallback makeCommandFailureHandler(const std::string &commandName)
declares semantics of a command
std::ostream & out
output stream
context for command execution
void formatStatusText(std::ostream &os) const override
format collected status as text
static void list(ExecuteContext &ctx)
the &#39;strategy list&#39; command
static void set(ExecuteContext &ctx)
the &#39;strategy set&#39; command
T get(const std::string &key, const T &defaultValue=T()) const
void fetchStatus(Controller &controller, const std::function< void()> &onSuccess, const Controller::DatasetFailCallback &onFailure, const CommandOptions &options) override
collect status from NFD
static void registerCommands(CommandParser &parser)
register &#39;strategy list&#39;, &#39;strategy show&#39;, &#39;strategy set&#39;, &#39;strategy unset&#39; commands ...
Controller::DatasetFailCallback makeDatasetFailureHandler(const std::string &datasetName)
CommandDefinition & setTitle(const std::string &title)
set one-line description
static void unset(ExecuteContext &ctx)
the &#39;strategy unset&#39; command
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
declare an argument
void formatItemXml(std::ostream &os, const StrategyChoice &item) const
format a single status item as XML
print attributes of an item
argument is required
static void show(ExecuteContext &ctx)
the &#39;strategy show&#39; command
std::ostream & err
error stream
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type< AvailableIn >::type 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"
static void formatItemText(std::ostream &os, const StrategyChoice &item, bool wantMultiLine=false)
format a single status item as text
void formatStatusXml(std::ostream &os) const override
format collected status as XML