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); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | StrategyChoiceModule::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 | |
| 64 | void |
| 65 | StrategyChoiceModule::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 | |
| 86 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 87 | StrategyChoiceModule::fetchStatus(Controller& controller, |
| 88 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 89 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 90 | 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 | |
| 100 | void |
| 101 | StrategyChoiceModule::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 | |
| 110 | void |
| 111 | StrategyChoiceModule::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 | |
| 119 | void |
| 120 | StrategyChoiceModule::formatStatusText(std::ostream& os) const |
| 121 | { |
| 122 | os << "Strategy choices:\n"; |
| 123 | for (const StrategyChoice& item : m_status) { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 124 | os << " "; |
| 125 | formatItemText(os, item); |
| 126 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 131 | StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 132 | { |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 133 | text::ItemAttributes ia(wantMultiLine, 8); |
| 134 | os << ia("prefix") << item.getName() |
| 135 | << ia("strategy") << item.getStrategy() |
| 136 | << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 139 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 140 | } // namespace tools |
| 141 | } // namespace nfd |