Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 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 |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 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/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 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 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 219 | FaceEntry faceEntry; |
| 220 | faceEntry.faceId = parameters.getFaceId(); |
| 221 | faceEntry.origin = parameters.getOrigin(); |
| 222 | faceEntry.cost = parameters.getCost(); |
| 223 | faceEntry.flags = parameters.getFlags(); |
| 224 | faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 225 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 226 | NFD_LOG_TRACE("register prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 227 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 228 | /// \todo For right now, just pass the options to fib as is |
| 229 | /// without processing flags. Later, options will first be added to |
| 230 | /// Rib tree, nrd will generate fib updates based on flags, and |
| 231 | /// will then add next hops one by one. |
| 232 | m_managedRib.insert(parameters.getName(), faceEntry); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 233 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 234 | ControlParameters() |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 235 | .setName(parameters.getName()) |
| 236 | .setFaceId(faceEntry.faceId) |
| 237 | .setCost(faceEntry.cost), |
| 238 | bind(&RibManager::onRegSuccess, this, request, parameters, faceEntry), |
| 239 | bind(&RibManager::onCommandError, this, _1, _2, request, faceEntry)); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void |
| 243 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
| 244 | ControlParameters& parameters) |
| 245 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 246 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 247 | |
| 248 | if (!validateParameters(command, parameters)) |
| 249 | { |
| 250 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 251 | sendResponse(request->getName(), 400, "Malformed command"); |
| 252 | return; |
| 253 | } |
| 254 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 255 | FaceEntry faceEntry; |
| 256 | faceEntry.faceId = parameters.getFaceId(); |
| 257 | faceEntry.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 258 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 259 | NFD_LOG_TRACE("unregister prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 260 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 261 | m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>( |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 262 | ControlParameters() |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 263 | .setName(parameters.getName()) |
| 264 | .setFaceId(faceEntry.faceId), |
| 265 | bind(&RibManager::onUnRegSuccess, this, request, parameters, faceEntry), |
| 266 | bind(&RibManager::onCommandError, this, _1, _2, request, faceEntry)); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void |
| 270 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 271 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 272 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 273 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 274 | sendResponse(request->getName(), 403, failureInfo); |
| 275 | } |
| 276 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 277 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 278 | bool |
| 279 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 280 | ControlParameters& extractedParameters) |
| 281 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 282 | try |
| 283 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 284 | Block rawParameters = parameterComponent.blockFromValue(); |
| 285 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 286 | } |
| 287 | catch (const ndn::Tlv::Error& e) |
| 288 | { |
| 289 | return false; |
| 290 | } |
| 291 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 292 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool |
| 297 | RibManager::validateParameters(const ControlCommand& command, |
| 298 | ControlParameters& parameters) |
| 299 | { |
| 300 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 301 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 302 | command.validateRequest(parameters); |
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 | catch (const ControlCommand::ArgumentError&) |
| 305 | { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | command.applyDefaultsToRequest(parameters); |
| 310 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 316 | const shared_ptr<const Interest>& request, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 317 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 318 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 319 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 320 | |
| 321 | ControlResponse response; |
| 322 | |
| 323 | if (code == 404) |
| 324 | { |
| 325 | response.setCode(code); |
| 326 | response.setText(error); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | response.setCode(533); |
| 331 | std::ostringstream os; |
| 332 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 333 | response.setText(os.str()); |
| 334 | } |
| 335 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 336 | sendResponse(request->getName(), response); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 337 | m_managedRib.erase(request->getName(), faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 341 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 342 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 343 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 344 | { |
| 345 | ControlResponse response; |
| 346 | |
| 347 | response.setCode(200); |
| 348 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 349 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 350 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 351 | NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 352 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 353 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 357 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 358 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 359 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 360 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 361 | { |
| 362 | ControlResponse response; |
| 363 | |
| 364 | response.setCode(200); |
| 365 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 366 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 367 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 368 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry); |
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 | sendResponse(request->getName(), response); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 371 | m_managedRib.erase(request->getName(), faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 374 | void |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 375 | RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix) |
| 376 | { |
| 377 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg) |
| 382 | { |
| 383 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | RibManager::onAddNextHopSuccess(const Name& prefix) |
| 388 | { |
| 389 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 390 | } |
| 391 | |
| 392 | void |
| 393 | RibManager::onAddNextHopError(const Name& name, const std::string& msg) |
| 394 | { |
| 395 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 396 | } |
| 397 | |
| 398 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 399 | RibManager::onControlHeaderSuccess() |
| 400 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 401 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void |
| 405 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 406 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 407 | std::ostringstream os; |
| 408 | os << "Couldn't enable local control header " |
| 409 | << "(code: " << code << ", info: " << reason << ")"; |
| 410 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void |
| 414 | RibManager::enableLocalControlHeader() |
| 415 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 416 | m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>( |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 417 | ControlParameters() |
| 418 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 419 | bind(&RibManager::onControlHeaderSuccess, this), |
| 420 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 421 | } |
| 422 | |
| 423 | void |
| 424 | RibManager::onNotification(const FaceEventNotification& notification) |
| 425 | { |
| 426 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 427 | NFD_LOG_TRACE("onNotification: " << notification); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 428 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed |
| 429 | m_managedRib.erase(notification.getFaceId()); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | } // namespace rib |
| 434 | } // namespace nfd |