Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 14 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "dispatcher.hpp" |
Junxiao Shi | a1478db | 2016-09-09 04:13:15 +0000 | [diff] [blame] | 23 | #include "../lp/tags.hpp" |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 24 | #include "../util/logger.hpp" |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 26 | NDN_LOG_INIT(ndn.mgmt.Dispatcher); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace mgmt { |
| 30 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 31 | const time::milliseconds DEFAULT_FRESHNESS_PERIOD = 1_s; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 32 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 33 | Authorization |
| 34 | makeAcceptAllAuthorization() |
| 35 | { |
| 36 | return [] (const Name& prefix, |
| 37 | const Interest& interest, |
| 38 | const ControlParameters* params, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 39 | const AcceptContinuation& accept, |
| 40 | const RejectContinuation& reject) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 41 | accept(""); |
| 42 | }; |
| 43 | } |
| 44 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 45 | Dispatcher::Dispatcher(Face& face, KeyChain& keyChain, |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 46 | const security::SigningInfo& signingInfo, |
| 47 | size_t imsCapacity) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 48 | : m_face(face) |
| 49 | , m_keyChain(keyChain) |
| 50 | , m_signingInfo(signingInfo) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 51 | , m_storage(m_face.getIoService(), imsCapacity) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | Dispatcher::~Dispatcher() |
| 56 | { |
| 57 | std::vector<Name> topPrefixNames; |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 58 | std::transform(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 59 | std::back_inserter(topPrefixNames), |
| 60 | [] (const std::unordered_map<Name, TopPrefixEntry>::value_type& entry) { |
| 61 | return entry.second.topPrefix; |
| 62 | }); |
| 63 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 64 | for (const auto& name : topPrefixNames) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 65 | removeTopPrefix(name); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 70 | Dispatcher::addTopPrefix(const Name& prefix, bool wantRegister, |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 71 | const security::SigningInfo& signingInfo) |
| 72 | { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 73 | bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 74 | [&] (const std::unordered_map<Name, TopPrefixEntry>::value_type& x) { |
| 75 | return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first); |
| 76 | }); |
| 77 | if (hasOverlap) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 78 | BOOST_THROW_EXCEPTION(std::out_of_range("top-level prefix overlaps")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 81 | TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix]; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 82 | topPrefixEntry.topPrefix = prefix; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 83 | |
| 84 | if (wantRegister) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 85 | topPrefixEntry.registeredPrefixId = m_face.registerPrefix(prefix, |
| 86 | nullptr, |
| 87 | [] (const Name&, const std::string& reason) { |
| 88 | BOOST_THROW_EXCEPTION(std::runtime_error("prefix registration failed: " + reason)); |
| 89 | }, |
| 90 | signingInfo); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 93 | for (const auto& entry : m_handlers) { |
| 94 | Name fullPrefix = Name(prefix).append(entry.first); |
| 95 | const InterestFilterId* filter = m_face.setInterestFilter(fullPrefix, bind(entry.second, prefix, _2)); |
| 96 | topPrefixEntry.interestFilters.push_back(filter); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | Dispatcher::removeTopPrefix(const Name& prefix) |
| 102 | { |
| 103 | auto it = m_topLevelPrefixes.find(prefix); |
| 104 | if (it == m_topLevelPrefixes.end()) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | const TopPrefixEntry& topPrefixEntry = it->second; |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 109 | if (topPrefixEntry.registeredPrefixId) { |
| 110 | m_face.unregisterPrefix(*topPrefixEntry.registeredPrefixId, nullptr, nullptr); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 111 | } |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 112 | for (const auto& filter : topPrefixEntry.interestFilters) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 113 | m_face.unsetInterestFilter(filter); |
| 114 | } |
| 115 | |
| 116 | m_topLevelPrefixes.erase(it); |
| 117 | } |
| 118 | |
| 119 | bool |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 120 | Dispatcher::isOverlappedWithOthers(const PartialName& relPrefix) const |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 121 | { |
| 122 | bool hasOverlapWithHandlers = |
| 123 | std::any_of(m_handlers.begin(), m_handlers.end(), |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 124 | [&] (const std::unordered_map<PartialName, InterestHandler>::value_type& entry) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 125 | return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first); |
| 126 | }); |
| 127 | bool hasOverlapWithStreams = |
| 128 | std::any_of(m_streams.begin(), m_streams.end(), |
| 129 | [&] (const std::unordered_map<PartialName, uint64_t>::value_type& entry) { |
| 130 | return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first); |
| 131 | }); |
| 132 | |
| 133 | return hasOverlapWithHandlers || hasOverlapWithStreams; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | Dispatcher::afterAuthorizationRejected(RejectReply act, const Interest& interest) |
| 138 | { |
| 139 | if (act == RejectReply::STATUS403) { |
| 140 | sendControlResponse(ControlResponse(403, "authorization rejected"), interest); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 145 | Dispatcher::queryStorage(const Name& prefix, const Interest& interest, |
| 146 | const InterestHandler& missContinuation) |
| 147 | { |
| 148 | auto data = m_storage.find(interest); |
| 149 | if (data == nullptr) { |
| 150 | // invoke missContinuation to process this Interest if the query fails. |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 151 | if (missContinuation) |
| 152 | missContinuation(prefix, interest); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 153 | } |
| 154 | else { |
| 155 | // send the fetched data through face if query succeeds. |
| 156 | sendOnFace(*data); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | Dispatcher::sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo, |
| 162 | SendDestination option, time::milliseconds imsFresh) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 163 | { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 164 | auto data = make_shared<Data>(dataName); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 165 | data->setContent(content).setMetaInfo(metaInfo).setFreshnessPeriod(DEFAULT_FRESHNESS_PERIOD); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 166 | |
| 167 | m_keyChain.sign(*data, m_signingInfo); |
| 168 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 169 | if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) { |
| 170 | lp::CachePolicy policy; |
| 171 | policy.setPolicy(lp::CachePolicyType::NO_CACHE); |
| 172 | data->setTag(make_shared<lp::CachePolicyTag>(policy)); |
| 173 | m_storage.insert(*data, imsFresh); |
| 174 | } |
| 175 | |
| 176 | if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) { |
| 177 | sendOnFace(*data); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | Dispatcher::sendOnFace(const Data& data) |
| 183 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 184 | try { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 185 | m_face.put(data); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 186 | } |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 187 | catch (const Face::Error& e) { |
| 188 | NDN_LOG_ERROR("sendOnFace: " << e.what()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | Dispatcher::processControlCommandInterest(const Name& prefix, |
| 194 | const Name& relPrefix, |
| 195 | const Interest& interest, |
| 196 | const ControlParametersParser& parser, |
| 197 | const Authorization& authorization, |
| 198 | const AuthorizationAcceptedCallback& accepted, |
| 199 | const AuthorizationRejectedCallback& rejected) |
| 200 | { |
| 201 | // /<prefix>/<relPrefix>/<parameters> |
| 202 | size_t parametersLoc = prefix.size() + relPrefix.size(); |
| 203 | const name::Component& pc = interest.getName().get(parametersLoc); |
| 204 | |
| 205 | shared_ptr<ControlParameters> parameters; |
| 206 | try { |
| 207 | parameters = parser(pc); |
| 208 | } |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 209 | catch (const tlv::Error&) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 213 | AcceptContinuation accept = bind(accepted, _1, prefix, interest, parameters); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 214 | RejectContinuation reject = bind(rejected, _1, interest); |
| 215 | authorization(prefix, interest, parameters.get(), accept, reject); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | Dispatcher::processAuthorizedControlCommandInterest(const std::string& requester, |
| 220 | const Name& prefix, |
| 221 | const Interest& interest, |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 222 | const shared_ptr<ControlParameters>& parameters, |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 223 | const ValidateParameters& validateParams, |
| 224 | const ControlCommandHandler& handler) |
| 225 | { |
| 226 | if (validateParams(*parameters)) { |
| 227 | handler(prefix, interest, *parameters, |
| 228 | bind(&Dispatcher::sendControlResponse, this, _1, interest, false)); |
| 229 | } |
| 230 | else { |
| 231 | sendControlResponse(ControlResponse(400, "failed in validating parameters"), interest); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 236 | Dispatcher::sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 237 | { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 238 | MetaInfo metaInfo; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 239 | if (isNack) { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 240 | metaInfo.setType(tlv::ContentType_Nack); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 241 | } |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 242 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 243 | // control response is always sent out through the face |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 244 | sendData(interest.getName(), resp.wireEncode(), metaInfo, |
| 245 | SendDestination::FACE, DEFAULT_FRESHNESS_PERIOD); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
| 249 | Dispatcher::addStatusDataset(const PartialName& relPrefix, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 250 | const Authorization& authorization, |
| 251 | const StatusDatasetHandler& handler) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 252 | { |
| 253 | if (!m_topLevelPrefixes.empty()) { |
| 254 | BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added")); |
| 255 | } |
| 256 | |
| 257 | if (isOverlappedWithOthers(relPrefix)) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 258 | BOOST_THROW_EXCEPTION(std::out_of_range("status dataset name overlaps")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | AuthorizationAcceptedCallback accepted = |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 262 | bind(&Dispatcher::processAuthorizedStatusDatasetInterest, this, _1, _2, _3, handler); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 263 | AuthorizationRejectedCallback rejected = |
| 264 | bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 265 | |
| 266 | // follow the general path if storage is a miss |
| 267 | InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest, this, |
| 268 | _1, _2, authorization, accepted, rejected); |
| 269 | m_handlers[relPrefix] = bind(&Dispatcher::queryStorage, this, _1, _2, missContinuation); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void |
| 273 | Dispatcher::processStatusDatasetInterest(const Name& prefix, |
| 274 | const Interest& interest, |
| 275 | const Authorization& authorization, |
| 276 | const AuthorizationAcceptedCallback& accepted, |
| 277 | const AuthorizationRejectedCallback& rejected) |
| 278 | { |
| 279 | const Name& interestName = interest.getName(); |
| 280 | bool endsWithVersionOrSegment = interestName.size() >= 1 && |
| 281 | (interestName[-1].isVersion() || interestName[-1].isSegment()); |
| 282 | if (endsWithVersionOrSegment) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | AcceptContinuation accept = bind(accepted, _1, prefix, interest, nullptr); |
| 287 | RejectContinuation reject = bind(rejected, _1, interest); |
| 288 | authorization(prefix, interest, nullptr, accept, reject); |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | Dispatcher::processAuthorizedStatusDatasetInterest(const std::string& requester, |
| 293 | const Name& prefix, |
| 294 | const Interest& interest, |
| 295 | const StatusDatasetHandler& handler) |
| 296 | { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 297 | StatusDatasetContext context(interest, |
| 298 | bind(&Dispatcher::sendStatusDatasetSegment, this, _1, _2, _3, _4), |
| 299 | bind(&Dispatcher::sendControlResponse, this, _1, interest, true)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 300 | handler(prefix, interest, context); |
| 301 | } |
| 302 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 303 | void |
| 304 | Dispatcher::sendStatusDatasetSegment(const Name& dataName, const Block& content, |
| 305 | time::milliseconds imsFresh, bool isFinalBlock) |
| 306 | { |
| 307 | // the first segment will be sent to both places (the face and the in-memory storage) |
| 308 | // other segments will be inserted to the in-memory storage only |
| 309 | auto destination = SendDestination::IMS; |
| 310 | if (dataName[-1].toSegment() == 0) { |
| 311 | destination = SendDestination::FACE_AND_IMS; |
| 312 | } |
| 313 | |
| 314 | MetaInfo metaInfo; |
| 315 | if (isFinalBlock) { |
| 316 | metaInfo.setFinalBlockId(dataName[-1]); |
| 317 | } |
| 318 | |
| 319 | sendData(dataName, content, metaInfo, destination, imsFresh); |
| 320 | } |
| 321 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 322 | PostNotification |
| 323 | Dispatcher::addNotificationStream(const PartialName& relPrefix) |
| 324 | { |
| 325 | if (!m_topLevelPrefixes.empty()) { |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 326 | BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (isOverlappedWithOthers(relPrefix)) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 330 | BOOST_THROW_EXCEPTION(std::out_of_range("notification stream name overlaps")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 333 | // register a handler for the subscriber of this notification stream |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 334 | // keep silent if Interest does not match a stored notification |
| 335 | m_handlers[relPrefix] = bind(&Dispatcher::queryStorage, this, _1, _2, nullptr); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 336 | m_streams[relPrefix] = 0; |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 337 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 338 | return bind(&Dispatcher::postNotification, this, _1, relPrefix); |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | Dispatcher::postNotification(const Block& notification, const PartialName& relPrefix) |
| 343 | { |
| 344 | if (m_topLevelPrefixes.empty() || m_topLevelPrefixes.size() > 1) { |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 345 | NDN_LOG_WARN("postNotification: no top-level prefix or too many top-level prefixes"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
| 349 | Name streamName(m_topLevelPrefixes.begin()->second.topPrefix); |
| 350 | streamName.append(relPrefix); |
| 351 | streamName.appendSequenceNumber(m_streams[streamName]++); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 352 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 353 | // notification is sent out via the face after inserting into the in-memory storage, |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 354 | // because a request may be pending in the PIT |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 355 | sendData(streamName, notification, {}, SendDestination::FACE_AND_IMS, DEFAULT_FRESHNESS_PERIOD); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | } // namespace mgmt |
| 359 | } // namespace ndn |