Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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. |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "remote-registrator.hpp" |
| 27 | #include "core/logger.hpp" |
| 28 | #include "core/scheduler.hpp" |
Alexander Afanasyev | 692842d | 2015-06-23 14:26:45 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/signing-helpers.hpp> |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace rib { |
| 33 | |
| 34 | NFD_LOG_INIT("RemoteRegistrator"); |
| 35 | |
| 36 | using ndn::nfd::ControlParameters; |
| 37 | using ndn::nfd::CommandOptions; |
| 38 | |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 39 | const Name RemoteRegistrator::LOCAL_REGISTRATION_PREFIX = "/localhost"; |
Yanbiao Li | de88b46 | 2015-01-30 11:20:35 -0800 | [diff] [blame] | 40 | const Name RemoteRegistrator::REMOTE_HUB_PREFIX = "/localhop/nfd"; |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 41 | const name::Component RemoteRegistrator::IGNORE_COMMPONENT("rib"); |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 42 | |
| 43 | RemoteRegistrator::RemoteRegistrator(ndn::nfd::Controller& controller, |
| 44 | ndn::KeyChain& keyChain, |
| 45 | Rib& rib) |
| 46 | : m_nfdController(controller) |
| 47 | , m_keyChain(keyChain) |
| 48 | , m_rib(rib) |
| 49 | , m_refreshInterval(time::seconds(25)) |
| 50 | , m_hasConnectedHub(false) |
| 51 | , m_nRetries(0) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | RemoteRegistrator::~RemoteRegistrator() |
| 56 | { |
| 57 | // cancel all periodically refresh events. |
| 58 | for (auto&& entry : m_regEntries) |
| 59 | { |
| 60 | scheduler::cancel(entry.second); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | RemoteRegistrator::loadConfig(const ConfigSection& configSection) |
| 66 | { |
| 67 | size_t cost = 15, timeout = 10000; |
| 68 | size_t retry = 0; |
| 69 | size_t interval = 0; |
| 70 | const size_t intervalDef = 25, intervalMax = 600; |
| 71 | |
| 72 | NFD_LOG_INFO("Load remote_register section in rib section"); |
| 73 | for (auto&& i : configSection) |
| 74 | { |
| 75 | if (i.first == "cost") |
| 76 | { |
| 77 | cost = i.second.get_value<size_t>(); |
| 78 | } |
| 79 | else if (i.first == "timeout") |
| 80 | { |
| 81 | timeout = i.second.get_value<size_t>(); |
| 82 | } |
| 83 | else if (i.first == "retry") |
| 84 | { |
| 85 | retry = i.second.get_value<size_t>(); |
| 86 | } |
| 87 | else if (i.first == "refresh_interval") |
| 88 | { |
| 89 | interval = i.second.get_value<size_t>(); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | throw ConfigFile::Error("Unrecognized option \"" + i.first + |
| 94 | "\" in \"remote-registrator\" section"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | m_controlParameters |
| 99 | .setCost(cost) |
| 100 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client. |
| 101 | .setFaceId(0);// the remote hub will take the input face as the faceId. |
| 102 | |
| 103 | m_commandOptions |
Yanbiao Li | de88b46 | 2015-01-30 11:20:35 -0800 | [diff] [blame] | 104 | .setPrefix(REMOTE_HUB_PREFIX) |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 105 | .setTimeout(time::milliseconds(timeout)); |
| 106 | |
| 107 | m_nRetries = retry; |
| 108 | |
| 109 | if (interval == 0) |
| 110 | { |
| 111 | interval = intervalDef; |
| 112 | } |
| 113 | |
| 114 | interval = std::min(interval, intervalMax); |
| 115 | |
| 116 | m_refreshInterval = time::seconds(interval); |
| 117 | } |
| 118 | |
| 119 | void |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 120 | RemoteRegistrator::enable() |
| 121 | { |
| 122 | // do remote registration after an entry is inserted into the RIB. |
| 123 | m_afterInsertConnection = |
| 124 | m_rib.afterInsertEntry.connect([this] (const Name& prefix) { |
| 125 | registerPrefix(prefix); |
| 126 | }); |
| 127 | |
| 128 | // do remote unregistration after an entry is erased from the RIB. |
| 129 | m_afterEraseConnection = |
| 130 | m_rib.afterEraseEntry.connect([this] (const Name& prefix) { |
| 131 | unregisterPrefix(prefix); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | RemoteRegistrator::disable() |
| 137 | { |
| 138 | m_afterInsertConnection.disconnect(); |
| 139 | m_afterEraseConnection.disconnect(); |
| 140 | } |
| 141 | |
| 142 | void |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 143 | RemoteRegistrator::registerPrefix(const Name& prefix) |
| 144 | { |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 145 | if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix)) |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 146 | { |
| 147 | NFD_LOG_INFO("local registration only for " << prefix); |
| 148 | return; |
| 149 | } |
| 150 | |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 151 | bool isHubPrefix = prefix == REMOTE_HUB_PREFIX; |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 152 | |
| 153 | if (isHubPrefix) |
| 154 | { |
| 155 | NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix); |
| 156 | |
| 157 | m_hasConnectedHub = true; |
| 158 | |
| 159 | redoRegistration(); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | if (!m_hasConnectedHub) |
| 164 | { |
| 165 | NFD_LOG_INFO("no hub connected when registering " << prefix); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | std::pair<Name, size_t> identity = findIdentityForRegistration(prefix); |
| 170 | |
| 171 | if (0 == identity.second) |
| 172 | { |
| 173 | NFD_LOG_INFO("no proper identity found for registering " << prefix); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | Name prefixForRegistration; |
| 178 | if (identity.first.size() == identity.second) |
| 179 | { |
| 180 | prefixForRegistration = identity.first; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | prefixForRegistration = identity.first.getPrefix(-1); |
| 185 | } |
| 186 | |
| 187 | if (m_regEntries.find(prefixForRegistration) != m_regEntries.end()) |
| 188 | { |
| 189 | NFD_LOG_INFO("registration already in process for " << prefix); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | // make copies of m_controlParameters and m_commandOptions to |
| 194 | // avoid unreasonable overwriting during concurrent registration |
| 195 | // and unregistration. |
| 196 | ControlParameters parameters = m_controlParameters; |
| 197 | CommandOptions options = m_commandOptions; |
| 198 | |
| 199 | startRegistration(parameters.setName(prefixForRegistration), |
Alexander Afanasyev | 692842d | 2015-06-23 14:26:45 -0700 | [diff] [blame] | 200 | options.setSigningInfo(signingByIdentity(identity.first)), |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 201 | m_nRetries); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | RemoteRegistrator::unregisterPrefix(const Name& prefix) |
| 206 | { |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 207 | if (prefix == REMOTE_HUB_PREFIX) |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 208 | { |
| 209 | NFD_LOG_INFO("disconnected to hub with prefix: " << prefix); |
| 210 | |
| 211 | // for phase 1: suppose there is at most one hub connected. |
| 212 | // if the hub prefix has been unregistered locally, there may |
| 213 | // be no connected hub. |
| 214 | m_hasConnectedHub = false; |
| 215 | |
| 216 | clearRefreshEvents(); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | if (!m_hasConnectedHub) |
| 221 | { |
| 222 | NFD_LOG_INFO("no hub connected when unregistering " << prefix); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | std::pair<Name, size_t> identity = findIdentityForRegistration(prefix); |
| 227 | |
| 228 | if (0 == identity.second) |
| 229 | { |
| 230 | NFD_LOG_INFO("no proper identity found for unregistering " << prefix); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | Name prefixForRegistration; |
| 235 | if (identity.first.size() == identity.second) |
| 236 | { |
| 237 | prefixForRegistration = identity.first; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | prefixForRegistration = identity.first.getPrefix(-1); |
| 242 | } |
| 243 | |
| 244 | RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration); |
| 245 | if (m_regEntries.end() == iRegEntry) |
| 246 | { |
| 247 | NFD_LOG_INFO("no existing entry found when unregistering " << prefix); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | for (auto&& entry : m_rib) |
| 252 | { |
| 253 | if (prefixForRegistration.isPrefixOf(entry.first) && |
| 254 | findIdentityForRegistration(entry.first) == identity) |
| 255 | { |
| 256 | NFD_LOG_INFO("this identity should be kept for other rib entry: " |
| 257 | << entry.first); |
| 258 | return; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | scheduler::cancel(iRegEntry->second); |
| 263 | m_regEntries.erase(iRegEntry); |
| 264 | |
| 265 | // make copies of m_controlParameters and m_commandOptions to |
| 266 | // avoid unreasonable overwriting during concurrent registration |
| 267 | // and unregistration. |
| 268 | ControlParameters parameters = m_controlParameters; |
| 269 | CommandOptions options = m_commandOptions; |
| 270 | |
| 271 | startUnregistration(parameters.setName(prefixForRegistration).unsetCost(), |
Alexander Afanasyev | 692842d | 2015-06-23 14:26:45 -0700 | [diff] [blame] | 272 | options.setSigningInfo(signingByIdentity(identity.first)), |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 273 | m_nRetries); |
| 274 | } |
| 275 | |
| 276 | std::pair<Name, size_t> |
| 277 | RemoteRegistrator::findIdentityForRegistration(const Name& prefix) |
| 278 | { |
| 279 | std::pair<Name, size_t> candidateIdentity; |
| 280 | std::vector<Name> identities; |
| 281 | bool isPrefix = false; |
| 282 | size_t maxLength = 0, curLength = 0; |
| 283 | |
| 284 | // get all identies from the key-cahin except the default one. |
| 285 | m_keyChain.getAllIdentities(identities, false); |
| 286 | |
| 287 | // get the default identity. |
| 288 | identities.push_back(m_keyChain.getDefaultIdentity()); |
| 289 | |
| 290 | // longest prefix matching to all indenties. |
| 291 | for (auto&& i : identities) |
| 292 | { |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 293 | if (!i.empty() && IGNORE_COMMPONENT == i.at(-1)) |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 294 | { |
| 295 | isPrefix = i.getPrefix(-1).isPrefixOf(prefix); |
| 296 | curLength = i.size() - 1; |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | isPrefix = i.isPrefixOf(prefix); |
| 301 | curLength = i.size(); |
| 302 | } |
| 303 | |
| 304 | if (isPrefix && curLength > maxLength) |
| 305 | { |
| 306 | candidateIdentity.first = i; |
| 307 | maxLength = curLength; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | candidateIdentity.second = maxLength; |
| 312 | |
| 313 | return candidateIdentity; |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | RemoteRegistrator::startRegistration(const ControlParameters& parameters, |
| 318 | const CommandOptions& options, |
| 319 | int nRetries) |
| 320 | { |
| 321 | NFD_LOG_INFO("start register " << parameters.getName()); |
| 322 | |
| 323 | m_nfdController.start<ndn::nfd::RibRegisterCommand>( |
| 324 | parameters, |
| 325 | bind(&RemoteRegistrator::onRegSuccess, |
| 326 | this, parameters, options), |
| 327 | bind(&RemoteRegistrator::onRegFailure, |
| 328 | this, _1, _2, parameters, options, nRetries), |
| 329 | options); |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | RemoteRegistrator::startUnregistration(const ControlParameters& parameters, |
| 334 | const CommandOptions& options, |
| 335 | int nRetries) |
| 336 | { |
| 337 | NFD_LOG_INFO("start unregister " << parameters.getName()); |
| 338 | |
| 339 | m_nfdController.start<ndn::nfd::RibUnregisterCommand>( |
| 340 | parameters, |
| 341 | bind(&RemoteRegistrator::onUnregSuccess, |
| 342 | this, parameters, options), |
| 343 | bind(&RemoteRegistrator::onUnregFailure, |
| 344 | this, _1, _2, parameters, options, nRetries), |
| 345 | options); |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | RemoteRegistrator::onRegSuccess(const ControlParameters& parameters, |
| 350 | const CommandOptions& options) |
| 351 | { |
| 352 | NFD_LOG_INFO("success to register " << parameters.getName()); |
| 353 | |
| 354 | RegisteredEntryIt iRegEntry = m_regEntries.find(parameters.getName()); |
| 355 | |
| 356 | if (m_regEntries.end() != iRegEntry) |
| 357 | { |
| 358 | NFD_LOG_DEBUG("Existing Entry: (" << iRegEntry->first |
| 359 | << ", " << iRegEntry->second |
| 360 | << ")"); |
| 361 | |
| 362 | scheduler::cancel(iRegEntry->second); |
| 363 | iRegEntry->second = scheduler::schedule( |
| 364 | m_refreshInterval, |
| 365 | bind(&RemoteRegistrator::startRegistration, |
| 366 | this, parameters, options, m_nRetries)); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | NFD_LOG_DEBUG("New Entry"); |
| 371 | m_regEntries.insert(RegisteredEntry( |
| 372 | parameters.getName(), |
| 373 | scheduler::schedule( |
| 374 | m_refreshInterval, |
| 375 | bind(&RemoteRegistrator::startRegistration, |
| 376 | this, parameters, options, m_nRetries)))); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | RemoteRegistrator::onRegFailure(uint32_t code, const std::string& reason, |
| 382 | const ControlParameters& parameters, |
| 383 | const CommandOptions& options, |
| 384 | int nRetries) |
| 385 | { |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 386 | NFD_LOG_INFO("fail to register " << parameters.getName() |
| 387 | << "\n\t reason:" << reason |
| 388 | << "\n\t remain retries:" << nRetries); |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 389 | |
| 390 | if (nRetries > 0) |
| 391 | { |
| 392 | startRegistration(parameters, options, nRetries - 1); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | RemoteRegistrator::onUnregSuccess(const ControlParameters& parameters, |
| 398 | const CommandOptions& options) |
| 399 | { |
| 400 | NFD_LOG_INFO("success to unregister " << parameters.getName()); |
| 401 | } |
| 402 | |
| 403 | void |
| 404 | RemoteRegistrator::onUnregFailure(uint32_t code, const std::string& reason, |
| 405 | const ControlParameters& parameters, |
| 406 | const CommandOptions& options, |
| 407 | int nRetries) |
| 408 | { |
| 409 | NFD_LOG_INFO("fail to unregister " << parameters.getName() |
| 410 | << "\n\t reason:" << reason |
| 411 | << "\n\t remain retries:" << nRetries); |
| 412 | |
| 413 | if (nRetries > 0) |
| 414 | { |
| 415 | startUnregistration(parameters, options, nRetries - 1); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | RemoteRegistrator::redoRegistration() |
| 421 | { |
| 422 | NFD_LOG_INFO("redo " << m_regEntries.size() |
| 423 | << " registration when new Hub connection is built."); |
| 424 | |
| 425 | for (auto&& entry : m_regEntries) |
| 426 | { |
| 427 | // make copies to avoid unreasonable overwrite. |
| 428 | ControlParameters parameters = m_controlParameters; |
| 429 | CommandOptions options = m_commandOptions; |
| 430 | startRegistration(parameters.setName(entry.first), |
Alexander Afanasyev | 692842d | 2015-06-23 14:26:45 -0700 | [diff] [blame] | 431 | options.setSigningInfo(signingByIdentity(entry.first)), |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 432 | m_nRetries); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | RemoteRegistrator::clearRefreshEvents() |
| 438 | { |
| 439 | for (auto&& entry : m_regEntries) |
| 440 | { |
| 441 | scheduler::cancel(entry.second); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | } // namespace rib |
| 446 | } // namespace nfd |