NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2021 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,
77  const RejectContinuation& reject)> Authorization;
78 
81 Authorization
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,
106  const CommandContinuation& done)> ControlCommandHandler;
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
144  ~Dispatcher();
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
269  PostNotification
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
Copyright (c) 2011-2015 Regents of the University of California.
Accept any value the remote endpoint offers.
Definition: enabled.hpp:207
Provides in-memory storage employing First-In-First-Out (FIFO) replacement policy.
std::function< void(const Block &notification)> PostNotification
a function to post a notification
Definition: dispatcher.hpp:124
ndn security KeyChain
Definition: key-chain.cpp:70
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:130
RejectReply
indicate how to reply in case authorization is rejected
Definition: dispatcher.hpp:49
Connection rejected.
Definition: error.hpp:130
reply with a ControlResponse where StatusCode is 403
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
Represents an Interest packet.
Definition: interest.hpp:48
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Definition: dispatcher.hpp:45
Authorization makeAcceptAllAuthorization()
return an Authorization that accepts all Interests, with empty string as requester ...
Definition: dispatcher.cpp:32
Signing parameters passed to KeyChain.
ndn mgmt Dispatcher
Definition: dispatcher.cpp:26
#define NDN_THROW(e)
Definition: exception.hpp:61
std::function< void(RejectReply reply)> RejectContinuation
a function to be called if authorization is rejected
Definition: dispatcher.hpp:60
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
void addControlCommand(const PartialName &relPrefix, Authorization authorize, ValidateParameters validate, ControlCommandHandler handle)
register a ControlCommand
Definition: dispatcher.hpp:451
Represents an absolute name.
Definition: name.hpp:41
bool validate(server *, websocketpp::connection_hdl)
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:95
base class for a struct that contains ControlCommand parameters
Represents a name component.
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
ControlCommand response.
Provides a context for generating the response to a StatusDataset request.
Represents a Data packet.
Definition: data.hpp:37
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 Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request
Definition: dispatcher.hpp:118