Skip to content

Commit e07923b

Browse files
committed
mgmt: remove one level of indirection in control command processing
Also refactor the check for overlapping prefixes in Dispatcher, make error messages more informative, and cleanup doxygen documentation. Change-Id: I5781524fdcde62b47d74c80b98db7d011250a226
1 parent 922329d commit e07923b

File tree

2 files changed

+159
-191
lines changed

2 files changed

+159
-191
lines changed

ndn-cxx/mgmt/dispatcher.cpp

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -51,18 +51,15 @@ Dispatcher::Dispatcher(Face& face, KeyChain& keyChain,
5151
{
5252
}
5353

54-
Dispatcher::~Dispatcher() = default;
55-
5654
void
5755
Dispatcher::addTopPrefix(const Name& prefix, bool wantRegister,
5856
const security::SigningInfo& signingInfo)
5957
{
60-
bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(),
61-
[&prefix] (const auto& x) {
62-
return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first);
63-
});
58+
bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), [&] (const auto& x) {
59+
return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first);
60+
});
6461
if (hasOverlap) {
65-
NDN_THROW(std::out_of_range("top-level prefix overlaps"));
62+
NDN_THROW(std::out_of_range("top-level prefix '" + prefix.toUri() + "' overlaps with another"));
6663
}
6764

6865
TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];
@@ -92,21 +89,19 @@ Dispatcher::removeTopPrefix(const Name& prefix)
9289
m_topLevelPrefixes.erase(prefix);
9390
}
9491

95-
bool
96-
Dispatcher::isOverlappedWithOthers(const PartialName& relPrefix) const
92+
void
93+
Dispatcher::checkPrefix(const PartialName& relPrefix) const
9794
{
98-
bool hasOverlapWithHandlers =
99-
std::any_of(m_handlers.begin(), m_handlers.end(),
100-
[&] (const auto& entry) {
101-
return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
102-
});
103-
bool hasOverlapWithStreams =
104-
std::any_of(m_streams.begin(), m_streams.end(),
105-
[&] (const auto& entry) {
106-
return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
107-
});
108-
109-
return hasOverlapWithHandlers || hasOverlapWithStreams;
95+
if (!m_topLevelPrefixes.empty()) {
96+
NDN_THROW(std::domain_error("one or more top-level prefix has been added"));
97+
}
98+
99+
bool hasOverlap = std::any_of(m_handlers.begin(), m_handlers.end(), [&] (const auto& entry) {
100+
return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
101+
});
102+
if (hasOverlap) {
103+
NDN_THROW(std::out_of_range("'" + relPrefix.toUri() + "' overlaps with another handler"));
104+
}
110105
}
111106

112107
void
@@ -166,38 +161,42 @@ Dispatcher::sendOnFace(const Data& data)
166161
}
167162

168163
void
169-
Dispatcher::processControlCommandInterest(const Name& prefix,
170-
const Name& relPrefix,
171-
const Interest& interest,
172-
const ControlParametersParser& parser,
173-
const Authorization& authorization,
174-
const AuthorizationAcceptedCallback& accepted,
175-
const AuthorizationRejectedCallback& rejected)
164+
Dispatcher::processCommand(const Name& prefix,
165+
const Name& relPrefix,
166+
const Interest& interest,
167+
const ControlParametersParser& parse,
168+
const Authorization& authorize,
169+
const ValidateParameters& validateParams,
170+
const ControlCommandHandler& handler)
176171
{
177172
// /<prefix>/<relPrefix>/<parameters>
178173
size_t parametersLoc = prefix.size() + relPrefix.size();
179174
const name::Component& pc = interest.getName().get(parametersLoc);
180175

181176
shared_ptr<ControlParameters> parameters;
182177
try {
183-
parameters = parser(pc);
178+
parameters = parse(pc);
184179
}
185180
catch (const tlv::Error&) {
186181
return;
187182
}
188183

189-
AcceptContinuation accept = [=] (const auto& req) { accepted(req, prefix, interest, parameters); };
190-
RejectContinuation reject = [=] (RejectReply reply) { rejected(reply, interest); };
191-
authorization(prefix, interest, parameters.get(), accept, reject);
184+
AcceptContinuation accept = [=] (const auto& req) {
185+
processAuthorizedCommand(req, prefix, interest, parameters, validateParams, handler);
186+
};
187+
RejectContinuation reject = [=] (RejectReply reply) {
188+
afterAuthorizationRejected(reply, interest);
189+
};
190+
authorize(prefix, interest, parameters.get(), accept, reject);
192191
}
193192

194193
void
195-
Dispatcher::processAuthorizedControlCommandInterest(const std::string& requester,
196-
const Name& prefix,
197-
const Interest& interest,
198-
const shared_ptr<ControlParameters>& parameters,
199-
const ValidateParameters& validateParams,
200-
const ControlCommandHandler& handler)
194+
Dispatcher::processAuthorizedCommand(const std::string& requester,
195+
const Name& prefix,
196+
const Interest& interest,
197+
const shared_ptr<ControlParameters>& parameters,
198+
const ValidateParameters& validateParams,
199+
const ControlCommandHandler& handler)
201200
{
202201
if (validateParams(*parameters)) {
203202
handler(prefix, interest, *parameters,
@@ -225,13 +224,7 @@ Dispatcher::addStatusDataset(const PartialName& relPrefix,
225224
Authorization auth,
226225
StatusDatasetHandler handler)
227226
{
228-
if (!m_topLevelPrefixes.empty()) {
229-
NDN_THROW(std::domain_error("one or more top-level prefix has been added"));
230-
}
231-
232-
if (isOverlappedWithOthers(relPrefix)) {
233-
NDN_THROW(std::out_of_range("status dataset name overlaps"));
234-
}
227+
checkPrefix(relPrefix);
235228

236229
AuthorizationAcceptedCallback accept =
237230
[this, handler = std::move(handler)] (auto&&, const auto& prefix, const auto& interest, auto&&) {
@@ -307,13 +300,7 @@ Dispatcher::sendStatusDatasetSegment(const Name& dataName, const Block& content,
307300
PostNotification
308301
Dispatcher::addNotificationStream(const PartialName& relPrefix)
309302
{
310-
if (!m_topLevelPrefixes.empty()) {
311-
NDN_THROW(std::domain_error("one or more top-level prefix has been added"));
312-
}
313-
314-
if (isOverlappedWithOthers(relPrefix)) {
315-
NDN_THROW(std::out_of_range("notification stream name overlaps"));
316-
}
303+
checkPrefix(relPrefix);
317304

318305
// register a handler for the subscriber of this notification stream
319306
// keep silent if Interest does not match a stored notification

0 commit comments

Comments
 (0)