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-2017 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 "../face.hpp"
26 #include "../encoding/block.hpp"
27 #include "../security/key-chain.hpp"
28 #include "../ims/in-memory-storage-fifo.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 act)> 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 
116 typedef std::function<void(const Name& prefix, const Interest& interest,
118 
119 //---- NOTIFICATION STREAM ----
120 
123 typedef std::function<void(const Block& notification)> PostNotification;
124 
125 // ---- DISPATCHER ----
126 
129 class Dispatcher : noncopyable
130 {
131  class Error : public std::runtime_error
132  {
133  public:
134  explicit
135  Error(const std::string& what)
136  : std::runtime_error(what)
137  {
138  }
139  };
140 
141 public:
148  Dispatcher(Face& face, KeyChain& keyChain,
149  const security::SigningInfo& signingInfo = security::SigningInfo(),
150  size_t imsCapacity = 256);
151 
152  virtual
153  ~Dispatcher();
154 
175  void
176  addTopPrefix(const Name& prefix, bool wantRegister = true,
177  const security::SigningInfo& signingInfo = security::SigningInfo());
178 
189  void
190  removeTopPrefix(const Name& prefix);
191 
192 public: // ControlCommand
219  template<typename CP>
220  void
221  addControlCommand(const PartialName& relPrefix,
222  const Authorization& authorization,
223  const ValidateParameters& validateParams,
224  const ControlCommandHandler& handler);
225 
226 public: // StatusDataset
258  void
259  addStatusDataset(const PartialName& relPrefix,
260  const Authorization& authorization,
261  const StatusDatasetHandler& handler);
262 
263 public: // NotificationStream
284  PostNotification
285  addNotificationStream(const PartialName& relPrefix);
286 
287 private:
288  typedef std::function<void(const Name& prefix,
289  const Interest& interest)> InterestHandler;
290 
291  typedef std::function<void(const std::string& requester,
292  const Name& prefix,
293  const Interest& interest,
294  const shared_ptr<ControlParameters>&)> AuthorizationAcceptedCallback;
295 
296  typedef std::function<void(RejectReply act,
297  const Interest& interest)> AuthorizationRejectedCallback;
298 
306  typedef std::function<shared_ptr<ControlParameters>(const name::Component& component)>
307  ControlParametersParser;
308 
309  bool
310  isOverlappedWithOthers(const PartialName& relPrefix);
311 
318  void
319  afterAuthorizationRejected(RejectReply act, const Interest& interest);
320 
330  void
331  queryStorage(const Name& prefix, const Interest& interest, const InterestHandler& missContinuation);
332 
333  enum class SendDestination {
334  NONE = 0,
335  FACE = 1,
336  IMS = 2,
337  FACE_AND_IMS = 3
338  };
339 
356  void
357  sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
358  SendDestination destination, time::milliseconds imsFresh);
359 
365  void
366  sendOnFace(const Data& data);
367 
379  void
380  processControlCommandInterest(const Name& prefix,
381  const Name& relPrefix,
382  const Interest& interest,
383  const ControlParametersParser& parser,
384  const Authorization& authorization,
385  const AuthorizationAcceptedCallback& accepted,
386  const AuthorizationRejectedCallback& rejected);
387 
398  void
399  processAuthorizedControlCommandInterest(const std::string& requester,
400  const Name& prefix,
401  const Interest& interest,
402  const shared_ptr<ControlParameters>& parameters,
403  const ValidateParameters& validate,
404  const ControlCommandHandler& handler);
405 
406  void
407  sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack = false);
408 
418  void
419  processStatusDatasetInterest(const Name& prefix,
420  const Interest& interest,
421  const Authorization& authorization,
422  const AuthorizationAcceptedCallback& accepted,
423  const AuthorizationRejectedCallback& rejected);
424 
433  void
434  processAuthorizedStatusDatasetInterest(const std::string& requester,
435  const Name& prefix,
436  const Interest& interest,
437  const StatusDatasetHandler& handler);
438 
447  void
448  sendStatusDatasetSegment(const Name& dataName, const Block& content,
449  time::milliseconds imsFresh, bool isFinalBlock);
450 
451  void
452  postNotification(const Block& notification, const PartialName& relPrefix);
453 
454 private:
455  struct TopPrefixEntry
456  {
457  Name topPrefix;
458  bool wantRegister;
459  const ndn::RegisteredPrefixId* registerPrefixId;
460  std::vector<const ndn::InterestFilterId*> interestFilters;
461  };
462  std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
463 
464  Face& m_face;
465  KeyChain& m_keyChain;
466  security::SigningInfo m_signingInfo;
467 
468  typedef std::unordered_map<PartialName, InterestHandler> HandlerMap;
469  typedef HandlerMap::iterator HandlerMapIt;
470  HandlerMap m_handlers;
471 
472  // NotificationStream name => next sequence number
473  std::unordered_map<Name, uint64_t> m_streams;
474 
476  InMemoryStorageFifo m_storage;
477 };
478 
479 template<typename CP>
480 void
482  const Authorization& authorization,
483  const ValidateParameters& validateParams,
484  const ControlCommandHandler& handler)
485 {
486  if (!m_topLevelPrefixes.empty()) {
487  BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added"));
488  }
489 
490  if (isOverlappedWithOthers(relPrefix)) {
491  BOOST_THROW_EXCEPTION(std::out_of_range("relPrefix overlaps with another relPrefix"));
492  }
493 
494  ControlParametersParser parser =
495  [] (const name::Component& component) -> shared_ptr<ControlParameters> {
496  return make_shared<CP>(component.blockFromValue());
497  };
498 
499  AuthorizationAcceptedCallback accepted =
500  bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
501  _1, _2, _3, _4, validateParams, handler);
502 
503  AuthorizationRejectedCallback rejected =
504  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
505 
506  m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
507  _1, relPrefix, _2, parser, authorization, accepted, rejected);
508 }
509 
510 } // namespace mgmt
511 } // namespace ndn
512 #endif // NDN_MGMT_DISPATCHER_HPP
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:123
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:129
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(RejectReply act)> RejectContinuation
a function to be called if authorization is rejected
Definition: dispatcher.hpp:60
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Definition: dispatcher.hpp:45
Authorization makeAcceptAllAuthorization()
Definition: dispatcher.cpp:34
Signing parameters passed to KeyChain.
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:481
Component holds a read-only name component value.
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:117