NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
26 #ifndef NDN_MGMT_DISPATCHER_HPP
27 #define NDN_MGMT_DISPATCHER_HPP
28 
29 #include "../face.hpp"
30 #include "../security/key-chain.hpp"
31 #include "../encoding/block.hpp"
32 #include "control-response.hpp"
33 #include "control-parameters.hpp"
35 
36 #include <unordered_map>
37 
38 namespace ndn {
39 namespace mgmt {
40 
41 // ---- AUTHORIZATION ----
42 
48 typedef std::function<void(const std::string& requester)> AcceptContinuation;
49 
52 enum class RejectReply {
55  SILENT,
58  STATUS403
59 };
60 
63 typedef std::function<void(RejectReply act)> RejectContinuation;
64 
77 typedef std::function<void(const Name& prefix, const Interest& interest,
78  const ControlParameters* params,
79  AcceptContinuation accept,
80  RejectContinuation reject)> Authorization;
81 
84 Authorization
86 
87 // ---- CONTROL COMMAND ----
88 
93 typedef std::function<bool(const ControlParameters& params)> ValidateParameters;
94 
98 typedef std::function<void(const ControlResponse& resp)> CommandContinuation;
99 
107 typedef std::function<void(const Name& prefix, const Interest& interest,
108  const ControlParameters& params,
109  CommandContinuation done)> ControlCommandHandler;
110 
111 
119 typedef std::function<void(const Name& prefix, const Interest& interest,
121 
122 //---- NOTIFICATION STREAM ----
123 
126 typedef std::function<void(const Block& notification)> PostNotification;
127 
128 // ---- DISPATCHER ----
129 
132 class Dispatcher : noncopyable
133 {
134  class Error : public std::runtime_error
135  {
136  public:
137  explicit
138  Error(const std::string& what)
139  : std::runtime_error(what)
140  {
141  }
142  };
143 
144 public:
150  Dispatcher(Face& face, security::KeyChain& keyChain,
151  const security::SigningInfo& signingInfo = security::SigningInfo());
152 
153  virtual
154  ~Dispatcher();
155 
176  void
177  addTopPrefix(const Name& prefix, bool wantRegister = true,
178  const security::SigningInfo& signingInfo = security::SigningInfo());
179 
190  void
191  removeTopPrefix(const Name& prefix);
192 
193 public: // ControlCommand
217  template<typename CP>
218  void
219  addControlCommand(const PartialName& relPrefix,
220  Authorization authorization,
221  ValidateParameters validateParams,
222  ControlCommandHandler handler);
223 
224 public: // StatusDataset
255  void
256  addStatusDataset(const PartialName& relPrefix,
257  Authorization authorization,
258  StatusDatasetHandler handler);
259 
260 public: // NotificationStream
281  PostNotification
282  addNotificationStream(const PartialName& relPrefix);
283 
284 private:
285  typedef std::function<void(const Name& prefix,
286  const Interest& interest)> InterestHandler;
287 
288  typedef std::function<void(const std::string& requester,
289  const Name& prefix,
290  const Interest& interest,
291  const ControlParameters*)> AuthorizationAcceptedCallback;
292 
293  typedef std::function<void(RejectReply act,
294  const Interest& interest)> AuthorizationRejectedCallback;
295 
303  typedef std::function<shared_ptr<ControlParameters>(const name::Component& component)>
304  ControlParametersParser;
305 
306  bool
307  isOverlappedWithOthers(const PartialName& relPrefix);
308 
315  void
316  afterAuthorizationRejected(RejectReply act, const Interest& interest);
317 
318  void
319  sendData(const Name& dataName, const Block& content,
320  const MetaInfo& metaInfo);
321 
333  void
334  processControlCommandInterest(const Name& prefix,
335  const Name& relPrefix,
336  const Interest& interest,
337  const ControlParametersParser& parser,
338  const Authorization& authorization,
339  const AuthorizationAcceptedCallback& accepted,
340  const AuthorizationRejectedCallback& rejected);
341 
352  void
353  processAuthorizedControlCommandInterest(const std::string& requester,
354  const Name& prefix,
355  const Interest& interest,
356  const ControlParameters* parameters,
357  const ValidateParameters& validate,
358  const ControlCommandHandler& handler);
359 
360  void
361  sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
362 
372  void
373  processStatusDatasetInterest(const Name& prefix,
374  const Interest& interest,
375  const Authorization& authorization,
376  const AuthorizationAcceptedCallback& accepted,
377  const AuthorizationRejectedCallback& rejected);
378 
387  void
388  processAuthorizedStatusDatasetInterest(const std::string& requester,
389  const Name& prefix,
390  const Interest& interest,
391  const StatusDatasetHandler& handler);
392 
393  void
394  postNotification(const Block& notification, const PartialName& relPrefix);
395 
396 private:
397  struct TopPrefixEntry
398  {
399  Name topPrefix;
400  bool wantRegister;
401  const ndn::RegisteredPrefixId* registerPrefixId;
402  std::vector<const ndn::InterestFilterId*> interestFilters;
403  };
404  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
405 
406  Face& m_face;
407  security::KeyChain& m_keyChain;
408  security::SigningInfo m_signingInfo;
409 
410  typedef std::unordered_map<PartialName, InterestHandler> HandlerMap;
411  typedef HandlerMap::iterator HandlerMapIt;
412  HandlerMap m_handlers;
413 
414  // NotificationStream name => next sequence number
415  std::unordered_map<Name, uint64_t> m_streams;
416 };
417 
418 template<typename CP>
419 void
421  Authorization authorization,
422  ValidateParameters validateParams,
423  ControlCommandHandler handler)
424 {
425  if (!m_topLevelPrefixes.empty()) {
426  throw std::domain_error("one or more top-level prefix has been added");
427  }
428 
429  if (isOverlappedWithOthers(relPrefix)) {
430  throw std::out_of_range("relPrefix overlaps with another relPrefix");
431  }
432 
433  ControlParametersParser parser =
434  [] (const name::Component& component) -> shared_ptr<ControlParameters> {
435  return make_shared<CP>(component.blockFromValue());
436  };
437 
438  AuthorizationAcceptedCallback accepted =
439  bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
440  _1, _2, _3, _4, validateParams, handler);
441 
442  AuthorizationRejectedCallback rejected =
443  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
444 
445  m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
446  _1, relPrefix, _2, parser, authorization, accepted, rejected);
447 }
448 
449 } // namespace mgmt
450 } // namespace ndn
451 #endif // NDN_MGMT_DISPATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
void addControlCommand(const PartialName &relPrefix, Authorization authorization, ValidateParameters validateParams, ControlCommandHandler handler)
register a ControlCommand
Definition: dispatcher.hpp:420
std::function< void(const Block &notification)> PostNotification
a function to post a notification
Definition: dispatcher.hpp:126
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:132
RejectReply
indicate how to reply in case authorization is rejected
Definition: dispatcher.hpp:52
reply with a ControlResponse where StatusCode is 403
The packet signing interface.
Definition: key-chain.hpp:48
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:45
std::function< void(RejectReply act)> RejectContinuation
a function to be called if authorization is rejected
Definition: dispatcher.hpp:63
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Definition: dispatcher.hpp:48
Authorization makeAcceptAllAuthorization()
Definition: dispatcher.cpp:36
Signing parameters passed to KeyChain.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:100
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:56
Name abstraction to represent an absolute name.
Definition: name.hpp:46
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:98
base class for a struct that contains ControlCommand parameters
Component holds a read-only name component value.
std::function< bool(const ControlParameters &params)> ValidateParameters
a function to validate input ControlParameters
Definition: dispatcher.hpp:93
ControlCommand response.
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters &params, CommandContinuation done)> ControlCommandHandler
a function to handle an authorized ControlCommand
Definition: dispatcher.hpp:109
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, AcceptContinuation accept, RejectContinuation reject)> Authorization
a function that performs authorization
Definition: dispatcher.hpp:80
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request
Definition: dispatcher.hpp:120