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
144  ~Dispatcher();
145 
166  void
167  addTopPrefix(const Name& prefix, bool wantRegister = true,
168  const security::SigningInfo& signingInfo = security::SigningInfo());
169 
180  void
181  removeTopPrefix(const Name& prefix);
182 
183 public: // ControlCommand
209  template<typename CP>
210  void
211  addControlCommand(const PartialName& relPrefix,
212  Authorization authorize,
213  ValidateParameters validate,
214  ControlCommandHandler handle);
215 
216 public: // StatusDataset
247  void
248  addStatusDataset(const PartialName& relPrefix,
249  Authorization authorize,
250  StatusDatasetHandler handle);
251 
252 public: // NotificationStream
273  addNotificationStream(const PartialName& relPrefix);
274 
275 private:
276  typedef std::function<void(const Name& prefix,
277  const Interest& interest)> InterestHandler;
278 
279  typedef std::function<void(const std::string& requester,
280  const Name& prefix,
281  const Interest& interest,
282  const shared_ptr<ControlParameters>&)> AuthorizationAcceptedCallback;
283 
284  typedef std::function<void(RejectReply act,
285  const Interest& interest)> AuthorizationRejectedCallback;
286 
293  typedef std::function<shared_ptr<ControlParameters>(const name::Component& comp)> ControlParametersParser;
294 
295  bool
296  isOverlappedWithOthers(const PartialName& relPrefix) const;
297 
303  void
304  afterAuthorizationRejected(RejectReply act, const Interest& interest);
305 
315  void
316  queryStorage(const Name& prefix, const Interest& interest, const InterestHandler& missContinuation);
317 
318  enum class SendDestination {
319  NONE = 0,
320  FACE = 1,
321  IMS = 2,
322  FACE_AND_IMS = 3
323  };
324 
341  void
342  sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
343  SendDestination destination, time::milliseconds imsFresh);
344 
350  void
351  sendOnFace(const Data& data);
352 
364  void
365  processControlCommandInterest(const Name& prefix,
366  const Name& relPrefix,
367  const Interest& interest,
368  const ControlParametersParser& parser,
369  const Authorization& authorization,
370  const AuthorizationAcceptedCallback& accepted,
371  const AuthorizationRejectedCallback& rejected);
372 
383  void
384  processAuthorizedControlCommandInterest(const std::string& requester,
385  const Name& prefix,
386  const Interest& interest,
387  const shared_ptr<ControlParameters>& parameters,
388  const ValidateParameters& validate,
389  const ControlCommandHandler& handler);
390 
391  void
392  sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
393 
403  void
404  processStatusDatasetInterest(const Name& prefix,
405  const Interest& interest,
406  const Authorization& authorization,
407  const AuthorizationAcceptedCallback& accepted,
408  const AuthorizationRejectedCallback& rejected);
409 
418  void
419  processAuthorizedStatusDatasetInterest(const std::string& requester,
420  const Name& prefix,
421  const Interest& interest,
422  const StatusDatasetHandler& handler);
423 
432  void
433  sendStatusDatasetSegment(const Name& dataName, const Block& content,
434  time::milliseconds imsFresh, bool isFinalBlock);
435 
436  void
437  postNotification(const Block& notification, const PartialName& relPrefix);
438 
439 private:
440  struct TopPrefixEntry
441  {
442  ScopedRegisteredPrefixHandle registeredPrefix;
443  std::vector<ScopedInterestFilterHandle> interestFilters;
444  };
445  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
446 
447  Face& m_face;
448  KeyChain& m_keyChain;
449  security::SigningInfo m_signingInfo;
450 
451  std::unordered_map<PartialName, InterestHandler> m_handlers;
452 
453  // NotificationStream name => next sequence number
454  std::unordered_map<Name, uint64_t> m_streams;
455 
457  InMemoryStorageFifo m_storage;
458 };
459 
460 template<typename CP>
461 void
463  Authorization authorize,
464  ValidateParameters validate,
465  ControlCommandHandler handle)
466 {
467  if (!m_topLevelPrefixes.empty()) {
468  BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added"));
469  }
470 
471  if (isOverlappedWithOthers(relPrefix)) {
472  BOOST_THROW_EXCEPTION(std::out_of_range("relPrefix overlaps with another relPrefix"));
473  }
474 
475  auto parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
476  return make_shared<CP>(comp.blockFromValue());
477  };
478 
479  AuthorizationAcceptedCallback accepted =
480  bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
481  _1, _2, _3, _4, std::move(validate), std::move(handle));
482 
483  AuthorizationRejectedCallback rejected =
484  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
485 
486  m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
487  _1, relPrefix, _2, std::move(parser), std::move(authorize),
488  std::move(accepted), std::move(rejected));
489 }
490 
491 } // namespace mgmt
492 } // namespace ndn
493 
494 #endif // NDN_MGMT_DISPATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
The interface of signing key management.
Definition: key-chain.hpp:46
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
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
reply with a ControlResponse where StatusCode is 403
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:44
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Definition: dispatcher.hpp:45
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
Definition: dispatcher.cpp:45
Authorization makeAcceptAllAuthorization()
return an Authorization that accepts all Interests, with empty string as requester
Definition: dispatcher.cpp:34
Signing parameters passed to KeyChain.
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:93
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
add a top-level prefix
Definition: dispatcher.cpp:58
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:43
void addControlCommand(const PartialName &relPrefix, Authorization authorize, ValidateParameters validate, ControlCommandHandler handle)
register a ControlCommand
Definition: dispatcher.hpp:462
Represents an absolute name.
Definition: name.hpp:43
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
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
Definition: dispatcher.cpp:88
ControlCommand response.
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
provides a context for generating response to a StatusDataset request
Represents a Data packet.
Definition: data.hpp:35
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
Cancels an operation automatically upon destruction.
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
Definition: dispatcher.cpp:300
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request
Definition: dispatcher.hpp:118