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 | |
| 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 | |
| 119 | const std::string SyncLogicHandler::NAME_COMPONENT = "name"; |
| 120 | const std::string SyncLogicHandler::ADJACENCY_COMPONENT = "adjacency"; |
| 121 | const std::string SyncLogicHandler::COORDINATE_COMPONENT = "coordinate"; |
| 122 | |
Alexander Afanasyev | 7c8882f | 2014-10-28 12:12:15 -0700 | [diff] [blame^] | 123 | |
| 124 | template<class T> |
| 125 | class NullDeleter |
| 126 | { |
| 127 | public: |
| 128 | void |
| 129 | operator()(T*) |
| 130 | { |
| 131 | } |
| 132 | }; |
| 133 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 135 | SyncLogicHandler::createSyncSocket() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 136 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 137 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 138 | |
| 139 | // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory |
| 140 | // of the object |
Alexander Afanasyev | 7c8882f | 2014-10-28 12:12:15 -0700 | [diff] [blame^] | 141 | ndn::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 142 | |
| 143 | m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, facePtr, |
| 144 | ndn::bind(&SyncLogicHandler::onNsyncUpdate, |
| 145 | this, _1, _2), |
| 146 | ndn::bind(&SyncLogicHandler::onNsyncRemoval, |
| 147 | this, _1)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 151 | SyncLogicHandler::onNsyncUpdate(const vector<Sync::MissingDataInfo>& v, Sync::SyncSocket* socket) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 152 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 153 | _LOG_DEBUG("Received Nsync update event"); |
| 154 | |
| 155 | for (size_t i = 0; i < v.size(); i++){ |
| 156 | _LOG_DEBUG("Update Name: " << v[i].prefix << " Seq no: " << v[i].high.getSeq()); |
| 157 | |
| 158 | SyncUpdate update(v[i].prefix, v[i].high.getSeq()); |
| 159 | |
| 160 | processUpdateFromSync(update); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 165 | SyncLogicHandler::onNsyncRemoval(const string& prefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 167 | _LOG_DEBUG("Received Nsync removal event"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 171 | SyncLogicHandler::processUpdateFromSync(const SyncUpdate& update) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 173 | ndn::Name originRouter; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 175 | try { |
| 176 | originRouter = update.getOriginRouter(); |
| 177 | } |
| 178 | catch (std::exception& e) { |
| 179 | _LOG_WARN("Received malformed sync update"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 180 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | } |
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 | // A router should not try to fetch its own LSA |
| 184 | if (originRouter != m_confParam.getRouterPrefix()) { |
| 185 | |
| 186 | update.getSequencingManager().writeLog(); |
| 187 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 188 | try { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 189 | if (isLsaNew(originRouter, NAME_COMPONENT, update.getNameLsaSeqNo())) { |
| 190 | _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB"); |
| 191 | |
| 192 | expressInterestForLsa(update, NAME_COMPONENT, update.getNameLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 193 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 194 | |
| 195 | if (isLsaNew(originRouter, ADJACENCY_COMPONENT, update.getAdjLsaSeqNo())) { |
| 196 | _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB"); |
| 197 | |
| 198 | expressInterestForLsa(update, ADJACENCY_COMPONENT, update.getAdjLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 199 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 200 | |
| 201 | if (isLsaNew(originRouter, COORDINATE_COMPONENT, update.getCorLsaSeqNo())) { |
| 202 | _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB"); |
| 203 | |
| 204 | expressInterestForLsa(update, COORDINATE_COMPONENT, update.getCorLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 205 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 207 | catch (std::exception& e) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 208 | std::cerr << e.what() << std::endl; |
| 209 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 214 | bool |
| 215 | SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType, |
| 216 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 217 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 218 | ndn::Name lsaKey = originRouter; |
| 219 | lsaKey.append(lsaType); |
| 220 | |
| 221 | if (lsaType == NAME_COMPONENT) |
| 222 | { |
| 223 | return m_lsdb.isNameLsaNew(lsaKey, seqNo); |
| 224 | } |
| 225 | else if (lsaType == ADJACENCY_COMPONENT) |
| 226 | { |
| 227 | return m_lsdb.isAdjLsaNew(lsaKey, seqNo); |
| 228 | } |
| 229 | else if (lsaType == COORDINATE_COMPONENT) |
| 230 | { |
| 231 | return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo); |
| 232 | } |
| 233 | |
| 234 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | } |
| 236 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 237 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 238 | SyncLogicHandler::expressInterestForLsa(const SyncUpdate& update, std::string lsaType, |
| 239 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 241 | ndn::Name interest(update.getName()); |
| 242 | interest.append(lsaType); |
| 243 | interest.appendNumber(seqNo); |
| 244 | |
| 245 | m_lsdb.expressInterest(interest, 0); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | SyncLogicHandler::publishRoutingUpdate(SequencingManager& manager, const ndn::Name& updatePrefix) |
| 250 | { |
| 251 | manager.writeSeqNoToFile(); |
| 252 | publishSyncUpdate(updatePrefix, manager.getCombinedSeqNo()); |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo) |
| 257 | { |
| 258 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo); |
| 259 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 260 | ndn::Name updateName(updatePrefix); |
| 261 | string data("NoData"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 262 | |
| 263 | m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), 1000, seqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | }//namespace nlsr |