tools: nfdc face list command with filters
refs #3864
Change-Id: Ie22e506bb2aeae8abf7c94b394e7aa56c840266f
diff --git a/tools/nfdc/command-arguments.hpp b/tools/nfdc/command-arguments.hpp
index b3175d5..1bec816 100644
--- a/tools/nfdc/command-arguments.hpp
+++ b/tools/nfdc/command-arguments.hpp
@@ -53,6 +53,19 @@
auto i = find(key);
return i == end() ? defaultValue : boost::any_cast<T>(i->second);
}
+
+ /** \return the argument value, or nullopt if the argument is omitted on command line
+ */
+ template<typename T>
+ ndn::optional<T>
+ getOptional(const std::string& key) const
+ {
+ auto i = find(key);
+ if (i == end()) {
+ return ndn::nullopt;
+ }
+ return boost::any_cast<T>(i->second);
+ }
};
} // namespace nfdc
diff --git a/tools/nfdc/face-module.cpp b/tools/nfdc/face-module.cpp
index 3608ac2..216ded6 100644
--- a/tools/nfdc/face-module.cpp
+++ b/tools/nfdc/face-module.cpp
@@ -34,6 +34,14 @@
void
FaceModule::registerCommands(CommandParser& parser)
{
+ CommandDefinition defFaceList("face", "list");
+ defFaceList
+ .setTitle("print face list")
+ .addArg("remote", ArgValueType::FACE_URI, Required::NO, Positional::YES)
+ .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO)
+ .addArg("scheme", ArgValueType::STRING, Required::NO, Positional::NO, "scheme");
+ parser.addCommand(defFaceList, &FaceModule::list);
+
CommandDefinition defFaceShow("face", "show");
defFaceShow
.setTitle("show face information")
@@ -55,6 +63,46 @@
}
void
+FaceModule::list(ExecuteContext& ctx)
+{
+ auto remoteUri = ctx.args.getOptional<FaceUri>("remote");
+ auto localUri = ctx.args.getOptional<FaceUri>("local");
+ auto uriScheme = ctx.args.getOptional<std::string>("scheme");
+
+ FaceQueryFilter filter;
+ if (remoteUri) {
+ filter.setRemoteUri(remoteUri->toString());
+ }
+ if (localUri) {
+ filter.setLocalUri(localUri->toString());
+ }
+ if (uriScheme) {
+ filter.setUriScheme(*uriScheme);
+ }
+
+ FindFace findFace(ctx);
+ FindFace::Code res = findFace.execute(filter, true);
+
+ ctx.exitCode = static_cast<int>(res);
+ switch (res) {
+ case FindFace::Code::OK:
+ for (const FaceStatus& item : findFace.getResults()) {
+ formatItemText(ctx.out, item, false);
+ ctx.out << '\n';
+ }
+ break;
+ case FindFace::Code::ERROR:
+ case FindFace::Code::NOT_FOUND:
+ case FindFace::Code::CANONIZE_ERROR:
+ ctx.err << findFace.getErrorReason() << '\n';
+ break;
+ default:
+ BOOST_ASSERT_MSG(false, "unexpected FindFace result");
+ break;
+ }
+}
+
+void
FaceModule::show(ExecuteContext& ctx)
{
uint64_t faceId = ctx.args.get<uint64_t>("id");
diff --git a/tools/nfdc/face-module.hpp b/tools/nfdc/face-module.hpp
index 3f70d48..6c190a1 100644
--- a/tools/nfdc/face-module.hpp
+++ b/tools/nfdc/face-module.hpp
@@ -41,11 +41,16 @@
class FaceModule : public Module, noncopyable
{
public:
- /** \brief register 'face show', 'face create', 'face destroy' commands
+ /** \brief register 'face list', 'face show', 'face create', 'face destroy' commands
*/
static void
registerCommands(CommandParser& parser);
+ /** \brief the 'face list' command
+ */
+ static void
+ list(ExecuteContext& ctx);
+
/** \brief the 'face show' command
*/
static void
diff --git a/tools/nfdc/find-face.cpp b/tools/nfdc/find-face.cpp
index c03e6dc..520ea92 100644
--- a/tools/nfdc/find-face.cpp
+++ b/tools/nfdc/find-face.cpp
@@ -70,7 +70,14 @@
m_filter.setRemoteUri(remoteUri->toString());
}
- ///\todo #3864 canonize localUri
+ if (m_filter.hasLocalUri()) {
+ auto localUri = this->canonize("local", FaceUri(m_filter.getLocalUri()));
+ if (!localUri) {
+ m_res = Code::CANONIZE_ERROR;
+ return m_res;
+ }
+ m_filter.setLocalUri(localUri->toString());
+ }
this->query();
if (m_res == Code::OK) {
@@ -109,17 +116,23 @@
void
FindFace::query()
{
- m_ctx.controller.fetch<ndn::nfd::FaceQueryDataset>(
- m_filter,
- [this] (const std::vector<ndn::nfd::FaceStatus>& result) {
- m_res = Code::OK;
- m_results = result;
- },
- [this] (uint32_t code, const std::string& reason) {
- m_res = Code::ERROR;
- m_errorReason = "Error " + std::to_string(code) + " when querying face: " + reason;
- },
- m_ctx.makeCommandOptions());
+ auto datasetCb = [this] (const std::vector<ndn::nfd::FaceStatus>& result) {
+ m_res = Code::OK;
+ m_results = result;
+ };
+ auto failureCb = [this] (uint32_t code, const std::string& reason) {
+ m_res = Code::ERROR;
+ m_errorReason = "Error " + std::to_string(code) + " when querying face: " + reason;
+ };
+
+ if (m_filter.empty()) {
+ m_ctx.controller.fetch<ndn::nfd::FaceDataset>(
+ datasetCb, failureCb, m_ctx.makeCommandOptions());
+ }
+ else {
+ m_ctx.controller.fetch<ndn::nfd::FaceQueryDataset>(
+ m_filter, datasetCb, failureCb, m_ctx.makeCommandOptions());
+ }
m_ctx.face.processEvents();
}
diff --git a/tools/nfdc/status.cpp b/tools/nfdc/status.cpp
index e7de814..6165565 100644
--- a/tools/nfdc/status.cpp
+++ b/tools/nfdc/status.cpp
@@ -131,11 +131,6 @@
parser.addCommand(defStatusShow, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantForwarderGeneral));
parser.addAlias("status", "show", "list");
- CommandDefinition defFaceList("face", "list");
- defFaceList
- .setTitle("print face list");
- parser.addCommand(defFaceList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFaces));
-
CommandDefinition defChannelList("channel", "list");
defChannelList
.setTitle("print channel list");
diff --git a/tools/nfdc/status.hpp b/tools/nfdc/status.hpp
index 6cc799e..57dac50 100644
--- a/tools/nfdc/status.hpp
+++ b/tools/nfdc/status.hpp
@@ -54,7 +54,6 @@
* Providing the following commands:
* \li status report
* \li status show
- * \li face list
* \li channel list
* \li strategy list
* \li fib list