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-2018 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 "../encoding/block.hpp"
26 #include "../face.hpp"
27 #include "../ims/in-memory-storage-fifo.hpp"
28 #include "../security/key-chain.hpp"
29 #include "control-response.hpp"
30 #include "control-parameters.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 
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  const Authorization& authorization,
213  const ValidateParameters& validateParams,
214  const ControlCommandHandler& handler);
215 
216 public: // StatusDataset
247  void
248  addStatusDataset(const PartialName& relPrefix,
249  const Authorization& authorization,
250  const StatusDatasetHandler& handler);
251 
252 public: // NotificationStream
272  PostNotification
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  Name topPrefix;
443  optional<const RegisteredPrefixId*> registeredPrefixId = nullopt;
444  std::vector<const InterestFilterId*> interestFilters;
445  };
446  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
447 
448  Face& m_face;
449  KeyChain& m_keyChain;
450  security::SigningInfo m_signingInfo;
451 
452  std::unordered_map<PartialName, InterestHandler> m_handlers;
453 
454  // NotificationStream name => next sequence number
455  std::unordered_map<Name, uint64_t> m_streams;
456 
458  InMemoryStorageFifo m_storage;
459 };
460 
461 template<typename CP>
462 void
464  const Authorization& authorization,
465  const ValidateParameters& validateParams,
466  const ControlCommandHandler& handler)
467 {
468  if (!m_topLevelPrefixes.empty()) {
469  BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added"));
470  }
471 
472  if (isOverlappedWithOthers(relPrefix)) {
473  BOOST_THROW_EXCEPTION(std::out_of_range("relPrefix overlaps with another relPrefix"));
474  }
475 
476  ControlParametersParser parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
477  return make_shared<CP>(comp.blockFromValue());
478  };
479 
480  AuthorizationAcceptedCallback accepted =
481  bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
482  _1, _2, _3, _4, validateParams, handler);
483 
484  AuthorizationRejectedCallback rejected =
485  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
486 
487  m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
488  _1, relPrefix, _2, parser, authorization, accepted, rejected);
489 }
490 
491 } // namespace mgmt
492 } // namespace ndn
493 
494 #endif // NDN_MGMT_DISPATCHER_HPP
constexpr nullopt_t nullopt
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
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
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
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:42
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.
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
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
Represents an absolute name.
Definition: name.hpp:42
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
void addControlCommand(const PartialName &relPrefix, const Authorization &authorization, const ValidateParameters &validateParams, const ControlCommandHandler &handler)
register a ControlCommand
Definition: dispatcher.hpp:463
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: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
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request
Definition: dispatcher.hpp:118