36 return [] (
const Name& prefix,
49 , m_keyChain(keyChain)
50 , m_signingInfo(signingInfo)
51 , m_storage(m_face.getIoService(), imsCapacity)
57 std::vector<Name> topPrefixNames;
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;
66 for (
auto&&
name : topPrefixNames) {
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);
82 BOOST_THROW_EXCEPTION(std::out_of_range(
"Top-level Prefixes overlapped"));
85 TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];;
86 topPrefixEntry.topPrefix = prefix;
87 topPrefixEntry.wantRegister = wantRegister;
91 BOOST_THROW_EXCEPTION(std::runtime_error(reason));
93 topPrefixEntry.registerPrefixId =
97 for (
auto&& entry : m_handlers) {
98 Name fullPrefix = prefix;
99 fullPrefix.
append(entry.first);
101 const InterestFilterId* interestFilterId =
104 topPrefixEntry.interestFilters.push_back(interestFilterId);
111 auto it = m_topLevelPrefixes.find(prefix);
112 if (it == m_topLevelPrefixes.end()) {
116 const TopPrefixEntry& topPrefixEntry = it->second;
117 if (topPrefixEntry.wantRegister) {
118 m_face.
unregisterPrefix(topPrefixEntry.registerPrefixId, bind([]{}), bind([]{}));
121 for (
auto&& filter : topPrefixEntry.interestFilters) {
125 m_topLevelPrefixes.erase(it);
129 Dispatcher::isOverlappedWithOthers(
const PartialName& relPrefix)
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);
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);
142 return hasOverlapWithHandlers || hasOverlapWithStreams;
149 sendControlResponse(
ControlResponse(403,
"authorization rejected"), interest);
154 Dispatcher::queryStorage(
const Name& prefix,
const Interest& interest,
155 const InterestHandler& missContinuation)
157 auto data = m_storage.find(interest);
158 if (data ==
nullptr) {
160 missContinuation(prefix, interest);
169 Dispatcher::sendData(
const Name& dataName,
const Block& content,
const MetaInfo& metaInfo,
170 SendDestination option, time::milliseconds imsFresh)
172 shared_ptr<Data> data = make_shared<Data>(dataName);
173 data->setContent(content).setMetaInfo(metaInfo).setFreshnessPeriod(DEFAULT_FRESHNESS_PERIOD);
175 m_keyChain.
sign(*data, m_signingInfo);
177 if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) {
180 data->setTag(make_shared<lp::CachePolicyTag>(policy));
181 m_storage.insert(*data, imsFresh);
184 if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) {
190 Dispatcher::sendOnFace(
const Data& data)
196 #ifdef NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING 197 std::clog << e.what() << std::endl;
198 #endif // NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING. 203 Dispatcher::processControlCommandInterest(
const Name& prefix,
204 const Name& relPrefix,
206 const ControlParametersParser& parser,
208 const AuthorizationAcceptedCallback& accepted,
209 const AuthorizationRejectedCallback& rejected)
212 size_t parametersLoc = prefix.
size() + relPrefix.
size();
215 shared_ptr<ControlParameters> parameters;
217 parameters = parser(pc);
225 authorization(prefix, interest, parameters.get(), accept, reject);
229 Dispatcher::processAuthorizedControlCommandInterest(
const std::string& requester,
236 if (validateParams(*parameters)) {
237 handler(prefix, interest, *parameters,
238 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
false));
241 sendControlResponse(
ControlResponse(400,
"failed in validating parameters"), interest);
263 if (!m_topLevelPrefixes.empty()) {
264 BOOST_THROW_EXCEPTION(std::domain_error(
"one or more top-level prefix has been added"));
267 if (isOverlappedWithOthers(relPrefix)) {
268 BOOST_THROW_EXCEPTION(std::out_of_range(
"relPrefix overlapped"));
271 AuthorizationAcceptedCallback accepted =
272 bind(&Dispatcher::processAuthorizedStatusDatasetInterest,
this,
273 _1, _2, _3, handler);
274 AuthorizationRejectedCallback rejected =
275 bind(&Dispatcher::afterAuthorizationRejected,
this, _1, _2);
278 InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest,
this,
279 _1, _2, authorization, accepted, rejected);
280 m_handlers[relPrefix] = bind(&Dispatcher::queryStorage,
this, _1, _2, missContinuation);
284 Dispatcher::processStatusDatasetInterest(
const Name& prefix,
287 const AuthorizationAcceptedCallback& accepted,
288 const AuthorizationRejectedCallback& rejected)
291 bool endsWithVersionOrSegment = interestName.
size() >= 1 &&
292 (interestName[-1].isVersion() || interestName[-1].isSegment());
293 if (endsWithVersionOrSegment) {
299 authorization(prefix, interest,
nullptr, accept, reject);
303 Dispatcher::processAuthorizedStatusDatasetInterest(
const std::string& requester,
309 bind(&Dispatcher::sendStatusDatasetSegment,
this, _1, _2, _3, _4),
310 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
true));
311 handler(prefix, interest, context);
315 Dispatcher::sendStatusDatasetSegment(
const Name& dataName,
const Block& content,
316 time::milliseconds imsFresh,
bool isFinalBlock)
320 auto destination = SendDestination::IMS;
321 if (dataName[-1].toSegment() == 0) {
322 destination = SendDestination::FACE_AND_IMS;
330 sendData(dataName, content, metaInfo, destination, imsFresh);
336 if (!m_topLevelPrefixes.empty()) {
337 throw std::domain_error(
"one or more top-level prefix has been added");
340 if (isOverlappedWithOthers(relPrefix)) {
341 throw std::out_of_range(
"relPrefix overlaps with another relPrefix");
345 InterestHandler missContinuation = bind([]{});
348 m_handlers[relPrefix] = bind(&Dispatcher::queryStorage,
this, _1, _2, missContinuation);
349 m_streams[relPrefix] = 0;
350 return bind(&Dispatcher::postNotification,
this, _1, relPrefix);
354 Dispatcher::postNotification(
const Block& notification,
const PartialName& relPrefix)
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. 363 Name streamName(m_topLevelPrefixes.begin()->second.topPrefix);
364 streamName.
append(relPrefix);
369 sendData(streamName, notification,
MetaInfo(), SendDestination::FACE_AND_IMS,
370 DEFAULT_FRESHNESS_PERIOD);
represents a CachePolicy header field
Copyright (c) 2011-2015 Regents of the University of California.
indicates a producer generated NACK
std::function< void(const Block ¬ification)> PostNotification
a function to post a notification
const Component & get(ssize_t i) const
Get the component at the given index.
RejectReply
indicate how to reply in case authorization is rejected
reply with a ControlResponse where StatusCode is 403
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
The packet signing interface.
Class representing a wire element of NDN-TLV packet format.
represents an Interest packet
const Block & wireEncode() const
std::function< void(RejectReply act)> RejectContinuation
a function to be called if authorization is rejected
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
void sign(Data &data, const SigningInfo ¶ms=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
Authorization makeAcceptAllAuthorization()
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Dispatcher(Face &face, security::KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
Name & appendSequenceNumber(uint64_t seqNo)
Append sequence number using NDN naming conventions.
ndn::mgmt::ControlResponse ControlResponse
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...
Abstraction to communicate with local or remote NDN forwarder.
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
add a top-level prefix
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback called when registerPrefix or setInterestFilter command fails.
Name abstraction to represent an absolute name.
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...
const time::milliseconds DEFAULT_FRESHNESS_PERIOD
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
base class for a struct that contains ControlCommand parameters
size_t size() const
Get the number of components.
Component holds a read-only name component value.
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters ¶ms, const CommandContinuation &done)> ControlCommandHandler
a function to handle an authorized ControlCommand
void addStatusDataset(const PartialName &relPrefix, const Authorization &authorization, const StatusDatasetHandler &handler)
register a StatusDataset or a prefix under which StatusDatasets can be requested
std::function< bool(const ControlParameters ¶ms)> ValidateParameters
a function to validate input ControlParameters
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
provides a context for generating response to a StatusDataset request
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
a function that performs authorization
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.
represents an error in TLV encoding or decoding
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
void put(const Data &data)
Publish data packet.
const Name & getName() const
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request