All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dispatcher.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2025 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_CXX_MGMT_DISPATCHER_HPP
23#define NDN_CXX_MGMT_DISPATCHER_HPP
24
25#include "ndn-cxx/face.hpp"
31
32#include <unordered_map>
33
34namespace ndn::mgmt {
35
36// ---- AUTHORIZATION ----
37
44using AcceptContinuation = std::function<void(const std::string& requester)>;
45
49enum class RejectReply {
53 SILENT,
58};
59
63using RejectContinuation = std::function<void(RejectReply)>;
64
77using Authorization = std::function<void(const Name& prefix, const Interest& interest,
78 const ControlParametersBase* params,
79 const AcceptContinuation& accept,
80 const RejectContinuation& reject)>;
81
87
88// ---- CONTROL COMMAND ----
89
95using ValidateParameters = std::function<bool(ControlParametersBase& params)>;
96
101using CommandContinuation = std::function<void(const ControlResponse& resp)>;
102
111using ControlCommandHandler = std::function<void(const Name& prefix, const Interest& interest,
112 const ControlParametersBase& params,
113 const CommandContinuation& done)>;
114
115// ---- STATUS DATASET ----
116
125using StatusDatasetHandler = std::function<void(const Name& prefix, const Interest& interest,
126 StatusDatasetContext& context)>;
127
128//---- NOTIFICATION STREAM ----
129
133using PostNotification = std::function<void(const Block& notification)>;
134
135// ---- DISPATCHER ----
136
140class Dispatcher : noncopyable
141{
142public:
150 Dispatcher(Face& face, KeyChain& keyChain,
151 const security::SigningInfo& signingInfo = security::SigningInfo(),
152 size_t imsCapacity = 256);
153
173 void
174 addTopPrefix(const Name& prefix, bool wantRegister = true,
175 const security::SigningInfo& signingInfo = security::SigningInfo());
176
186 void
187 removeTopPrefix(const Name& prefix);
188
189public: // ControlCommand
217 template<typename ParametersType,
218 std::enable_if_t<std::is_convertible_v<ParametersType*, ControlParametersBase*>, int> = 0>
219 void
221 Authorization authorize,
222 ValidateParameters validate,
224 {
225 checkPrefix(relPrefix);
226
227 auto relPrefixLen = relPrefix.size();
228 ParametersParser parse = [relPrefixLen] (const Name& prefix,
229 const auto& interest) -> ControlParametersPtr {
230 const name::Component& comp = interest.getName().get(prefix.size() + relPrefixLen);
231 return make_shared<ParametersType>(comp.blockFromValue());
232 };
233
234 m_handlers[relPrefix] = [this,
235 parser = std::move(parse),
236 authorizer = std::move(authorize),
237 validator = std::move(validate),
238 handler = std::move(handle)] (const auto& prefix, const auto& interest) {
239 processCommand(prefix, interest, parser, authorizer, std::move(validator), std::move(handler));
240 };
241 }
242
266 template<typename Command>
267 void
269 {
270 auto relPrefix = Command::getName();
271 checkPrefix(relPrefix);
272
273 ParametersParser parse = [] (const Name& prefix, const auto& interest) {
274 return Command::parseRequest(interest, prefix.size());
275 };
276 ValidateParameters validate = [] (auto& params) {
277 auto& reqParams = static_cast<typename Command::RequestParameters&>(params);
278 Command::validateRequest(reqParams);
279 Command::applyDefaultsToRequest(reqParams);
280 // for compatibility with ValidateParameters signature; consider refactoring in the future
281 return true;
282 };
283
284 m_handlers[relPrefix] = [this,
285 parser = std::move(parse),
286 authorizer = std::move(authorize),
287 validator = std::move(validate),
288 handler = std::move(handle)] (const auto& prefix, const auto& interest) {
289 processCommand(prefix, interest, parser, authorizer, std::move(validator), std::move(handler));
290 };
291 }
292
293public: // StatusDataset
326 void
327 addStatusDataset(const PartialName& relPrefix,
328 Authorization authorize,
329 StatusDatasetHandler handle);
330
331public: // NotificationStream
353 addNotificationStream(const PartialName& relPrefix);
354
355private:
356 using ControlParametersPtr = shared_ptr<ControlParametersBase>;
357 using InterestHandler = std::function<void(const Name& prefix, const Interest&)>;
358
364 using ParametersParser = std::function<ControlParametersPtr(const Name& prefix, const Interest&)>;
365
366 void
367 checkPrefix(const PartialName& relPrefix) const;
368
374 void
375 afterAuthorizationRejected(RejectReply act, const Interest& interest);
376
386 void
387 queryStorage(const Name& prefix, const Interest& interest, const InterestHandler& missContinuation);
388
389 enum class SendDestination {
390 NONE = 0,
391 FACE = 1,
392 IMS = 2,
393 FACE_AND_IMS = 3,
394 };
395
411 void
412 sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
413 SendDestination destination);
414
418 void
419 sendOnFace(const Data& data);
420
431 void
432 processCommand(const Name& prefix,
433 const Interest& interest,
434 const ParametersParser& parse,
435 const Authorization& authorize,
436 ValidateParameters validate,
437 ControlCommandHandler handler);
438
448 void
449 processAuthorizedCommand(const Name& prefix,
450 const Interest& interest,
451 const ControlParametersPtr& parameters,
452 const ValidateParameters& validate,
453 const ControlCommandHandler& handler);
454
455 void
456 sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
457
466 void
467 processStatusDatasetInterest(const Name& prefix,
468 const Interest& interest,
469 const Authorization& authorize,
470 StatusDatasetHandler handler);
471
479 void
480 processAuthorizedStatusDatasetInterest(const Name& prefix,
481 const Interest& interest,
482 const StatusDatasetHandler& handler);
483
491 void
492 sendStatusDatasetSegment(const Name& dataName, const Block& content, bool isFinalBlock);
493
494 void
495 postNotification(const Block& notification, const PartialName& relPrefix);
496
497private:
498 Face& m_face;
499 KeyChain& m_keyChain;
500 security::SigningInfo m_signingInfo;
501
502 struct TopPrefixEntry
503 {
504 ScopedRegisteredPrefixHandle registeredPrefix;
505 std::vector<ScopedInterestFilterHandle> interestFilters;
506 };
507 std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
508
509 std::unordered_map<PartialName, InterestHandler> m_handlers;
510
511 // NotificationStream name => next sequence number
512 std::unordered_map<Name, uint64_t> m_streams;
513
515 InMemoryStorageFifo m_storage;
516};
517
518} // namespace ndn::mgmt
519
520#endif // NDN_CXX_MGMT_DISPATCHER_HPP
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
Block blockFromValue() const
Return a new Block constructed from the TLV-VALUE of this Block.
Definition block.cpp:314
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition block.cpp:414
Represents a Data packet.
Definition data.hpp:39
Provide a communication channel with local or remote NDN forwarder.
Definition face.hpp:91
Represents an Interest packet.
Definition interest.hpp:50
A MetaInfo holds the meta info which is signed inside the Data packet.
Definition meta-info.hpp:62
Represents an absolute name.
Definition name.hpp:45
size_t size() const noexcept
Returns the number of components.
Definition name.hpp:180
Base class for a struct that contains the parameters for a ControlCommand.
ControlCommand response.
Implements a request dispatcher on server side of NFD Management protocol.
PostNotification addNotificationStream(const PartialName &relPrefix)
Register a NotificationStream.
void addStatusDataset(const PartialName &relPrefix, Authorization authorize, StatusDatasetHandler handle)
Register a StatusDataset or a prefix under which StatusDatasets can be requested.
void addControlCommand(Authorization authorize, ControlCommandHandler handle)
Register a ControlCommand (new style).
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
Add a top-level prefix.
void removeTopPrefix(const Name &prefix)
Remove a top-level prefix.
void addControlCommand(const PartialName &relPrefix, Authorization authorize, ValidateParameters validate, ControlCommandHandler handle)
Register a ControlCommand (old style).
Provides a context for generating the response to a StatusDataset request.
Represents a name component.
The main interface for signing key management.
Definition key-chain.hpp:87
Signing parameters passed to KeyChain.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition common.hpp:49
std::function< void(const std::string &requester)> AcceptContinuation
A function to be called if authorization is successful.
std::function< void(const Name &prefix, const Interest &interest, const ControlParametersBase &params, const CommandContinuation &done)> ControlCommandHandler
A function to handle an authorized ControlCommand.
std::function< void(const Block &notification)> PostNotification
A function to post a notification.
std::function< void(RejectReply)> RejectContinuation
A function to be called if authorization is rejected.
RejectReply
Indicates how to reply in case authorization is rejected.
@ SILENT
Do not reply.
@ STATUS403
Reply with a ControlResponse where StatusCode is 403.
std::function< bool(ControlParametersBase &params)> ValidateParameters
A function to validate and normalize the incoming request parameters.
std::function< void(const Name &prefix, const Interest &interest, const ControlParametersBase *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
A function that performs authorization.
Authorization makeAcceptAllAuthorization()
Return an Authorization that accepts all Interests, with empty string as requester.
std::function< void(const ControlResponse &resp)> CommandContinuation
A function to be called after a ControlCommandHandler completes.
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
A function to handle a StatusDataset request.