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 | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 58 | std::transform(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), std::back_inserter(topPrefixNames), |
| 59 | [] (const auto& entry) { return entry.second.topPrefix; }); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 60 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 61 | for (const auto& name : topPrefixNames) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 62 | removeTopPrefix(name); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 67 | Dispatcher::addTopPrefix(const Name& prefix, bool wantRegister, |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 68 | const security::SigningInfo& signingInfo) |
| 69 | { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 70 | bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 71 | [&prefix] (const auto& x) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 72 | return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first); |
| 73 | }); |
| 74 | if (hasOverlap) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 75 | BOOST_THROW_EXCEPTION(std::out_of_range("top-level prefix overlaps")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 78 | TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix]; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 79 | topPrefixEntry.topPrefix = prefix; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 80 | |
| 81 | if (wantRegister) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 82 | topPrefixEntry.registeredPrefixId = m_face.registerPrefix(prefix, |
| 83 | nullptr, |
| 84 | [] (const Name&, const std::string& reason) { |
| 85 | BOOST_THROW_EXCEPTION(std::runtime_error("prefix registration failed: " + reason)); |
| 86 | }, |
| 87 | signingInfo); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 90 | for (const auto& entry : m_handlers) { |
| 91 | Name fullPrefix = Name(prefix).append(entry.first); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 92 | const auto* filterId = m_face.setInterestFilter(fullPrefix, bind(entry.second, prefix, _2)); |
| 93 | topPrefixEntry.interestFilters.push_back(filterId); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | Dispatcher::removeTopPrefix(const Name& prefix) |
| 99 | { |
| 100 | auto it = m_topLevelPrefixes.find(prefix); |
| 101 | if (it == m_topLevelPrefixes.end()) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | const TopPrefixEntry& topPrefixEntry = it->second; |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 106 | if (topPrefixEntry.registeredPrefixId) { |
| 107 | m_face.unregisterPrefix(*topPrefixEntry.registeredPrefixId, nullptr, nullptr); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 108 | } |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 109 | for (const auto& filter : topPrefixEntry.interestFilters) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 110 | m_face.unsetInterestFilter(filter); |
| 111 | } |
| 112 | |
| 113 | m_topLevelPrefixes.erase(it); |
| 114 | } |
| 115 | |
| 116 | bool |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 117 | Dispatcher::isOverlappedWithOthers(const PartialName& relPrefix) const |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 118 | { |
| 119 | bool hasOverlapWithHandlers = |
| 120 | std::any_of(m_handlers.begin(), m_handlers.end(), |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 121 | [&] (const auto& entry) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 122 | return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first); |
| 123 | }); |
| 124 | bool hasOverlapWithStreams = |
| 125 | std::any_of(m_streams.begin(), m_streams.end(), |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 126 | [&] (const auto& entry) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 127 | return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first); |
| 128 | }); |
| 129 | |
| 130 | return hasOverlapWithHandlers || hasOverlapWithStreams; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | Dispatcher::afterAuthorizationRejected(RejectReply act, const Interest& interest) |
| 135 | { |
| 136 | if (act == RejectReply::STATUS403) { |
| 137 | sendControlResponse(ControlResponse(403, "authorization rejected"), interest); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 142 | Dispatcher::queryStorage(const Name& prefix, const Interest& interest, |
| 143 | const InterestHandler& missContinuation) |
| 144 | { |
| 145 | auto data = m_storage.find(interest); |
| 146 | if (data == nullptr) { |
| 147 | // invoke missContinuation to process this Interest if the query fails. |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 148 | if (missContinuation) |
| 149 | missContinuation(prefix, interest); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 150 | } |
| 151 | else { |
| 152 | // send the fetched data through face if query succeeds. |
| 153 | sendOnFace(*data); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | Dispatcher::sendData(const Name& dataName, const Block& content, const MetaInfo& metaInfo, |
| 159 | SendDestination option, time::milliseconds imsFresh) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 160 | { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 161 | auto data = make_shared<Data>(dataName); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 162 | data->setContent(content).setMetaInfo(metaInfo).setFreshnessPeriod(DEFAULT_FRESHNESS_PERIOD); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 163 | |
| 164 | m_keyChain.sign(*data, m_signingInfo); |
| 165 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 166 | if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) { |
| 167 | lp::CachePolicy policy; |
| 168 | policy.setPolicy(lp::CachePolicyType::NO_CACHE); |
| 169 | data->setTag(make_shared<lp::CachePolicyTag>(policy)); |
| 170 | m_storage.insert(*data, imsFresh); |
| 171 | } |
| 172 | |
| 173 | if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) { |
| 174 | sendOnFace(*data); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | Dispatcher::sendOnFace(const Data& data) |
| 180 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 181 | try { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 182 | m_face.put(data); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 183 | } |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 184 | catch (const Face::Error& e) { |
| 185 | NDN_LOG_ERROR("sendOnFace: " << e.what()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | Dispatcher::processControlCommandInterest(const Name& prefix, |
| 191 | const Name& relPrefix, |
| 192 | const Interest& interest, |
| 193 | const ControlParametersParser& parser, |
| 194 | const Authorization& authorization, |
| 195 | const AuthorizationAcceptedCallback& accepted, |
| 196 | const AuthorizationRejectedCallback& rejected) |
| 197 | { |
| 198 | // /<prefix>/<relPrefix>/<parameters> |
| 199 | size_t parametersLoc = prefix.size() + relPrefix.size(); |
| 200 | const name::Component& pc = interest.getName().get(parametersLoc); |
| 201 | |
| 202 | shared_ptr<ControlParameters> parameters; |
| 203 | try { |
| 204 | parameters = parser(pc); |
| 205 | } |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 206 | catch (const tlv::Error&) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 210 | AcceptContinuation accept = [=] (const auto& req) { accepted(req, prefix, interest, parameters); }; |
| 211 | RejectContinuation reject = [=] (RejectReply reply) { rejected(reply, interest); }; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 212 | authorization(prefix, interest, parameters.get(), accept, reject); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | Dispatcher::processAuthorizedControlCommandInterest(const std::string& requester, |
| 217 | const Name& prefix, |
| 218 | const Interest& interest, |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 219 | const shared_ptr<ControlParameters>& parameters, |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 220 | const ValidateParameters& validateParams, |
| 221 | const ControlCommandHandler& handler) |
| 222 | { |
| 223 | if (validateParams(*parameters)) { |
| 224 | handler(prefix, interest, *parameters, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 225 | [=] (const auto& resp) { this->sendControlResponse(resp, interest); }); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 226 | } |
| 227 | else { |
| 228 | sendControlResponse(ControlResponse(400, "failed in validating parameters"), interest); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 233 | Dispatcher::sendControlResponse(const ControlResponse& resp, const Interest& interest, bool isNack) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 234 | { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 235 | MetaInfo metaInfo; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 236 | if (isNack) { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 237 | metaInfo.setType(tlv::ContentType_Nack); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 238 | } |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 239 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 240 | // control response is always sent out through the face |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 241 | sendData(interest.getName(), resp.wireEncode(), metaInfo, |
| 242 | SendDestination::FACE, DEFAULT_FRESHNESS_PERIOD); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void |
| 246 | Dispatcher::addStatusDataset(const PartialName& relPrefix, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 247 | Authorization authorize, |
| 248 | StatusDatasetHandler handle) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 249 | { |
| 250 | if (!m_topLevelPrefixes.empty()) { |
| 251 | BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added")); |
| 252 | } |
| 253 | |
| 254 | if (isOverlappedWithOthers(relPrefix)) { |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 255 | BOOST_THROW_EXCEPTION(std::out_of_range("status dataset name overlaps")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | AuthorizationAcceptedCallback accepted = |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 259 | bind(&Dispatcher::processAuthorizedStatusDatasetInterest, this, _1, _2, _3, std::move(handle)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 260 | AuthorizationRejectedCallback rejected = |
| 261 | bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 262 | |
| 263 | // follow the general path if storage is a miss |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 264 | InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest, this, _1, _2, |
| 265 | std::move(authorize), std::move(accepted), std::move(rejected)); |
| 266 | |
| 267 | m_handlers[relPrefix] = [this, miss = std::move(missContinuation)] (auto&&... args) { |
| 268 | this->queryStorage(std::forward<decltype(args)>(args)..., miss); |
| 269 | }; |
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 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 286 | AcceptContinuation accept = [=] (const auto& req) { accepted(req, prefix, interest, nullptr); }; |
| 287 | RejectContinuation reject = [=] (RejectReply reply) { rejected(reply, interest); }; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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) { |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 316 | metaInfo.setFinalBlock(dataName[-1]); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 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 |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 335 | m_handlers[relPrefix] = [this] (auto&&... args) { |
| 336 | this->queryStorage(std::forward<decltype(args)>(args)..., nullptr); |
| 337 | }; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 338 | m_streams[relPrefix] = 0; |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 339 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 340 | return [=] (const Block& b) { postNotification(b, relPrefix); }; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void |
| 344 | Dispatcher::postNotification(const Block& notification, const PartialName& relPrefix) |
| 345 | { |
| 346 | if (m_topLevelPrefixes.empty() || m_topLevelPrefixes.size() > 1) { |
Junxiao Shi | 43a7932 | 2016-08-08 05:48:43 +0000 | [diff] [blame] | 347 | 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] | 348 | return; |
| 349 | } |
| 350 | |
| 351 | Name streamName(m_topLevelPrefixes.begin()->second.topPrefix); |
| 352 | streamName.append(relPrefix); |
| 353 | streamName.appendSequenceNumber(m_streams[streamName]++); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 354 | |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 355 | // 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] | 356 | // because a request may be pending in the PIT |
Davide Pesavento | 3c8a8b0 | 2018-03-01 14:46:55 -0500 | [diff] [blame] | 357 | sendData(streamName, notification, {}, SendDestination::FACE_AND_IMS, DEFAULT_FRESHNESS_PERIOD); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | } // namespace mgmt |
| 361 | } // namespace ndn |