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-2020 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,
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  typedef std::function<void(const Name& prefix,
274  const Interest& interest)> InterestHandler;
275 
276  typedef std::function<void(const std::string& requester,
277  const Name& prefix,
278  const Interest& interest,
279  const shared_ptr<ControlParameters>&)> AuthorizationAcceptedCallback;
280 
281  typedef std::function<void(RejectReply act,
282  const Interest& interest)> AuthorizationRejectedCallback;
283 
290  typedef std::function<shared_ptr<ControlParameters>(const name::Component& comp)> ControlParametersParser;
291 
292  bool
293  isOverlappedWithOthers(const PartialName& relPrefix) const;
294 
300  void
301  afterAuthorizationRejected(RejectReply act, const Interest& interest);
302 
312  void
313  queryStorage(const Name& prefix, const Interest& interest, const InterestHandler& missContinuation);
314 
315  enum class SendDestination {
316  NONE = 0,
317  FACE = 1,
318  IMS = 2,
319  FACE_AND_IMS = 3
320  };
321 
338  void
339  sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
340  SendDestination destination, time::milliseconds imsFresh);
341 
347  void
348  sendOnFace(const Data& data);
349 
361  void
362  processControlCommandInterest(const Name& prefix,
363  const Name& relPrefix,
364  const Interest& interest,
365  const ControlParametersParser& parser,
366  const Authorization& authorization,
367  const AuthorizationAcceptedCallback& accepted,
368  const AuthorizationRejectedCallback& rejected);
369 
380  void
381  processAuthorizedControlCommandInterest(const std::string& requester,
382  const Name& prefix,
383  const Interest& interest,
384  const shared_ptr<ControlParameters>& parameters,
385  const ValidateParameters& validate,
386  const ControlCommandHandler& handler);
387 
388  void
389  sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
390 
400  void
401  processStatusDatasetInterest(const Name& prefix,
402  const Interest& interest,
403  const Authorization& authorization,
404  const AuthorizationAcceptedCallback& accepted,
405  const AuthorizationRejectedCallback& rejected);
406 
415  void
416  processAuthorizedStatusDatasetInterest(const std::string& requester,
417  const Name& prefix,
418  const Interest& interest,
419  const StatusDatasetHandler& handler);
420 
429  void
430  sendStatusDatasetSegment(const Name& dataName, const Block& content,
431  time::milliseconds imsFresh, bool isFinalBlock);
432 
433  void
434  postNotification(const Block& notification, const PartialName& relPrefix);
435 
436 private:
437  struct TopPrefixEntry
438  {
439  ScopedRegisteredPrefixHandle registeredPrefix;
440  std::vector<ScopedInterestFilterHandle> interestFilters;
441  };
442  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
443 
444  Face& m_face;
445  KeyChain& m_keyChain;
446  security::SigningInfo m_signingInfo;
447 
448  std::unordered_map<PartialName, InterestHandler> m_handlers;
449 
450  // NotificationStream name => next sequence number
451  std::unordered_map<Name, uint64_t> m_streams;
452 
454  InMemoryStorageFifo m_storage;
455 };
456 
457 template<typename CP>
458 void
460  Authorization authorize,
461  ValidateParameters validate,
462  ControlCommandHandler handle)
463 {
464  if (!m_topLevelPrefixes.empty()) {
465  NDN_THROW(std::domain_error("one or more top-level prefix has been added"));
466  }
467 
468  if (isOverlappedWithOthers(relPrefix)) {
469  NDN_THROW(std::out_of_range("relPrefix overlaps with another relPrefix"));
470  }
471 
472  auto parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
473  return make_shared<CP>(comp.blockFromValue());
474  };
475 
476  AuthorizationAcceptedCallback accepted =
477  bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
478  _1, _2, _3, _4, std::move(validate), std::move(handle));
479 
480  AuthorizationRejectedCallback rejected =
481  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
482 
483  m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
484  _1, relPrefix, _2, std::move(parser), std::move(authorize),
485  std::move(accepted), std::move(rejected));
486 }
487 
488 } // namespace mgmt
489 } // namespace ndn
490 
491 #endif // NDN_MGMT_DISPATCHER_HPP
Definition: data.cpp:26
The interface of signing key management.
Definition: key-chain.hpp:45
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 the NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:50
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:34
Signing parameters passed to KeyChain.
#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:459
Represents an absolute name.
Definition: name.hpp:44
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 response to a StatusDataset request
Represents a Data packet.
Definition: data.hpp:39
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