akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #include "sync-logic-handler.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 23 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 24 | #include "common.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 25 | #include "conf-parameter.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 26 | #include "logger.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 27 | #include "lsa.hpp" |
| 28 | #include "lsdb.hpp" |
| 29 | #include "sequencing-manager.hpp" |
| 30 | #include "utility/name-helper.hpp" |
| 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | INIT_LOGGER("SyncLogicHandler"); |
| 35 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 36 | using namespace ndn; |
| 37 | using namespace std; |
| 38 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 39 | class SyncUpdate |
| 40 | { |
| 41 | public: |
| 42 | class Error : public std::runtime_error |
| 43 | { |
| 44 | public: |
| 45 | explicit |
| 46 | Error(const std::string& what) |
| 47 | : std::runtime_error(what) |
| 48 | { |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | public: |
| 53 | SyncUpdate(const ndn::Name& name, uint64_t seqNo) |
| 54 | : m_name(name) |
| 55 | , m_seqManager(seqNo) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | const ndn::Name& |
| 60 | getName() const |
| 61 | { |
| 62 | return m_name; |
| 63 | } |
| 64 | |
| 65 | const ndn::Name |
| 66 | getOriginRouter() const |
| 67 | { |
| 68 | int32_t nlsrPosition = util::getNameComponentPosition(m_name, NLSR_COMPONENT); |
| 69 | int32_t lsaPosition = util::getNameComponentPosition(m_name, LSA_COMPONENT); |
| 70 | |
| 71 | if (nlsrPosition < 0 || lsaPosition < 0) { |
| 72 | throw Error("Cannot parse update name because expected components are missing"); |
| 73 | } |
| 74 | |
| 75 | ndn::Name networkName = m_name.getSubName(0, nlsrPosition); |
| 76 | ndn::Name routerName = m_name.getSubName(lsaPosition + 1); |
| 77 | |
| 78 | ndn::Name originRouter = networkName; |
| 79 | originRouter.append(routerName); |
| 80 | |
| 81 | return originRouter; |
| 82 | } |
| 83 | |
| 84 | uint64_t |
| 85 | getNameLsaSeqNo() const |
| 86 | { |
| 87 | return m_seqManager.getNameLsaSeq(); |
| 88 | } |
| 89 | |
| 90 | uint64_t |
| 91 | getAdjLsaSeqNo() const |
| 92 | { |
| 93 | return m_seqManager.getAdjLsaSeq(); |
| 94 | } |
| 95 | |
| 96 | uint64_t |
| 97 | getCorLsaSeqNo() const |
| 98 | { |
| 99 | return m_seqManager.getCorLsaSeq(); |
| 100 | } |
| 101 | |
| 102 | const SequencingManager& |
| 103 | getSequencingManager() const |
| 104 | { |
| 105 | return m_seqManager; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | const ndn::Name m_name; |
| 110 | SequencingManager m_seqManager; |
| 111 | |
| 112 | static const std::string NLSR_COMPONENT; |
| 113 | static const std::string LSA_COMPONENT; |
| 114 | }; |
| 115 | |
| 116 | const std::string SyncUpdate::NLSR_COMPONENT = "NLSR"; |
| 117 | const std::string SyncUpdate::LSA_COMPONENT = "LSA"; |
| 118 | |
Alexander Afanasyev | 7c8882f | 2014-10-28 12:12:15 -0700 | [diff] [blame] | 119 | template<class T> |
| 120 | class NullDeleter |
| 121 | { |
| 122 | public: |
| 123 | void |
| 124 | operator()(T*) |
| 125 | { |
| 126 | } |
| 127 | }; |
| 128 | |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 129 | SyncLogicHandler::SyncLogicHandler(ndn::Face& face, |
| 130 | Lsdb& lsdb, ConfParameter& conf, |
| 131 | SequencingManager& seqManager) |
| 132 | : m_validator(new ndn::ValidatorNull()) |
| 133 | , m_syncFace(face) |
| 134 | , m_lsdb(lsdb) |
| 135 | , m_confParam(conf) |
| 136 | , m_sequencingManager(seqManager) |
| 137 | { |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 138 | } |
| 139 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 140 | void |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 141 | SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 143 | if (m_syncSocket != nullptr) { |
| 144 | _LOG_WARN("Trying to create Sync socket, but Sync socket already exists"); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | m_syncPrefix = syncPrefix; |
| 149 | |
| 150 | // Build LSA sync update prefix |
| 151 | buildUpdatePrefix(); |
| 152 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 153 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 154 | |
| 155 | // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory |
| 156 | // of the object |
Alexander Afanasyev | 7c8882f | 2014-10-28 12:12:15 -0700 | [diff] [blame] | 157 | ndn::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 158 | |
| 159 | m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, facePtr, |
| 160 | ndn::bind(&SyncLogicHandler::onNsyncUpdate, |
| 161 | this, _1, _2), |
| 162 | ndn::bind(&SyncLogicHandler::onNsyncRemoval, |
| 163 | this, _1)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 167 | SyncLogicHandler::onNsyncUpdate(const vector<Sync::MissingDataInfo>& v, Sync::SyncSocket* socket) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 169 | _LOG_DEBUG("Received Nsync update event"); |
| 170 | |
| 171 | for (size_t i = 0; i < v.size(); i++){ |
| 172 | _LOG_DEBUG("Update Name: " << v[i].prefix << " Seq no: " << v[i].high.getSeq()); |
| 173 | |
| 174 | SyncUpdate update(v[i].prefix, v[i].high.getSeq()); |
| 175 | |
| 176 | processUpdateFromSync(update); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 181 | SyncLogicHandler::onNsyncRemoval(const string& prefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 183 | _LOG_DEBUG("Received Nsync removal event"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 187 | SyncLogicHandler::processUpdateFromSync(const SyncUpdate& update) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 189 | ndn::Name originRouter; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 191 | try { |
| 192 | originRouter = update.getOriginRouter(); |
| 193 | } |
| 194 | catch (std::exception& e) { |
| 195 | _LOG_WARN("Received malformed sync update"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 196 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 197 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 199 | // A router should not try to fetch its own LSA |
| 200 | if (originRouter != m_confParam.getRouterPrefix()) { |
| 201 | |
| 202 | update.getSequencingManager().writeLog(); |
| 203 | |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 204 | if (isLsaNew(originRouter, NameLsa::TYPE_STRING, update.getNameLsaSeqNo())) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 205 | _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB"); |
| 206 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 207 | expressInterestForLsa(update, NameLsa::TYPE_STRING, update.getNameLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 208 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 209 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 210 | if (isLsaNew(originRouter, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo())) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 211 | _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB"); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 212 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 213 | if (update.getAdjLsaSeqNo() != 0) { |
| 214 | _LOG_ERROR("Tried to fetch an adjacency LSA when hyperbolic routing" |
| 215 | << " is enabled."); |
| 216 | } |
| 217 | } |
| 218 | else { |
| 219 | expressInterestForLsa(update, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo()); |
| 220 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 221 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 222 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 223 | if (isLsaNew(originRouter, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo())) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 224 | _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB"); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 225 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
| 226 | if (update.getCorLsaSeqNo() != 0) { |
| 227 | _LOG_ERROR("Tried to fetch a coordinate LSA when link-state" |
| 228 | << " is enabled."); |
| 229 | } |
| 230 | } |
| 231 | else { |
| 232 | expressInterestForLsa(update, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo()); |
| 233 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 234 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 238 | bool |
| 239 | SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType, |
| 240 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 241 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 242 | ndn::Name lsaKey = originRouter; |
| 243 | lsaKey.append(lsaType); |
| 244 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 245 | if (lsaType == NameLsa::TYPE_STRING) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 246 | { |
| 247 | return m_lsdb.isNameLsaNew(lsaKey, seqNo); |
| 248 | } |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 249 | else if (lsaType == AdjLsa::TYPE_STRING) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 250 | { |
| 251 | return m_lsdb.isAdjLsaNew(lsaKey, seqNo); |
| 252 | } |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 253 | else if (lsaType == CoordinateLsa::TYPE_STRING) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 254 | { |
| 255 | return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo); |
| 256 | } |
| 257 | |
| 258 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 259 | } |
| 260 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 261 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 262 | SyncLogicHandler::expressInterestForLsa(const SyncUpdate& update, std::string lsaType, |
| 263 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 264 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 265 | ndn::Name interest(update.getName()); |
| 266 | interest.append(lsaType); |
| 267 | interest.appendNumber(seqNo); |
| 268 | |
| 269 | m_lsdb.expressInterest(interest, 0); |
| 270 | } |
| 271 | |
| 272 | void |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 273 | SyncLogicHandler::publishRoutingUpdate() |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 274 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 275 | if (m_syncSocket == nullptr) { |
| 276 | _LOG_FATAL("Cannot publish routing update; SyncSocket does not exist"); |
| 277 | |
| 278 | throw SyncLogicHandler::Error("Cannot publish routing update; SyncSocket does not exist"); |
| 279 | } |
| 280 | |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 281 | m_sequencingManager.writeSeqNoToFile(); |
| 282 | |
| 283 | publishSyncUpdate(m_updatePrefix, m_sequencingManager.getCombinedSeqNo()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 287 | SyncLogicHandler::buildUpdatePrefix() |
| 288 | { |
| 289 | m_updatePrefix = m_confParam.getLsaPrefix(); |
| 290 | m_updatePrefix.append(m_confParam.getSiteName()); |
| 291 | m_updatePrefix.append(m_confParam.getRouterName()); |
| 292 | } |
| 293 | |
| 294 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 295 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo) |
| 296 | { |
| 297 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo); |
| 298 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 299 | ndn::Name updateName(updatePrefix); |
| 300 | string data("NoData"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 301 | |
| 302 | m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), 1000, seqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | }//namespace nlsr |