tools: Update code to use unified NFD Management Protocol
Change-Id: I0e638f189afa87431292ca9471d95cba02ccd831
Refs: #1418
diff --git a/tools/ndn-autoconfig.cpp b/tools/ndn-autoconfig.cpp
index 1fb02b1..3059dc4 100644
--- a/tools/ndn-autoconfig.cpp
+++ b/tools/ndn-autoconfig.cpp
@@ -6,7 +6,6 @@
#include <ndn-cpp-dev/face.hpp>
#include <ndn-cpp-dev/management/nfd-controller.hpp>
-#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
#include <ndn-cpp-dev/security/key-chain.hpp>
#include <sys/types.h>
@@ -18,7 +17,9 @@
#include <arpa/nameser_compat.h>
#endif
-class NdnAutoconfig : public ndn::nfd::Controller
+namespace tools {
+
+class NdnAutoconfig
{
public:
union QueryAnswer
@@ -26,17 +27,20 @@
HEADER header;
uint8_t buf[NS_PACKETSZ];
};
-
- struct Error : public std::runtime_error
+
+ class Error : public std::runtime_error
{
- Error(const std::string& what) : std::runtime_error(what)
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
{
}
};
-
+
explicit
- NdnAutoconfig(ndn::Face& face)
- : ndn::nfd::Controller(face)
+ NdnAutoconfig()
+ : m_controller(m_face)
{
}
@@ -47,41 +51,42 @@
ndn::Interest interest(ndn::Name("/localhop/ndn-autoconf/hub"));
interest.setInterestLifetime(ndn::time::milliseconds(4000)); // 4 seconds
interest.setMustBeFresh(true);
-
+
std::cerr << "Stage 1: Trying muticast discovery..." << std::endl;
m_face.expressInterest(interest,
ndn::bind(&NdnAutoconfig::onDiscoverHubStage1Success, this, _1, _2),
ndn::bind(&NdnAutoconfig::discoverHubStage2, this, _1, "Timeout"));
-
+
m_face.processEvents();
}
-
+
// First stage OnData Callback
void
onDiscoverHubStage1Success(const ndn::Interest& interest, ndn::Data& data)
{
const ndn::Block& content = data.getContent();
content.parse();
-
+
// Get Uri
- ndn::Block::element_const_iterator bockValue = content.find(ndn::tlv::nfd::Uri);
- if (bockValue == content.elements_end())
+ ndn::Block::element_const_iterator blockValue = content.find(ndn::tlv::nfd::Uri);
+ if (blockValue == content.elements_end())
{
discoverHubStage2(interest, "Incorrect reply to stage1");
return;
}
- std::string faceMgmtUri(reinterpret_cast<const char*>(bockValue->value()), bockValue->value_size());
+ std::string faceMgmtUri(reinterpret_cast<const char*>(blockValue->value()),
+ blockValue->value_size());
connectToHub(faceMgmtUri);
}
-
+
// First stage OnTimeout callback - start 2nd stage
void
discoverHubStage2(const ndn::Interest& interest, const std::string& message)
{
std::cerr << message << std::endl;
std::cerr << "Stage 2: Trying DNS query with default suffix..." << std::endl;
-
- _res.retry = 2;
+
+ _res.retry = 2;
_res.ndots = 10;
QueryAnswer queryAnswer;
@@ -91,7 +96,7 @@
ns_t_srv,
queryAnswer.buf,
sizeof(queryAnswer));
-
+
// 2nd stage failed - move on to the third stage
if (answerSize < 0)
{
@@ -107,18 +112,18 @@
}
}
}
-
+
// Second stage OnTimeout callback
void
discoverHubStage3(const std::string& message)
{
std::cerr << message << std::endl;
std::cerr << "Stage 3: Trying to find home router..." << std::endl;
-
+
ndn::KeyChain keyChain;
ndn::Name identity = keyChain.getDefaultIdentity();
std::string serverName = "_ndn._udp.";
-
+
for (ndn::Name::const_reverse_iterator i = identity.rbegin(); i != identity.rend(); i++)
{
serverName.append(i->toEscapedString());
@@ -126,7 +131,7 @@
}
serverName += "_homehub._autoconf.named-data.net";
std::cerr << "Stage3: About to query for a home router: " << serverName << std::endl;
-
+
QueryAnswer queryAnswer;
int answerSize = res_query(serverName.c_str(),
@@ -135,7 +140,7 @@
queryAnswer.buf,
sizeof(queryAnswer));
-
+
// 3rd stage failed - abort
if (answerSize < 0)
{
@@ -151,69 +156,71 @@
throw Error("Failed to parse DNS response");
}
}
-
+
}
-
+
void
connectToHub(const std::string& uri)
{
std::cerr << "about to connect to: " << uri << std::endl;
- ndn::nfd::FaceManagementOptions faceOptions;
-
- faceOptions.setUri(uri);
- startFaceCommand("create",
- faceOptions,
- bind(&NdnAutoconfig::onHubConnectSuccess, this, _1, "Succesfully created face: "),
- bind(&NdnAutoconfig::onHubConnectError, this, _1, "Failed to create face: "));
+
+ m_controller.start<ndn::nfd::FaceCreateCommand>(
+ ndn::nfd::ControlParameters()
+ .setUri(uri),
+ bind(&NdnAutoconfig::onHubConnectSuccess, this,
+ _1, "Succesfully created face: "),
+ bind(&NdnAutoconfig::onHubConnectError, this,
+ _1, _2, "Failed to create face: ")
+ );
}
-
+
void
- onHubConnectSuccess(const ndn::nfd::FaceManagementOptions& resp, const std::string& message)
+ onHubConnectSuccess(const ndn::nfd::ControlParameters& resp, const std::string& message)
{
std::cerr << message << resp << std::endl;
}
-
+
void
- onHubConnectError(const std::string& error, const std::string& message)
+ onHubConnectError(uint32_t code, const std::string& error, const std::string& message)
{
- throw Error(message + ": " + error);
+ std::ostringstream os;
+ os << message << ": " << error << " (code: " << code << ")";
+ throw Error(os.str());
}
-
+
bool parseHostAndConnectToHub(QueryAnswer& queryAnswer, int answerSize)
{
// The references of the next classes are:
// http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm
// https://gist.github.com/mologie/6027597
-
- class rechdr
+
+ struct rechdr
{
- public:
uint16_t type;
uint16_t iclass;
uint32_t ttl;
uint16_t length;
};
-
- class srv_t
+
+ struct srv_t
{
- public:
uint16_t priority;
uint16_t weight;
uint16_t port;
uint8_t* target;
};
-
+
if (ntohs(queryAnswer.header.ancount) == 0)
{
std::cerr << "No records found\n" << std::endl;
return false;
}
-
+
uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
-
+
blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
-
+
for (int i = 0; i < ntohs(queryAnswer.header.ancount); i++)
{
char hostName[NS_MAXDNAME];
@@ -226,7 +233,7 @@
{
return false;
}
-
+
blob += domainNameSize;
domainNameSize = dn_expand(queryAnswer.buf,
@@ -245,28 +252,33 @@
uri.append(hostName);
uri.append(":");
uri.append(boost::lexical_cast<std::string>(convertedPort));
-
+
connectToHub(uri);
return true;
}
return false;
}
+
+private:
+ ndn::Face m_face;
+ ndn::nfd::Controller m_controller;
};
+} // namespace tools
int
main()
{
- ndn::Face face;
- NdnAutoconfig autoConfig(face);
try
- {
- autoConfig.discoverHubStage1();
- }
+ {
+ tools::NdnAutoconfig autoConfigInstance;
+
+ autoConfigInstance.discoverHubStage1();
+ }
catch (const std::exception& error)
- {
- std::cerr << "ERROR: " << error.what() << std::endl;
- }
+ {
+ std::cerr << "ERROR: " << error.what() << std::endl;
+ }
return 0;
}
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index 9ed5840..ca7d1e6 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.cpp
@@ -41,9 +41,10 @@
}
void
- onNfdCommandFailure(const FaceEventNotification& notification, const std::string& reason)
+ onNfdCommandFailure(const FaceEventNotification& notification,
+ uint32_t code, const std::string& reason)
{
- std::cerr << "FAILED: " << notification << std::endl;
+ std::cerr << "FAILED: " << notification << " (code: " << code << ")" << std::endl;
}
bool
@@ -91,9 +92,14 @@
++prefix)
{
std::cout << "Try auto-register: " << *prefix << std::endl;
- m_controller.fibAddNextHop(*prefix, notification.getFaceId(), m_cost,
- bind(&AutoregServer::onNfdCommandSuccess, this, notification),
- bind(&AutoregServer::onNfdCommandFailure, this, notification, _1));
+
+ m_controller.start<FibAddNextHopCommand>(
+ ControlParameters()
+ .setName(*prefix)
+ .setFaceId(notification.getFaceId())
+ .setCost(m_cost),
+ bind(&AutoregServer::onNfdCommandSuccess, this, notification),
+ bind(&AutoregServer::onNfdCommandFailure, this, notification, _1, _2));
}
}
diff --git a/tools/nfdc.cpp b/tools/nfdc.cpp
index 285bd9d..fd2fbc3 100644
--- a/tools/nfdc.cpp
+++ b/tools/nfdc.cpp
@@ -13,38 +13,39 @@
usage(const char* programName)
{
std::cout << "Usage:\n" << programName << " [-h] COMMAND\n"
- " -h print usage and exit\n"
- "\n"
- " COMMAND can be one of following:\n"
- " add-nexthop <name> <faceId> [<cost>]\n"
- " Add a nexthop to a FIB entry\n"
- " remove-nexthop <name> <faceId> \n"
- " Remove a nexthop from a FIB entry\n"
- " create <uri> \n"
- " Create a face in one of the following formats:\n"
- " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n"
- " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n"
- " destroy <faceId> \n"
- " Destroy a face\n"
- " set-strategy <name> <strategy> \n"
- " Set the strategy for a namespace \n"
- " unset-strategy <name> \n"
- " Unset the strategy for a namespace \n"
- << std::endl;
+ " -h print usage and exit\n"
+ "\n"
+ " COMMAND can be one of following:\n"
+ " add-nexthop <name> <faceId> [<cost>]\n"
+ " Add a nexthop to a FIB entry\n"
+ " remove-nexthop <name> <faceId> \n"
+ " Remove a nexthop from a FIB entry\n"
+ " create <uri> \n"
+ " Create a face in one of the following formats:\n"
+ " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n"
+ " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n"
+ " destroy <faceId> \n"
+ " Destroy a face\n"
+ " set-strategy <name> <strategy> \n"
+ " Set the strategy for a namespace \n"
+ " unset-strategy <name> \n"
+ " Unset the strategy for a namespace \n"
+ << std::endl;
}
namespace nfdc {
-
-Controller::Controller(ndn::Face& face)
- : ndn::nfd::Controller(face)
+
+Nfdc::Nfdc(ndn::Face& face)
+ : m_controller(face)
{
}
-
-Controller::~Controller()
+
+Nfdc::~Nfdc()
{
}
+
bool
-Controller::dispatch(const std::string& command, const char* commandOptions[], int nOptions)
+Nfdc::dispatch(const std::string& command, const char* commandOptions[], int nOptions)
{
if (command == "add-nexthop") {
if (nOptions == 2)
@@ -86,44 +87,48 @@
}
void
-Controller::fibAddNextHop(const char* commandOptions[], bool hasCost)
+Nfdc::fibAddNextHop(const char* commandOptions[], bool hasCost)
{
- ndn::nfd::FibManagementOptions fibOptions;
-
const std::string& name = commandOptions[0];
const int faceId = boost::lexical_cast<int>(commandOptions[1]);
- fibOptions.setName(name);
- fibOptions.setFaceId(faceId);
+ ControlParameters parameters;
+ parameters
+ .setName(name)
+ .setFaceId(faceId);
if (hasCost)
{
- const int cost = boost::lexical_cast<int>(commandOptions[2]);
- fibOptions.setCost(cost);
+ const uint64_t cost = boost::lexical_cast<uint64_t>(commandOptions[2]);
+ parameters.setCost(cost);
}
- startFibCommand("add-nexthop",
- fibOptions,
- bind(&Controller::onFibSuccess, this, _1, "Nexthop insertion succeeded"),
- bind(&Controller::onError, this, _1, "Nexthop insertion failed"));
+
+ m_controller.start<FibAddNextHopCommand>(
+ parameters,
+ bind(&Nfdc::onSuccess, this, _1, "Nexthop insertion succeeded"),
+ bind(&Nfdc::onError, this, _1, _2, "Nexthop insertion failed"));
}
void
-Controller::fibRemoveNextHop(const char* commandOptions[])
+Nfdc::fibRemoveNextHop(const char* commandOptions[])
{
const std::string& name = commandOptions[0];
const int faceId = boost::lexical_cast<int>(commandOptions[1]);
- ndn::nfd::FibManagementOptions fibOptions;
- fibOptions.setName(name);
- fibOptions.setFaceId(faceId);
- startFibCommand("remove-nexthop",
- fibOptions,
- bind(&Controller::onFibSuccess, this, _1, "Nexthop Removal succeeded"),
- bind(&Controller::onError, this, _1, "Nexthop Removal failed"));
+ ControlParameters parameters;
+ parameters
+ .setName(name)
+ .setFaceId(faceId);
+
+ m_controller.start<FibRemoveNextHopCommand>(
+ parameters,
+ bind(&Nfdc::onSuccess, this, _1, "Nexthop removal succeeded"),
+ bind(&Nfdc::onError, this, _1, _2, "Nexthop removal failed"));
}
namespace {
-bool
+
+inline bool
isValidUri(const std::string& input)
{
// an extended regex to support the validation of uri structure
@@ -131,105 +136,94 @@
boost::regex e("^[a-z0-9]+\\:.*");
return boost::regex_match(input, e);
}
+
} // anonymous namespace
void
-Controller::faceCreate(const char* commandOptions[])
+Nfdc::faceCreate(const char* commandOptions[])
{
- ndn::nfd::FaceManagementOptions faceOptions;
const std::string& uri = commandOptions[0];
- faceOptions.setUri(uri);
-
- if (isValidUri(uri))
- {
- startFaceCommand("create",
- faceOptions,
- bind(&Controller::onFaceSuccess, this, _1, "Face creation succeeded"),
- bind(&Controller::onError, this, _1, "Face creation failed"));
- }
- else
+ if (!isValidUri(uri))
throw Error("invalid uri format");
+
+ ControlParameters parameters;
+ parameters
+ .setUri(uri);
+
+ m_controller.start<FaceCreateCommand>(
+ parameters,
+ bind(&Nfdc::onSuccess, this, _1, "Face creation succeeded"),
+ bind(&Nfdc::onError, this, _1, _2, "Face creation failed"));
}
-
+
void
-Controller::faceDestroy(const char* commandOptions[])
+Nfdc::faceDestroy(const char* commandOptions[])
{
- ndn::nfd::FaceManagementOptions faceOptions;
const int faceId = boost::lexical_cast<int>(commandOptions[0]);
- faceOptions.setFaceId(faceId);
-
- startFaceCommand("destroy",
- faceOptions,
- bind(&Controller::onFaceSuccess, this, _1, "Face destroy succeeded"),
- bind(&Controller::onError, this, _1, "Face destroy failed"));
+
+ ControlParameters parameters;
+ parameters
+ .setFaceId(faceId);
+
+ m_controller.start<FaceDestroyCommand>(
+ parameters,
+ bind(&Nfdc::onSuccess, this, _1, "Face destroy succeeded"),
+ bind(&Nfdc::onError, this, _1, _2, "Face destroy failed"));
}
-
+
void
-Controller::strategyChoiceSet(const char* commandOptions[])
+Nfdc::strategyChoiceSet(const char* commandOptions[])
{
const std::string& name = commandOptions[0];
const std::string& strategy = commandOptions[1];
- ndn::nfd::StrategyChoiceOptions strategyChoiceOptions;
-
- strategyChoiceOptions.setName(name);
- strategyChoiceOptions.setStrategy(strategy);
-
- startStrategyChoiceCommand("set",
- strategyChoiceOptions,
- bind(&Controller::onSetStrategySuccess,
- this,
- _1,
- "Successfully set strategy choice"),
- bind(&Controller::onError, this, _1, "Failed to set strategy choice"));
-
+
+ ControlParameters parameters;
+ parameters
+ .setName(name)
+ .setStrategy(strategy);
+
+ m_controller.start<StrategyChoiceSetCommand>(
+ parameters,
+ bind(&Nfdc::onSuccess, this, _1, "Successfully set strategy choice"),
+ bind(&Nfdc::onError, this, _1, _2, "Failed to set strategy choice"));
}
-
+
void
-Controller::strategyChoiceUnset(const char* commandOptions[])
+Nfdc::strategyChoiceUnset(const char* commandOptions[])
{
const std::string& name = commandOptions[0];
- ndn::nfd::StrategyChoiceOptions strategyChoiceOptions;
-
- strategyChoiceOptions.setName(name);
- startStrategyChoiceCommand("unset",
- strategyChoiceOptions,
- bind(&Controller::onSetStrategySuccess,
- this,
- _1,
- "Successfully unset strategy choice"),
- bind(&Controller::onError, this, _1, "Failed to unset strategy choice"));
+
+ ControlParameters parameters;
+ parameters
+ .setName(name);
+
+ m_controller.start<StrategyChoiceUnsetCommand>(
+ parameters,
+ bind(&Nfdc::onSuccess, this, _1, "Successfully unset strategy choice"),
+ bind(&Nfdc::onError, this, _1, _2, "Failed to unset strategy choice"));
}
-
+
void
-Controller::onFibSuccess(const ndn::nfd::FibManagementOptions& resp, const std::string& message)
+Nfdc::onSuccess(const ControlParameters& parameters, const std::string& message)
{
- std::cout << message << ": " << resp << std::endl;
+ std::cout << message << ": " << parameters << std::endl;
}
-
+
void
-Controller::onFaceSuccess(const ndn::nfd::FaceManagementOptions& resp, const std::string& message)
+Nfdc::onError(uint32_t code, const std::string& error, const std::string& message)
{
- std::cout << message << ": " << resp << std::endl;
+ std::ostringstream os;
+ os << message << ": " << error << " (code: " << code << ")";
+ throw Error(os.str());
}
-void
-Controller::onSetStrategySuccess(const ndn::nfd::StrategyChoiceOptions& resp,
- const std::string& message)
-{
- std::cout << message << ": " << resp << std::endl;
-}
-
-void
-Controller::onError(const std::string& error, const std::string& message)
-{
- throw Error(message + ": " + error);
-}
+
} // namespace nfdc
int
main(int argc, char** argv)
{
ndn::Face face;
- nfdc::Controller p(face);
+ nfdc::Nfdc p(face);
p.m_programName = argv[0];
int opt;
@@ -238,7 +232,7 @@
case 'h':
usage(p.m_programName);
return 0;
-
+
default:
usage(p.m_programName);
return 1;
@@ -251,14 +245,14 @@
}
try {
- bool hasSucceeded = p.dispatch(argv[optind],
- const_cast<const char**>(argv + optind + 1),
- argc - optind - 1);
- if (hasSucceeded == false) {
+ bool isOk = p.dispatch(argv[optind],
+ const_cast<const char**>(argv + optind + 1),
+ argc - optind - 1);
+ if (!isOk) {
usage(p.m_programName);
return 1;
}
-
+
face.processEvents();
}
catch (const std::exception& e) {
@@ -267,4 +261,3 @@
}
return 0;
}
-
diff --git a/tools/nfdc.hpp b/tools/nfdc.hpp
index fa131fb..02d5f9a 100644
--- a/tools/nfdc.hpp
+++ b/tools/nfdc.hpp
@@ -10,42 +10,48 @@
#include <ndn-cpp-dev/face.hpp>
#include <ndn-cpp-dev/management/controller.hpp>
#include <ndn-cpp-dev/management/nfd-controller.hpp>
-#include <ndn-cpp-dev/management/nfd-fib-management-options.hpp>
-#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
-#include <ndn-cpp-dev/management/nfd-strategy-choice-options.hpp>
#include <vector>
namespace nfdc {
-
-class Controller : public ndn::nfd::Controller
+
+using namespace ndn::nfd;
+
+class Nfdc
{
public:
- struct Error : public std::runtime_error
+ class Error : public std::runtime_error
{
- Error(const std::string& what) : std::runtime_error(what) {}
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
};
-
+
explicit
- Controller(ndn::Face& face);
-
- ~Controller();
-
+ Nfdc(ndn::Face& face);
+
+ ~Nfdc();
+
bool
dispatch(const std::string& cmd,
const char* cmdOptions[],
int nOptions);
+
/**
- * \brief Adds a nexthop to a FIB entry.
+ * \brief Adds a nexthop to a FIB entry.
*
* If the FIB entry does not exist, it is inserted automatically
*
* cmd format:
* name
*
- * @param cmdOptions add command without leading 'insert' component
+ * \param cmdOptions Add next hop command parameters without leading 'add-nexthop' component
*/
void
fibAddNextHop(const char* cmdOptions[], bool hasCost);
+
/**
* \brief Removes a nexthop from an existing FIB entry
*
@@ -54,10 +60,12 @@
* cmd format:
* name faceId
*
- * @param cmdOptions delNext command without leading 'remove-nexthop' component
+ * \param cmdOptions Remove next hop command parameters without leading
+ * 'remove-nexthop' component
*/
void
- fibRemoveNextHop(const char* cmdOptions[]);
+ fibRemoveNextHop(const char* cmdOptions[]);
+
/**
* \brief create new face
*
@@ -66,20 +74,22 @@
* cmd format:
* uri
*
- * @param cmdOptions create command without leading 'create' component
+ * \param cmdOptions Create face command parameters without leading 'create' component
*/
void
faceCreate(const char* cmdOptions[]);
+
/**
* \brief destroy a face
*
* cmd format:
* faceId
*
- * @param cmdOptions destroy command without leading 'destroy' component
+ * \param cmdOptions Destroy face command parameters without leading 'destroy' component
*/
void
faceDestroy(const char* cmdOptions[]);
+
/**
* \brief Set the strategy for a namespace
*
@@ -87,10 +97,12 @@
* cmd format:
* name strategy
*
- * @param cmdOptions Set command without leading 'Unset' component
+ * \param cmdOptions Set strategy choice command parameters without leading
+ * 'set-strategy' component
*/
void
strategyChoiceSet(const char* cmdOptions[]);
+
/**
* \brief Unset the strategy for a namespace
*
@@ -98,31 +110,27 @@
* cmd format:
* name strategy
*
- * @param cmdOptions Unset command without leading 'Unset' component
+ * \param cmdOptions Unset strategy choice command parameters without leading
+ * 'unset-strategy' component
*/
void
strategyChoiceUnset(const char* cmdOptions[]);
-
+
private:
void
- onFibSuccess(const ndn::nfd::FibManagementOptions& fibOptions,
- const std::string& message);
+ onSuccess(const ControlParameters& parameters,
+ const std::string& message);
void
- onFaceSuccess(const ndn::nfd::FaceManagementOptions& faceOptions,
- const std::string& message);
-
- void
- onSetStrategySuccess(const ndn::nfd::StrategyChoiceOptions& resp,
- const std::string& message);
- void
- onError(const std::string& error, const std::string& message);
-
+ onError(uint32_t code, const std::string& error, const std::string& message);
+
public:
const char* m_programName;
+
+private:
+ Controller m_controller;
};
} // namespace nfdc
#endif // NFD_TOOLS_NFDC_HPP
-