blob: 212ae42a731deec463feea3f1e210ef475911ea6 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- 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 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <string>
24#include <utility>
akmhoque157b0a42014-05-13 00:26:37 -050025
akmhoque53353462014-04-22 08:43:45 -050026#include "lsdb.hpp"
27#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050028#include "conf-parameter.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050029#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050031
32namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("Lsdb");
35
Vince Lehman18841082014-08-19 17:15:24 -050036const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050037const steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE = steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050038
akmhoque53353462014-04-22 08:43:45 -050039using namespace std;
40
41void
akmhoque31d1d4b2014-05-05 22:08:14 -050042Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050043{
Vince Lehman7c603292014-09-11 17:48:16 -050044 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050045}
46
47static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050048nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050049{
50 return nlsa1.getKey() == key;
51}
52
53
54bool
akmhoque31d1d4b2014-05-05 22:08:14 -050055Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050056{
akmhoque31d1d4b2014-05-05 22:08:14 -050057 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
58 "name",
59 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -050060 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -050061 m_nlsr.getNamePrefixList());
62 m_nlsr.getSequencingManager().increaseNameLsaSeq();
63 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050064}
65
akmhoqueb6450b12014-04-24 00:01:03 -050066NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050067Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050068{
69 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
70 m_nameLsdb.end(),
71 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -050072 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -050073 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050074 }
akmhoqueb6450b12014-04-24 00:01:03 -050075 return 0;
akmhoque53353462014-04-22 08:43:45 -050076}
77
78bool
akmhoque31d1d4b2014-05-05 22:08:14 -050079Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050080{
akmhoqueb6450b12014-04-24 00:01:03 -050081 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -050082 if (nameLsaCheck != 0) {
83 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -050084 return true;
85 }
akmhoque157b0a42014-05-13 00:26:37 -050086 else {
akmhoque53353462014-04-22 08:43:45 -050087 return false;
88 }
89 }
90 return true;
91}
92
93ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -050094Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
95 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050096{
Vince Lehman7c603292014-09-11 17:48:16 -050097 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
98 ndn::bind(&Lsdb::exprireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050099}
100
101bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500102Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500103{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700104 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500105 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500106 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500107 addNameLsa(nlsa);
akmhoque2f423352014-06-03 11:49:35 -0500108 _LOG_DEBUG("New Name LSA");
109 _LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500110 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500111
akmhoque157b0a42014-05-13 00:26:37 -0500112 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
114 nlsa.getOrigRouter());
115 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
116 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500117 it++) {
118 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500119 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500120 }
121 }
122 }
akmhoque157b0a42014-05-13 00:26:37 -0500123 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500124 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
125 ndn::time::system_clock::now();
126 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500127 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500129 nlsa.getLsSeqNo(),
130 timeToExpire));
131 }
akmhoque157b0a42014-05-13 00:26:37 -0500132 else {
133 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500134 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500135 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500136 chkNameLsa->writeLog();
137 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500138 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500139 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500140 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500141 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500142 std::set_difference(nlsa.getNpl().getNameList().begin(),
143 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500144 chkNameLsa->getNpl().getNameList().begin(),
145 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500146 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500148 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500149 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500150 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
151 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500152 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500153 }
154 }
155 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500156
157 chkNameLsa->getNpl().sort();
158
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500160 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
161 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500162 nlsa.getNpl().getNameList().begin(),
163 nlsa.getNpl().getNameList().end(),
164 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500166 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500167 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500168 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
169 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500170 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500171 }
172 }
173 }
akmhoque157b0a42014-05-13 00:26:37 -0500174 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500175 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
176 ndn::time::system_clock::now();
177 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500178 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500179 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
180 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500181 nlsa.getLsSeqNo(),
182 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500183 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500184 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500185 }
186 }
187 return true;
188}
189
190bool
191Lsdb::addNameLsa(NameLsa& nlsa)
192{
193 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
194 m_nameLsdb.end(),
195 bind(nameLsaCompareByKey, _1,
196 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500197 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500198 m_nameLsdb.push_back(nlsa);
199 return true;
200 }
201 return false;
202}
203
204bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500205Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500206{
207 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
208 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500209 ndn::bind(nameLsaCompareByKey, _1, key));
210 if (it != m_nameLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500211 _LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500212 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500213 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500214 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500215 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
216 (*it).getOrigRouter());
217 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500218 nit != (*it).getNpl().getNameList().end(); ++nit) {
219 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500220 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500221 }
222 }
223 }
224 m_nameLsdb.erase(it);
225 return true;
226 }
227 return false;
228}
229
230bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500231Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500232{
233 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
234 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500235 ndn::bind(nameLsaCompareByKey, _1, key));
236 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500237 return false;
238 }
239 return true;
240}
241
242void
akmhoque2f423352014-06-03 11:49:35 -0500243Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500244{
akmhoque2f423352014-06-03 11:49:35 -0500245 _LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500246 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500247 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500248 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500249 }
250}
251
252// Cor LSA and LSDB related Functions start here
253
akmhoque53353462014-04-22 08:43:45 -0500254static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500255corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500256{
257 return clsa.getKey() == key;
258}
259
260bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500261Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500262{
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
264 "coordinate",
265 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500266 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500267 m_nlsr.getConfParameter().getCorR(),
268 m_nlsr.getConfParameter().getCorTheta());
269 m_nlsr.getSequencingManager().increaseCorLsaSeq();
270 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500271 return true;
272}
273
akmhoqueb6450b12014-04-24 00:01:03 -0500274CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500275Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500276{
akmhoqueb6450b12014-04-24 00:01:03 -0500277 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
278 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500279 ndn::bind(corLsaCompareByKey, _1, key));
280 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500281 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500282 }
akmhoqueb6450b12014-04-24 00:01:03 -0500283 return 0;
akmhoque53353462014-04-22 08:43:45 -0500284}
285
286bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500287Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500288{
akmhoqueb6450b12014-04-24 00:01:03 -0500289 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500290 if (clsa != 0) {
291 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500292 return true;
293 }
akmhoque157b0a42014-05-13 00:26:37 -0500294 else {
akmhoque53353462014-04-22 08:43:45 -0500295 return false;
296 }
297 }
298 return true;
299}
300
301ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500302Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500303 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500304{
Vince Lehman7c603292014-09-11 17:48:16 -0500305 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
306 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
307 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500308}
309
310bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500311Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500312{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700313 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500314 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500315 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500316 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500317 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500318 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500319 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500320
akmhoque157b0a42014-05-13 00:26:37 -0500321 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500322 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
323 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500324 }
akmhoque157b0a42014-05-13 00:26:37 -0500325 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500326 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500327 }
akmhoque157b0a42014-05-13 00:26:37 -0500328 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500329 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
330 ndn::time::system_clock::now();
331 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500332 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500333 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500334 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500335 }
akmhoque157b0a42014-05-13 00:26:37 -0500336 else {
337 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500338 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500339 _LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500340 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500341 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500342 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500343 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500344 chkCorLsa->setCorRadius(clsa.getCorRadius());
345 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500346 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500347 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500348 }
349 }
akmhoque157b0a42014-05-13 00:26:37 -0500350 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500351 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
352 ndn::time::system_clock::now();
353 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500354 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500355 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
356 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500357 clsa.getLsSeqNo(),
358 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500359 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500360 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500361 }
362 }
363 return true;
364}
365
366bool
akmhoqueb6450b12014-04-24 00:01:03 -0500367Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500368{
akmhoqueb6450b12014-04-24 00:01:03 -0500369 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
370 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500371 ndn::bind(corLsaCompareByKey, _1,
372 clsa.getKey()));
373 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500374 m_corLsdb.push_back(clsa);
375 return true;
376 }
377 return false;
378}
379
380bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500381Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500382{
akmhoqueb6450b12014-04-24 00:01:03 -0500383 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
384 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500385 ndn::bind(corLsaCompareByKey,
386 _1, key));
387 if (it != m_corLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500388 _LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500389 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500390 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500391 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500392 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
393 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500394 }
395 m_corLsdb.erase(it);
396 return true;
397 }
398 return false;
399}
400
401bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500402Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500403{
akmhoqueb6450b12014-04-24 00:01:03 -0500404 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
405 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500406 ndn::bind(corLsaCompareByKey,
407 _1, key));
408 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500409 return false;
410 }
411 return true;
412}
413
414void
akmhoque2f423352014-06-03 11:49:35 -0500415Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500416{
akmhoque2f423352014-06-03 11:49:35 -0500417 _LOG_DEBUG("---------------Cor LSDB-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500418 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500419 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500420 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500421 }
422}
423
akmhoque53353462014-04-22 08:43:45 -0500424// Adj LSA and LSDB related function starts here
425
426static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500427adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500428{
429 return alsa.getKey() == key;
430}
431
akmhoque53353462014-04-22 08:43:45 -0500432void
akmhoque31d1d4b2014-05-05 22:08:14 -0500433Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500434{
akmhoque674b0b12014-05-20 14:33:28 -0500435 _LOG_DEBUG("scheduledAdjLsaBuild Called");
436 m_nlsr.setIsBuildAdjLsaSheduled(false);
akmhoque157b0a42014-05-13 00:26:37 -0500437 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500438 int adjBuildCount = m_nlsr.getAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500439 if (adjBuildCount > 0) {
440 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500441 _LOG_DEBUG("Building and installing Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500442 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500443 }
akmhoque157b0a42014-05-13 00:26:37 -0500444 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500445 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
446 key.append("adjacency");
447 removeAdjLsa(key);
448 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500449 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500450 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500451 }
452 }
akmhoque157b0a42014-05-13 00:26:37 -0500453 else {
akmhoque674b0b12014-05-20 14:33:28 -0500454 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500455 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
456 m_nlsr.getConfParameter().getInterestResendTime();
Vince Lehman7c603292014-09-11 17:48:16 -0500457 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
458 ndn::bind(&Lsdb::scheduledAdjLsaBuild, this));
akmhoque53353462014-04-22 08:43:45 -0500459 }
460}
461
462
463bool
464Lsdb::addAdjLsa(AdjLsa& alsa)
465{
466 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
467 m_adjLsdb.end(),
468 bind(adjLsaCompareByKey, _1,
469 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500470 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500471 m_adjLsdb.push_back(alsa);
472 return true;
473 }
474 return false;
475}
476
akmhoqueb6450b12014-04-24 00:01:03 -0500477AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500478Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500479{
480 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
481 m_adjLsdb.end(),
482 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500483 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500484 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500485 }
akmhoqueb6450b12014-04-24 00:01:03 -0500486 return 0;
akmhoque53353462014-04-22 08:43:45 -0500487}
488
489
490bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500491Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500492{
akmhoqueb6450b12014-04-24 00:01:03 -0500493 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500494 if (adjLsaCheck != 0) {
495 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500496 return true;
497 }
akmhoque157b0a42014-05-13 00:26:37 -0500498 else {
akmhoque53353462014-04-22 08:43:45 -0500499 return false;
500 }
501 }
502 return true;
503}
504
505
506ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500507Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
508 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500509{
Vince Lehman7c603292014-09-11 17:48:16 -0500510 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
511 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500512}
513
514bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500515Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500516{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700517 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500518 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500519 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500520 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500521 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500522 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500523 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500524 alsa.addNptEntries(m_nlsr);
525 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500526 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500527 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
528 ndn::time::system_clock::now();
529 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500530 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500531 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500532 alsa.getLsSeqNo(), timeToExpire);
533 }
akmhoque157b0a42014-05-13 00:26:37 -0500534 else {
535 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500536 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500537 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500538 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500539 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500540 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500541 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500542 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500543 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500544 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500545 }
akmhoque157b0a42014-05-13 00:26:37 -0500546 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500547 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
548 ndn::time::system_clock::now();
549 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500550 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500551 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
552 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500553 alsa.getLsSeqNo(),
554 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500555 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500556 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500557 }
558 }
559 return true;
560}
561
562bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500563Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500564{
akmhoque31d1d4b2014-05-05 22:08:14 -0500565 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
566 "adjacency",
567 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500568 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500569 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
570 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500571
akmhoque31d1d4b2014-05-05 22:08:14 -0500572 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
Vince Lehman904c2412014-09-23 19:36:11 -0500573
574 bool isInstalled = installAdjLsa(adjLsa);
575
576 // Delay Sync prefix registration until the first Adjacency LSA is built
577 if (isInstalled && !m_hasSyncPrefixBeenRegistered) {
578 m_nlsr.getSyncLogicHandler().createSyncSocket();
579 m_hasSyncPrefixBeenRegistered = true;
580 }
581
akmhoque157b0a42014-05-13 00:26:37 -0500582 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500583 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
584 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
Vince Lehman904c2412014-09-23 19:36:11 -0500585
586 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(), lsaPrefix);
587
588 return isInstalled;
akmhoque53353462014-04-22 08:43:45 -0500589}
590
591bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500592Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500593{
594 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
595 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500596 ndn::bind(adjLsaCompareByKey, _1, key));
597 if (it != m_adjLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500598 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500599 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500600 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500601 m_adjLsdb.erase(it);
602 return true;
603 }
604 return false;
605}
606
607bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500608Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500609{
610 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
611 m_adjLsdb.end(),
612 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500613 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500614 return false;
615 }
616 return true;
617}
618
619std::list<AdjLsa>&
620Lsdb::getAdjLsdb()
621{
622 return m_adjLsdb;
623}
624
625void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700626Lsdb::setLsaRefreshTime(const seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500627{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700628 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500629}
630
631void
632Lsdb::setThisRouterPrefix(string trp)
633{
634 m_thisRouterPrefix = trp;
635}
636
637void
akmhoque31d1d4b2014-05-05 22:08:14 -0500638Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500639{
akmhoque674b0b12014-05-20 14:33:28 -0500640 _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called");
641 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500642 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500643 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500644 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500645 if (chkNameLsa->getLsSeqNo() == seqNo) {
646 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500647 _LOG_DEBUG("Own Name LSA, so refreshing it");
648 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500649 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500650 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500651 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500652 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500653 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500654 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500655 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500656 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500657 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700658 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500659 // publish routing update
akmhoque50125a92014-06-30 08:54:17 -0500660 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
661 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500662 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500663 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
664 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500665 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
666 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500667 }
akmhoque157b0a42014-05-13 00:26:37 -0500668 else {
akmhoque674b0b12014-05-20 14:33:28 -0500669 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500670 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500671 }
672 }
673 }
674}
675
676void
akmhoque31d1d4b2014-05-05 22:08:14 -0500677Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500678{
akmhoque674b0b12014-05-20 14:33:28 -0500679 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
680 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500681 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500682 if (chkAdjLsa != 0) {
akmhoque2f423352014-06-03 11:49:35 -0500683 _LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500684 if (chkAdjLsa->getLsSeqNo() == seqNo) {
685 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500686 _LOG_DEBUG("Own Adj LSA, so refreshing it");
687 _LOG_DEBUG("Deleting Adj Lsa");
688 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500689 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500690 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500691 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500692 _LOG_DEBUG("Adding Adj Lsa");
693 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500694 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500695 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500696 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700697 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500698 // publish routing update
akmhoque50125a92014-06-30 08:54:17 -0500699 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
700 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500701 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500702 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
703 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500704 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
705 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500706 }
akmhoque157b0a42014-05-13 00:26:37 -0500707 else {
akmhoque674b0b12014-05-20 14:33:28 -0500708 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500709 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500710 }
711 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500712 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500713 }
714 }
715}
716
717void
akmhoque31d1d4b2014-05-05 22:08:14 -0500718Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500719 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500720{
akmhoque674b0b12014-05-20 14:33:28 -0500721 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
722 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500723 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500724 if (chkCorLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500725 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500726 if (chkCorLsa->getLsSeqNo() == seqNo) {
727 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500728 _LOG_DEBUG("Own Cor LSA, so refreshing it");
729 _LOG_DEBUG("Deleting Coordinate Lsa");
730 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500731 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500732 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500733 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500734 _LOG_DEBUG("Adding Coordinate Lsa");
735 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500736 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500737 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
738 chkCorLsa->getKey(),
739 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700740 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500741 // publish routing update
akmhoque50125a92014-06-30 08:54:17 -0500742 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
743 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500744 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500745 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
746 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500747 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
748 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500749 }
akmhoque157b0a42014-05-13 00:26:37 -0500750 else {
akmhoque674b0b12014-05-20 14:33:28 -0500751 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500752 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500753 }
akmhoque157b0a42014-05-13 00:26:37 -0500754 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500755 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500756 }
757 }
758 }
759}
760
761
762void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700763Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500764 steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500765{
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500766 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
767 deadline = steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700768 }
769
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500770 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
771
akmhoque31d1d4b2014-05-05 22:08:14 -0500772 ndn::Interest interest(interestName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500773 uint64_t seqNo = interestName[-1].toNumber();
774
775 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
776 m_highestSeqNo[lsaName] = seqNo;
777 }
778 else if (seqNo > m_highestSeqNo[lsaName]) {
779 m_highestSeqNo[lsaName] = seqNo;
780 }
781 else if (seqNo < m_highestSeqNo[lsaName]) {
782 return;
783 }
784
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700785 interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500786
787 _LOG_DEBUG("Expressing Interest for LSA: " << interestName << " Seq number: " << seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500788 m_nlsr.getNlsrFace().expressInterest(interest,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700789 ndn::bind(&Lsdb::onContent,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500790 this, _2, deadline, lsaName, seqNo),
akmhoque31d1d4b2014-05-05 22:08:14 -0500791 ndn::bind(&Lsdb::processInterestTimedOut,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500792 this, _1, timeoutCount, deadline, lsaName, seqNo));
akmhoque31d1d4b2014-05-05 22:08:14 -0500793}
794
795void
796Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
797{
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500798 const ndn::Name& interestName(interest.getName());
799 _LOG_DEBUG("Interest received for LSA: " << interestName);
800
akmhoque31d1d4b2014-05-05 22:08:14 -0500801 string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500802 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
803
akmhoque157b0a42014-05-13 00:26:37 -0500804 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500805
806 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
807 originRouter.append(interestName.getSubName(lsaPosition + 1,
808 interest.getName().size() - lsaPosition - 3));
809
810 uint64_t seqNo = interestName[-1].toNumber();
811 _LOG_DEBUG("LSA sequence number from interest: " << seqNo);
812
813 std::string interestedLsType = interestName[-2].toUri();
814
akmhoque157b0a42014-05-13 00:26:37 -0500815 if (interestedLsType == "name") {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500816 processInterestForNameLsa(interest, originRouter.append(interestedLsType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500817 }
akmhoque157b0a42014-05-13 00:26:37 -0500818 else if (interestedLsType == "adjacency") {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500819 processInterestForAdjacencyLsa(interest, originRouter.append(interestedLsType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500820 }
akmhoque157b0a42014-05-13 00:26:37 -0500821 else if (interestedLsType == "coordinate") {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500822 processInterestForCoordinateLsa(interest, originRouter.append(interestedLsType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500823 }
akmhoque157b0a42014-05-13 00:26:37 -0500824 else {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500825 _LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -0500826 }
827 }
828}
829
830void
akmhoque69c9aa92014-07-23 15:15:05 -0500831Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
832{
833 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
834 data->setName(ndn::Name(interest.getName()).appendVersion());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700835 data->setFreshnessPeriod(m_lsaRefreshTime);
akmhoque69c9aa92014-07-23 15:15:05 -0500836 data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
837 m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
akmhoquedfe615f2014-07-27 14:12:21 -0500838 ndn::SignatureSha256WithRsa signature(data->getSignature());
839 ndn::Name signingCertName = signature.getKeyLocator().getName();
akmhoque69c9aa92014-07-23 15:15:05 -0500840 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500841 _LOG_DEBUG("Data signed with: " << signingCertName);
akmhoque69c9aa92014-07-23 15:15:05 -0500842 m_nlsr.getNlsrFace().put(*data);
843}
844
845void
akmhoque31d1d4b2014-05-05 22:08:14 -0500846Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
847 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500848 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500849{
850 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500851 if (nameLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500852 if (nameLsa->getLsSeqNo() == seqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500853 std::string content = nameLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500854 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500855 }
856 }
857}
858
859void
860Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
861 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500862 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500863{
864 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500865 if (adjLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500866 if (adjLsa->getLsSeqNo() == seqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500867 std::string content = adjLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500868 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500869 }
870 }
871}
872
873void
874Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
875 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500876 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500877{
878 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500879 if (corLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500880 if (corLsa->getLsSeqNo() == seqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500881 std::string content = corLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500882 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500883 }
884 }
885}
886
887void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700888Lsdb::onContent(const ndn::Data& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500889 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
890 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500891{
akmhoquedfe615f2014-07-27 14:12:21 -0500892 _LOG_DEBUG("Received data for LSA(name): " << data.getName());
893 if (data.getSignature().hasKeyLocator()) {
894 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
895 _LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
896 }
897 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700898 m_nlsr.getValidator().validate(data,
899 ndn::bind(&Lsdb::onContentValidated, this, _1),
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700900 ndn::bind(&Lsdb::onContentValidationFailed, this, _1, _2,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500901 deadline, lsaName, seqNo));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700902
903}
904
905void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700906Lsdb::retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500907 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
908 uint64_t seqNo)
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700909{
910 _LOG_DEBUG("Retrying validation of LSA(name): " << data->getName());
911 if (data->getSignature().hasKeyLocator()) {
912 if (data->getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
913 _LOG_DEBUG("Data signed with: " << data->getSignature().getKeyLocator().getName());
914 }
915 }
916 m_nlsr.getValidator().validate(*data,
917 ndn::bind(&Lsdb::onContentValidated, this, _1),
918 ndn::bind(&Lsdb::onContentValidationFailed, this, _1, _2,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500919 deadline, lsaName, seqNo));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700920}
921
922void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700923Lsdb::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
924{
925 const ndn::Name& dataName = data->getName();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500926 _LOG_DEBUG("Data validation successful for LSA: " << dataName);
927
akmhoque31d1d4b2014-05-05 22:08:14 -0500928 string chkString("LSA");
929 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500930
akmhoque157b0a42014-05-13 00:26:37 -0500931 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500932
933 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
934 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 4));
935
936 uint64_t seqNo = dataName[-2].toNumber();
937 string dataContent(reinterpret_cast<const char*>(data->getContent().value()));
938
939 std::string interestedLsType = dataName[-3].toUri();
940
akmhoque157b0a42014-05-13 00:26:37 -0500941 if (interestedLsType == "name") {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500942 processContentNameLsa(originRouter.append(interestedLsType), seqNo, dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500943 }
akmhoque157b0a42014-05-13 00:26:37 -0500944 else if (interestedLsType == "adjacency") {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500945 processContentAdjacencyLsa(originRouter.append(interestedLsType), seqNo, dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500946 }
akmhoque157b0a42014-05-13 00:26:37 -0500947 else if (interestedLsType == "coordinate") {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500948 processContentCoordinateLsa(originRouter.append(interestedLsType), seqNo, dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500949 }
akmhoque157b0a42014-05-13 00:26:37 -0500950 else {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500951 _LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -0500952 }
953 }
954}
955
956void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700957Lsdb::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
958 const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500959 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
960 uint64_t seqNo)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700961{
akmhoque2f423352014-06-03 11:49:35 -0500962 _LOG_DEBUG("Validation Error: " << msg);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700963
964 // Delay re-validation by LSA Interest Lifetime. When error callback will have an error
965 // code, re-validation should be done only when some keys from certification chain failed
966 // to be fetched. After that change, delaying will no longer be necessary.
967
968 // Stop retrying if delayed re-validation will be scheduled pass the deadline
969 if (steady_clock::now() + m_nlsr.getConfParameter().getLsaInterestLifetime() < deadline) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500970
971 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
972
973 if (it != m_highestSeqNo.end() && it->second == seqNo) {
974 _LOG_DEBUG("Scheduling revalidation attempt");
975 m_scheduler.scheduleEvent(m_nlsr.getConfParameter().getLsaInterestLifetime(),
976 ndn::bind(&Lsdb::retryContentValidation, this, data,
977 deadline, lsaName, seqNo));
978 }
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700979 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700980}
981
982void
akmhoque31d1d4b2014-05-05 22:08:14 -0500983Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500984 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -0500985{
akmhoque157b0a42014-05-13 00:26:37 -0500986 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500987 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500988 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500989 installNameLsa(nameLsa);
990 }
akmhoque157b0a42014-05-13 00:26:37 -0500991 else {
akmhoque2f423352014-06-03 11:49:35 -0500992 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500993 }
994 }
995}
996
997void
998Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500999 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001000{
akmhoque157b0a42014-05-13 00:26:37 -05001001 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001002 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -05001003 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001004 installAdjLsa(adjLsa);
1005 }
akmhoque157b0a42014-05-13 00:26:37 -05001006 else {
akmhoque2f423352014-06-03 11:49:35 -05001007 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001008 }
1009 }
1010}
1011
1012void
1013Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001014 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001015{
akmhoque157b0a42014-05-13 00:26:37 -05001016 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001017 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -05001018 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001019 installCoordinateLsa(corLsa);
1020 }
akmhoque157b0a42014-05-13 00:26:37 -05001021 else {
akmhoque2f423352014-06-03 11:49:35 -05001022 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001023 }
1024 }
1025}
1026
1027void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -07001028Lsdb::processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001029 const ndn::time::steady_clock::TimePoint& deadline, ndn::Name lsaName,
1030 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001031{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -07001032 const ndn::Name& interestName = interest.getName();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001033 _LOG_DEBUG("Interest timed out for LSA: " << interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -07001034
1035 if (ndn::time::steady_clock::now() < deadline) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001036
1037 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
1038
1039 if (it != m_highestSeqNo.end() && it->second == seqNo) {
1040 expressInterest(interestName, retransmitNo + 1, deadline);
1041 }
akmhoque06986672014-05-27 13:55:53 -05001042 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001043}
1044
akmhoquec7a79b22014-05-26 08:06:19 -05001045ndn::time::system_clock::TimePoint
1046Lsdb::getLsaExpirationTimePoint()
1047{
1048 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1049 expirationTimePoint = expirationTimePoint +
1050 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1051 return expirationTimePoint;
1052}
akmhoque31d1d4b2014-05-05 22:08:14 -05001053
1054void
akmhoque2f423352014-06-03 11:49:35 -05001055Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001056{
akmhoque2f423352014-06-03 11:49:35 -05001057 _LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -05001058 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -05001059 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -05001060 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -05001061 }
1062}
1063
1064//-----utility function -----
1065bool
akmhoque31d1d4b2014-05-05 22:08:14 -05001066Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -05001067{
akmhoque157b0a42014-05-13 00:26:37 -05001068 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -05001069 return doesNameLsaExist(key);
1070 }
akmhoque157b0a42014-05-13 00:26:37 -05001071 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -05001072 return doesAdjLsaExist(key);
1073 }
akmhoque157b0a42014-05-13 00:26:37 -05001074 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -05001075 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -05001076 }
1077 return false;
1078}
1079
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001080} // namespace nlsr