blob: e5fe5841aeea27191180cf3f92adb83993e05151 [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);
46}
47
48void
49StrategyChoiceModule::list(ExecuteContext& ctx)
50{
51 ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
52 [&] (const std::vector<StrategyChoice>& dataset) {
53 for (const StrategyChoice& entry : dataset) {
54 formatItemText(ctx.out, entry);
55 ctx.out << '\n';
56 }
57 },
58 ctx.makeDatasetFailureHandler("strategy choice dataset"),
59 ctx.makeCommandOptions());
60
61 ctx.face.processEvents();
62}
63
64void
65StrategyChoiceModule::show(ExecuteContext& ctx)
66{
67 auto prefix = ctx.args.get<Name>("prefix");
68
69 ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
70 [&] (const std::vector<StrategyChoice>& dataset) {
71 StrategyChoice match; // longest prefix match
72 for (const StrategyChoice& entry : dataset) {
73 if (entry.getName().isPrefixOf(prefix) &&
74 entry.getName().size() >= match.getName().size()) {
75 match = entry;
76 }
77 }
78 formatItemText(ctx.out, match, true);
79 },
80 ctx.makeDatasetFailureHandler("strategy choice dataset"),
81 ctx.makeCommandOptions());
82
83 ctx.face.processEvents();
84}
85
86void
Junxiao Shi38f4ce92016-08-04 10:01:52 +000087StrategyChoiceModule::fetchStatus(Controller& controller,
88 const function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +000089 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +000090 const CommandOptions& options)
91{
92 controller.fetch<ndn::nfd::StrategyChoiceDataset>(
93 [this, onSuccess] (const std::vector<StrategyChoice>& result) {
94 m_status = result;
95 onSuccess();
96 },
97 onFailure, options);
98}
99
100void
101StrategyChoiceModule::formatStatusXml(std::ostream& os) const
102{
103 os << "<strategyChoices>";
104 for (const StrategyChoice& item : m_status) {
105 this->formatItemXml(os, item);
106 }
107 os << "</strategyChoices>";
108}
109
110void
111StrategyChoiceModule::formatItemXml(std::ostream& os, const StrategyChoice& item) const
112{
113 os << "<strategyChoice>";
114 os << "<namespace>" << xml::Text{item.getName().toUri()} << "</namespace>";
115 os << "<strategy><name>" << xml::Text{item.getStrategy().toUri()} << "</name></strategy>";
116 os << "</strategyChoice>";
117}
118
119void
120StrategyChoiceModule::formatStatusText(std::ostream& os) const
121{
122 os << "Strategy choices:\n";
123 for (const StrategyChoice& item : m_status) {
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000124 os << " ";
125 formatItemText(os, item);
126 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000127 }
128}
129
130void
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000131StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000132{
Junxiao Shi5d3e4812017-04-05 16:52:59 +0000133 text::ItemAttributes ia(wantMultiLine, 8);
134 os << ia("prefix") << item.getName()
135 << ia("strategy") << item.getStrategy()
136 << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000137}
138
Junxiao Shi331ade72016-08-19 14:07:19 +0000139} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000140} // namespace tools
141} // namespace nfd