23 #include "../lp/tags.hpp" 24 #include "../util/logger.hpp" 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,
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)
201 Dispatcher::processControlCommandInterest(
const Name& prefix,
202 const Name& relPrefix,
204 const ControlParametersParser& parser,
206 const AuthorizationAcceptedCallback&
accepted,
207 const AuthorizationRejectedCallback&
rejected)
210 size_t parametersLoc = prefix.
size() + relPrefix.
size();
213 shared_ptr<ControlParameters> parameters;
215 parameters = parser(pc);
223 authorization(prefix, interest, parameters.get(),
accept, reject);
227 Dispatcher::processAuthorizedControlCommandInterest(
const std::string& requester,
230 const shared_ptr<ControlParameters>& parameters,
234 if (validateParams(*parameters)) {
235 handler(prefix, interest, *parameters,
236 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
false));
239 sendControlResponse(
ControlResponse(400,
"failed in validating parameters"), interest);
261 if (!m_topLevelPrefixes.empty()) {
262 BOOST_THROW_EXCEPTION(std::domain_error(
"one or more top-level prefix has been added"));
265 if (isOverlappedWithOthers(relPrefix)) {
266 BOOST_THROW_EXCEPTION(std::out_of_range(
"relPrefix overlapped"));
269 AuthorizationAcceptedCallback accepted =
270 bind(&Dispatcher::processAuthorizedStatusDatasetInterest,
this,
271 _1, _2, _3, handler);
272 AuthorizationRejectedCallback rejected =
273 bind(&Dispatcher::afterAuthorizationRejected,
this, _1, _2);
276 InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest,
this,
277 _1, _2, authorization, accepted, rejected);
278 m_handlers[relPrefix] = bind(&Dispatcher::queryStorage,
this, _1, _2, missContinuation);
282 Dispatcher::processStatusDatasetInterest(
const Name& prefix,
285 const AuthorizationAcceptedCallback& accepted,
286 const AuthorizationRejectedCallback& rejected)
289 bool endsWithVersionOrSegment = interestName.
size() >= 1 &&
290 (interestName[-1].isVersion() || interestName[-1].isSegment());
291 if (endsWithVersionOrSegment) {
297 authorization(prefix, interest,
nullptr, accept, reject);
301 Dispatcher::processAuthorizedStatusDatasetInterest(
const std::string& requester,
307 bind(&Dispatcher::sendStatusDatasetSegment,
this, _1, _2, _3, _4),
308 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
true));
309 handler(prefix, interest, context);
313 Dispatcher::sendStatusDatasetSegment(
const Name& dataName,
const Block& content,
318 auto destination = SendDestination::IMS;
319 if (dataName[-1].toSegment() == 0) {
320 destination = SendDestination::FACE_AND_IMS;
328 sendData(dataName, content, metaInfo, destination, imsFresh);
334 if (!m_topLevelPrefixes.empty()) {
335 BOOST_THROW_EXCEPTION(std::domain_error(
"one or more top-level prefix has been added"));
338 if (isOverlappedWithOthers(relPrefix)) {
339 BOOST_THROW_EXCEPTION(std::out_of_range(
"relPrefix overlaps with another relPrefix"));
343 InterestHandler missContinuation = bind([]{});
346 m_handlers[relPrefix] = bind(&Dispatcher::queryStorage,
this, _1, _2, missContinuation);
347 m_streams[relPrefix] = 0;
348 return bind(&Dispatcher::postNotification,
this, _1, relPrefix);
352 Dispatcher::postNotification(
const Block& notification,
const PartialName& relPrefix)
354 if (m_topLevelPrefixes.empty() || m_topLevelPrefixes.size() > 1) {
355 NDN_LOG_WARN(
"postNotification: no top-level prefix or too many top-level prefixes");
359 Name streamName(m_topLevelPrefixes.begin()->second.topPrefix);
360 streamName.
append(relPrefix);
365 sendData(streamName, notification,
MetaInfo(), SendDestination::FACE_AND_IMS,
366 DEFAULT_FRESHNESS_PERIOD);
represents a CachePolicy header field
const Name & getName() const
Copyright (c) 2011-2015 Regents of the University of California.
indicates a producer generated NACK
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
RejectReply
indicate how to reply in case authorization is rejected
const RegisteredPrefixId * setInterestFilter(const InterestFilter &interestFilter, 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...
reply with a ControlResponse where StatusCode is 403
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
boost::posix_time::time_duration milliseconds(long duration)
Represents a TLV element of NDN packet format.
represents an Interest packet
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
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
const Block & wireEncode() const
Authorization makeAcceptAllAuthorization()
Name & append(const Component &component)
Append a component.
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
#define NDN_LOG_INIT(name)
declare a log module
mgmt::ControlResponse ControlResponse
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
size_t size() const
Get number of components.
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Represents an absolute name.
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another 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
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
void put(Data data)
Publish data packet.
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
#define NDN_LOG_ERROR(expression)
#define NDN_LOG_WARN(expression)
provides a context for generating response to a StatusDataset request
Represents a Data packet.
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 Component & get(ssize_t i) const
Get the component at the given index.
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
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request