38 return [] (
const Name& prefix,
50 , m_keyChain(keyChain)
51 , m_signingInfo(signingInfo)
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::sendData(
const Name& dataName,
const Block& content,
157 shared_ptr<Data> data = make_shared<Data>(dataName);
158 data->setContent(content).setMetaInfo(metaInfo);
160 m_keyChain.
sign(*data, m_signingInfo);
166 #ifdef NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING 167 std::clog << e.what() << std::endl;
168 #endif // NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING. 173 Dispatcher::processControlCommandInterest(
const Name& prefix,
174 const Name& relPrefix,
176 const ControlParametersParser& parser,
178 const AuthorizationAcceptedCallback& accepted,
179 const AuthorizationRejectedCallback& rejected)
182 size_t parametersLoc = prefix.
size() + relPrefix.
size();
185 shared_ptr<ControlParameters> parameters;
187 parameters = parser(pc);
195 authorization(prefix, interest, parameters.get(), accept, reject);
199 Dispatcher::processAuthorizedControlCommandInterest(
const std::string& requester,
206 if (validateParams(*parameters)) {
207 handler(prefix, interest, *parameters,
208 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
false));
211 sendControlResponse(
ControlResponse(400,
"failed in validating parameters"), interest);
232 if (!m_topLevelPrefixes.empty()) {
233 BOOST_THROW_EXCEPTION(std::domain_error(
"one or more top-level prefix has been added"));
236 if (isOverlappedWithOthers(relPrefix)) {
237 BOOST_THROW_EXCEPTION(std::out_of_range(
"relPrefix overlapped"));
240 AuthorizationAcceptedCallback accepted =
241 bind(&Dispatcher::processAuthorizedStatusDatasetInterest,
this,
242 _1, _2, _3, handler);
243 AuthorizationRejectedCallback rejected =
244 bind(&Dispatcher::afterAuthorizationRejected,
this, _1, _2);
245 m_handlers[relPrefix] = bind(&Dispatcher::processStatusDatasetInterest,
this,
246 _1, _2, authorization, accepted, rejected);
250 Dispatcher::processStatusDatasetInterest(
const Name& prefix,
253 const AuthorizationAcceptedCallback& accepted,
254 const AuthorizationRejectedCallback& rejected)
257 bool endsWithVersionOrSegment = interestName.
size() >= 1 &&
258 (interestName[-1].isVersion() || interestName[-1].isSegment());
259 if (endsWithVersionOrSegment) {
265 authorization(prefix, interest,
nullptr, accept, reject);
269 Dispatcher::processAuthorizedStatusDatasetInterest(
const std::string& requester,
275 handler(prefix, interest, context);
281 if (!m_topLevelPrefixes.empty()) {
282 throw std::domain_error(
"one or more top-level prefix has been added");
285 if (isOverlappedWithOthers(relPrefix)) {
286 throw std::out_of_range(
"relPrefix overlaps with another relPrefix");
289 m_streams[relPrefix] = 0;
290 return bind(&Dispatcher::postNotification,
this, _1, relPrefix);
294 Dispatcher::postNotification(
const Block& notification,
const PartialName& relPrefix)
296 if (m_topLevelPrefixes.empty() || m_topLevelPrefixes.size() > 1) {
297 #ifdef NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING 298 std::clog <<
"no top-level prefix or too many top-level prefixes" << std::endl;
299 #endif // NDN_CXX_MGMT_DISPATCHER_ENABLE_LOGGING. 303 Name streamName(m_topLevelPrefixes.begin()->second.topPrefix);
304 streamName.
append(relPrefix);
306 sendData(streamName, notification,
MetaInfo());
const Name & getName() const
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
RejectReply
indicate how to reply in case authorization is rejected
reply with a ControlResponse where StatusCode is 403
The packet signing interface.
Class representing a wire element of NDN-TLV 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
const Block & wireEncode() const
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.
Name & appendSequenceNumber(uint64_t seqNo)
Append sequence number using NDN naming conventions.
ndn::mgmt::ControlResponse ControlResponse
void addStatusDataset(const PartialName &relPrefix, Authorization authorization, StatusDatasetHandler handler)
register a StatusDataset or a prefix under which StatusDatasets can be requested
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...
Dispatcher(Face &face, security::KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo())
constructor
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
size_t size() const
Get the number of components.
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback called when registerPrefix or setInterestFilter command fails.
Name abstraction to represent an absolute name.
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< 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.
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...
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters ¶ms, CommandContinuation done)> ControlCommandHandler
a function to handle an authorized ControlCommand
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.
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, AcceptContinuation accept, RejectContinuation reject)> Authorization
a function that performs authorization
represents an error in TLV encoding or decoding
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
void put(const Data &data)
Publish data packet.
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request