nfdc: strategy-choice, Adding StrategyChoiceManagementOptions

refs: #1312

Change-Id: I20c7d76b7a5740202e194c1016399df3684d63d8
diff --git a/src/encoding/tlv-nfd.hpp b/src/encoding/tlv-nfd.hpp
index ec1b60a..77f15b0 100644
--- a/src/encoding/tlv-nfd.hpp
+++ b/src/encoding/tlv-nfd.hpp
@@ -24,11 +24,14 @@
   FibManagementOptions = 104,
   FaceId          = 105,
   Cost            = 106,
-  Strategy        = 107,
 
   // Face Management Protocol
   FaceManagementOptions = 108,
   Uri                   = 114,
+  
+  // Strategy Choice Protocol
+  StrategyChoiceOptions = 115,
+  Strategy              = 107,
 
   // Face Status Protocol
   FaceStatus                   = 128,
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 7a99693..19e98f8 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -10,6 +10,7 @@
 #include "nfd-controller.hpp"
 #include "nfd-fib-management-options.hpp"
 #include "nfd-face-management-options.hpp"
+#include "nfd-strategy-choice-options.hpp"
 #include "nfd-control-response.hpp"
 
 namespace ndn {
@@ -123,29 +124,73 @@
                               onSuccess, onFail),
                          bind(onFail, "Command Interest timed out"));
 }
-
+  
 void
 Controller::processFaceCommandResponse(Data& data,
                                        const FaceCommandSucceedCallback& onSuccess,
                                        const FailCallback& onFail)
 {
   /// \todo Add validation of incoming Data
-
+  
   try
-    {
-      ControlResponse response(data.getContent().blockFromValue());
-      if (response.getCode() != 200)
-        return onFail(response.getText());
-
-      FaceManagementOptions options(response.getBody());
-      return onSuccess(options);
-    }
+  {
+    ControlResponse response(data.getContent().blockFromValue());
+    if (response.getCode() != 200)
+      return onFail(response.getText());
+    
+    FaceManagementOptions options(response.getBody());
+    return onSuccess(options);
+  }
   catch(ndn::Tlv::Error& e)
-    {
-      if (static_cast<bool>(onFail))
-        return onFail(e.what());
-    }
+  {
+    if (static_cast<bool>(onFail))
+      return onFail(e.what());
+  }
 }
 
+void
+Controller::startStrategyChoiceCommand(const std::string& command,
+                                       const StrategyChoiceOptions& options,
+                                       const StrategyChoiceCommandSucceedCallback& onSuccess,
+                                       const FailCallback& onFail)
+{
+  Name strategyChoiceCommandInterestName("/localhost/nfd/strategy-choice");
+  strategyChoiceCommandInterestName
+    .append(command)
+    .append(options.wireEncode());
+  
+  Interest strategyChoiceCommandInterest(strategyChoiceCommandInterestName);
+  m_commandInterestGenerator.generate(strategyChoiceCommandInterest);
+  
+  m_face.expressInterest(strategyChoiceCommandInterest,
+                         bind(&Controller::processStrategyChoiceCommandResponse, this, _2,
+                              onSuccess, onFail),
+                         bind(onFail, "Command Interest timed out"));
+}
+void
+Controller::processStrategyChoiceCommandResponse(
+    Data& data,
+    const StrategyChoiceCommandSucceedCallback& onSuccess,
+    const FailCallback& onFail)
+{
+  /// \todo Add validation of incoming Data
+  
+  try
+  {
+    ControlResponse response(data.getContent().blockFromValue());
+    if (response.getCode() != 200)
+      return onFail(response.getText());
+    
+    StrategyChoiceOptions options(response.getBody());
+    return onSuccess(options);
+  }
+  catch (ndn::Tlv::Error& error)
+  {
+    if (static_cast<bool>(onFail))
+      return onFail(error.what());
+  }
+}
+
+  
 } // namespace nfd
 } // namespace ndn
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index a864db3..cb7f37b 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -16,13 +16,15 @@
 
 class FibManagementOptions;
 class FaceManagementOptions;
-
+class StrategyChoiceOptions;
+  
 class Controller : public ndn::Controller
 {
 public:
   typedef function<void(const FibManagementOptions&)> FibCommandSucceedCallback;
   typedef function<void(const FaceManagementOptions&)> FaceCommandSucceedCallback;
-
+  typedef function<void(const StrategyChoiceOptions&)> StrategyChoiceCommandSucceedCallback;
+  
   /**
    * @brief Construct ndnd::Control object
    */
@@ -88,6 +90,12 @@
                    const FaceCommandSucceedCallback& onSuccess,
                    const FailCallback& onFailure);
 
+  void
+  startStrategyChoiceCommand(const std::string& command,
+                             const StrategyChoiceOptions& options,
+                             const StrategyChoiceCommandSucceedCallback& onSuccess,
+                             const FailCallback& onFailure);
+  
 private:
   void
   processFibCommandResponse(Data& data,
@@ -99,6 +107,11 @@
                              const FaceCommandSucceedCallback& onSuccess,
                              const FailCallback& onFail);
 
+  void
+  processStrategyChoiceCommandResponse(Data& data,
+                                       const StrategyChoiceCommandSucceedCallback& onSuccess,
+                                       const FailCallback& onFail);
+
 protected:
   Face& m_face;
   CommandInterestGenerator m_commandInterestGenerator;
diff --git a/src/management/nfd-strategy-choice-options.hpp b/src/management/nfd-strategy-choice-options.hpp
new file mode 100644
index 0000000..60d846d
--- /dev/null
+++ b/src/management/nfd-strategy-choice-options.hpp
@@ -0,0 +1,163 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (C) 2014 Named Data Networking Project
+* See COPYING for copyright and distribution information.
+*/
+
+#ifndef NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_OPTIONS_HPP
+#define NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_OPTIONS_HPP
+
+#include "../encoding/block.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/tlv-nfd.hpp"
+#include "../name.hpp"
+
+namespace ndn {
+namespace nfd {
+
+class StrategyChoiceOptions{
+public:
+  struct Error : public Tlv::Error
+  {
+    Error(const std::string& what) : Tlv::Error(what)
+    {
+    }
+  };
+  
+  StrategyChoiceOptions()
+  {
+  }
+  
+  StrategyChoiceOptions(const Block& block)
+  {
+    wireDecode(block);
+  }
+  
+  const Name&
+  getName() const
+  {
+    return m_name;
+  }
+  
+  StrategyChoiceOptions&
+  setName(const Name& name)
+  {
+    m_name = name;
+    m_wire.reset();
+    return *this;
+  }
+  
+  const Name&
+  getStrategy() const
+  {
+    return m_strategy;
+  }
+  
+  StrategyChoiceOptions&
+  setStrategy(const Name& strategy)
+  {
+    m_strategy = strategy;
+    m_wire.reset();
+    return *this;
+  }
+  
+  template<bool T>
+  size_t
+  wireEncode(EncodingImpl<T>& block) const;
+  
+  const Block&
+  wireEncode() const;
+  
+  void
+  wireDecode(const Block& wire);
+  
+private:
+  Name m_name;
+  Name m_strategy;
+  
+  mutable Block m_wire;
+};
+
+template<bool T>
+inline size_t
+StrategyChoiceOptions::wireEncode(EncodingImpl<T>& block) const
+{
+  size_t totalLength = 0;
+  
+  if (!m_strategy.empty())
+  {
+    totalLength += prependNestedBlock(block, tlv::nfd::Strategy, m_strategy);
+  }
+  
+  totalLength += m_name.wireEncode(block);
+  
+  totalLength += block.prependVarNumber(totalLength);
+  totalLength += block.prependVarNumber(tlv::nfd::StrategyChoiceOptions);
+  return totalLength;
+}
+
+inline const Block&
+StrategyChoiceOptions::wireEncode() const
+{
+  if (m_wire.hasWire())
+    return m_wire;
+  
+  EncodingEstimator estimator;
+  size_t estimatedSize = wireEncode(estimator);
+  
+  EncodingBuffer buffer(estimatedSize, 0);
+  wireEncode(buffer);
+  
+  m_wire = buffer.block();
+  return m_wire;
+}
+
+inline void
+StrategyChoiceOptions::wireDecode(const Block& wire)
+{
+  m_name.clear();
+  m_strategy.clear();
+  
+  m_wire = wire;
+  
+  if (m_wire.type() != tlv::nfd::StrategyChoiceOptions)
+    throw Error("Requested decoding of StrategyChoiceOptions, but Block is of different type");
+  
+  m_wire.parse();
+  
+  // Name
+  Block::element_const_iterator val = m_wire.find(Tlv::Name);
+  if (val != m_wire.elements_end())
+  {
+    m_name.wireDecode(*val);
+  }
+  
+  // Strategy
+  val = m_wire.find(tlv::nfd::Strategy);
+  if (val != m_wire.elements_end())
+  {
+    val->parse();
+    if (!val->elements().empty())
+      m_strategy.wireDecode(*val->elements_begin());
+  }
+}
+
+inline std::ostream&
+operator << (std::ostream& os, const StrategyChoiceOptions& option)
+{
+  os << "StrategyChoiceOptions(";
+  
+  // Name
+  os << "Name: " << option.getName() << ", ";
+  
+  // Strategy
+  os << "Strategy: " << option.getStrategy() << ", ";
+  
+  os << ")";
+  return os;
+}
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_MANAGEMENT_OPTIONS_HPP