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 | |
| 60 | RibManager::RibManager() |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 61 | : m_face(getGlobalIoService()) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 62 | , m_nfdController(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 63 | , m_localhostValidator(m_face) |
| 64 | , m_localhopValidator(m_face) |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 65 | , m_faceMonitor(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 66 | , m_isLocalhopEnabled(false) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 67 | , m_verbDispatch(COMMAND_VERBS, |
| 68 | COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 73 | RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest) |
| 74 | { |
| 75 | NFD_LOG_INFO("Listening on: " << commandPrefix); |
| 76 | |
| 77 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 78 | ControlParameters() |
| 79 | .setName(commandPrefix) |
| 80 | .setFaceId(0), |
| 81 | bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)), |
| 82 | bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2)); |
| 83 | |
| 84 | m_face.setInterestFilter(commandPrefix, onRequest); |
| 85 | } |
| 86 | |
| 87 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 88 | RibManager::registerWithNfd() |
| 89 | { |
| 90 | //check whether the components of localhop and localhost prefixes are same |
| 91 | BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size()); |
| 92 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 93 | this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2)); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 94 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 95 | if (m_isLocalhopEnabled) { |
| 96 | this->startListening(REMOTE_COMMAND_PREFIX, |
| 97 | bind(&RibManager::onLocalhopRequest, this, _2)); |
| 98 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 100 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 101 | m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 102 | m_faceMonitor.startNotifications(); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | RibManager::setConfigFile(ConfigFile& configFile) |
| 107 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 108 | configFile.addSectionHandler("rib", |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 109 | bind(&RibManager::onConfig, this, _1, _2, _3)); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | RibManager::onConfig(const ConfigSection& configSection, |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 114 | bool isDryRun, |
| 115 | const std::string& filename) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 116 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 117 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 118 | i != configSection.end(); ++i) |
| 119 | { |
| 120 | if (i->first == "localhost_security") |
| 121 | m_localhostValidator.load(i->second, filename); |
| 122 | else if (i->first == "localhop_security") |
| 123 | { |
| 124 | m_localhopValidator.load(i->second, filename); |
| 125 | m_isLocalhopEnabled = true; |
| 126 | } |
| 127 | else |
| 128 | throw Error("Unrecognized rib property: " + i->first); |
| 129 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 133 | RibManager::sendResponse(const Name& name, |
| 134 | const ControlResponse& response) |
| 135 | { |
| 136 | const Block& encodedControl = response.wireEncode(); |
| 137 | |
| 138 | Data responseData(name); |
| 139 | responseData.setContent(encodedControl); |
| 140 | |
| 141 | m_keyChain.sign(responseData); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 142 | m_face.put(responseData); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
| 146 | RibManager::sendResponse(const Name& name, |
| 147 | uint32_t code, |
| 148 | const std::string& text) |
| 149 | { |
| 150 | ControlResponse response(code, text); |
| 151 | sendResponse(name, response); |
| 152 | } |
| 153 | |
| 154 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 155 | RibManager::onLocalhostRequest(const Interest& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 156 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 157 | m_localhostValidator.validate(request, |
| 158 | bind(&RibManager::onCommandValidated, this, _1), |
| 159 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | RibManager::onLocalhopRequest(const Interest& request) |
| 164 | { |
| 165 | m_localhopValidator.validate(request, |
| 166 | bind(&RibManager::onCommandValidated, this, _1), |
| 167 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 171 | RibManager::onCommandValidated(const shared_ptr<const Interest>& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 172 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 173 | // REMOTE_COMMAND_PREFIX number of componenets are same as |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 174 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 176 | const Name& command = request->getName(); |
| 177 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 178 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 179 | |
| 180 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 181 | if (verbProcessor != m_verbDispatch.end()) |
| 182 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 183 | ControlParameters parameters; |
| 184 | if (!extractParameters(parameterComponent, parameters)) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 185 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 186 | NFD_LOG_DEBUG("command result: malformed verb: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 187 | sendResponse(command, 400, "Malformed command"); |
| 188 | return; |
| 189 | } |
| 190 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 191 | if (!parameters.hasFaceId() || parameters.getFaceId() == 0) |
| 192 | { |
| 193 | parameters.setFaceId(request->getIncomingFaceId()); |
| 194 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 195 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 196 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 197 | (verbProcessor->second)(this, request, parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 201 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 202 | sendResponse(request->getName(), 501, "Unsupported command"); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 207 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 208 | ControlParameters& parameters) |
| 209 | { |
| 210 | ndn::nfd::RibRegisterCommand command; |
| 211 | |
| 212 | if (!validateParameters(command, parameters)) |
| 213 | { |
| 214 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 215 | sendResponse(request->getName(), 400, "Malformed command"); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | RibEntry ribEntry; |
| 220 | ribEntry.name = parameters.getName(); |
| 221 | ribEntry.faceId = parameters.getFaceId(); |
| 222 | ribEntry.origin = parameters.getOrigin(); |
| 223 | ribEntry.cost = parameters.getCost(); |
| 224 | ribEntry.flags = parameters.getFlags(); |
| 225 | ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
| 226 | |
| 227 | NFD_LOG_TRACE("register prefix: " << ribEntry); |
| 228 | |
| 229 | // For right now, just pass the options to fib as it is, |
| 230 | // without processing flags. Later options will be first added to |
| 231 | // Rib tree, then nrd will generate fib updates based on flags and then |
| 232 | // will add next hops one by one.. |
| 233 | m_managedRib.insert(ribEntry); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 234 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 235 | ControlParameters() |
| 236 | .setName(ribEntry.name) |
| 237 | .setFaceId(ribEntry.faceId) |
| 238 | .setCost(ribEntry.cost), |
| 239 | bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry), |
| 240 | bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry)); |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
| 245 | ControlParameters& parameters) |
| 246 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 247 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 248 | |
| 249 | if (!validateParameters(command, parameters)) |
| 250 | { |
| 251 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 252 | sendResponse(request->getName(), 400, "Malformed command"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | RibEntry ribEntry; |
| 257 | ribEntry.name = parameters.getName(); |
| 258 | ribEntry.faceId = parameters.getFaceId(); |
| 259 | ribEntry.origin = parameters.getOrigin(); |
| 260 | |
| 261 | NFD_LOG_TRACE("unregister prefix: " << ribEntry); |
| 262 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 263 | m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>( |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 264 | ControlParameters() |
| 265 | .setName(ribEntry.name) |
| 266 | .setFaceId(ribEntry.faceId), |
| 267 | bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry), |
| 268 | bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry)); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 273 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 274 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 275 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 276 | sendResponse(request->getName(), 403, failureInfo); |
| 277 | } |
| 278 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 279 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 280 | bool |
| 281 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 282 | ControlParameters& extractedParameters) |
| 283 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 284 | try |
| 285 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 286 | Block rawParameters = parameterComponent.blockFromValue(); |
| 287 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 288 | } |
| 289 | catch (const ndn::Tlv::Error& e) |
| 290 | { |
| 291 | return false; |
| 292 | } |
| 293 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 294 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | bool |
| 299 | RibManager::validateParameters(const ControlCommand& command, |
| 300 | ControlParameters& parameters) |
| 301 | { |
| 302 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 303 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 304 | command.validateRequest(parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 305 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 306 | catch (const ControlCommand::ArgumentError&) |
| 307 | { |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | command.applyDefaultsToRequest(parameters); |
| 312 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 313 | return true; |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 318 | const shared_ptr<const Interest>& request, |
| 319 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 320 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 321 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 322 | |
| 323 | ControlResponse response; |
| 324 | |
| 325 | if (code == 404) |
| 326 | { |
| 327 | response.setCode(code); |
| 328 | response.setText(error); |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | response.setCode(533); |
| 333 | std::ostringstream os; |
| 334 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 335 | response.setText(os.str()); |
| 336 | } |
| 337 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 338 | sendResponse(request->getName(), response); |
| 339 | m_managedRib.erase(ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 343 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 344 | const ControlParameters& parameters, |
| 345 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 346 | { |
| 347 | ControlResponse response; |
| 348 | |
| 349 | response.setCode(200); |
| 350 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 351 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 352 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 353 | NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 354 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 355 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 358 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 359 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 360 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 361 | const ControlParameters& parameters, |
| 362 | const RibEntry& ribEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 363 | { |
| 364 | ControlResponse response; |
| 365 | |
| 366 | response.setCode(200); |
| 367 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 368 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 369 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 370 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 371 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 372 | sendResponse(request->getName(), response); |
| 373 | m_managedRib.erase(ribEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 376 | void |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 377 | RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix) |
| 378 | { |
| 379 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 380 | } |
| 381 | |
| 382 | void |
| 383 | RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg) |
| 384 | { |
| 385 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 386 | } |
| 387 | |
| 388 | void |
| 389 | RibManager::onAddNextHopSuccess(const Name& prefix) |
| 390 | { |
| 391 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 392 | } |
| 393 | |
| 394 | void |
| 395 | RibManager::onAddNextHopError(const Name& name, const std::string& msg) |
| 396 | { |
| 397 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 398 | } |
| 399 | |
| 400 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 401 | RibManager::onControlHeaderSuccess() |
| 402 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 403 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void |
| 407 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 408 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 409 | std::ostringstream os; |
| 410 | os << "Couldn't enable local control header " |
| 411 | << "(code: " << code << ", info: " << reason << ")"; |
| 412 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void |
| 416 | RibManager::enableLocalControlHeader() |
| 417 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 418 | m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>( |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 419 | ControlParameters() |
| 420 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 421 | bind(&RibManager::onControlHeaderSuccess, this), |
| 422 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | RibManager::onNotification(const FaceEventNotification& notification) |
| 427 | { |
| 428 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 429 | NFD_LOG_TRACE("onNotification: " << notification); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 430 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed |
| 431 | m_managedRib.erase(notification.getFaceId()); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | } // namespace rib |
| 436 | } // namespace nfd |