NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
dispatcher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "dispatcher.hpp"
23 
24 #include <algorithm>
25 
26 // #define NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING
27 
28 namespace ndn {
29 namespace mgmt {
30 
31 const time::milliseconds DEFAULT_FRESHNESS_PERIOD = time::milliseconds(1000);
32 
35 {
36  return [] (const Name& prefix,
37  const Interest& interest,
38  const ControlParameters* params,
39  const AcceptContinuation& accept,
40  const RejectContinuation& reject) {
41  accept("");
42  };
43 }
44 
46  const security::SigningInfo& signingInfo,
47  size_t imsCapacity)
48  : m_face(face)
49  , m_keyChain(keyChain)
50  , m_signingInfo(signingInfo)
51  , m_storage(m_face.getIoService(), imsCapacity)
52 {
53 }
54 
56 {
57  std::vector<Name> topPrefixNames;
58 
59  std::transform(m_topLevelPrefixes.begin(),
60  m_topLevelPrefixes.end(),
61  std::back_inserter(topPrefixNames),
62  [] (const std::unordered_map<Name, TopPrefixEntry>::value_type& entry) {
63  return entry.second.topPrefix;
64  });
65 
66  for (auto&& name : topPrefixNames) {
68  }
69 }
70 
71 void
73  bool wantRegister,
74  const security::SigningInfo& signingInfo)
75 {
76  bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(),
77  m_topLevelPrefixes.end(),
78  [&] (const std::unordered_map<Name, TopPrefixEntry>::value_type& x) {
79  return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first);
80  });
81  if (hasOverlap) {
82  BOOST_THROW_EXCEPTION(std::out_of_range("Top-level Prefixes overlapped"));
83  }
84 
85  TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];;
86  topPrefixEntry.topPrefix = prefix;
87  topPrefixEntry.wantRegister = wantRegister;
88 
89  if (wantRegister) {
90  RegisterPrefixFailureCallback failure = [] (const Name& name, const std::string& reason) {
91  BOOST_THROW_EXCEPTION(std::runtime_error(reason));
92  };
93  topPrefixEntry.registerPrefixId =
94  m_face.registerPrefix(prefix, bind([]{}), failure, signingInfo);
95  }
96 
97  for (auto&& entry : m_handlers) {
98  Name fullPrefix = prefix;
99  fullPrefix.append(entry.first);
100 
101  const InterestFilterId* interestFilterId =
102  m_face.setInterestFilter(fullPrefix, std::bind(entry.second, prefix, _2));
103 
104  topPrefixEntry.interestFilters.push_back(interestFilterId);
105  }
106 }
107 
108 void
110 {
111  auto it = m_topLevelPrefixes.find(prefix);
112  if (it == m_topLevelPrefixes.end()) {
113  return;
114  }
115 
116  const TopPrefixEntry& topPrefixEntry = it->second;
117  if (topPrefixEntry.wantRegister) {
118  m_face.unregisterPrefix(topPrefixEntry.registerPrefixId, bind([]{}), bind([]{}));
119  }
120 
121  for (auto&& filter : topPrefixEntry.interestFilters) {
122  m_face.unsetInterestFilter(filter);
123  }
124 
125  m_topLevelPrefixes.erase(it);
126 }
127 
128 bool
129 Dispatcher::isOverlappedWithOthers(const PartialName& relPrefix)
130 {
131  bool hasOverlapWithHandlers =
132  std::any_of(m_handlers.begin(), m_handlers.end(),
133  [&] (const HandlerMap::value_type& entry) {
134  return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
135  });
136  bool hasOverlapWithStreams =
137  std::any_of(m_streams.begin(), m_streams.end(),
138  [&] (const std::unordered_map<PartialName, uint64_t>::value_type& entry) {
139  return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
140  });
141 
142  return hasOverlapWithHandlers || hasOverlapWithStreams;
143 }
144 
145 void
146 Dispatcher::afterAuthorizationRejected(RejectReply act, const Interest& interest)
147 {
148  if (act == RejectReply::STATUS403) {
149  sendControlResponse(ControlResponse(403, "authorization rejected"), interest);
150  }
151 }
152 
153 void
154 Dispatcher::queryStorage(const Name& prefix, const Interest& interest,
155  const InterestHandler& missContinuation)
156 {
157  auto data = m_storage.find(interest);
158  if (data == nullptr) {
159  // invoke missContinuation to process this Interest if the query fails.
160  missContinuation(prefix, interest);
161  }
162  else {
163  // send the fetched data through face if query succeeds.
164  sendOnFace(*data);
165  }
166 }
167 
168 void
169 Dispatcher::sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo,
170  SendDestination option, time::milliseconds imsFresh)
171 {
172  shared_ptr<Data> data = make_shared<Data>(dataName);
173  data->setContent(content).setMetaInfo(metaInfo).setFreshnessPeriod(DEFAULT_FRESHNESS_PERIOD);
174 
175  m_keyChain.sign(*data, m_signingInfo);
176 
177  if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) {
178  lp::CachePolicy policy;
180  data->setTag(make_shared<lp::CachePolicyTag>(policy));
181  m_storage.insert(*data, imsFresh);
182  }
183 
184  if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) {
185  sendOnFace(*data);
186  }
187 }
188 
189 void
190 Dispatcher::sendOnFace(const Data& data)
191 {
192  try {
193  m_face.put(data);
194  }
195  catch (Face::Error& e) {
196 #ifdef NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING
197  std::clog << e.what() << std::endl;
198 #endif // NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING.
199  }
200 }
201 
202 void
203 Dispatcher::processControlCommandInterest(const Name& prefix,
204  const Name& relPrefix,
205  const Interest& interest,
206  const ControlParametersParser& parser,
207  const Authorization& authorization,
208  const AuthorizationAcceptedCallback& accepted,
209  const AuthorizationRejectedCallback& rejected)
210 {
211  // /<prefix>/<relPrefix>/<parameters>
212  size_t parametersLoc = prefix.size() + relPrefix.size();
213  const name::Component& pc = interest.getName().get(parametersLoc);
214 
215  shared_ptr<ControlParameters> parameters;
216  try {
217  parameters = parser(pc);
218  }
219  catch (tlv::Error& e) {
220  return;
221  }
222 
223  AcceptContinuation accept = bind(accepted, _1, prefix, interest, parameters.get());
224  RejectContinuation reject = bind(rejected, _1, interest);
225  authorization(prefix, interest, parameters.get(), accept, reject);
226 }
227 
228 void
229 Dispatcher::processAuthorizedControlCommandInterest(const std::string& requester,
230  const Name& prefix,
231  const Interest& interest,
232  const ControlParameters* parameters,
233  const ValidateParameters& validateParams,
234  const ControlCommandHandler& handler)
235 {
236  if (validateParams(*parameters)) {
237  handler(prefix, interest, *parameters,
238  bind(&Dispatcher::sendControlResponse, this, _1, interest, false));
239  }
240  else {
241  sendControlResponse(ControlResponse(400, "failed in validating parameters"), interest);
242  }
243 }
244 
245 void
246 Dispatcher::sendControlResponse(const ControlResponse& resp, const Interest& interest,
247  bool isNack)
248 {
249  MetaInfo metaInfo;
250  if (isNack) {
251  metaInfo.setType(tlv::ContentType_Nack);
252  }
253  // control response is always sent out through the face
254  sendData(interest.getName(), resp.wireEncode(), metaInfo, SendDestination::FACE,
256 }
257 
258 void
260  const Authorization& authorization,
261  const StatusDatasetHandler& handler)
262 {
263  if (!m_topLevelPrefixes.empty()) {
264  BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added"));
265  }
266 
267  if (isOverlappedWithOthers(relPrefix)) {
268  BOOST_THROW_EXCEPTION(std::out_of_range("relPrefix overlapped"));
269  }
270 
271  AuthorizationAcceptedCallback accepted =
272  bind(&Dispatcher::processAuthorizedStatusDatasetInterest, this,
273  _1, _2, _3, handler);
274  AuthorizationRejectedCallback rejected =
275  bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
276 
277  // follow the general path if storage is a miss
278  InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest, this,
279  _1, _2, authorization, accepted, rejected);
280  m_handlers[relPrefix] = bind(&Dispatcher::queryStorage, this, _1, _2, missContinuation);
281 }
282 
283 void
284 Dispatcher::processStatusDatasetInterest(const Name& prefix,
285  const Interest& interest,
286  const Authorization& authorization,
287  const AuthorizationAcceptedCallback& accepted,
288  const AuthorizationRejectedCallback& rejected)
289 {
290  const Name& interestName = interest.getName();
291  bool endsWithVersionOrSegment = interestName.size() >= 1 &&
292  (interestName[-1].isVersion() || interestName[-1].isSegment());
293  if (endsWithVersionOrSegment) {
294  return;
295  }
296 
297  AcceptContinuation accept = bind(accepted, _1, prefix, interest, nullptr);
298  RejectContinuation reject = bind(rejected, _1, interest);
299  authorization(prefix, interest, nullptr, accept, reject);
300 }
301 
302 void
303 Dispatcher::processAuthorizedStatusDatasetInterest(const std::string& requester,
304  const Name& prefix,
305  const Interest& interest,
306  const StatusDatasetHandler& handler)
307 {
308  StatusDatasetContext context(interest,
309  bind(&Dispatcher::sendStatusDatasetSegment, this, _1, _2, _3, _4),
310  bind(&Dispatcher::sendControlResponse, this, _1, interest, true));
311  handler(prefix, interest, context);
312 }
313 
314 void
315 Dispatcher::sendStatusDatasetSegment(const Name& dataName, const Block& content,
316  time::milliseconds imsFresh, bool isFinalBlock)
317 {
318  // the first segment will be sent to both places (the face and the in-memory storage)
319  // other segments will be inserted to the in-memory storage only
320  auto destination = SendDestination::IMS;
321  if (dataName[-1].toSegment() == 0) {
322  destination = SendDestination::FACE_AND_IMS;
323  }
324 
325  MetaInfo metaInfo;
326  if (isFinalBlock) {
327  metaInfo.setFinalBlockId(dataName[-1]);
328  }
329 
330  sendData(dataName, content, metaInfo, destination, imsFresh);
331 }
332 
335 {
336  if (!m_topLevelPrefixes.empty()) {
337  throw std::domain_error("one or more top-level prefix has been added");
338  }
339 
340  if (isOverlappedWithOthers(relPrefix)) {
341  throw std::out_of_range("relPrefix overlaps with another relPrefix");
342  }
343 
344  // keep silent if Interest does not match a stored notification
345  InterestHandler missContinuation = bind([]{});
346 
347  // register a handler for the subscriber of this notification stream
348  m_handlers[relPrefix] = bind(&Dispatcher::queryStorage, this, _1, _2, missContinuation);
349  m_streams[relPrefix] = 0;
350  return bind(&Dispatcher::postNotification, this, _1, relPrefix);
351 }
352 
353 void
354 Dispatcher::postNotification(const Block& notification, const PartialName& relPrefix)
355 {
356  if (m_topLevelPrefixes.empty() || m_topLevelPrefixes.size() > 1) {
357 #ifdef NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING
358  std::clog << "no top-level prefix or too many top-level prefixes" << std::endl;
359 #endif // NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING.
360  return;
361  }
362 
363  Name streamName(m_topLevelPrefixes.begin()->second.topPrefix);
364  streamName.append(relPrefix);
365  streamName.appendSequenceNumber(m_streams[streamName]++);
366 
367  // notification is sent out the by face after inserting into the in-memory storage,
368  // because a request may be pending in the PIT
369  sendData(streamName, notification, MetaInfo(), SendDestination::FACE_AND_IMS,
370  DEFAULT_FRESHNESS_PERIOD);
371 }
372 
373 } // namespace mgmt
374 } // namespace ndn
represents a CachePolicy header field
Copyright (c) 2011-2015 Regents of the University of California.
indicates a producer generated NACK
Definition: tlv.hpp:135
std::function< void(const Block &notification)> PostNotification
a function to post a notification
Definition: dispatcher.hpp:123
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419
RejectReply
indicate how to reply in case authorization is rejected
Definition: dispatcher.hpp:49
reply with a ControlResponse where StatusCode is 403
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
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
const Block & wireEncode() const
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
void sign(Data &data, const SigningInfo &params=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
Definition: key-chain.cpp:501
Authorization makeAcceptAllAuthorization()
Definition: dispatcher.cpp:34
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Definition: face.cpp:387
Dispatcher(Face &face, security::KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
Definition: dispatcher.cpp:45
Name & appendSequenceNumber(uint64_t seqNo)
Append sequence number using NDN naming conventions.
Definition: name.cpp:253
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
Definition: meta-info.cpp:63
ndn::mgmt::ControlResponse ControlResponse
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
const RegisteredPrefixId * setInterestFilter(const InterestFilter &interestFilter, const OnInterest &onInterest, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Set InterestFilter to dispatch incoming matching interest to onInterest callback and register the fil...
Definition: face.cpp:216
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:119
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
add a top-level prefix
Definition: dispatcher.cpp:72
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:56
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback called when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:104
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:320
const time::milliseconds DEFAULT_FRESHNESS_PERIOD
Definition: dispatcher.cpp:31
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
Definition: face.cpp:373
base class for a struct that contains ControlCommand parameters
size_t size() const
Get the number of components.
Definition: name.hpp:408
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
void addStatusDataset(const PartialName &relPrefix, const Authorization &authorization, const StatusDatasetHandler &handler)
register a StatusDataset or a prefix under which StatusDatasets can be requested
Definition: dispatcher.cpp:259
std::function< bool(const ControlParameters &params)> ValidateParameters
a function to validate input ControlParameters
Definition: dispatcher.hpp:90
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:148
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
Definition: dispatcher.cpp:109
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
const RegisteredPrefixId * registerPrefix(const Name &prefix, const RegisterPrefixSuccessCallback &onSuccess, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Register prefix with the connected NDN forwarder.
Definition: face.cpp:328
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
Definition: dispatcher.cpp:334
void put(const Data &data)
Publish data packet.
Definition: face.cpp:171
const Name & getName() const
Definition: interest.hpp:218
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request
Definition: dispatcher.hpp:117