35 return [] (
const Name& prefix,
47 , m_keyChain(keyChain)
48 , m_signingInfo(signingInfo)
49 , m_storage(m_face.getIoContext(), imsCapacity)
57 bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), [&] (
const auto& x) {
58 return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first);
61 NDN_THROW(std::out_of_range(
"top-level prefix '" + prefix.
toUri() +
"' overlaps with another"));
64 TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];
69 [] (
const Name&,
const std::string& reason) {
70 NDN_THROW(std::runtime_error(
"prefix registration failed: " + reason));
75 for (
const auto& entry : m_handlers) {
78 [=, cb = entry.second] (
const auto&,
const auto& interest) {
81 topPrefixEntry.interestFilters.emplace_back(std::move(filterHdl));
88 m_topLevelPrefixes.erase(prefix);
92Dispatcher::checkPrefix(
const PartialName& relPrefix)
const
94 if (!m_topLevelPrefixes.empty()) {
95 NDN_THROW(std::domain_error(
"one or more top-level prefixes have already been added"));
98 bool hasOverlap = std::any_of(m_handlers.begin(), m_handlers.end(), [&] (
const auto& entry) {
99 return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
102 NDN_THROW(std::out_of_range(
"'" + relPrefix.
toUri() +
"' overlaps with another handler"));
107Dispatcher::afterAuthorizationRejected(
RejectReply act,
const Interest& interest)
110 sendControlResponse(
ControlResponse(403,
"authorization rejected"), interest);
115Dispatcher::queryStorage(
const Name& prefix,
const Interest& interest,
116 const InterestHandler& missContinuation)
118 auto data = m_storage.
find(interest);
119 if (data ==
nullptr) {
121 if (missContinuation)
122 missContinuation(prefix, interest);
131Dispatcher::sendData(
const Name& dataName,
const Block& content,
const MetaInfo& metaInfo,
132 SendDestination option)
134 auto data = make_shared<Data>(dataName);
135 data->setContent(content).setMetaInfo(metaInfo).setFreshnessPeriod(1_s);
137 m_keyChain.
sign(*data, m_signingInfo);
139 if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) {
140 lp::CachePolicy policy;
142 data->setTag(make_shared<lp::CachePolicyTag>(policy));
143 m_storage.
insert(*data, 1_s);
146 if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) {
152Dispatcher::sendOnFace(
const Data& data)
157 catch (
const Face::Error& e) {
158 NDN_LOG_ERROR(
"sendOnFace(" << data.getName() <<
"): " << e.what());
163Dispatcher::processCommand(
const Name& prefix,
164 const Interest& interest,
165 const ParametersParser& parse,
170 ControlParametersPtr parameters;
172 parameters = parse(prefix, interest);
174 catch (
const std::exception& e) {
175 NDN_LOG_DEBUG(
"malformed command " << interest.getName() <<
": " << e.what());
179 AcceptContinuation accept = [=, v = std::move(validate), h = std::move(handler)] (
const auto&) {
180 processAuthorizedCommand(prefix, interest, parameters, v, h);
183 afterAuthorizationRejected(reply, interest);
185 authorize(prefix, interest, parameters.get(), accept, reject);
189Dispatcher::processAuthorizedCommand(
const Name& prefix,
190 const Interest& interest,
191 const ControlParametersPtr& parameters,
197 ok = validate(*parameters);
199 catch (
const std::exception& e) {
200 NDN_LOG_DEBUG(
"invalid parameters for command " << interest.getName() <<
": " << e.what());
204 handler(prefix, interest, *parameters, [
this, interest] (
const auto& resp) {
205 sendControlResponse(resp, interest);
209 sendControlResponse(
ControlResponse(400,
"failed in validating parameters"), interest);
214Dispatcher::sendControlResponse(
const ControlResponse& resp,
const Interest& interest,
bool isNack)
222 sendData(interest.getName(), resp.wireEncode(), metaInfo, SendDestination::FACE);
230 checkPrefix(relPrefix);
233 InterestHandler afterMiss = [
this,
234 authorizer = std::move(authorize),
235 handler = std::move(handle)] (
const auto& prefix,
const auto& interest) {
236 processStatusDatasetInterest(prefix, interest, authorizer, std::move(handler));
239 m_handlers[relPrefix] = [
this, miss = std::move(afterMiss)] (
auto&&... args) {
240 queryStorage(std::forward<
decltype(args)>(args)..., miss);
245Dispatcher::processStatusDatasetInterest(
const Name& prefix,
251 bool endsWithVersionOrSegment = interestName.
size() >= 1 &&
252 (interestName[-1].isVersion() || interestName[-1].isSegment());
253 if (endsWithVersionOrSegment) {
258 processAuthorizedStatusDatasetInterest(prefix, interest, h);
261 afterAuthorizationRejected(reply, interest);
263 authorize(prefix, interest,
nullptr, accept, reject);
267Dispatcher::processAuthorizedStatusDatasetInterest(
const Name& prefix,
268 const Interest& interest,
271 StatusDatasetContext context(interest,
272 [
this] (
auto&&... args) {
273 sendStatusDatasetSegment(std::forward<
decltype(args)>(args)...);
275 [
this, interest] (
auto&&... args) {
276 sendControlResponse(std::forward<
decltype(args)>(args)..., interest,
true);
278 handler(prefix, interest, context);
282Dispatcher::sendStatusDatasetSegment(
const Name& dataName,
const Block& content,
bool isFinalBlock)
286 auto destination = SendDestination::IMS;
287 if (dataName[-1].toSegment() == 0) {
288 destination = SendDestination::FACE_AND_IMS;
293 metaInfo.setFinalBlock(dataName[-1]);
296 sendData(dataName, content, metaInfo, destination);
302 checkPrefix(relPrefix);
306 m_handlers[relPrefix] = [
this] (
auto&&... args) {
307 queryStorage(std::forward<
decltype(args)>(args)...,
nullptr);
309 m_streams[relPrefix] = 0;
311 return [=] (
const Block& b) { postNotification(b, relPrefix); };
315Dispatcher::postNotification(
const Block& notification,
const PartialName& relPrefix)
317 if (m_topLevelPrefixes.size() != 1) {
318 NDN_LOG_WARN(
"postNotification: no top-level prefix or too many top-level prefixes");
322 Name streamName(m_topLevelPrefixes.begin()->first);
323 streamName.append(relPrefix);
324 streamName.appendSequenceNumber(m_streams[streamName]++);
328 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(const 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
Get the Interest name.
Represents an absolute name.
Name & append(const Component &component)
Append a name component.
size_t size() const noexcept
Returns the number of components.
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Base class for a struct that contains the parameters for a ControlCommand.
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.
The main interface for signing key management.
void sign(Data &data, const SigningInfo ¶ms=SigningInfo())
Sign a Data packet according to the supplied signing information.
Signing parameters passed to KeyChain.
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
#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 std::string &requester)> AcceptContinuation
A function to be called if authorization is successful.
std::function< void(const Name &prefix, const Interest &interest, const ControlParametersBase ¶ms, const CommandContinuation &done)> ControlCommandHandler
A function to handle an authorized ControlCommand.
std::function< void(const Block ¬ification)> PostNotification
A function to post a notification.
std::function< void(RejectReply)> RejectContinuation
A function to be called if authorization is rejected.
RejectReply
Indicates how to reply in case authorization is rejected.
@ STATUS403
Reply with a ControlResponse where StatusCode is 403.
std::function< bool(ControlParametersBase ¶ms)> ValidateParameters
A function to validate and normalize the incoming request parameters.
std::function< void(const Name &prefix, const Interest &interest, const ControlParametersBase *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
A function that performs authorization.
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