mgmt: migrate to new Dispatcher API for control commands
Change-Id: Iaba68dde879fbb562e13ef383e0b753174708baa
diff --git a/daemon/mgmt/cs-manager.cpp b/daemon/mgmt/cs-manager.cpp
index a807df8..3be976f 100644
--- a/daemon/mgmt/cs-manager.cpp
+++ b/daemon/mgmt/cs-manager.cpp
@@ -39,12 +39,15 @@
, m_cs(cs)
, m_fwCounters(fwCounters)
{
- registerCommandHandler<ndn::nfd::CsConfigCommand>("config",
- [this] (auto&&, auto&&, auto&&... args) { changeConfig(std::forward<decltype(args)>(args)...); });
- registerCommandHandler<ndn::nfd::CsEraseCommand>("erase",
- [this] (auto&&, auto&&, auto&&... args) { erase(std::forward<decltype(args)>(args)...); });
- registerStatusDatasetHandler("info",
- [this] (auto&&, auto&&, auto&&... args) { serveInfo(std::forward<decltype(args)>(args)...); });
+ registerCommandHandler<ndn::nfd::CsConfigCommand>([this] (auto&&, auto&&, auto&&... args) {
+ changeConfig(std::forward<decltype(args)>(args)...);
+ });
+ registerCommandHandler<ndn::nfd::CsEraseCommand>([this] (auto&&, auto&&, auto&&... args) {
+ erase(std::forward<decltype(args)>(args)...);
+ });
+ registerStatusDatasetHandler("info", [this] (auto&&, auto&&, auto&&... args) {
+ serveInfo(std::forward<decltype(args)>(args)...);
+ });
}
void
diff --git a/daemon/mgmt/face-manager.cpp b/daemon/mgmt/face-manager.cpp
index e822f12..34eb8e6 100644
--- a/daemon/mgmt/face-manager.cpp
+++ b/daemon/mgmt/face-manager.cpp
@@ -49,12 +49,15 @@
, m_faceTable(faceSystem.getFaceTable())
{
// register handlers for ControlCommand
- registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
- [this] (auto&&, auto&&, auto&&... args) { createFace(std::forward<decltype(args)>(args)...); });
- registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
- [this] (auto&&, auto&&... args) { updateFace(std::forward<decltype(args)>(args)...); });
- registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
- [this] (auto&&, auto&&, auto&&... args) { destroyFace(std::forward<decltype(args)>(args)...); });
+ registerCommandHandler<ndn::nfd::FaceCreateCommand>([this] (auto&&, auto&&, auto&&... args) {
+ createFace(std::forward<decltype(args)>(args)...);
+ });
+ registerCommandHandler<ndn::nfd::FaceUpdateCommand>([this] (auto&&, auto&&... args) {
+ updateFace(std::forward<decltype(args)>(args)...);
+ });
+ registerCommandHandler<ndn::nfd::FaceDestroyCommand>([this] (auto&&, auto&&, auto&&... args) {
+ destroyFace(std::forward<decltype(args)>(args)...);
+ });
// register handlers for StatusDataset
registerStatusDatasetHandler("list",
diff --git a/daemon/mgmt/fib-manager.cpp b/daemon/mgmt/fib-manager.cpp
index cdae3f6..4de989e 100644
--- a/daemon/mgmt/fib-manager.cpp
+++ b/daemon/mgmt/fib-manager.cpp
@@ -44,12 +44,15 @@
, m_fib(fib)
, m_faceTable(faceTable)
{
- registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
- [this] (auto&&, auto&&... args) { addNextHop(std::forward<decltype(args)>(args)...); });
- registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
- [this] (auto&&, auto&&... args) { removeNextHop(std::forward<decltype(args)>(args)...); });
- registerStatusDatasetHandler("list",
- [this] (auto&&, auto&&, auto&&... args) { listEntries(std::forward<decltype(args)>(args)...); });
+ registerCommandHandler<ndn::nfd::FibAddNextHopCommand>([this] (auto&&, auto&&... args) {
+ addNextHop(std::forward<decltype(args)>(args)...);
+ });
+ registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>([this] (auto&&, auto&&... args) {
+ removeNextHop(std::forward<decltype(args)>(args)...);
+ });
+ registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
+ listEntries(std::forward<decltype(args)>(args)...);
+ });
}
void
diff --git a/daemon/mgmt/manager-base.hpp b/daemon/mgmt/manager-base.hpp
index 53a5fc6..ec03463 100644
--- a/daemon/mgmt/manager-base.hpp
+++ b/daemon/mgmt/manager-base.hpp
@@ -76,15 +76,23 @@
~ManagerBase();
NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
- // difference from ndn::mgmt::ControlCommandHandler: accepts nfd::ControlParameters
+ template<typename Command>
using ControlCommandHandler = std::function<void(const Name& prefix, const Interest& interest,
- const ControlParameters& parameters,
+ const typename Command::RequestParameters& parameters,
const CommandContinuation& done)>;
template<typename Command>
void
- registerCommandHandler(const std::string& verb,
- ControlCommandHandler handler);
+ registerCommandHandler(ControlCommandHandler<Command> handler)
+ {
+ auto handle = [h = std::move(handler)] (const auto& prefix, const auto& interest,
+ const auto& params, const auto& done) {
+ const auto& reqParams = static_cast<const typename Command::RequestParameters&>(params);
+ h(prefix, interest, reqParams, done);
+ };
+ m_dispatcher.addControlCommand<Command>(makeAuthorization(Command::verb.toUri()),
+ std::move(handle));
+ }
void
registerStatusDatasetHandler(const std::string& verb,
@@ -128,34 +136,6 @@
CommandAuthenticator* m_authenticator = nullptr;
};
-template<typename Command>
-void
-ManagerBase::registerCommandHandler(const std::string& verb, ControlCommandHandler handler)
-{
- auto validate = [] (const ndn::mgmt::ControlParametersBase& params) {
- BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶ms) != nullptr);
- try {
- Command::validateRequest(static_cast<const ControlParameters&>(params));
- return true;
- }
- catch (const std::invalid_argument&) {
- return false;
- }
- };
-
- auto handle = [handler = std::move(handler)] (const Name& prefix, const Interest& interest,
- const ndn::mgmt::ControlParametersBase& params,
- const CommandContinuation& done) {
- BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶ms) != nullptr);
- ControlParameters parameters = static_cast<const ControlParameters&>(params);
- Command::applyDefaultsToRequest(parameters);
- handler(prefix, interest, parameters, done);
- };
-
- m_dispatcher.addControlCommand<ControlParameters>(makeRelPrefix(verb), makeAuthorization(verb),
- std::move(validate), std::move(handle));
-}
-
} // namespace nfd
#endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP
diff --git a/daemon/mgmt/rib-manager.cpp b/daemon/mgmt/rib-manager.cpp
index 865b30f..6cc614a 100644
--- a/daemon/mgmt/rib-manager.cpp
+++ b/daemon/mgmt/rib-manager.cpp
@@ -61,12 +61,15 @@
, m_paValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
, m_isLocalhopEnabled(false)
{
- registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
- [this] (auto&&, auto&&... args) { registerEntry(std::forward<decltype(args)>(args)...); });
- registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
- [this] (auto&&, auto&&... args) { unregisterEntry(std::forward<decltype(args)>(args)...); });
- registerStatusDatasetHandler("list",
- [this] (auto&&, auto&&, auto&&... args) { listEntries(std::forward<decltype(args)>(args)...); });
+ registerCommandHandler<ndn::nfd::RibRegisterCommand>([this] (auto&&, auto&&... args) {
+ registerEntry(std::forward<decltype(args)>(args)...);
+ });
+ registerCommandHandler<ndn::nfd::RibUnregisterCommand>([this] (auto&&, auto&&... args) {
+ unregisterEntry(std::forward<decltype(args)>(args)...);
+ });
+ registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
+ listEntries(std::forward<decltype(args)>(args)...);
+ });
}
void
diff --git a/daemon/mgmt/strategy-choice-manager.cpp b/daemon/mgmt/strategy-choice-manager.cpp
index e31ae76..1b4628e 100644
--- a/daemon/mgmt/strategy-choice-manager.cpp
+++ b/daemon/mgmt/strategy-choice-manager.cpp
@@ -42,12 +42,15 @@
: ManagerBase("strategy-choice", dispatcher, authenticator)
, m_table(strategyChoice)
{
- registerCommandHandler<ndn::nfd::StrategyChoiceSetCommand>("set",
- [this] (auto&&, auto&&, auto&&... args) { setStrategy(std::forward<decltype(args)>(args)...); });
- registerCommandHandler<ndn::nfd::StrategyChoiceUnsetCommand>("unset",
- [this] (auto&&, auto&&, auto&&... args) { unsetStrategy(std::forward<decltype(args)>(args)...); });
- registerStatusDatasetHandler("list",
- [this] (auto&&, auto&&, auto&&... args) { listChoices(std::forward<decltype(args)>(args)...); });
+ registerCommandHandler<ndn::nfd::StrategyChoiceSetCommand>([this] (auto&&, auto&&, auto&&... args) {
+ setStrategy(std::forward<decltype(args)>(args)...);
+ });
+ registerCommandHandler<ndn::nfd::StrategyChoiceUnsetCommand>([this] (auto&&, auto&&, auto&&... args) {
+ unsetStrategy(std::forward<decltype(args)>(args)...);
+ });
+ registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
+ listChoices(std::forward<decltype(args)>(args)...);
+ });
}
void
diff --git a/tests/daemon/mgmt/face-manager.t.cpp b/tests/daemon/mgmt/face-manager.t.cpp
index 458599f..1e3ba99 100644
--- a/tests/daemon/mgmt/face-manager.t.cpp
+++ b/tests/daemon/mgmt/face-manager.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2024, Regents of the University of California,
+ * Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -218,7 +218,7 @@
auto face3 = addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST); // test://
auto generateQuery = [] (const auto& filter) {
- return Interest(Name("/localhost/nfd/faces/query").append(tlv::GenericNameComponent, filter.wireEncode()))
+ return Interest(Name("/localhost/nfd/faces/query").append(filter.wireEncode()))
.setCanBePrefix(true);
};
@@ -226,7 +226,7 @@
auto idQuery = generateQuery(FaceQueryFilter().setFaceId(face1->getId()));
auto scopeQuery = generateQuery(FaceQueryFilter().setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL));
auto invalidQueryName = Name("/localhost/nfd/faces/query")
- .append(tlv::GenericNameComponent, ndn::makeStringBlock(tlv::Content, "invalid"));
+ .append(ndn::makeStringBlock(tlv::Content, "invalid"));
auto invalidQuery = Interest(invalidQueryName).setCanBePrefix(true);
receiveInterest(schemeQuery); // face1 and face2 expected
diff --git a/tests/daemon/mgmt/manager-base.t.cpp b/tests/daemon/mgmt/manager-base.t.cpp
index 2fcdd62..70eb1b6 100644
--- a/tests/daemon/mgmt/manager-base.t.cpp
+++ b/tests/daemon/mgmt/manager-base.t.cpp
@@ -31,14 +31,14 @@
class TestCommandVoidParameters : public ndn::nfd::ControlCommand<TestCommandVoidParameters>
{
- NDN_CXX_CONTROL_COMMAND(TestCommandVoidParameters, "test-module", "test-void-parameters");
+ NDN_CXX_CONTROL_COMMAND("test-module", "test-void");
};
const TestCommandVoidParameters::RequestFormat TestCommandVoidParameters::s_requestFormat;
class TestCommandRequireName : public ndn::nfd::ControlCommand<TestCommandRequireName>
{
- NDN_CXX_CONTROL_COMMAND(TestCommandRequireName, "test-module", "test-require-name");
+ NDN_CXX_CONTROL_COMMAND("test-module", "test-name-required");
};
const TestCommandRequireName::RequestFormat TestCommandRequireName::s_requestFormat =
@@ -81,8 +81,8 @@
bool wasHandlerCalled = false;
auto handler = [&] (auto&&...) { wasHandlerCalled = true; };
- m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
- m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
+ m_manager.registerCommandHandler<TestCommandVoidParameters>(handler);
+ m_manager.registerCommandHandler<TestCommandRequireName>(handler);
setTopPrefix();
auto testRegisterCommandHandler = [&] (const Name& commandName) {
@@ -93,11 +93,11 @@
testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
BOOST_CHECK(wasHandlerCalled);
- testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
+ testRegisterCommandHandler("/localhost/nfd/test-module/test-name-required");
BOOST_CHECK(!wasHandlerCalled);
- testRegisterCommandHandler(Name("/localhost/nfd/test-module/test-require-name")
- .append(ControlParameters().setName("test-name").wireEncode()));
+ testRegisterCommandHandler(Name("/localhost/nfd/test-module/test-name-required")
+ .append(ControlParameters().setName("/foo").wireEncode()));
BOOST_CHECK(wasHandlerCalled);
}
diff --git a/tests/daemon/mgmt/manager-common-fixture.cpp b/tests/daemon/mgmt/manager-common-fixture.cpp
index d69763d..3b98dd2 100644
--- a/tests/daemon/mgmt/manager-common-fixture.cpp
+++ b/tests/daemon/mgmt/manager-common-fixture.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2022, Regents of the University of California,
+ * Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -38,7 +38,7 @@
ndn::security::SignedInterestFormat format,
const Name& identity)
{
- commandName.append(tlv::GenericNameComponent, params.wireEncode());
+ commandName.append(params.wireEncode());
switch (format) {
case ndn::security::SignedInterestFormat::V02: