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