Skip to content

Commit

Permalink
mgmt: migrate to new Dispatcher API for control commands
Browse files Browse the repository at this point in the history
Change-Id: Iaba68dde879fbb562e13ef383e0b753174708baa
  • Loading branch information
Pesa committed Jan 6, 2025
1 parent 45c1f6a commit 1db1bb6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 74 deletions.
15 changes: 9 additions & 6 deletions daemon/mgmt/cs-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCounters,
, 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
Expand Down
15 changes: 9 additions & 6 deletions daemon/mgmt/face-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ FaceManager::FaceManager(FaceSystem& faceSystem,
, 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",
Expand Down
15 changes: 9 additions & 6 deletions daemon/mgmt/fib-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ FibManager::FibManager(Fib& fib, const FaceTable& faceTable,
, 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
Expand Down
44 changes: 12 additions & 32 deletions daemon/mgmt/manager-base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,23 @@ class ManagerBase : noncopyable
~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,
Expand Down Expand Up @@ -128,34 +136,6 @@ class ManagerBase : noncopyable
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*>(&params) != 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*>(&params) != 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
15 changes: 9 additions & 6 deletions daemon/mgmt/rib-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
, 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
Expand Down
15 changes: 9 additions & 6 deletions daemon/mgmt/strategy-choice-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
: 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
Expand Down
6 changes: 3 additions & 3 deletions tests/daemon/mgmt/face-manager.t.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -218,15 +218,15 @@ BOOST_AUTO_TEST_CASE(FaceQuery)
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);
};

auto schemeQuery = generateQuery(FaceQueryFilter().setUriScheme("dummy"));
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
Expand Down
14 changes: 7 additions & 7 deletions tests/daemon/mgmt/manager-base.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ namespace nfd::tests {

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 =
Expand Down Expand Up @@ -81,8 +81,8 @@ BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
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) {
Expand All @@ -93,11 +93,11 @@ BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
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);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/daemon/mgmt/manager-common-fixture.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -38,7 +38,7 @@ InterestSignerFixture::makeControlCommandRequest(Name commandName,
ndn::security::SignedInterestFormat format,
const Name& identity)
{
commandName.append(tlv::GenericNameComponent, params.wireEncode());
commandName.append(params.wireEncode());

switch (format) {
case ndn::security::SignedInterestFormat::V02:
Expand Down

0 comments on commit 1db1bb6

Please sign in to comment.