|
1 | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
2 | 2 | /*
|
3 |
| - * Copyright (c) 2013-2023 Regents of the University of California. |
| 3 | + * Copyright (c) 2013-2025 Regents of the University of California. |
4 | 4 | *
|
5 | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
|
6 | 6 | *
|
@@ -51,18 +51,15 @@ Dispatcher::Dispatcher(Face& face, KeyChain& keyChain,
|
51 | 51 | {
|
52 | 52 | }
|
53 | 53 |
|
54 |
| -Dispatcher::~Dispatcher() = default; |
55 |
| - |
56 | 54 | void
|
57 | 55 | Dispatcher::addTopPrefix(const Name& prefix, bool wantRegister,
|
58 | 56 | const security::SigningInfo& signingInfo)
|
59 | 57 | {
|
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 | + }); |
64 | 61 | 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")); |
66 | 63 | }
|
67 | 64 |
|
68 | 65 | TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];
|
@@ -92,21 +89,19 @@ Dispatcher::removeTopPrefix(const Name& prefix)
|
92 | 89 | m_topLevelPrefixes.erase(prefix);
|
93 | 90 | }
|
94 | 91 |
|
95 |
| -bool |
96 |
| -Dispatcher::isOverlappedWithOthers(const PartialName& relPrefix) const |
| 92 | +void |
| 93 | +Dispatcher::checkPrefix(const PartialName& relPrefix) const |
97 | 94 | {
|
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 | + } |
110 | 105 | }
|
111 | 106 |
|
112 | 107 | void
|
@@ -166,38 +161,42 @@ Dispatcher::sendOnFace(const Data& data)
|
166 | 161 | }
|
167 | 162 |
|
168 | 163 | 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) |
176 | 171 | {
|
177 | 172 | // /<prefix>/<relPrefix>/<parameters>
|
178 | 173 | size_t parametersLoc = prefix.size() + relPrefix.size();
|
179 | 174 | const name::Component& pc = interest.getName().get(parametersLoc);
|
180 | 175 |
|
181 | 176 | shared_ptr<ControlParameters> parameters;
|
182 | 177 | try {
|
183 |
| - parameters = parser(pc); |
| 178 | + parameters = parse(pc); |
184 | 179 | }
|
185 | 180 | catch (const tlv::Error&) {
|
186 | 181 | return;
|
187 | 182 | }
|
188 | 183 |
|
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); |
192 | 191 | }
|
193 | 192 |
|
194 | 193 | 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) |
201 | 200 | {
|
202 | 201 | if (validateParams(*parameters)) {
|
203 | 202 | handler(prefix, interest, *parameters,
|
@@ -225,13 +224,7 @@ Dispatcher::addStatusDataset(const PartialName& relPrefix,
|
225 | 224 | Authorization auth,
|
226 | 225 | StatusDatasetHandler handler)
|
227 | 226 | {
|
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); |
235 | 228 |
|
236 | 229 | AuthorizationAcceptedCallback accept =
|
237 | 230 | [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,
|
307 | 300 | PostNotification
|
308 | 301 | Dispatcher::addNotificationStream(const PartialName& relPrefix)
|
309 | 302 | {
|
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); |
317 | 304 |
|
318 | 305 | // register a handler for the subscriber of this notification stream
|
319 | 306 | // keep silent if Interest does not match a stored notification
|
|
0 commit comments