Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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); |
| 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); |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 46 | |
| 47 | CommandDefinition defStrategySet("strategy", "set"); |
| 48 | defStrategySet |
| 49 | .setTitle("set strategy choice for a name prefix") |
| 50 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 51 | .addArg("strategy", ArgValueType::NAME, Required::YES, Positional::YES); |
| 52 | parser.addCommand(defStrategySet, &StrategyChoiceModule::set); |
| 53 | |
| 54 | CommandDefinition defStrategyUnset("strategy", "unset"); |
| 55 | defStrategyUnset |
| 56 | .setTitle("clear strategy choice at a name prefix") |
| 57 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES); |
| 58 | parser.addCommand(defStrategyUnset, &StrategyChoiceModule::unset); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | StrategyChoiceModule::list(ExecuteContext& ctx) |
| 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 |
| 78 | StrategyChoiceModule::show(ExecuteContext& ctx) |
| 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 |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 100 | StrategyChoiceModule::set(ExecuteContext& ctx) |
| 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 "; |
| 109 | text::ItemAttributes ia; |
| 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'; |
| 117 | ///\todo #3887 list available strategies |
| 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 |
| 128 | StrategyChoiceModule::unset(ExecuteContext& ctx) |
| 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 "; |
| 142 | text::ItemAttributes ia; |
| 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 |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 152 | StrategyChoiceModule::fetchStatus(Controller& controller, |
| 153 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 154 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 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 |
| 166 | StrategyChoiceModule::formatStatusXml(std::ostream& os) const |
| 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 |
| 185 | StrategyChoiceModule::formatStatusText(std::ostream& os) const |
| 186 | { |
| 187 | os << "Strategy choices:\n"; |
| 188 | for (const StrategyChoice& item : m_status) { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 189 | os << " "; |
| 190 | formatItemText(os, item); |
| 191 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 196 | StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 197 | { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 198 | text::ItemAttributes ia(wantMultiLine, 8); |
| 199 | os << ia("prefix") << item.getName() |
| 200 | << ia("strategy") << item.getStrategy() |
| 201 | << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 204 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 205 | } // namespace tools |
| 206 | } // namespace nfd |