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 | |
| 32 | #include <boost/serialization/shared_ptr.hpp> |
| 33 | |
| 34 | using namespace boost::serialization; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | |
| 36 | namespace nlsr { |
| 37 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 38 | INIT_LOGGER("SyncLogicHandler"); |
| 39 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 40 | using namespace ndn; |
| 41 | using namespace std; |
| 42 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 43 | class SyncUpdate |
| 44 | { |
| 45 | public: |
| 46 | class Error : public std::runtime_error |
| 47 | { |
| 48 | public: |
| 49 | explicit |
| 50 | Error(const std::string& what) |
| 51 | : std::runtime_error(what) |
| 52 | { |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | public: |
| 57 | SyncUpdate(const ndn::Name& name, uint64_t seqNo) |
| 58 | : m_name(name) |
| 59 | , m_seqManager(seqNo) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | const ndn::Name& |
| 64 | getName() const |
| 65 | { |
| 66 | return m_name; |
| 67 | } |
| 68 | |
| 69 | const ndn::Name |
| 70 | getOriginRouter() const |
| 71 | { |
| 72 | int32_t nlsrPosition = util::getNameComponentPosition(m_name, NLSR_COMPONENT); |
| 73 | int32_t lsaPosition = util::getNameComponentPosition(m_name, LSA_COMPONENT); |
| 74 | |
| 75 | if (nlsrPosition < 0 || lsaPosition < 0) { |
| 76 | throw Error("Cannot parse update name because expected components are missing"); |
| 77 | } |
| 78 | |
| 79 | ndn::Name networkName = m_name.getSubName(0, nlsrPosition); |
| 80 | ndn::Name routerName = m_name.getSubName(lsaPosition + 1); |
| 81 | |
| 82 | ndn::Name originRouter = networkName; |
| 83 | originRouter.append(routerName); |
| 84 | |
| 85 | return originRouter; |
| 86 | } |
| 87 | |
| 88 | uint64_t |
| 89 | getNameLsaSeqNo() const |
| 90 | { |
| 91 | return m_seqManager.getNameLsaSeq(); |
| 92 | } |
| 93 | |
| 94 | uint64_t |
| 95 | getAdjLsaSeqNo() const |
| 96 | { |
| 97 | return m_seqManager.getAdjLsaSeq(); |
| 98 | } |
| 99 | |
| 100 | uint64_t |
| 101 | getCorLsaSeqNo() const |
| 102 | { |
| 103 | return m_seqManager.getCorLsaSeq(); |
| 104 | } |
| 105 | |
| 106 | const SequencingManager& |
| 107 | getSequencingManager() const |
| 108 | { |
| 109 | return m_seqManager; |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | const ndn::Name m_name; |
| 114 | SequencingManager m_seqManager; |
| 115 | |
| 116 | static const std::string NLSR_COMPONENT; |
| 117 | static const std::string LSA_COMPONENT; |
| 118 | }; |
| 119 | |
| 120 | const std::string SyncUpdate::NLSR_COMPONENT = "NLSR"; |
| 121 | const std::string SyncUpdate::LSA_COMPONENT = "LSA"; |
| 122 | |
| 123 | const std::string SyncLogicHandler::NAME_COMPONENT = "name"; |
| 124 | const std::string SyncLogicHandler::ADJACENCY_COMPONENT = "adjacency"; |
| 125 | const std::string SyncLogicHandler::COORDINATE_COMPONENT = "coordinate"; |
| 126 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 128 | SyncLogicHandler::createSyncSocket() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 130 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 131 | |
| 132 | // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory |
| 133 | // of the object |
| 134 | ndn::shared_ptr<ndn::Face> facePtr(&m_syncFace, null_deleter()); |
| 135 | |
| 136 | m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, facePtr, |
| 137 | ndn::bind(&SyncLogicHandler::onNsyncUpdate, |
| 138 | this, _1, _2), |
| 139 | ndn::bind(&SyncLogicHandler::onNsyncRemoval, |
| 140 | this, _1)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 144 | SyncLogicHandler::onNsyncUpdate(const vector<Sync::MissingDataInfo>& v, Sync::SyncSocket* socket) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 146 | _LOG_DEBUG("Received Nsync update event"); |
| 147 | |
| 148 | for (size_t i = 0; i < v.size(); i++){ |
| 149 | _LOG_DEBUG("Update Name: " << v[i].prefix << " Seq no: " << v[i].high.getSeq()); |
| 150 | |
| 151 | SyncUpdate update(v[i].prefix, v[i].high.getSeq()); |
| 152 | |
| 153 | processUpdateFromSync(update); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 158 | SyncLogicHandler::onNsyncRemoval(const string& prefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 160 | _LOG_DEBUG("Received Nsync removal event"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 164 | SyncLogicHandler::processUpdateFromSync(const SyncUpdate& update) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 165 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 166 | ndn::Name originRouter; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 168 | try { |
| 169 | originRouter = update.getOriginRouter(); |
| 170 | } |
| 171 | catch (std::exception& e) { |
| 172 | _LOG_WARN("Received malformed sync update"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 173 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 176 | // A router should not try to fetch its own LSA |
| 177 | if (originRouter != m_confParam.getRouterPrefix()) { |
| 178 | |
| 179 | update.getSequencingManager().writeLog(); |
| 180 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 181 | try { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 182 | if (isLsaNew(originRouter, NAME_COMPONENT, update.getNameLsaSeqNo())) { |
| 183 | _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB"); |
| 184 | |
| 185 | expressInterestForLsa(update, NAME_COMPONENT, update.getNameLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 186 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 187 | |
| 188 | if (isLsaNew(originRouter, ADJACENCY_COMPONENT, update.getAdjLsaSeqNo())) { |
| 189 | _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB"); |
| 190 | |
| 191 | expressInterestForLsa(update, ADJACENCY_COMPONENT, update.getAdjLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 192 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 193 | |
| 194 | if (isLsaNew(originRouter, COORDINATE_COMPONENT, update.getCorLsaSeqNo())) { |
| 195 | _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB"); |
| 196 | |
| 197 | expressInterestForLsa(update, COORDINATE_COMPONENT, update.getCorLsaSeqNo()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 198 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 199 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 200 | catch (std::exception& e) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 201 | std::cerr << e.what() << std::endl; |
| 202 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 207 | bool |
| 208 | SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType, |
| 209 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 210 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 211 | ndn::Name lsaKey = originRouter; |
| 212 | lsaKey.append(lsaType); |
| 213 | |
| 214 | if (lsaType == NAME_COMPONENT) |
| 215 | { |
| 216 | return m_lsdb.isNameLsaNew(lsaKey, seqNo); |
| 217 | } |
| 218 | else if (lsaType == ADJACENCY_COMPONENT) |
| 219 | { |
| 220 | return m_lsdb.isAdjLsaNew(lsaKey, seqNo); |
| 221 | } |
| 222 | else if (lsaType == COORDINATE_COMPONENT) |
| 223 | { |
| 224 | return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo); |
| 225 | } |
| 226 | |
| 227 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 228 | } |
| 229 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 231 | SyncLogicHandler::expressInterestForLsa(const SyncUpdate& update, std::string lsaType, |
| 232 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 233 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 234 | ndn::Name interest(update.getName()); |
| 235 | interest.append(lsaType); |
| 236 | interest.appendNumber(seqNo); |
| 237 | |
| 238 | m_lsdb.expressInterest(interest, 0); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | SyncLogicHandler::publishRoutingUpdate(SequencingManager& manager, const ndn::Name& updatePrefix) |
| 243 | { |
| 244 | manager.writeSeqNoToFile(); |
| 245 | publishSyncUpdate(updatePrefix, manager.getCombinedSeqNo()); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo) |
| 250 | { |
| 251 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo); |
| 252 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 253 | ndn::Name updateName(updatePrefix); |
| 254 | string data("NoData"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame^] | 255 | |
| 256 | m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), 1000, seqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | }//namespace nlsr |