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