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)
162 catch (
const Face::Error& e) {
163 NDN_LOG_ERROR(
"sendOnFace(" << data.getName() <<
"): " << e.what());
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();
178 const name::Component& pc = interest.getName().get(parametersLoc);
180 shared_ptr<ControlParameters> parameters;
182 parameters = parser(pc);
184 catch (
const tlv::Error&) {
188 AcceptContinuation accept = [=] (
const auto& req) { accepted(req, prefix, interest, parameters); };
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,
243 std::move(authorize), std::move(accepted), std::move(rejected));
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) {
264 AcceptContinuation accept = [=] (
const auto& req) { accepted(req, prefix, interest,
nullptr); };
266 authorization(prefix, interest,
nullptr, accept, reject);
270 Dispatcher::processAuthorizedStatusDatasetInterest(
const Name& prefix,
274 StatusDatasetContext context(interest,
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;
296 metaInfo.setFinalBlock(dataName[-1]);
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);
333 streamName.appendSequenceNumber(m_streams[streamName]++);
337 sendData(streamName, notification, {}, SendDestination::FACE_AND_IMS);
Represents a TLV element of the NDN packet format.
Provide a communication channel with local or remote NDN forwarder.
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.
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...
void put(Data data)
Publish a Data packet.
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
Represents an Interest packet.
const Name & getName() const noexcept
Represents an absolute name.
size_t size() const noexcept
Returns the number of components.
Base class for a struct that contains ControlCommand parameters.
Implements a request dispatcher on server side of NFD Management protocol.
PostNotification addNotificationStream(const PartialName &relPrefix)
Register a NotificationStream.
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
Constructor.
void addStatusDataset(const PartialName &relPrefix, Authorization authorize, StatusDatasetHandler handle)
Register a StatusDataset or a prefix under which StatusDatasets can be requested.
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
Add a top-level prefix.
void removeTopPrefix(const Name &prefix)
Remove a top-level prefix.
Signing parameters passed to KeyChain.
#define NDN_LOG_WARN(expression)
Log at WARN level.
#define NDN_LOG_ERROR(expression)
Log at ERROR level.
#define NDN_LOG_INIT(name)
Define a non-member log module.
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
A function that performs authorization.
std::function< void(const Block ¬ification)> PostNotification
A function to post a notification.
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.
RejectReply
Indicate how to reply in case authorization is rejected.
@ STATUS403
Reply with a ControlResponse where StatusCode is 403.
std::function< void(const std::string &requester)> AcceptContinuation
A function to be called if authorization is successful.
std::function< void(RejectReply reply)> RejectContinuation
A function to be called if authorization is rejected.
Authorization makeAcceptAllAuthorization()
Return an Authorization that accepts all Interests, with empty string as requester.
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
A function to handle a StatusDataset request.
mgmt::ControlResponse ControlResponse
@ ContentType_Nack
application-level nack