tools: nfdc strategy list/show commands

refs #3865

Change-Id: I6cc115b3c3f3d0840814a329c68356bbe6ad2e3f
diff --git a/tools/nfdc/available-commands.cpp b/tools/nfdc/available-commands.cpp
index dced4b1..a1d22cb 100644
--- a/tools/nfdc/available-commands.cpp
+++ b/tools/nfdc/available-commands.cpp
@@ -30,6 +30,7 @@
 #include "legacy-status.hpp"
 #include "rib-module.hpp"
 #include "status.hpp"
+#include "strategy-choice-module.hpp"
 
 namespace nfd {
 namespace tools {
@@ -42,6 +43,7 @@
   registerStatusCommands(parser);
   FaceModule::registerCommands(parser);
   RibModule::registerCommands(parser);
+  StrategyChoiceModule::registerCommands(parser);
 
   registerLegacyStatusCommand(parser);
 
diff --git a/tools/nfdc/status.cpp b/tools/nfdc/status.cpp
index a09fd7e..f87cd8b 100644
--- a/tools/nfdc/status.cpp
+++ b/tools/nfdc/status.cpp
@@ -136,11 +136,6 @@
     .setTitle("print channel list");
   parser.addCommand(defChannelList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantChannels));
 
-  CommandDefinition defStrategyList("strategy", "list");
-  defStrategyList
-    .setTitle("print strategy choices");
-  parser.addCommand(defStrategyList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantStrategyChoice));
-
   CommandDefinition defFibList("fib", "list");
   defFibList
     .setTitle("print FIB entries");
diff --git a/tools/nfdc/strategy-choice-module.cpp b/tools/nfdc/strategy-choice-module.cpp
index 4a6b36f..e5fe584 100644
--- a/tools/nfdc/strategy-choice-module.cpp
+++ b/tools/nfdc/strategy-choice-module.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -31,6 +31,59 @@
 namespace nfdc {
 
 void
+StrategyChoiceModule::registerCommands(CommandParser& parser)
+{
+  CommandDefinition defStrategyList("strategy", "list");
+  defStrategyList
+    .setTitle("print strategy choices");
+  parser.addCommand(defStrategyList, &StrategyChoiceModule::list);
+
+  CommandDefinition defStrategyShow("strategy", "show");
+  defStrategyShow
+    .setTitle("show strategy choice of an entry")
+    .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES);
+  parser.addCommand(defStrategyShow, &StrategyChoiceModule::show);
+}
+
+void
+StrategyChoiceModule::list(ExecuteContext& ctx)
+{
+  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
+    [&] (const std::vector<StrategyChoice>& dataset) {
+      for (const StrategyChoice& entry : dataset) {
+        formatItemText(ctx.out, entry);
+        ctx.out << '\n';
+      }
+    },
+    ctx.makeDatasetFailureHandler("strategy choice dataset"),
+    ctx.makeCommandOptions());
+
+  ctx.face.processEvents();
+}
+
+void
+StrategyChoiceModule::show(ExecuteContext& ctx)
+{
+  auto prefix = ctx.args.get<Name>("prefix");
+
+  ctx.controller.fetch<ndn::nfd::StrategyChoiceDataset>(
+    [&] (const std::vector<StrategyChoice>& dataset) {
+      StrategyChoice match; // longest prefix match
+      for (const StrategyChoice& entry : dataset) {
+        if (entry.getName().isPrefixOf(prefix) &&
+            entry.getName().size() >= match.getName().size()) {
+          match = entry;
+        }
+      }
+      formatItemText(ctx.out, match, true);
+    },
+    ctx.makeDatasetFailureHandler("strategy choice dataset"),
+    ctx.makeCommandOptions());
+
+  ctx.face.processEvents();
+}
+
+void
 StrategyChoiceModule::fetchStatus(Controller& controller,
                                   const function<void()>& onSuccess,
                                   const Controller::DatasetFailCallback& onFailure,
@@ -68,16 +121,19 @@
 {
   os << "Strategy choices:\n";
   for (const StrategyChoice& item : m_status) {
-    this->formatItemText(os, item);
+    os << "  ";
+    formatItemText(os, item);
+    os << '\n';
   }
 }
 
 void
-StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item) const
+StrategyChoiceModule::formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine)
 {
-  os << "  " << item.getName()
-     << " strategy=" << item.getStrategy()
-     << "\n";
+  text::ItemAttributes ia(wantMultiLine, 8);
+  os << ia("prefix") << item.getName()
+     << ia("strategy") << item.getStrategy()
+     << ia.end();
 }
 
 } // namespace nfdc
diff --git a/tools/nfdc/strategy-choice-module.hpp b/tools/nfdc/strategy-choice-module.hpp
index 4d26896..aabc3db 100644
--- a/tools/nfdc/strategy-choice-module.hpp
+++ b/tools/nfdc/strategy-choice-module.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -23,10 +23,11 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NFD_TOOLS_NFDC_STARTEGY_CHOICE_MODULE_HPP
-#define NFD_TOOLS_NFDC_STARTEGY_CHOICE_MODULE_HPP
+#ifndef NFD_TOOLS_NFDC_STRATEGY_CHOICE_MODULE_HPP
+#define NFD_TOOLS_NFDC_STRATEGY_CHOICE_MODULE_HPP
 
 #include "module.hpp"
+#include "command-parser.hpp"
 
 namespace nfd {
 namespace tools {
@@ -40,13 +41,28 @@
 class StrategyChoiceModule : public Module, noncopyable
 {
 public:
-  virtual void
+  /** \brief register 'strategy list', 'strategy show', 'strategy set', 'strategy unset' commands
+   */
+  static void
+  registerCommands(CommandParser& parser);
+
+  /** \brief the 'strategy list' command
+   */
+  static void
+  list(ExecuteContext& ctx);
+
+  /** \brief the 'strategy show' command
+   */
+  static void
+  show(ExecuteContext& ctx);
+
+  void
   fetchStatus(Controller& controller,
               const function<void()>& onSuccess,
               const Controller::DatasetFailCallback& onFailure,
               const CommandOptions& options) override;
 
-  virtual void
+  void
   formatStatusXml(std::ostream& os) const override;
 
   /** \brief format a single status item as XML
@@ -56,15 +72,16 @@
   void
   formatItemXml(std::ostream& os, const StrategyChoice& item) const;
 
-  virtual void
+  void
   formatStatusText(std::ostream& os) const override;
 
   /** \brief format a single status item as text
    *  \param os output stream
    *  \param item status item
+   *  \param wantMultiLine use multi-line style
    */
-  void
-  formatItemText(std::ostream& os, const StrategyChoice& item) const;
+  static void
+  formatItemText(std::ostream& os, const StrategyChoice& item, bool wantMultiLine = false);
 
 private:
   std::vector<StrategyChoice> m_status;
@@ -74,4 +91,4 @@
 } // namespace tools
 } // namespace nfd
 
-#endif // NFD_TOOLS_NFDC_STARTEGY_CHOICE_MODULE_HPP
+#endif // NFD_TOOLS_NFDC_STRATEGY_CHOICE_MODULE_HPP