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-2022 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"
32 
33 #include <unordered_map>
34 
35 namespace ndn {
36 namespace mgmt {
37 
38 // ---- AUTHORIZATION ----
39 
45 typedef std::function<void(const std::string& requester)> AcceptContinuation;
46 
49 enum class RejectReply {
52  SILENT,
55  STATUS403
56 };
57 
60 typedef std::function<void(RejectReply reply)> RejectContinuation;
61 
74 typedef std::function<void(const Name& prefix, const Interest& interest,
75  const ControlParameters* params,
76  const AcceptContinuation& accept,
78 
83 
84 // ---- CONTROL COMMAND ----
85 
90 typedef std::function<bool(const ControlParameters& params)> ValidateParameters;
91 
95 typedef std::function<void(const ControlResponse& resp)> CommandContinuation;
96 
104 typedef std::function<void(const Name& prefix, const Interest& interest,
105  const ControlParameters& params,
107 
108 // ---- STATUS DATASET ----
109 
117 typedef std::function<void(const Name& prefix, const Interest& interest,
119 
120 //---- NOTIFICATION STREAM ----
121 
124 typedef std::function<void(const Block& notification)> PostNotification;
125 
126 // ---- DISPATCHER ----
127 
130 class Dispatcher : noncopyable
131 {
132 public:
139  Dispatcher(Face& face, KeyChain& keyChain,
140  const security::SigningInfo& signingInfo = security::SigningInfo(),
141  size_t imsCapacity = 256);
142 
143  virtual
145 
165  void
166  addTopPrefix(const Name& prefix, bool wantRegister = true,
167  const security::SigningInfo& signingInfo = security::SigningInfo());
168 
177  void
178  removeTopPrefix(const Name& prefix);
179 
180 public: // ControlCommand
206  template<typename CP>
207  void
208  addControlCommand(const PartialName& relPrefix,
209  Authorization authorize,
210  ValidateParameters validate,
211  ControlCommandHandler handle);
212 
213 public: // StatusDataset
244  void
245  addStatusDataset(const PartialName& relPrefix,
246  Authorization authorize,
247  StatusDatasetHandler handle);
248 
249 public: // NotificationStream
270  addNotificationStream(const PartialName& relPrefix);
271 
272 private:
273  using InterestHandler = std::function<void(const Name& prefix, const Interest&)>;
274 
275  using AuthorizationAcceptedCallback = std::function<void(const std::string& requester,
276  const Name& prefix,
277  const Interest&,
278  const shared_ptr<ControlParameters>&)>;
279 
280  using AuthorizationRejectedCallback = std::function<void(RejectReply, const Interest&)>;
281 
287  using ControlParametersParser = std::function<shared_ptr<ControlParameters>(const name::Component&)>;
288 
289  bool
290  isOverlappedWithOthers(const PartialName& relPrefix) const;
291 
297  void
298  afterAuthorizationRejected(RejectReply act, const Interest& interest);
299 
309  void
310  queryStorage(const Name& prefix, const Interest& interest, const InterestHandler& missContinuation);
311 
312  enum class SendDestination {
313  NONE = 0,
314  FACE = 1,
315  IMS = 2,
316  FACE_AND_IMS = 3
317  };
318 
334  void
335  sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
336  SendDestination destination);
337 
343  void
344  sendOnFace(const Data& data);
345 
357  void
358  processControlCommandInterest(const Name& prefix,
359  const Name& relPrefix,
360  const Interest& interest,
361  const ControlParametersParser& parser,
362  const Authorization& authorization,
363  const AuthorizationAcceptedCallback& accepted,
364  const AuthorizationRejectedCallback& rejected);
365 
376  void
377  processAuthorizedControlCommandInterest(const std::string& requester,
378  const Name& prefix,
379  const Interest& interest,
380  const shared_ptr<ControlParameters>& parameters,
381  const ValidateParameters& validate,
382  const ControlCommandHandler& handler);
383 
384  void
385  sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
386 
396  void
397  processStatusDatasetInterest(const Name& prefix,
398  const Interest& interest,
399  const Authorization& authorization,
400  const AuthorizationAcceptedCallback& accepted,
401  const AuthorizationRejectedCallback& rejected);
402 
410  void
411  processAuthorizedStatusDatasetInterest(const Name& prefix,
412  const Interest& interest,
413  const StatusDatasetHandler& handler);
414 
422  void
423  sendStatusDatasetSegment(const Name& dataName, const Block& content, bool isFinalBlock);
424 
425  void
426  postNotification(const Block& notification, const PartialName& relPrefix);
427 
428 private:
429  struct TopPrefixEntry
430  {
431  ScopedRegisteredPrefixHandle registeredPrefix;
432  std::vector<ScopedInterestFilterHandle> interestFilters;
433  };
434  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
435 
436  Face& m_face;
437  KeyChain& m_keyChain;
438  security::SigningInfo m_signingInfo;
439 
440  std::unordered_map<PartialName, InterestHandler> m_handlers;
441 
442  // NotificationStream name => next sequence number
443  std::unordered_map<Name, uint64_t> m_streams;
444 
446  InMemoryStorageFifo m_storage;
447 };
448 
449 template<typename CP>
450 void
452  Authorization authorize,
453  ValidateParameters validate,
454  ControlCommandHandler handle)
455 {
456  if (!m_topLevelPrefixes.empty()) {
457  NDN_THROW(std::domain_error("one or more top-level prefix has been added"));
458  }
459 
460  if (isOverlappedWithOthers(relPrefix)) {
461  NDN_THROW(std::out_of_range("relPrefix overlaps with another relPrefix"));
462  }
463 
464  ControlParametersParser parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
465  return make_shared<CP>(comp.blockFromValue());
466  };
467  AuthorizationAcceptedCallback accepted = [this, validate = std::move(validate),
468  handle = std::move(handle)] (auto&&... args) {
469  processAuthorizedControlCommandInterest(std::forward<decltype(args)>(args)..., validate, handle);
470  };
471  AuthorizationRejectedCallback rejected = [this] (auto&&... args) {
472  afterAuthorizationRejected(std::forward<decltype(args)>(args)...);
473  };
474 
475  m_handlers[relPrefix] = [this, relPrefix,
476  parser = std::move(parser),
477  authorize = std::move(authorize),
478  accepted = std::move(accepted),
479  rejected = std::move(rejected)] (const auto& prefix, const auto& interest) {
480  processControlCommandInterest(prefix, relPrefix, interest, parser, authorize, accepted, rejected);
481  };
482 }
483 
484 } // namespace mgmt
485 } // namespace ndn
486 
487 #endif // NDN_CXX_MGMT_DISPATCHER_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Represents a Data packet.
Definition: data.hpp:39
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
Provides in-memory storage employing First-In-First-Out (FIFO) replacement policy.
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:44
Base class for a struct that contains ControlCommand parameters.
ControlCommand response.
Implements a request dispatcher on server side of NFD Management protocol.
Definition: dispatcher.hpp:131
PostNotification addNotificationStream(const PartialName &relPrefix)
Register a NotificationStream.
Definition: dispatcher.cpp:303
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
Constructor.
Definition: dispatcher.cpp:43
void addStatusDataset(const PartialName &relPrefix, Authorization authorize, StatusDatasetHandler handle)
Register a StatusDataset or a prefix under which StatusDatasets can be requested.
Definition: dispatcher.cpp:223
void addControlCommand(const PartialName &relPrefix, Authorization authorize, ValidateParameters validate, ControlCommandHandler handle)
Register a ControlCommand.
Definition: dispatcher.hpp:451
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
Add a top-level prefix.
Definition: dispatcher.cpp:56
void removeTopPrefix(const Name &prefix)
Remove a top-level prefix.
Definition: dispatcher.cpp:89
Provides a context for generating the response to a StatusDataset request.
Represents a name component.
Signing parameters passed to KeyChain.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
#define NDN_THROW(e)
Definition: exception.hpp:61
std::function< void(const ControlResponse &resp)> CommandContinuation
A function to be called after ControlCommandHandler completes.
Definition: dispatcher.hpp:95
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
A function that performs authorization.
Definition: dispatcher.hpp:77
std::function< void(const Block &notification)> PostNotification
A function to post a notification.
Definition: dispatcher.hpp:124
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters &params, const CommandContinuation &done)> ControlCommandHandler
A function to handle an authorized ControlCommand.
Definition: dispatcher.hpp:106
std::function< bool(const ControlParameters &params)> ValidateParameters
A function to validate input ControlParameters.
Definition: dispatcher.hpp:90
RejectReply
Indicate how to reply in case authorization is rejected.
Definition: dispatcher.hpp:49
@ SILENT
Do not reply.
@ STATUS403
Reply with a ControlResponse where StatusCode is 403.
std::function< void(const std::string &requester)> AcceptContinuation
A function to be called if authorization is successful.
Definition: dispatcher.hpp:45
std::function< void(RejectReply reply)> RejectContinuation
A function to be called if authorization is rejected.
Definition: dispatcher.hpp:60
Authorization makeAcceptAllAuthorization()
Return an Authorization that accepts all Interests, with empty string as requester.
Definition: dispatcher.cpp:32
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
A function to handle a StatusDataset request.
Definition: dispatcher.hpp:118
Definition: data.cpp:25