Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | **/ |
| 25 | |
| 26 | #include "rib-manager.hpp" |
Alexander Afanasyev | 03ea3eb | 2014-04-17 18:19:06 -0700 | [diff] [blame] | 27 | #include "core/global-io.hpp" |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace rib { |
| 32 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 33 | NFD_LOG_INIT("RibManager"); |
| 34 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 35 | const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib"; |
| 36 | const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib"; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 37 | |
| 38 | const size_t RibManager::COMMAND_UNSIGNED_NCOMPS = |
| 39 | RibManager::COMMAND_PREFIX.size() + |
| 40 | 1 + // verb |
| 41 | 1; // verb options |
| 42 | |
| 43 | const size_t RibManager::COMMAND_SIGNED_NCOMPS = |
| 44 | RibManager::COMMAND_UNSIGNED_NCOMPS + |
| 45 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
| 46 | |
| 47 | const RibManager::VerbAndProcessor RibManager::COMMAND_VERBS[] = |
| 48 | { |
| 49 | VerbAndProcessor( |
| 50 | Name::Component("register"), |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 51 | &RibManager::registerEntry |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 52 | ), |
| 53 | |
| 54 | VerbAndProcessor( |
| 55 | Name::Component("unregister"), |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 56 | &RibManager::unregisterEntry |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 57 | ), |
| 58 | }; |
| 59 | |
Alexander Afanasyev | 03ea3eb | 2014-04-17 18:19:06 -0700 | [diff] [blame] | 60 | inline static void |
| 61 | NullDeleter(boost::asio::io_service* variable) |
| 62 | { |
| 63 | // do nothing |
| 64 | } |
| 65 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 66 | RibManager::RibManager() |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 67 | : m_face(shared_ptr<boost::asio::io_service>(&getGlobalIoService(), &NullDeleter)) |
| 68 | , m_nfdController(new ndn::nfd::Controller(m_face)) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 69 | , m_localhostValidator(m_face) |
| 70 | , m_localhopValidator(m_face) |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 71 | , m_faceMonitor(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 72 | , m_isLocalhopEnabled(false) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 73 | , m_verbDispatch(COMMAND_VERBS, |
| 74 | COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | RibManager::registerWithNfd() |
| 80 | { |
| 81 | //check whether the components of localhop and localhost prefixes are same |
| 82 | BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size()); |
| 83 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 84 | NFD_LOG_INFO("Setting interest filter on: " << COMMAND_PREFIX); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 85 | m_face.setController(m_nfdController); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 86 | m_face.setInterestFilter(COMMAND_PREFIX, |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 87 | bind(&RibManager::onLocalhostRequest, this, _2), |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 88 | bind(&RibManager::setInterestFilterFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 89 | |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 90 | if (m_isLocalhopEnabled) |
| 91 | { |
| 92 | NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX); |
| 93 | m_face.setInterestFilter(REMOTE_COMMAND_PREFIX, |
| 94 | bind(&RibManager::onLocalhopRequest, this, _2), |
| 95 | bind(&RibManager::setInterestFilterFailed, this, _1, _2)); |
| 96 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 98 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 99 | m_faceMonitor.addSubscriber(boost::bind(&RibManager::onNotification, this, _1)); |
| 100 | m_faceMonitor.startNotifications(); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | RibManager::setConfigFile(ConfigFile& configFile) |
| 105 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 106 | configFile.addSectionHandler("rib", |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 107 | bind(&RibManager::onConfig, this, _1, _2, _3)); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | RibManager::onConfig(const ConfigSection& configSection, |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 112 | bool isDryRun, |
| 113 | const std::string& filename) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 114 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 115 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 116 | i != configSection.end(); ++i) |
| 117 | { |
| 118 | if (i->first == "localhost_security") |
| 119 | m_localhostValidator.load(i->second, filename); |
| 120 | else if (i->first == "localhop_security") |
| 121 | { |
| 122 | m_localhopValidator.load(i->second, filename); |
| 123 | m_isLocalhopEnabled = true; |
| 124 | } |
| 125 | else |
| 126 | throw Error("Unrecognized rib property: " + i->first); |
| 127 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
| 131 | RibManager::setInterestFilterFailed(const Name& name, const std::string& msg) |
| 132 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 133 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
| 137 | RibManager::sendResponse(const Name& name, |
| 138 | const ControlResponse& response) |
| 139 | { |
| 140 | const Block& encodedControl = response.wireEncode(); |
| 141 | |
| 142 | Data responseData(name); |
| 143 | responseData.setContent(encodedControl); |
| 144 | |
| 145 | m_keyChain.sign(responseData); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 146 | m_face.put(responseData); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | RibManager::sendResponse(const Name& name, |
| 151 | uint32_t code, |
| 152 | const std::string& text) |
| 153 | { |
| 154 | ControlResponse response(code, text); |
| 155 | sendResponse(name, response); |
| 156 | } |
| 157 | |
| 158 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 159 | RibManager::onLocalhostRequest(const Interest& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 160 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 161 | m_localhostValidator.validate(request, |
| 162 | bind(&RibManager::onCommandValidated, this, _1), |
| 163 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | RibManager::onLocalhopRequest(const Interest& request) |
| 168 | { |
| 169 | m_localhopValidator.validate(request, |
| 170 | bind(&RibManager::onCommandValidated, this, _1), |
| 171 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 175 | RibManager::onCommandValidated(const shared_ptr<const Interest>& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 176 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 177 | // REMOTE_COMMAND_PREFIX number of componenets are same as |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 178 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 180 | const Name& command = request->getName(); |
| 181 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 182 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 183 | |
| 184 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 185 | if (verbProcessor != m_verbDispatch.end()) |
| 186 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 187 | ControlParameters parameters; |
| 188 | if (!extractParameters(parameterComponent, parameters)) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 189 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 190 | NFD_LOG_DEBUG("command result: malformed verb: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 191 | sendResponse(command, 400, "Malformed command"); |
| 192 | return; |
| 193 | } |
| 194 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 195 | if (!parameters.hasFaceId() || parameters.getFaceId() == 0) |
| 196 | { |
| 197 | parameters.setFaceId(request->getIncomingFaceId()); |
| 198 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 200 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 201 | (verbProcessor->second)(this, request, parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 202 | } |
| 203 | else |
| 204 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 205 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 206 | sendResponse(request->getName(), 501, "Unsupported command"); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 211 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 212 | ControlParameters& parameters) |
| 213 | { |
| 214 | ndn::nfd::RibRegisterCommand command; |
| 215 | |
| 216 | if (!validateParameters(command, parameters)) |
| 217 | { |
| 218 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 219 | sendResponse(request->getName(), 400, "Malformed command"); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | RibEntry ribEntry; |
| 224 | ribEntry.name = parameters.getName(); |
| 225 | ribEntry.faceId = parameters.getFaceId(); |
| 226 | ribEntry.origin = parameters.getOrigin(); |
| 227 | ribEntry.cost = parameters.getCost(); |
| 228 | ribEntry.flags = parameters.getFlags(); |
| 229 | ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
| 230 | |
| 231 | NFD_LOG_TRACE("register prefix: " << ribEntry); |
| 232 | |
| 233 | // For right now, just pass the options to fib as it is, |
| 234 | // without processing flags. Later options will be first added to |
| 235 | // Rib tree, then nrd will generate fib updates based on flags and then |
| 236 | // will add next hops one by one.. |
| 237 | m_managedRib.insert(ribEntry); |
| 238 | m_nfdController->start<ndn::nfd::FibAddNextHopCommand>( |
| 239 | ControlParameters() |
| 240 | .setName(ribEntry.name) |
| 241 | .setFaceId(ribEntry.faceId) |
| 242 | .setCost(ribEntry.cost), |
| 243 | bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry), |
| 244 | bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry)); |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
| 249 | ControlParameters& parameters) |
| 250 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 251 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 252 | |
| 253 | if (!validateParameters(command, parameters)) |
| 254 | { |
| 255 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 256 | sendResponse(request->getName(), 400, "Malformed command"); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | RibEntry ribEntry; |
| 261 | ribEntry.name = parameters.getName(); |
| 262 | ribEntry.faceId = parameters.getFaceId(); |
| 263 | ribEntry.origin = parameters.getOrigin(); |
| 264 | |
| 265 | NFD_LOG_TRACE("unregister prefix: " << ribEntry); |
| 266 | |
| 267 | m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>( |
| 268 | ControlParameters() |
| 269 | .setName(ribEntry.name) |
| 270 | .setFaceId(ribEntry.faceId), |
| 271 | bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry), |
| 272 | bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry)); |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 277 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 278 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 279 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 280 | sendResponse(request->getName(), 403, failureInfo); |
| 281 | } |
| 282 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 283 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 284 | bool |
| 285 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 286 | ControlParameters& extractedParameters) |
| 287 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 288 | try |
| 289 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 290 | Block rawParameters = parameterComponent.blockFromValue(); |
| 291 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 292 | } |
| 293 | catch (const ndn::Tlv::Error& e) |
| 294 | { |
| 295 | return false; |
| 296 | } |
| 297 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 298 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | bool |
| 303 | RibManager::validateParameters(const ControlCommand& command, |
| 304 | ControlParameters& parameters) |
| 305 | { |
| 306 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 307 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 308 | command.validateRequest(parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 309 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 310 | catch (const ControlCommand::ArgumentError&) |
| 311 | { |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | command.applyDefaultsToRequest(parameters); |
| 316 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
| 320 | void |
| 321 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 322 | const shared_ptr<const Interest>& request, |
| 323 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 324 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 325 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 326 | |
| 327 | ControlResponse response; |
| 328 | |
| 329 | if (code == 404) |
| 330 | { |
| 331 | response.setCode(code); |
| 332 | response.setText(error); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | response.setCode(533); |
| 337 | std::ostringstream os; |
| 338 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 339 | response.setText(os.str()); |
| 340 | } |
| 341 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 342 | sendResponse(request->getName(), response); |
| 343 | m_managedRib.erase(ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 347 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 348 | const ControlParameters& parameters, |
| 349 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 350 | { |
| 351 | ControlResponse response; |
| 352 | |
| 353 | response.setCode(200); |
| 354 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 355 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 357 | NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 358 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 359 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 362 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 363 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 364 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 365 | const ControlParameters& parameters, |
| 366 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 367 | { |
| 368 | ControlResponse response; |
| 369 | |
| 370 | response.setCode(200); |
| 371 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 372 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 373 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 374 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 375 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 376 | sendResponse(request->getName(), response); |
| 377 | m_managedRib.erase(ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 380 | void |
| 381 | RibManager::onControlHeaderSuccess() |
| 382 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 383 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void |
| 387 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 388 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 389 | std::ostringstream os; |
| 390 | os << "Couldn't enable local control header " |
| 391 | << "(code: " << code << ", info: " << reason << ")"; |
| 392 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void |
| 396 | RibManager::enableLocalControlHeader() |
| 397 | { |
| 398 | m_nfdController->start<ndn::nfd::FaceEnableLocalControlCommand>( |
| 399 | ControlParameters() |
| 400 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 401 | bind(&RibManager::onControlHeaderSuccess, this), |
| 402 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | RibManager::onNotification(const FaceEventNotification& notification) |
| 407 | { |
| 408 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 409 | NFD_LOG_TRACE("onNotification: " << notification); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 410 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed |
| 411 | m_managedRib.erase(notification.getFaceId()); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | } // namespace rib |
| 416 | } // namespace nfd |