Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 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 | |
| 26 | #include "strategy-choice-module.hpp" |
| 27 | #include "format-helpers.hpp" |
| 28 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 29 | namespace nfd::tools::nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 30 | |
| 31 | void |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 32 | StrategyChoiceModule::registerCommands(CommandParser& parser) |
| 33 | { |
| 34 | CommandDefinition defStrategyList("strategy", "list"); |
| 35 | defStrategyList |
| 36 | .setTitle("print strategy choices"); |
| 37 | parser.addCommand(defStrategyList, &StrategyChoiceModule::list); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 38 | parser.addAlias("strategy", "list", ""); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 39 | |
| 40 | CommandDefinition defStrategyShow("strategy", "show"); |
| 41 | defStrategyShow |
| 42 | .setTitle("show strategy choice of an entry") |
| 43 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES); |
| 44 | parser.addCommand(defStrategyShow, &StrategyChoiceModule::show); |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 45 | |
| 46 | CommandDefinition defStrategySet("strategy", "set"); |
| 47 | defStrategySet |
| 48 | .setTitle("set strategy choice for a name prefix") |
| 49 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 50 | .addArg("strategy", ArgValueType::NAME, Required::YES, Positional::YES); |
| 51 | parser.addCommand(defStrategySet, &StrategyChoiceModule::set); |
| 52 | |
| 53 | CommandDefinition defStrategyUnset("strategy", "unset"); |
| 54 | defStrategyUnset |
| 55 | .setTitle("clear strategy choice at a name prefix") |
| 56 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES); |
| 57 | parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | StrategyChoiceModule::list(ExecuteContext& ctx) |
| 62 | { |
| 63 | ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>( |
| 64 | [&] (const std::vector<StrategyChoice>& dataset) { |
| 65 | for (const StrategyChoice& entry : dataset) { |
| 66 | formatItemText(ctx.out, entry); |
| 67 | ctx.out << '\n'; |
| 68 | } |
| 69 | }, |
| 70 | ctx.makeDatasetFailureHandler("strategy choice dataset"), |
| 71 | ctx.makeCommandOptions()); |
| 72 | |
| 73 | ctx.face.processEvents(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | StrategyChoiceModule::show(ExecuteContext& ctx) |
| 78 | { |
| 79 | auto prefix = ctx.args.get<Name>("prefix"); |
| 80 | |
| 81 | ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>( |
| 82 | [&] (const std::vector<StrategyChoice>& dataset) { |
| 83 | StrategyChoice match; // longest prefix match |
| 84 | for (const StrategyChoice& entry : dataset) { |
| 85 | if (entry.getName().isPrefixOf(prefix) && |
| 86 | entry.getName().size() >= match.getName().size()) { |
| 87 | match = entry; |
| 88 | } |
| 89 | } |
| 90 | formatItemText(ctx.out, match, true); |
| 91 | }, |
| 92 | ctx.makeDatasetFailureHandler("strategy choice dataset"), |
| 93 | ctx.makeCommandOptions()); |
| 94 | |
| 95 | ctx.face.processEvents(); |
| 96 | } |
| 97 | |
| 98 | void |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 99 | StrategyChoiceModule::set(ExecuteContext& ctx) |
| 100 | { |
| 101 | auto prefix = ctx.args.get<Name>("prefix"); |
| 102 | auto strategy = ctx.args.get<Name>("strategy"); |
| 103 | |
| 104 | ctx.controller.start<ndn::nfd::StrategyChoiceSetCommand>( |
| 105 | ControlParameters().setName(prefix).setStrategy(strategy), |
| 106 | [&] (const ControlParameters& resp) { |
| 107 | ctx.out << "strategy-set "; |
| 108 | text::ItemAttributes ia; |
| 109 | ctx.out << ia("prefix") << resp.getName() |
| 110 | << ia("strategy") << resp.getStrategy() << '\n'; |
| 111 | }, |
| 112 | [&] (const ControlResponse& resp) { |
| 113 | if (resp.getCode() == 404) { |
| 114 | ctx.exitCode = 7; |
| 115 | ctx.err << "Unknown strategy: " << strategy << '\n'; |
| 116 | ///\todo #3887 list available strategies |
| 117 | return; |
| 118 | } |
| 119 | ctx.makeCommandFailureHandler("setting strategy")(resp); // invoke general error handler |
| 120 | }, |
| 121 | ctx.makeCommandOptions()); |
| 122 | |
| 123 | ctx.face.processEvents(); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | StrategyChoiceModule::unset(ExecuteContext& ctx) |
| 128 | { |
| 129 | auto prefix = ctx.args.get<Name>("prefix"); |
| 130 | |
| 131 | if (prefix.empty()) { |
| 132 | ctx.exitCode = 2; |
| 133 | ctx.err << "Unsetting default strategy is prohibited\n"; |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | ctx.controller.start<ndn::nfd::StrategyChoiceUnsetCommand>( |
| 138 | ControlParameters().setName(prefix), |
| 139 | [&] (const ControlParameters& resp) { |
| 140 | ctx.out << "strategy-unset "; |
| 141 | text::ItemAttributes ia; |
| 142 | ctx.out << ia("prefix") << resp.getName() << '\n'; |
| 143 | }, |
| 144 | ctx.makeCommandFailureHandler("unsetting strategy"), |
| 145 | ctx.makeCommandOptions()); |
| 146 | |
| 147 | ctx.face.processEvents(); |
| 148 | } |
| 149 | |
| 150 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 151 | StrategyChoiceModule::fetchStatus(Controller& controller, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 152 | const std::function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 153 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 154 | const CommandOptions& options) |
| 155 | { |
| 156 | controller.fetch<ndn::nfd::StrategyChoiceDataset>( |
| 157 | [this, onSuccess] (const std::vector<StrategyChoice>& result) { |
| 158 | m_status = result; |
| 159 | onSuccess(); |
| 160 | }, |
| 161 | onFailure, options); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | StrategyChoiceModule::formatStatusXml(std::ostream& os) const |
| 166 | { |
| 167 | os << "<strategyChoices>"; |
| 168 | for (const StrategyChoice& item : m_status) { |
| 169 | this->formatItemXml(os, item); |
| 170 | } |
| 171 | os << "</strategyChoices>"; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | StrategyChoiceModule::formatItemXml(std::ostream& os, const StrategyChoice& item) const |
| 176 | { |
| 177 | os << "<strategyChoice>"; |
| 178 | os << "<namespace>" << xml::Text{item.getName().toUri()} << "</namespace>"; |
| 179 | os << "<strategy><name>" << xml::Text{item.getStrategy().toUri()} << "</name></strategy>"; |
| 180 | os << "</strategyChoice>"; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | StrategyChoiceModule::formatStatusText(std::ostream& os) const |
| 185 | { |
| 186 | os << "Strategy choices:\n"; |
| 187 | for (const StrategyChoice& item : m_status) { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 188 | os << " "; |
| 189 | formatItemText(os, item); |
| 190 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | void |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 195 | StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 196 | { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 197 | text::ItemAttributes ia(wantMultiLine, 8); |
| 198 | os << ia("prefix") << item.getName() |
| 199 | << ia("strategy") << item.getStrategy() |
| 200 | << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 203 | } // namespace nfd::tools::nfdc |