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 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | |
| 29 | namespace nfd { |
| 30 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 31 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 32 | |
| 33 | void |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 34 | StrategyChoiceModule::registerCommands(CommandParser& parser) |
| 35 | { |
| 36 | CommandDefinition defStrategyList("strategy", "list"); |
| 37 | defStrategyList |
| 38 | .setTitle("print strategy choices"); |
| 39 | parser.addCommand(defStrategyList, &StrategyChoiceModule::list); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame^] | 40 | parser.addAlias("strategy", "list", ""); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 41 | |
| 42 | CommandDefinition defStrategyShow("strategy", "show"); |
| 43 | defStrategyShow |
| 44 | .setTitle("show strategy choice of an entry") |
| 45 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES); |
| 46 | parser.addCommand(defStrategyShow, &StrategyChoiceModule::show); |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 47 | |
| 48 | CommandDefinition defStrategySet("strategy", "set"); |
| 49 | defStrategySet |
| 50 | .setTitle("set strategy choice for a name prefix") |
| 51 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 52 | .addArg("strategy", ArgValueType::NAME, Required::YES, Positional::YES); |
| 53 | parser.addCommand(defStrategySet, &StrategyChoiceModule::set); |
| 54 | |
| 55 | CommandDefinition defStrategyUnset("strategy", "unset"); |
| 56 | defStrategyUnset |
| 57 | .setTitle("clear strategy choice at a name prefix") |
| 58 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES); |
| 59 | parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
| 63 | StrategyChoiceModule::list(ExecuteContext& ctx) |
| 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 |
| 79 | StrategyChoiceModule::show(ExecuteContext& ctx) |
| 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 |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 101 | StrategyChoiceModule::set(ExecuteContext& ctx) |
| 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 "; |
| 110 | text::ItemAttributes ia; |
| 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'; |
| 118 | ///\todo #3887 list available strategies |
| 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 |
| 129 | StrategyChoiceModule::unset(ExecuteContext& ctx) |
| 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 "; |
| 143 | text::ItemAttributes ia; |
| 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 |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 153 | StrategyChoiceModule::fetchStatus(Controller& controller, |
| 154 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 155 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 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 |
| 167 | StrategyChoiceModule::formatStatusXml(std::ostream& os) const |
| 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 |
| 186 | StrategyChoiceModule::formatStatusText(std::ostream& os) const |
| 187 | { |
| 188 | os << "Strategy choices:\n"; |
| 189 | for (const StrategyChoice& item : m_status) { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 190 | os << " "; |
| 191 | formatItemText(os, item); |
| 192 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | void |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 197 | StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 198 | { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 199 | text::ItemAttributes ia(wantMultiLine, 8); |
| 200 | os << ia("prefix") << item.getName() |
| 201 | << ia("strategy") << item.getStrategy() |
| 202 | << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 205 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 206 | } // namespace tools |
| 207 | } // namespace nfd |