strategy-choice-module.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 
41  CommandDefinition defStrategyShow("strategy", "show");
42  defStrategyShow
43  .setTitle("show strategy choice of an entry")
45  parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
46 
47  CommandDefinition defStrategySet("strategy", "set");
48  defStrategySet
49  .setTitle("set strategy choice for a name prefix")
52  parser.addCommand(defStrategySet, &StrategyChoiceModule::set);
53 
54  CommandDefinition defStrategyUnset("strategy", "unset");
55  defStrategyUnset
56  .setTitle("clear strategy choice at a name prefix")
58  parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset);
59 }
60 
61 void
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 
77 void
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 
99 void
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 ";
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';
118  return;
119  }
120  ctx.makeCommandFailureHandler("setting strategy")(resp); // invoke general error handler
121  },
122  ctx.makeCommandOptions());
123 
124  ctx.face.processEvents();
125 }
126 
127 void
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 ";
143  ctx.out << ia("prefix") << resp.getName() << '\n';
144  },
145  ctx.makeCommandFailureHandler("unsetting strategy"),
146  ctx.makeCommandOptions());
147 
148  ctx.face.processEvents();
149 }
150 
151 void
152 StrategyChoiceModule::fetchStatus(Controller& controller,
153  const function<void()>& onSuccess,
154  const Controller::DatasetFailCallback& onFailure,
155  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 
165 void
167 {
168  os << "<strategyChoices>";
169  for (const StrategyChoice& item : m_status) {
170  this->formatItemXml(os, item);
171  }
172  os << "</strategyChoices>";
173 }
174 
175 void
176 StrategyChoiceModule::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 
184 void
186 {
187  os << "Strategy choices:\n";
188  for (const StrategyChoice& item : m_status) {
189  os << " ";
190  formatItemText(os, item);
191  os << '\n';
192  }
193 }
194 
195 void
196 StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
197 {
198  text::ItemAttributes ia(wantMultiLine, 8);
199  os << ia("prefix") << item.getName()
200  << ia("strategy") << item.getStrategy()
201  << ia.end();
202 }
203 
204 } // namespace nfdc
205 } // namespace tools
206 } // 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 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
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