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 | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 133 | NFD_LOG_ERROR("Error in setting interest filter (" << name << "): " << msg); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 134 | m_face.shutdown(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
| 138 | RibManager::sendResponse(const Name& name, |
| 139 | const ControlResponse& response) |
| 140 | { |
| 141 | const Block& encodedControl = response.wireEncode(); |
| 142 | |
| 143 | Data responseData(name); |
| 144 | responseData.setContent(encodedControl); |
| 145 | |
| 146 | m_keyChain.sign(responseData); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 147 | m_face.put(responseData); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void |
| 151 | RibManager::sendResponse(const Name& name, |
| 152 | uint32_t code, |
| 153 | const std::string& text) |
| 154 | { |
| 155 | ControlResponse response(code, text); |
| 156 | sendResponse(name, response); |
| 157 | } |
| 158 | |
| 159 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 160 | RibManager::onLocalhostRequest(const Interest& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 161 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 162 | m_localhostValidator.validate(request, |
| 163 | bind(&RibManager::onCommandValidated, this, _1), |
| 164 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | RibManager::onLocalhopRequest(const Interest& request) |
| 169 | { |
| 170 | m_localhopValidator.validate(request, |
| 171 | bind(&RibManager::onCommandValidated, this, _1), |
| 172 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 176 | RibManager::onCommandValidated(const shared_ptr<const Interest>& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 177 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 178 | // REMOTE_COMMAND_PREFIX number of componenets are same as |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 179 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 181 | const Name& command = request->getName(); |
| 182 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 183 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 184 | |
| 185 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 186 | if (verbProcessor != m_verbDispatch.end()) |
| 187 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 188 | ControlParameters parameters; |
| 189 | if (!extractParameters(parameterComponent, parameters)) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 190 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 191 | NFD_LOG_DEBUG("command result: malformed verb: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 192 | sendResponse(command, 400, "Malformed command"); |
| 193 | return; |
| 194 | } |
| 195 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 196 | if (!parameters.hasFaceId() || parameters.getFaceId() == 0) |
| 197 | { |
| 198 | parameters.setFaceId(request->getIncomingFaceId()); |
| 199 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 200 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 201 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 202 | (verbProcessor->second)(this, request, parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 203 | } |
| 204 | else |
| 205 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 206 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 207 | sendResponse(request->getName(), 501, "Unsupported command"); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 212 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 213 | ControlParameters& parameters) |
| 214 | { |
| 215 | ndn::nfd::RibRegisterCommand command; |
| 216 | |
| 217 | if (!validateParameters(command, parameters)) |
| 218 | { |
| 219 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 220 | sendResponse(request->getName(), 400, "Malformed command"); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | RibEntry ribEntry; |
| 225 | ribEntry.name = parameters.getName(); |
| 226 | ribEntry.faceId = parameters.getFaceId(); |
| 227 | ribEntry.origin = parameters.getOrigin(); |
| 228 | ribEntry.cost = parameters.getCost(); |
| 229 | ribEntry.flags = parameters.getFlags(); |
| 230 | ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
| 231 | |
| 232 | NFD_LOG_TRACE("register prefix: " << ribEntry); |
| 233 | |
| 234 | // For right now, just pass the options to fib as it is, |
| 235 | // without processing flags. Later options will be first added to |
| 236 | // Rib tree, then nrd will generate fib updates based on flags and then |
| 237 | // will add next hops one by one.. |
| 238 | m_managedRib.insert(ribEntry); |
| 239 | m_nfdController->start<ndn::nfd::FibAddNextHopCommand>( |
| 240 | ControlParameters() |
| 241 | .setName(ribEntry.name) |
| 242 | .setFaceId(ribEntry.faceId) |
| 243 | .setCost(ribEntry.cost), |
| 244 | bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry), |
| 245 | bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry)); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
| 250 | ControlParameters& parameters) |
| 251 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 252 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 253 | |
| 254 | if (!validateParameters(command, parameters)) |
| 255 | { |
| 256 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 257 | sendResponse(request->getName(), 400, "Malformed command"); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | RibEntry ribEntry; |
| 262 | ribEntry.name = parameters.getName(); |
| 263 | ribEntry.faceId = parameters.getFaceId(); |
| 264 | ribEntry.origin = parameters.getOrigin(); |
| 265 | |
| 266 | NFD_LOG_TRACE("unregister prefix: " << ribEntry); |
| 267 | |
| 268 | m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>( |
| 269 | ControlParameters() |
| 270 | .setName(ribEntry.name) |
| 271 | .setFaceId(ribEntry.faceId), |
| 272 | bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry), |
| 273 | bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry)); |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 278 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 279 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 280 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 281 | sendResponse(request->getName(), 403, failureInfo); |
| 282 | } |
| 283 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 284 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 285 | bool |
| 286 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 287 | ControlParameters& extractedParameters) |
| 288 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 289 | try |
| 290 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 291 | Block rawParameters = parameterComponent.blockFromValue(); |
| 292 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 293 | } |
| 294 | catch (const ndn::Tlv::Error& e) |
| 295 | { |
| 296 | return false; |
| 297 | } |
| 298 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 299 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | bool |
| 304 | RibManager::validateParameters(const ControlCommand& command, |
| 305 | ControlParameters& parameters) |
| 306 | { |
| 307 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 308 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 309 | command.validateRequest(parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 310 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 311 | catch (const ControlCommand::ArgumentError&) |
| 312 | { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | command.applyDefaultsToRequest(parameters); |
| 317 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
| 321 | void |
| 322 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 323 | const shared_ptr<const Interest>& request, |
| 324 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 325 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 326 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 327 | |
| 328 | ControlResponse response; |
| 329 | |
| 330 | if (code == 404) |
| 331 | { |
| 332 | response.setCode(code); |
| 333 | response.setText(error); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | response.setCode(533); |
| 338 | std::ostringstream os; |
| 339 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 340 | response.setText(os.str()); |
| 341 | } |
| 342 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 343 | sendResponse(request->getName(), response); |
| 344 | m_managedRib.erase(ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 348 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 349 | const ControlParameters& parameters, |
| 350 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 351 | { |
| 352 | ControlResponse response; |
| 353 | |
| 354 | response.setCode(200); |
| 355 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 356 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 357 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 358 | NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 359 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 360 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 363 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 364 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 365 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 366 | const ControlParameters& parameters, |
| 367 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 368 | { |
| 369 | ControlResponse response; |
| 370 | |
| 371 | response.setCode(200); |
| 372 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 373 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 374 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 375 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 376 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 377 | sendResponse(request->getName(), response); |
| 378 | m_managedRib.erase(ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 381 | void |
| 382 | RibManager::onControlHeaderSuccess() |
| 383 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 384 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void |
| 388 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 389 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 390 | NFD_LOG_ERROR("Error: couldn't enable local control header " |
| 391 | << "(code: " << code << ", info: " << reason << ")"); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 392 | m_face.shutdown(); |
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 |