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-2019 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_MGMT_DISPATCHER_HPP
23 #define NDN_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 
166  void
167  addTopPrefix(const Name& prefix, bool wantRegister = true,
168  const security::SigningInfo& signingInfo = security::SigningInfo());
169 
178  void
179  removeTopPrefix(const Name& prefix);
180 
181 public: // ControlCommand
207  template<typename CP>
208  void
209  addControlCommand(const PartialName& relPrefix,
210  Authorization authorize,
211  ValidateParameters validate,
212  ControlCommandHandler handle);
213 
214 public: // StatusDataset
245  void
246  addStatusDataset(const PartialName& relPrefix,
247  Authorization authorize,
248  StatusDatasetHandler handle);
249 
250 public: // NotificationStream
271  addNotificationStream(const PartialName& relPrefix);
272 
273 private:
274  typedef std::function<void(const Name& prefix,
275  const Interest& interest)> InterestHandler;
276 
277  typedef std::function<void(const std::string& requester,
278  const Name& prefix,
279  const Interest& interest,
280  const shared_ptr<ControlParameters>&)> AuthorizationAcceptedCallback;
281 
282  typedef std::function<void(RejectReply act,
283  const Interest& interest)> AuthorizationRejectedCallback;
284 
291  typedef std::function<shared_ptr<ControlParameters>(const name::Component& comp)> ControlParametersParser;
292 
293  bool
294  isOverlappedWithOthers(const PartialName& relPrefix) const;
295 
301  void
302  afterAuthorizationRejected(RejectReply act, const Interest& interest);
303 
313  void
314  queryStorage(const Name& prefix, const Interest& interest, const InterestHandler& missContinuation);
315 
316  enum class SendDestination {
317  NONE = 0,
318  FACE = 1,
319  IMS = 2,
320  FACE_AND_IMS = 3
321  };
322 
339  void
340  sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
341  SendDestination destination, time::milliseconds imsFresh);
342 
348  void
349  sendOnFace(const Data& data);
350 
362  void
363  processControlCommandInterest(const Name& prefix,
364  const Name& relPrefix,
365  const Interest& interest,
366  const ControlParametersParser& parser,
367  const Authorization& authorization,
368  const AuthorizationAcceptedCallback& accepted,
369  const AuthorizationRejectedCallback& rejected);
370 
381  void
382  processAuthorizedControlCommandInterest(const std::string& requester,
383  const Name& prefix,
384  const Interest& interest,
385  const shared_ptr<ControlParameters>& parameters,
386  const ValidateParameters& validate,
387  const ControlCommandHandler& handler);
388 
389  void
390  sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
391 
401  void
402  processStatusDatasetInterest(const Name& prefix,
403  const Interest& interest,
404  const Authorization& authorization,
405  const AuthorizationAcceptedCallback& accepted,
406  const AuthorizationRejectedCallback& rejected);
407 
416  void
417  processAuthorizedStatusDatasetInterest(const std::string& requester,
418  const Name& prefix,
419  const Interest& interest,
420  const StatusDatasetHandler& handler);
421 
430  void
431  sendStatusDatasetSegment(const Name& dataName, const Block& content,
432  time::milliseconds imsFresh, bool isFinalBlock);
433 
434  void
435  postNotification(const Block& notification, const PartialName& relPrefix);
436 
437 private:
438  struct TopPrefixEntry
439  {
440  ScopedRegisteredPrefixHandle registeredPrefix;
441  std::vector<ScopedInterestFilterHandle> interestFilters;
442  };
443  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
444 
445  Face& m_face;
446  KeyChain& m_keyChain;
447  security::SigningInfo m_signingInfo;
448 
449  std::unordered_map<PartialName, InterestHandler> m_handlers;
450 
451  // NotificationStream name => next sequence number
452  std::unordered_map<Name, uint64_t> m_streams;
453 
455  InMemoryStorageFifo m_storage;
456 };
457 
458 template<typename CP>
459 void
461  Authorization authorize,
462  ValidateParameters validate,
463  ControlCommandHandler handle)
464 {
465  if (!m_topLevelPrefixes.empty()) {
466  NDN_THROW(std::domain_error("one or more top-level prefix has been added"));
467  }
468 
469  if (isOverlappedWithOthers(relPrefix)) {
470  NDN_THROW(std::out_of_range("relPrefix overlaps with another relPrefix"));
471  }
472 
473  auto parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
474  return make_shared<CP>(comp.blockFromValue());
475  };
476 
477  AuthorizationAcceptedCallback accepted =
478  bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
479  _1, _2, _3, _4, std::move(validate), std::move(handle));
480 
481  AuthorizationRejectedCallback rejected =
482  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
483 
484  m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
485  _1, relPrefix, _2, std::move(parser), std::move(authorize),
486  std::move(accepted), std::move(rejected));
487 }
488 
489 } // namespace mgmt
490 } // namespace ndn
491 
492 #endif // NDN_MGMT_DISPATCHER_HPP
control-parameters.hpp
ndn::mgmt::AcceptContinuation
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Definition: dispatcher.hpp:45
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::mgmt::ValidateParameters
std::function< bool(const ControlParameters &params)> ValidateParameters
a function to validate input ControlParameters
Definition: dispatcher.hpp:90
key-chain.hpp
ndn::mgmt::RejectReply
RejectReply
indicate how to reply in case authorization is rejected
Definition: dispatcher.hpp:49
ndn::mgmt::CommandContinuation
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:95
ndn::security::SigningInfo
Signing parameters passed to KeyChain.
Definition: signing-info.hpp:42
block.hpp
ndn::mgmt::StatusDatasetHandler
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request
Definition: dispatcher.hpp:118
ndn::mgmt::PostNotification
std::function< void(const Block &notification)> PostNotification
a function to post a notification
Definition: dispatcher.hpp:124
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
ndn::mgmt::ControlParameters
base class for a struct that contains ControlCommand parameters
Definition: control-parameters.hpp:33
ndn::MetaInfo
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:59
ndn::mgmt::Dispatcher::addStatusDataset
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
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
ndn::mgmt::Dispatcher::~Dispatcher
virtual ~Dispatcher()
ndn::mgmt::StatusDatasetContext
provides a context for generating response to a StatusDataset request
Definition: status-dataset-context.hpp:37
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::mgmt::makeAcceptAllAuthorization
Authorization makeAcceptAllAuthorization()
return an Authorization that accepts all Interests, with empty string as requester
Definition: dispatcher.cpp:34
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
ndn::mgmt::Authorization
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
ndn::mgmt::Dispatcher::addTopPrefix
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
add a top-level prefix
Definition: dispatcher.cpp:58
ndn::mgmt::Dispatcher::addControlCommand
void addControlCommand(const PartialName &relPrefix, Authorization authorize, ValidateParameters validate, ControlCommandHandler handle)
register a ControlCommand
Definition: dispatcher.hpp:460
ndn::mgmt::RejectReply::SILENT
@ SILENT
do not reply
ndn::mgmt::ControlResponse
ControlCommand response.
Definition: control-response.hpp:33
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::InMemoryStorageFifo
Provides in-memory storage employing First-In-First-Out (FIFO) replacement policy.
Definition: in-memory-storage-fifo.hpp:36
ndn::mgmt::ControlCommandHandler
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
ndn::mgmt::RejectReply::STATUS403
@ STATUS403
reply with a ControlResponse where StatusCode is 403
face.hpp
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::mgmt::Dispatcher::Dispatcher
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
Definition: dispatcher.cpp:45
ndn::mgmt::Dispatcher
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:131
ndn::mgmt::Dispatcher::addNotificationStream
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
Definition: dispatcher.cpp:300
status-dataset-context.hpp
ndn::mgmt::RejectContinuation
std::function< void(RejectReply reply)> RejectContinuation
a function to be called if authorization is rejected
Definition: dispatcher.hpp:60
ndn::mgmt::Dispatcher::removeTopPrefix
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
Definition: dispatcher.cpp:88
control-response.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::ScopedCancelHandle< RegisteredPrefixHandle >
in-memory-storage-fifo.hpp