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 <string> |
| 24 | #include <utility> |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 25 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | #include "lsdb.hpp" |
| 27 | #include "nlsr.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 28 | #include "conf-parameter.hpp" |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 29 | #include "utility/name-helper.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 30 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
| 32 | namespace nlsr { |
| 33 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | INIT_LOGGER("Lsdb"); |
| 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
| 38 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 39 | Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 41 | m_nlsr.getScheduler().cancelEvent(eid); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 45 | nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | { |
| 47 | return nlsa1.getKey() == key; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 52 | Lsdb::buildAndInstallOwnNameLsa() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 53 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 54 | NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(), |
| 55 | "name", |
| 56 | m_nlsr.getSequencingManager().getNameLsaSeq() + 1, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 57 | getLsaExpirationTimePoint(), |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 58 | m_nlsr.getNamePrefixList()); |
| 59 | m_nlsr.getSequencingManager().increaseNameLsaSeq(); |
| 60 | return installNameLsa(nameLsa); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | } |
| 62 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 63 | NameLsa* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 64 | Lsdb::findNameLsa(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | { |
| 66 | std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(), |
| 67 | m_nameLsdb.end(), |
| 68 | bind(nameLsaCompareByKey, _1, key)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | if (it != m_nameLsdb.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 70 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 72 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 76 | Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 78 | NameLsa* nameLsaCheck = findNameLsa(key); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | if (nameLsaCheck != 0) { |
| 80 | if (nameLsaCheck->getLsSeqNo() < seqNo) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | return true; |
| 82 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 83 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | ndn::EventId |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 91 | Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo, |
| 92 | const ndn::time::seconds& expTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 94 | return m_nlsr.getScheduler().scheduleEvent(expTime, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 95 | ndn::bind(&Lsdb::exprireOrRefreshNameLsa, |
| 96 | this, key, seqNo)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 100 | Lsdb::installNameLsa(NameLsa& nlsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 102 | ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 103 | NameLsa* chkNameLsa = findNameLsa(nlsa.getKey()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 104 | if (chkNameLsa == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 105 | addNameLsa(nlsa); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 106 | _LOG_DEBUG("New Name LSA. Adding to LSDB"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | nlsa.writeLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 108 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 109 | printNameLsdb(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 110 | if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 111 | m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(), |
| 112 | nlsa.getOrigRouter()); |
| 113 | std::list<ndn::Name> nameList = nlsa.getNpl().getNameList(); |
| 114 | for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 115 | it++) { |
| 116 | if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 117 | m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 121 | if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 122 | ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() - |
| 123 | ndn::time::system_clock::now(); |
| 124 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 126 | nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(), |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | nlsa.getLsSeqNo(), |
| 128 | timeToExpire)); |
| 129 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 130 | else { |
| 131 | if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 132 | _LOG_DEBUG("Updated Name LSA. Updating LSDB"); |
| 133 | _LOG_DEBUG("Old Name LSA "); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 134 | chkNameLsa->writeLog(); |
| 135 | chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo()); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 136 | chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint()); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 137 | chkNameLsa->getNpl().sort(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | nlsa.getNpl().sort(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 139 | std::list<ndn::Name> nameToAdd; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 140 | std::set_difference(nlsa.getNpl().getNameList().begin(), |
| 141 | nlsa.getNpl().getNameList().end(), |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 142 | chkNameLsa->getNpl().getNameList().begin(), |
| 143 | chkNameLsa->getNpl().getNameList().end(), |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 144 | std::inserter(nameToAdd, nameToAdd.begin())); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 145 | for (std::list<ndn::Name>::iterator it = nameToAdd.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 146 | it != nameToAdd.end(); ++it) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 147 | chkNameLsa->addName((*it)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 148 | if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
| 149 | if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 150 | m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 154 | std::list<ndn::Name> nameToRemove; |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 155 | std::set_difference(chkNameLsa->getNpl().getNameList().begin(), |
| 156 | chkNameLsa->getNpl().getNameList().end(), |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | nlsa.getNpl().getNameList().begin(), |
| 158 | nlsa.getNpl().getNameList().end(), |
| 159 | std::inserter(nameToRemove, nameToRemove.begin())); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 160 | for (std::list<ndn::Name>::iterator it = nameToRemove.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 161 | it != nameToRemove.end(); ++it) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 162 | chkNameLsa->removeName((*it)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 163 | if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
| 164 | if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 165 | m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 169 | if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 170 | ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() - |
| 171 | ndn::time::system_clock::now(); |
| 172 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 173 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 174 | cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId()); |
| 175 | chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(), |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 176 | nlsa.getLsSeqNo(), |
| 177 | timeToExpire)); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 178 | _LOG_DEBUG("Updated Name LSA"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 179 | chkNameLsa->writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | bool |
| 186 | Lsdb::addNameLsa(NameLsa& nlsa) |
| 187 | { |
| 188 | std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(), |
| 189 | m_nameLsdb.end(), |
| 190 | bind(nameLsaCompareByKey, _1, |
| 191 | nlsa.getKey())); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 192 | if (it == m_nameLsdb.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 193 | m_nameLsdb.push_back(nlsa); |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 200 | Lsdb::removeNameLsa(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 201 | { |
| 202 | std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(), |
| 203 | m_nameLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 204 | ndn::bind(nameLsaCompareByKey, _1, key)); |
| 205 | if (it != m_nameLsdb.end()) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 206 | _LOG_DEBUG("Removing Name LSA"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 207 | (*it).writeLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 208 | if ((*it).getOrigRouter() != |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 209 | m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 210 | m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(), |
| 211 | (*it).getOrigRouter()); |
| 212 | for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 213 | nit != (*it).getNpl().getNameList().end(); ++nit) { |
| 214 | if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 215 | m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | m_nameLsdb.erase(it); |
| 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 226 | Lsdb::doesNameLsaExist(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | { |
| 228 | std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(), |
| 229 | m_nameLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 230 | ndn::bind(nameLsaCompareByKey, _1, key)); |
| 231 | if (it == m_nameLsdb.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | Lsdb::printNameLsdb() |
| 239 | { |
| 240 | cout << "---------------Name LSDB-------------------" << endl; |
| 241 | for (std::list<NameLsa>::iterator it = m_nameLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 242 | it != m_nameLsdb.end() ; it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 243 | cout << (*it) << endl; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Cor LSA and LSDB related Functions start here |
| 248 | |
| 249 | |
| 250 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 251 | corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 252 | { |
| 253 | return clsa.getKey() == key; |
| 254 | } |
| 255 | |
| 256 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 257 | Lsdb::buildAndInstallOwnCoordinateLsa() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 258 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 259 | CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(), |
| 260 | "coordinate", |
| 261 | m_nlsr.getSequencingManager().getCorLsaSeq() + 1, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 262 | getLsaExpirationTimePoint(), |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 263 | m_nlsr.getConfParameter().getCorR(), |
| 264 | m_nlsr.getConfParameter().getCorTheta()); |
| 265 | m_nlsr.getSequencingManager().increaseCorLsaSeq(); |
| 266 | installCoordinateLsa(corLsa); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 270 | CoordinateLsa* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 271 | Lsdb::findCoordinateLsa(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 272 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 273 | std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(), |
| 274 | m_corLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 275 | ndn::bind(corLsaCompareByKey, _1, key)); |
| 276 | if (it != m_corLsdb.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 277 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 278 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 279 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 283 | Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 284 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 285 | CoordinateLsa* clsa = findCoordinateLsa(key); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 286 | if (clsa != 0) { |
| 287 | if (clsa->getLsSeqNo() < seqNo) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 288 | return true; |
| 289 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 290 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | } |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | ndn::EventId |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 298 | Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 299 | const ndn::time::seconds& expTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 300 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 301 | return m_nlsr.getScheduler().scheduleEvent(expTime, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 302 | ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa, |
| 303 | this, key, seqNo)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 307 | Lsdb::installCoordinateLsa(CoordinateLsa& clsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 308 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 309 | ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 310 | CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 311 | if (chkCorLsa == 0) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 312 | _LOG_DEBUG("New Coordinate LSA. Adding to LSDB"); |
| 313 | clsa.writeLog(); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 314 | addCoordinateLsa(clsa); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 315 | //debugging purpose |
| 316 | printCorLsdb(); |
| 317 | if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 318 | m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(), |
| 319 | clsa.getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 320 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 321 | if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 322 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 323 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 324 | if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 325 | ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() - |
| 326 | ndn::time::system_clock::now(); |
| 327 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 328 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 329 | scheduleCoordinateLsaExpiration(clsa.getKey(), |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 330 | clsa.getLsSeqNo(), timeToExpire); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 331 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 332 | else { |
| 333 | if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 334 | _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB"); |
| 335 | _LOG_DEBUG("Old Coordinate LSA"); |
| 336 | chkCorLsa->writeLog(); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 337 | chkCorLsa->setLsSeqNo(clsa.getLsSeqNo()); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 338 | chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 339 | if (!chkCorLsa->isEqualContent(clsa)) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 340 | chkCorLsa->setCorRadius(clsa.getCorRadius()); |
| 341 | chkCorLsa->setCorTheta(clsa.getCorTheta()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 342 | if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 343 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 344 | } |
| 345 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 346 | if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 347 | ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() - |
| 348 | ndn::time::system_clock::now(); |
| 349 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 350 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 351 | cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId()); |
| 352 | chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(), |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 353 | clsa.getLsSeqNo(), |
| 354 | timeToExpire)); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 355 | _LOG_DEBUG("Updated Coordinate LSA"); |
| 356 | chkCorLsa->writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | bool |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 363 | Lsdb::addCoordinateLsa(CoordinateLsa& clsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 364 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 365 | std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(), |
| 366 | m_corLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 367 | ndn::bind(corLsaCompareByKey, _1, |
| 368 | clsa.getKey())); |
| 369 | if (it == m_corLsdb.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 370 | m_corLsdb.push_back(clsa); |
| 371 | return true; |
| 372 | } |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 377 | Lsdb::removeCoordinateLsa(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 378 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 379 | std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(), |
| 380 | m_corLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 381 | ndn::bind(corLsaCompareByKey, |
| 382 | _1, key)); |
| 383 | if (it != m_corLsdb.end()) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 384 | _LOG_DEBUG("Removing Coordinate LSA"); |
| 385 | (*it).writeLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 386 | if ((*it).getOrigRouter() != |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 387 | m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 388 | m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(), |
| 389 | (*it).getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 390 | } |
| 391 | m_corLsdb.erase(it); |
| 392 | return true; |
| 393 | } |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 398 | Lsdb::doesCoordinateLsaExist(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 399 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 400 | std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(), |
| 401 | m_corLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 402 | ndn::bind(corLsaCompareByKey, |
| 403 | _1, key)); |
| 404 | if (it == m_corLsdb.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | return true; |
| 408 | } |
| 409 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 410 | //debugging |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 411 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 412 | Lsdb::printCorLsdb() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 413 | { |
| 414 | cout << "---------------Cor LSDB-------------------" << endl; |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 415 | for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 416 | it != m_corLsdb.end() ; it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 417 | cout << (*it) << endl; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | // Adj LSA and LSDB related function starts here |
| 423 | |
| 424 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 425 | adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 426 | { |
| 427 | return alsa.getKey() == key; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 432 | Lsdb::scheduledAdjLsaBuild() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 433 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 434 | std::cout << "scheduledAdjLsaBuild Called" << endl; |
| 435 | _LOG_DEBUG("scheduledAdjLsaBuild Called"); |
| 436 | m_nlsr.setIsBuildAdjLsaSheduled(false); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 437 | if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 438 | int adjBuildCount = m_nlsr.getAdjBuildCount(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 439 | if (adjBuildCount > 0) { |
| 440 | if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 441 | _LOG_DEBUG("Building and installing Adj LSA"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 442 | buildAndInstallOwnAdjLsa(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 443 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 444 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 445 | ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix(); |
| 446 | key.append("adjacency"); |
| 447 | removeAdjLsa(key); |
| 448 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 449 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 450 | m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 451 | } |
| 452 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 453 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 454 | m_nlsr.setIsBuildAdjLsaSheduled(true); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 455 | int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() * |
| 456 | m_nlsr.getConfParameter().getInterestResendTime(); |
| 457 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime), |
| 458 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, |
| 459 | this)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | bool |
| 465 | Lsdb::addAdjLsa(AdjLsa& alsa) |
| 466 | { |
| 467 | std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(), |
| 468 | m_adjLsdb.end(), |
| 469 | bind(adjLsaCompareByKey, _1, |
| 470 | alsa.getKey())); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 471 | if (it == m_adjLsdb.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 472 | m_adjLsdb.push_back(alsa); |
| 473 | return true; |
| 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 478 | AdjLsa* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 479 | Lsdb::findAdjLsa(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 480 | { |
| 481 | std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(), |
| 482 | m_adjLsdb.end(), |
| 483 | bind(adjLsaCompareByKey, _1, key)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 484 | if (it != m_adjLsdb.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 485 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 486 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 487 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
| 491 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 492 | Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 493 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 494 | AdjLsa* adjLsaCheck = findAdjLsa(key); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 495 | if (adjLsaCheck != 0) { |
| 496 | if (adjLsaCheck->getLsSeqNo() < seqNo) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 497 | return true; |
| 498 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 499 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | ndn::EventId |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 508 | Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo, |
| 509 | const ndn::time::seconds& expTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 510 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 511 | return m_nlsr.getScheduler().scheduleEvent(expTime, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 512 | ndn::bind(&Lsdb::exprireOrRefreshAdjLsa, |
| 513 | this, key, seqNo)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 517 | Lsdb::installAdjLsa(AdjLsa& alsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 518 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 519 | ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 520 | AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 521 | if (chkAdjLsa == 0) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 522 | _LOG_DEBUG("New Adj LSA. Adding to LSDB"); |
| 523 | alsa.writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 524 | addAdjLsa(alsa); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 525 | alsa.addNptEntries(m_nlsr); |
| 526 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 527 | if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 528 | ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() - |
| 529 | ndn::time::system_clock::now(); |
| 530 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 531 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 532 | scheduleAdjLsaExpiration(alsa.getKey(), |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 533 | alsa.getLsSeqNo(), timeToExpire); |
| 534 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 535 | else { |
| 536 | if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 537 | _LOG_DEBUG("Updated Adj LSA. Updating LSDB"); |
| 538 | _LOG_DEBUG("Old Adj LSA"); |
| 539 | chkAdjLsa->writeLog(); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 540 | chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo()); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 541 | chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 542 | if (!chkAdjLsa->isEqualContent(alsa)) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 543 | chkAdjLsa->getAdl().reset(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 544 | chkAdjLsa->getAdl().addAdjacents(alsa.getAdl()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 545 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 546 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 547 | if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 548 | ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() - |
| 549 | ndn::time::system_clock::now(); |
| 550 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 551 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 552 | cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId()); |
| 553 | chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(), |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 554 | alsa.getLsSeqNo(), |
| 555 | timeToExpire)); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 556 | _LOG_DEBUG("Updated Adj LSA"); |
| 557 | chkAdjLsa->writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 564 | Lsdb::buildAndInstallOwnAdjLsa() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 565 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 566 | AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(), |
| 567 | "adjacency", |
| 568 | m_nlsr.getSequencingManager().getAdjLsaSeq() + 1, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 569 | getLsaExpirationTimePoint(), |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 570 | m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(), |
| 571 | m_nlsr.getAdjacencyList()); |
| 572 | m_nlsr.getSequencingManager().increaseAdjLsaSeq(); |
| 573 | // publish routing update |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 574 | ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 575 | lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 576 | m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(), |
| 577 | lsaPrefix); |
| 578 | return installAdjLsa(adjLsa); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 582 | Lsdb::removeAdjLsa(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 583 | { |
| 584 | std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(), |
| 585 | m_adjLsdb.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 586 | ndn::bind(adjLsaCompareByKey, _1, key)); |
| 587 | if (it != m_adjLsdb.end()) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 588 | _LOG_DEBUG("Removing Adj LSA"); |
| 589 | (*it).writeLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 590 | (*it).removeNptEntries(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 591 | m_adjLsdb.erase(it); |
| 592 | return true; |
| 593 | } |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 598 | Lsdb::doesAdjLsaExist(const ndn::Name& key) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 599 | { |
| 600 | std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(), |
| 601 | m_adjLsdb.end(), |
| 602 | bind(adjLsaCompareByKey, _1, key)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 603 | if (it == m_adjLsdb.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | return true; |
| 607 | } |
| 608 | |
| 609 | std::list<AdjLsa>& |
| 610 | Lsdb::getAdjLsdb() |
| 611 | { |
| 612 | return m_adjLsdb; |
| 613 | } |
| 614 | |
| 615 | void |
| 616 | Lsdb::setLsaRefreshTime(int lrt) |
| 617 | { |
| 618 | m_lsaRefreshTime = lrt; |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | Lsdb::setThisRouterPrefix(string trp) |
| 623 | { |
| 624 | m_thisRouterPrefix = trp; |
| 625 | } |
| 626 | |
| 627 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 628 | Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 629 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 630 | std::cout << "Lsdb::exprireOrRefreshNameLsa Called " << std::endl; |
| 631 | std::cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << std::endl; |
| 632 | _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called"); |
| 633 | _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 634 | NameLsa* chkNameLsa = findNameLsa(lsaKey); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 635 | if (chkNameLsa != 0) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 636 | std::cout << "LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << std::endl; |
| 637 | _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 638 | if (chkNameLsa->getLsSeqNo() == seqNo) { |
| 639 | if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 640 | chkNameLsa->writeLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 641 | std::cout << "Own Name LSA, so refreshing name LSA" << std::endl; |
| 642 | _LOG_DEBUG("Own Name LSA, so refreshing name LSA"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 643 | chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 644 | m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo()); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 645 | chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint()); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 646 | chkNameLsa->writeLog(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 647 | // schedule refreshing event again |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 648 | chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 649 | chkNameLsa->getLsSeqNo(), |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 650 | ndn::time::seconds(m_lsaRefreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 651 | // publish routing update |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 652 | ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 653 | lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 654 | m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(), |
| 655 | lsaPrefix); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 656 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 657 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 658 | std::cout << "Other's Name LSA, so removing form LSDB" << std::endl; |
| 659 | _LOG_DEBUG("Other's Name LSA, so removing form LSDB"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 660 | removeNameLsa(lsaKey); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 667 | Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 668 | { |
| 669 | cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl; |
| 670 | cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 671 | _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called"); |
| 672 | _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 673 | AdjLsa* chkAdjLsa = findAdjLsa(lsaKey); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 674 | if (chkAdjLsa != 0) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 675 | cout << "LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl; |
| 676 | _LOG_DEBUG("LSA Exists with seq no: "); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 677 | if (chkAdjLsa->getLsSeqNo() == seqNo) { |
| 678 | if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 679 | cout << "Own Adj LSA, so refreshing Adj LSA" << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 680 | _LOG_DEBUG("Own Adj LSA, so refreshing Adj LSA"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 681 | chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 682 | m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo()); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 683 | chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 684 | // schedule refreshing event again |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 685 | chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 686 | chkAdjLsa->getLsSeqNo(), |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 687 | ndn::time::seconds(m_lsaRefreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 688 | // publish routing update |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 689 | ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 690 | lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 691 | m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(), |
| 692 | lsaPrefix); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 693 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 694 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 695 | cout << "Other's Adj LSA, so removing form LSDB" << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 696 | _LOG_DEBUG("Other's Adj LSA, so removing form LSDB"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 697 | removeAdjLsa(lsaKey); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 698 | } |
| 699 | // schedule Routing table calculaiton |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 700 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 706 | Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey, |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 707 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 708 | { |
| 709 | cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl; |
| 710 | cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 711 | _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called "); |
| 712 | _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 713 | CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 714 | if (chkCorLsa != 0) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 715 | cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 716 | _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 717 | if (chkCorLsa->getLsSeqNo() == seqNo) { |
| 718 | if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 719 | cout << "Own Cor LSA, so refreshing Cor LSA" << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 720 | _LOG_DEBUG("Own Cor LSA, so refreshing Cor LSA"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 721 | chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 722 | m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo()); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 723 | chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 724 | // schedule refreshing event again |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 725 | chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration( |
| 726 | chkCorLsa->getKey(), |
| 727 | chkCorLsa->getLsSeqNo(), |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 728 | ndn::time::seconds(m_lsaRefreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 729 | // publish routing update |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 730 | ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 731 | lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 732 | m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(), |
| 733 | lsaPrefix); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 734 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 735 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 736 | cout << "Other's Cor LSA, so removing form LSDB" << endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 737 | _LOG_DEBUG("Other's Cor LSA, so removing form LSDB"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 738 | removeCoordinateLsa(lsaKey); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 739 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 740 | if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 741 | m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | |
| 748 | void |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 749 | Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime, |
| 750 | uint32_t timeoutCount) |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 751 | { |
| 752 | std::cout << "Expressing Interest :" << interestName << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 753 | _LOG_DEBUG("Expressing Interest for LSA(name): " << interestName); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 754 | ndn::Interest interest(interestName); |
| 755 | interest.setInterestLifetime(ndn::time::seconds(interestLifeTime)); |
| 756 | interest.setMustBeFresh(true); |
| 757 | m_nlsr.getNlsrFace().expressInterest(interest, |
| 758 | ndn::bind(&Lsdb::processContent, |
| 759 | this, _1, _2), |
| 760 | ndn::bind(&Lsdb::processInterestTimedOut, |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 761 | this, _1, timeoutCount)); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void |
| 765 | Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 766 | { |
| 767 | const ndn::Name& intName(interest.getName()); |
| 768 | std::cout << "Interest recevied for LSA: " << intName << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 769 | _LOG_DEBUG("Interest recevied for LSA(name): " << intName); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 770 | string chkString("LSA"); |
| 771 | int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), |
| 772 | chkString); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 773 | if (lsaPosition >= 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 774 | std::string interestedLsType; |
| 775 | uint64_t interestedLsSeqNo; |
| 776 | ndn::Name origRouter = intName.getSubName(lsaPosition + 1, |
| 777 | interest.getName().size() - lsaPosition - 3); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 778 | interestedLsType = intName[-2].toUri(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 779 | interestedLsSeqNo = intName[-1].toNumber(); |
| 780 | std::cout << "Router Name: " << origRouter << std::endl; |
| 781 | std::cout << "Ls Type : " << interestedLsType << std::endl; |
| 782 | std::cout << "Ls Seq : " << interestedLsSeqNo << endl; |
| 783 | std::cout << "Ls Type: " << interestedLsType << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 784 | if (interestedLsType == "name") { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 785 | processInterestForNameLsa(interest, |
| 786 | origRouter.append(interestedLsType), |
| 787 | interestedLsSeqNo); |
| 788 | return; |
| 789 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 790 | else if (interestedLsType == "adjacency") { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 791 | processInterestForAdjacencyLsa(interest, |
| 792 | origRouter.append(interestedLsType), |
| 793 | interestedLsSeqNo); |
| 794 | return; |
| 795 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 796 | else if (interestedLsType == "coordinate") { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 797 | processInterestForCoordinateLsa(interest, |
| 798 | origRouter.append(interestedLsType), |
| 799 | interestedLsSeqNo); |
| 800 | return; |
| 801 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 802 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 803 | cout << "Unrecognized LSA Type :(" << endl; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void |
| 809 | Lsdb::processInterestForNameLsa(const ndn::Interest& interest, |
| 810 | const ndn::Name& lsaKey, |
| 811 | uint32_t interestedlsSeqNo) |
| 812 | { |
| 813 | NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 814 | if (nameLsa != 0) { |
| 815 | if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 816 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 817 | _LOG_DEBUG("Sending data for LSA(name): " << interest.getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 818 | data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec |
| 819 | std::string content = nameLsa->getData(); |
| 820 | data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), |
| 821 | content.size()); |
| 822 | m_keyChain.sign(data); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 823 | //std::cout << ">> D: " << data << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 824 | m_nlsr.getNlsrFace().put(data); |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void |
| 830 | Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest, |
| 831 | const ndn::Name& lsaKey, |
| 832 | uint32_t interestedlsSeqNo) |
| 833 | { |
| 834 | AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 835 | if (adjLsa != 0) { |
| 836 | if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 837 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 838 | _LOG_DEBUG("Sending data for LSA(name): " << interest.getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 839 | data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec |
| 840 | std::string content = adjLsa->getData(); |
| 841 | data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), |
| 842 | content.size()); |
| 843 | m_keyChain.sign(data); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 844 | //std::cout << ">> D: " << data << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 845 | m_nlsr.getNlsrFace().put(data); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void |
| 851 | Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest, |
| 852 | const ndn::Name& lsaKey, |
| 853 | uint32_t interestedlsSeqNo) |
| 854 | { |
| 855 | CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 856 | if (corLsa != 0) { |
| 857 | if (corLsa->getLsSeqNo() >= interestedlsSeqNo) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 858 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 859 | _LOG_DEBUG("Sending data for LSA(name): " << interest.getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 860 | data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec |
| 861 | std::string content = corLsa->getData(); |
| 862 | data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), |
| 863 | content.size()); |
| 864 | m_keyChain.sign(data); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 865 | //std::cout << ">> D: " << data << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 866 | m_nlsr.getNlsrFace().put(data); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | void |
| 872 | Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data) |
| 873 | { |
| 874 | const ndn::Name& dataName = data.getName(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 875 | std::cout << "Data received for LSA(name): " << dataName << std::endl; |
| 876 | _LOG_DEBUG("Data received for LSA(name): " << dataName); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 877 | string dataContent(reinterpret_cast<const char*>(data.getContent().value())); |
| 878 | string chkString("LSA"); |
| 879 | int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 880 | if (lsaPosition >= 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 881 | std::string interestedLsType; |
| 882 | uint64_t interestedLsSeqNo; |
| 883 | ndn::Name origRouter = dataName.getSubName(lsaPosition + 1, |
| 884 | dataName.size() - lsaPosition - 4); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 885 | interestedLsType = dataName[-3].toUri(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 886 | interestedLsSeqNo = dataName[-2].toNumber(); |
| 887 | std::cout << "Ls Type : " << interestedLsType << std::endl; |
| 888 | std::cout << "Ls Seq : " << interestedLsSeqNo << std::endl; |
| 889 | std::cout << "Ls Type: " << interestedLsType << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 890 | if (interestedLsType == "name") { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 891 | processContentNameLsa(origRouter.append(interestedLsType), |
| 892 | interestedLsSeqNo, dataContent); |
| 893 | return; |
| 894 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 895 | else if (interestedLsType == "adjacency") { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 896 | processContentAdjacencyLsa(origRouter.append(interestedLsType), |
| 897 | interestedLsSeqNo, dataContent); |
| 898 | return; |
| 899 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 900 | else if (interestedLsType == "coordinate") { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 901 | processContentCoordinateLsa(origRouter.append(interestedLsType), |
| 902 | interestedLsSeqNo, dataContent); |
| 903 | return; |
| 904 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 905 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 906 | cout << "Unrecognized LSA Type :(" << endl; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | void |
| 912 | Lsdb::processContentNameLsa(const ndn::Name& lsaKey, |
| 913 | uint32_t lsSeqNo, std::string& dataContent) |
| 914 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 915 | if (isNameLsaNew(lsaKey, lsSeqNo)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 916 | NameLsa nameLsa; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 917 | if (nameLsa.initializeFromContent(dataContent)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 918 | installNameLsa(nameLsa); |
| 919 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 920 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 921 | std::cout << "LSA data decoding error :(" << std::endl; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | void |
| 927 | Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey, |
| 928 | uint32_t lsSeqNo, std::string& dataContent) |
| 929 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 930 | if (isAdjLsaNew(lsaKey, lsSeqNo)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 931 | AdjLsa adjLsa; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 932 | if (adjLsa.initializeFromContent(dataContent)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 933 | installAdjLsa(adjLsa); |
| 934 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 935 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 936 | std::cout << "LSA data decoding error :(" << std::endl; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | void |
| 942 | Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey, |
| 943 | uint32_t lsSeqNo, std::string& dataContent) |
| 944 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 945 | if (isCoordinateLsaNew(lsaKey, lsSeqNo)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 946 | CoordinateLsa corLsa; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 947 | if (corLsa.initializeFromContent(dataContent)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 948 | installCoordinateLsa(corLsa); |
| 949 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 950 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 951 | std::cout << "LSA data decoding error :(" << std::endl; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | void |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 957 | Lsdb::processInterestTimedOut(const ndn::Interest& interest, uint32_t timeoutCount) |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 958 | { |
| 959 | const ndn::Name& interestName(interest.getName()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 960 | _LOG_DEBUG("Interest timed out for LSA(name): " << interestName); |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 961 | if ((timeoutCount + 1) <= m_nlsr.getConfParameter().getInterestRetryNumber()) { |
| 962 | _LOG_DEBUG("Interest timeoutCount: " << (timeoutCount + 1)); |
| 963 | _LOG_DEBUG("Need to express interest again for LSA(name): " << interestName); |
| 964 | expressInterest(interestName, |
| 965 | m_nlsr.getConfParameter().getInterestResendTime(), |
| 966 | timeoutCount + 1); |
| 967 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 968 | } |
| 969 | |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 970 | ndn::time::system_clock::TimePoint |
| 971 | Lsdb::getLsaExpirationTimePoint() |
| 972 | { |
| 973 | ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now(); |
| 974 | expirationTimePoint = expirationTimePoint + |
| 975 | ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval()); |
| 976 | return expirationTimePoint; |
| 977 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 978 | |
| 979 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 980 | Lsdb::printAdjLsdb() |
| 981 | { |
| 982 | cout << "---------------Adj LSDB-------------------" << endl; |
| 983 | for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 984 | it != m_adjLsdb.end() ; it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 985 | cout << (*it) << endl; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | //-----utility function ----- |
| 990 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 991 | Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 992 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 993 | if (lsType == "name") { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 994 | return doesNameLsaExist(key); |
| 995 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 996 | else if (lsType == "adjacency") { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 997 | return doesAdjLsaExist(key); |
| 998 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 999 | else if (lsType == "coordinate") { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 1000 | return doesCoordinateLsaExist(key); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1001 | } |
| 1002 | return false; |
| 1003 | } |
| 1004 | |
| 1005 | }//namespace nlsr |
| 1006 | |