34 return [] (
const Name& prefix,
47 , m_keyChain(keyChain)
48 , m_signingInfo(signingInfo)
49 , m_storage(m_face.getIoService(), imsCapacity)
59 bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(),
60 [&prefix] (
const auto& x) {
61 return x.first.isPrefixOf(prefix) || prefix.
isPrefixOf(x.first);
64 NDN_THROW(std::out_of_range(
"top-level prefix overlaps"));
67 TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];
72 [] (
const Name&,
const std::string& reason) {
73 NDN_THROW(std::runtime_error(
"prefix registration failed: " + reason));
78 for (
const auto& entry : m_handlers) {
79 Name fullPrefix =
Name(prefix).append(entry.first);
81 [=, cb = entry.second] (
const auto&,
const auto& interest) {
84 topPrefixEntry.interestFilters.emplace_back(
std::move(filterHdl));
91 m_topLevelPrefixes.erase(prefix);
95 Dispatcher::isOverlappedWithOthers(
const PartialName& relPrefix)
const 97 bool hasOverlapWithHandlers =
98 std::any_of(m_handlers.begin(), m_handlers.end(),
99 [&] (
const auto& entry) {
100 return entry.first.isPrefixOf(relPrefix) || relPrefix.
isPrefixOf(entry.first);
102 bool hasOverlapWithStreams =
103 std::any_of(m_streams.begin(), m_streams.end(),
104 [&] (
const auto& entry) {
105 return entry.first.isPrefixOf(relPrefix) || relPrefix.
isPrefixOf(entry.first);
108 return hasOverlapWithHandlers || hasOverlapWithStreams;
115 sendControlResponse(
ControlResponse(403,
"authorization rejected"), interest);
120 Dispatcher::queryStorage(
const Name& prefix,
const Interest& interest,
121 const InterestHandler& missContinuation)
123 auto data = m_storage.find(interest);
124 if (data ==
nullptr) {
126 if (missContinuation)
127 missContinuation(prefix, interest);
136 Dispatcher::sendData(
const Name& dataName,
const Block& content,
const MetaInfo& metaInfo,
137 SendDestination option)
139 auto data = make_shared<Data>(dataName);
140 data->setContent(content).setMetaInfo(metaInfo).setFreshnessPeriod(1_s);
142 m_keyChain.sign(*data, m_signingInfo);
144 if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) {
147 data->setTag(make_shared<lp::CachePolicyTag>(policy));
148 m_storage.insert(*data, 1_s);
151 if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) {
157 Dispatcher::sendOnFace(
const Data& data)
168 Dispatcher::processControlCommandInterest(
const Name& prefix,
169 const Name& relPrefix,
171 const ControlParametersParser& parser,
173 const AuthorizationAcceptedCallback&
accepted,
174 const AuthorizationRejectedCallback&
rejected)
177 size_t parametersLoc = prefix.
size() + relPrefix.
size();
180 shared_ptr<ControlParameters> parameters;
182 parameters = parser(pc);
190 authorization(prefix, interest, parameters.get(),
accept, reject);
194 Dispatcher::processAuthorizedControlCommandInterest(
const std::string& requester,
197 const shared_ptr<ControlParameters>& parameters,
201 if (validateParams(*parameters)) {
202 handler(prefix, interest, *parameters,
203 [=] (
const auto& resp) { this->sendControlResponse(resp, interest); });
206 sendControlResponse(
ControlResponse(400,
"failed in validating parameters"), interest);
219 sendData(interest.
getName(), resp.
wireEncode(), metaInfo, SendDestination::FACE);
227 if (!m_topLevelPrefixes.empty()) {
228 NDN_THROW(std::domain_error(
"one or more top-level prefix has been added"));
231 if (isOverlappedWithOthers(relPrefix)) {
232 NDN_THROW(std::out_of_range(
"status dataset name overlaps"));
235 AuthorizationAcceptedCallback accepted =
236 std::bind(&Dispatcher::processAuthorizedStatusDatasetInterest,
this, _2, _3,
std::move(handle));
237 AuthorizationRejectedCallback rejected = [
this] (
auto&&... args) {
238 afterAuthorizationRejected(std::forward<decltype(args)>(args)...);
242 InterestHandler missContinuation = std::bind(&Dispatcher::processStatusDatasetInterest,
this, _1, _2,
245 m_handlers[relPrefix] = [
this, miss =
std::move(missContinuation)] (
auto&&... args) {
246 this->queryStorage(std::forward<decltype(args)>(args)..., miss);
251 Dispatcher::processStatusDatasetInterest(
const Name& prefix,
254 const AuthorizationAcceptedCallback& accepted,
255 const AuthorizationRejectedCallback& rejected)
258 bool endsWithVersionOrSegment = interestName.
size() >= 1 &&
259 (interestName[-1].isVersion() || interestName[-1].isSegment());
260 if (endsWithVersionOrSegment) {
266 authorization(prefix, interest,
nullptr, accept, reject);
270 Dispatcher::processAuthorizedStatusDatasetInterest(
const Name& prefix,
275 [
this] (
auto&&... args) {
276 sendStatusDatasetSegment(std::forward<decltype(args)>(args)...);
278 [
this, interest] (
auto&&... args) {
279 sendControlResponse(std::forward<decltype(args)>(args)..., interest,
true);
281 handler(prefix, interest, context);
285 Dispatcher::sendStatusDatasetSegment(
const Name& dataName,
const Block& content,
bool isFinalBlock)
289 auto destination = SendDestination::IMS;
290 if (dataName[-1].toSegment() == 0) {
291 destination = SendDestination::FACE_AND_IMS;
299 sendData(dataName, content, metaInfo, destination);
305 if (!m_topLevelPrefixes.empty()) {
306 NDN_THROW(std::domain_error(
"one or more top-level prefix has been added"));
309 if (isOverlappedWithOthers(relPrefix)) {
310 NDN_THROW(std::out_of_range(
"notification stream name overlaps"));
315 m_handlers[relPrefix] = [
this] (
auto&&... args) {
316 this->queryStorage(std::forward<decltype(args)>(args)...,
nullptr);
318 m_streams[relPrefix] = 0;
320 return [=] (
const Block& b) { postNotification(b, relPrefix); };
324 Dispatcher::postNotification(
const Block& notification,
const PartialName& relPrefix)
326 if (m_topLevelPrefixes.size() != 1) {
327 NDN_LOG_WARN(
"postNotification: no top-level prefix or too many top-level prefixes");
331 Name streamName(m_topLevelPrefixes.begin()->first);
332 streamName.
append(relPrefix);
337 sendData(streamName, notification, {}, SendDestination::FACE_AND_IMS);
represents a CachePolicy header field
Copyright (c) 2011-2015 Regents of the University of California.
Accept any value the remote endpoint offers.
std::function< void(const Block ¬ification)> PostNotification
a function to post a notification
represents a dispatcher on server side of NFD Management protocol
const Component & get(ssize_t i) const
Returns an immutable reference to the component at the specified index.
RejectReply
indicate how to reply in case authorization is rejected
#define NDN_LOG_WARN(expression)
reply with a ControlResponse where StatusCode is 403
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
Represents a TLV element of the NDN packet format.
Represents an Interest packet.
const Block & wireEncode() const
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
Authorization makeAcceptAllAuthorization()
return an Authorization that accepts all Interests, with empty string as requester ...
Name & append(const Component &component)
Append a component.
Signing parameters passed to KeyChain.
#define NDN_LOG_ERROR(expression)
mgmt::ControlResponse ControlResponse
RegisteredPrefixHandle 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.
std::function< void(RejectReply reply)> RejectContinuation
a function to be called if authorization is rejected
Provide a communication channel 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
const Name & getName() const noexcept
Get name.
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Represents an absolute name.
base class for a struct that contains ControlCommand parameters
size_t size() const
Returns the number of components.
Represents a name component.
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters ¶ms, const CommandContinuation &done)> ControlCommandHandler
a function to handle an authorized ControlCommand
std::function< bool(const ControlParameters ¶ms)> ValidateParameters
a function to validate input ControlParameters
const Name & getName() const noexcept
void put(Data data)
Publish data packet.
#define NDN_LOG_INIT(name)
declare a log module
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
void addStatusDataset(const PartialName &relPrefix, Authorization authorize, StatusDatasetHandler handle)
register a StatusDataset or a prefix under which StatusDatasets can be requested
Provides a context for generating the response to a StatusDataset request.
Represents a Data packet.
RegisteredPrefixHandle setInterestFilter(const InterestFilter &filter, const InterestCallback &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...
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
a function that performs authorization
represents an error in TLV encoding or decoding
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request