blob: e3d3dd5aaf571b40b1619564e040eab7677e45b8 [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
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37
38void
akmhoque31d1d4b2014-05-05 22:08:14 -050039Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050040{
akmhoque31d1d4b2014-05-05 22:08:14 -050041 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050042}
43
44static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050045nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050046{
47 return nlsa1.getKey() == key;
48}
49
50
51bool
akmhoque31d1d4b2014-05-05 22:08:14 -050052Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050053{
akmhoque31d1d4b2014-05-05 22:08:14 -050054 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
55 "name",
56 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -050057 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -050058 m_nlsr.getNamePrefixList());
59 m_nlsr.getSequencingManager().increaseNameLsaSeq();
60 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050061}
62
akmhoqueb6450b12014-04-24 00:01:03 -050063NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050064Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050065{
66 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
67 m_nameLsdb.end(),
68 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -050069 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -050070 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050071 }
akmhoqueb6450b12014-04-24 00:01:03 -050072 return 0;
akmhoque53353462014-04-22 08:43:45 -050073}
74
75bool
akmhoque31d1d4b2014-05-05 22:08:14 -050076Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050077{
akmhoqueb6450b12014-04-24 00:01:03 -050078 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -050079 if (nameLsaCheck != 0) {
80 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -050081 return true;
82 }
akmhoque157b0a42014-05-13 00:26:37 -050083 else {
akmhoque53353462014-04-22 08:43:45 -050084 return false;
85 }
86 }
87 return true;
88}
89
90ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -050091Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
92 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050093{
akmhoquec7a79b22014-05-26 08:06:19 -050094 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -050095 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
96 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050097}
98
99bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500100Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500101{
akmhoquec7a79b22014-05-26 08:06:19 -0500102 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500103 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500105 addNameLsa(nlsa);
akmhoque674b0b12014-05-20 14:33:28 -0500106 _LOG_DEBUG("New Name LSA. Adding to LSDB");
akmhoque53353462014-04-22 08:43:45 -0500107 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500108
akmhoque53353462014-04-22 08:43:45 -0500109 printNameLsdb();
akmhoque157b0a42014-05-13 00:26:37 -0500110 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 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();
akmhoque157b0a42014-05-13 00:26:37 -0500115 it++) {
116 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500118 }
119 }
120 }
akmhoque157b0a42014-05-13 00:26:37 -0500121 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500122 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
123 ndn::time::system_clock::now();
124 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500125 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500127 nlsa.getLsSeqNo(),
128 timeToExpire));
129 }
akmhoque157b0a42014-05-13 00:26:37 -0500130 else {
131 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500132 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
133 _LOG_DEBUG("Old Name LSA ");
akmhoqueb6450b12014-04-24 00:01:03 -0500134 chkNameLsa->writeLog();
135 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500136 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500137 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500138 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500139 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500140 std::set_difference(nlsa.getNpl().getNameList().begin(),
141 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500142 chkNameLsa->getNpl().getNameList().begin(),
143 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500144 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500146 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500147 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500148 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
149 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500151 }
152 }
153 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500154 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500155 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
156 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500157 nlsa.getNpl().getNameList().begin(),
158 nlsa.getNpl().getNameList().end(),
159 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500161 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500162 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500163 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
164 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500166 }
167 }
168 }
akmhoque157b0a42014-05-13 00:26:37 -0500169 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500170 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
171 ndn::time::system_clock::now();
172 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500173 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
175 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500176 nlsa.getLsSeqNo(),
177 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500178 _LOG_DEBUG("Updated Name LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500179 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500180 }
181 }
182 return true;
183}
184
185bool
186Lsdb::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()));
akmhoque157b0a42014-05-13 00:26:37 -0500192 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500193 m_nameLsdb.push_back(nlsa);
194 return true;
195 }
196 return false;
197}
198
199bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500200Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500201{
202 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
203 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500204 ndn::bind(nameLsaCompareByKey, _1, key));
205 if (it != m_nameLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500206 _LOG_DEBUG("Removing Name LSA");
akmhoque53353462014-04-22 08:43:45 -0500207 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500208 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500209 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500210 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
211 (*it).getOrigRouter());
212 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500213 nit != (*it).getNpl().getNameList().end(); ++nit) {
214 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500215 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500216 }
217 }
218 }
219 m_nameLsdb.erase(it);
220 return true;
221 }
222 return false;
223}
224
225bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500226Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500227{
228 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
229 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500230 ndn::bind(nameLsaCompareByKey, _1, key));
231 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500232 return false;
233 }
234 return true;
235}
236
237void
238Lsdb::printNameLsdb()
239{
240 cout << "---------------Name LSDB-------------------" << endl;
241 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500242 it != m_nameLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500243 cout << (*it) << endl;
244 }
245}
246
247// Cor LSA and LSDB related Functions start here
248
249
250static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500251corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500252{
253 return clsa.getKey() == key;
254}
255
256bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500257Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500258{
akmhoque31d1d4b2014-05-05 22:08:14 -0500259 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
260 "coordinate",
261 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500262 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 m_nlsr.getConfParameter().getCorR(),
264 m_nlsr.getConfParameter().getCorTheta());
265 m_nlsr.getSequencingManager().increaseCorLsaSeq();
266 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500267 return true;
268}
269
akmhoqueb6450b12014-04-24 00:01:03 -0500270CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500271Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500272{
akmhoqueb6450b12014-04-24 00:01:03 -0500273 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
274 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500275 ndn::bind(corLsaCompareByKey, _1, key));
276 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500277 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500278 }
akmhoqueb6450b12014-04-24 00:01:03 -0500279 return 0;
akmhoque53353462014-04-22 08:43:45 -0500280}
281
282bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500283Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500284{
akmhoqueb6450b12014-04-24 00:01:03 -0500285 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500286 if (clsa != 0) {
287 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500288 return true;
289 }
akmhoque157b0a42014-05-13 00:26:37 -0500290 else {
akmhoque53353462014-04-22 08:43:45 -0500291 return false;
292 }
293 }
294 return true;
295}
296
297ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500298Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500299 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500300{
akmhoquec7a79b22014-05-26 08:06:19 -0500301 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -0500302 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
303 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500304}
305
306bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500307Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500308{
akmhoquec7a79b22014-05-26 08:06:19 -0500309 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500310 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500311 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500312 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
313 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500314 addCoordinateLsa(clsa);
akmhoque157b0a42014-05-13 00:26:37 -0500315 //debugging purpose
316 printCorLsdb();
317 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500318 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
319 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500320 }
akmhoque157b0a42014-05-13 00:26:37 -0500321 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500322 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500323 }
akmhoque157b0a42014-05-13 00:26:37 -0500324 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500325 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
326 ndn::time::system_clock::now();
327 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500328 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500329 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500330 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500331 }
akmhoque157b0a42014-05-13 00:26:37 -0500332 else {
333 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500334 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
335 _LOG_DEBUG("Old Coordinate LSA");
336 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500337 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500338 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500339 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500340 chkCorLsa->setCorRadius(clsa.getCorRadius());
341 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500342 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500343 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500344 }
345 }
akmhoque157b0a42014-05-13 00:26:37 -0500346 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500347 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
348 ndn::time::system_clock::now();
349 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500350 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500351 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
352 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500353 clsa.getLsSeqNo(),
354 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500355 _LOG_DEBUG("Updated Coordinate LSA");
356 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500357 }
358 }
359 return true;
360}
361
362bool
akmhoqueb6450b12014-04-24 00:01:03 -0500363Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500364{
akmhoqueb6450b12014-04-24 00:01:03 -0500365 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
366 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500367 ndn::bind(corLsaCompareByKey, _1,
368 clsa.getKey()));
369 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500370 m_corLsdb.push_back(clsa);
371 return true;
372 }
373 return false;
374}
375
376bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500377Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500378{
akmhoqueb6450b12014-04-24 00:01:03 -0500379 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
380 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500381 ndn::bind(corLsaCompareByKey,
382 _1, key));
383 if (it != m_corLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500384 _LOG_DEBUG("Removing Coordinate LSA");
385 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500386 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500387 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500388 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
389 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500390 }
391 m_corLsdb.erase(it);
392 return true;
393 }
394 return false;
395}
396
397bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500398Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500399{
akmhoqueb6450b12014-04-24 00:01:03 -0500400 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
401 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500402 ndn::bind(corLsaCompareByKey,
403 _1, key));
404 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500405 return false;
406 }
407 return true;
408}
409
akmhoque157b0a42014-05-13 00:26:37 -0500410//debugging
akmhoque53353462014-04-22 08:43:45 -0500411void
akmhoque157b0a42014-05-13 00:26:37 -0500412Lsdb::printCorLsdb()
akmhoque53353462014-04-22 08:43:45 -0500413{
414 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500415 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500416 it != m_corLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500417 cout << (*it) << endl;
418 }
419}
420
421
422// Adj LSA and LSDB related function starts here
423
424static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500425adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500426{
427 return alsa.getKey() == key;
428}
429
430
431void
akmhoque31d1d4b2014-05-05 22:08:14 -0500432Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500433{
akmhoque674b0b12014-05-20 14:33:28 -0500434 std::cout << "scheduledAdjLsaBuild Called" << endl;
435 _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();
457 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
458 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
459 this));
akmhoque53353462014-04-22 08:43:45 -0500460 }
461}
462
463
464bool
465Lsdb::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()));
akmhoque157b0a42014-05-13 00:26:37 -0500471 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500472 m_adjLsdb.push_back(alsa);
473 return true;
474 }
475 return false;
476}
477
akmhoqueb6450b12014-04-24 00:01:03 -0500478AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500479Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500480{
481 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
482 m_adjLsdb.end(),
483 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500484 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500485 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500486 }
akmhoqueb6450b12014-04-24 00:01:03 -0500487 return 0;
akmhoque53353462014-04-22 08:43:45 -0500488}
489
490
491bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500492Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500493{
akmhoqueb6450b12014-04-24 00:01:03 -0500494 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500495 if (adjLsaCheck != 0) {
496 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500497 return true;
498 }
akmhoque157b0a42014-05-13 00:26:37 -0500499 else {
akmhoque53353462014-04-22 08:43:45 -0500500 return false;
501 }
502 }
503 return true;
504}
505
506
507ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500508Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
509 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500510{
akmhoquec7a79b22014-05-26 08:06:19 -0500511 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -0500512 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
513 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500514}
515
516bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500517Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500518{
akmhoquec7a79b22014-05-26 08:06:19 -0500519 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500520 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500521 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500522 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
523 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500524 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500525 alsa.addNptEntries(m_nlsr);
526 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500527 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500528 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
529 ndn::time::system_clock::now();
530 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500531 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500532 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500533 alsa.getLsSeqNo(), timeToExpire);
534 }
akmhoque157b0a42014-05-13 00:26:37 -0500535 else {
536 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500537 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
538 _LOG_DEBUG("Old Adj LSA");
539 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500540 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500541 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500542 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500543 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500544 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500545 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500546 }
akmhoque157b0a42014-05-13 00:26:37 -0500547 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500548 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
549 ndn::time::system_clock::now();
550 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500551 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500552 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
553 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500554 alsa.getLsSeqNo(),
555 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500556 _LOG_DEBUG("Updated Adj LSA");
557 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500558 }
559 }
560 return true;
561}
562
563bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500564Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500565{
akmhoque31d1d4b2014-05-05 22:08:14 -0500566 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
567 "adjacency",
568 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500569 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500570 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
571 m_nlsr.getAdjacencyList());
572 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
573 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500574 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500575 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
576 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
577 lsaPrefix);
578 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500579}
580
581bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500582Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500583{
584 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
585 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500586 ndn::bind(adjLsaCompareByKey, _1, key));
587 if (it != m_adjLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500588 _LOG_DEBUG("Removing Adj LSA");
589 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500590 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500591 m_adjLsdb.erase(it);
592 return true;
593 }
594 return false;
595}
596
597bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500598Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500599{
600 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
601 m_adjLsdb.end(),
602 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500603 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500604 return false;
605 }
606 return true;
607}
608
609std::list<AdjLsa>&
610Lsdb::getAdjLsdb()
611{
612 return m_adjLsdb;
613}
614
615void
616Lsdb::setLsaRefreshTime(int lrt)
617{
618 m_lsaRefreshTime = lrt;
619}
620
621void
622Lsdb::setThisRouterPrefix(string trp)
623{
624 m_thisRouterPrefix = trp;
625}
626
627void
akmhoque31d1d4b2014-05-05 22:08:14 -0500628Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500629{
akmhoque674b0b12014-05-20 14:33:28 -0500630 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);
akmhoqueb6450b12014-04-24 00:01:03 -0500634 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500635 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500636 std::cout << "LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << std::endl;
637 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500638 if (chkNameLsa->getLsSeqNo() == seqNo) {
639 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoqueb6450b12014-04-24 00:01:03 -0500640 chkNameLsa->writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500641 std::cout << "Own Name LSA, so refreshing name LSA" << std::endl;
642 _LOG_DEBUG("Own Name LSA, so refreshing name LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500643 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500644 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500645 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500646 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500647 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500648 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500649 chkNameLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500650 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500651 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500652 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500653 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
654 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
655 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500656 }
akmhoque157b0a42014-05-13 00:26:37 -0500657 else {
akmhoque674b0b12014-05-20 14:33:28 -0500658 std::cout << "Other's Name LSA, so removing form LSDB" << std::endl;
659 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500660 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500661 }
662 }
663 }
664}
665
666void
akmhoque31d1d4b2014-05-05 22:08:14 -0500667Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500668{
669 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
670 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500671 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
672 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500673 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500674 if (chkAdjLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500675 cout << "LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
676 _LOG_DEBUG("LSA Exists with seq no: ");
akmhoque157b0a42014-05-13 00:26:37 -0500677 if (chkAdjLsa->getLsSeqNo() == seqNo) {
678 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500679 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500680 _LOG_DEBUG("Own Adj LSA, so refreshing Adj LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500681 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500682 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500683 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoquefdbddb12014-05-02 18:35:19 -0500684 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500685 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500686 chkAdjLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500687 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500688 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500689 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500690 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
691 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
692 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500693 }
akmhoque157b0a42014-05-13 00:26:37 -0500694 else {
akmhoque53353462014-04-22 08:43:45 -0500695 cout << "Other's Adj LSA, so removing form LSDB" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500696 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500697 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500698 }
699 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500700 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500701 }
702 }
703}
704
705void
akmhoque31d1d4b2014-05-05 22:08:14 -0500706Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500707 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500708{
709 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
710 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500711 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
712 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500713 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500714 if (chkCorLsa != 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500715 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500716 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500717 if (chkCorLsa->getLsSeqNo() == seqNo) {
718 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500719 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500720 _LOG_DEBUG("Own Cor LSA, so refreshing Cor LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500721 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500722 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500723 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoquefdbddb12014-05-02 18:35:19 -0500724 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500725 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
726 chkCorLsa->getKey(),
727 chkCorLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500728 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500729 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500730 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500731 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
732 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
733 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500734 }
akmhoque157b0a42014-05-13 00:26:37 -0500735 else {
akmhoque53353462014-04-22 08:43:45 -0500736 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500737 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500738 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500739 }
akmhoque157b0a42014-05-13 00:26:37 -0500740 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500741 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500742 }
743 }
744 }
745}
746
747
748void
akmhoque06986672014-05-27 13:55:53 -0500749Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime,
750 uint32_t timeoutCount)
akmhoque31d1d4b2014-05-05 22:08:14 -0500751{
752 std::cout << "Expressing Interest :" << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500753 _LOG_DEBUG("Expressing Interest for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500754 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,
akmhoque06986672014-05-27 13:55:53 -0500761 this, _1, timeoutCount));
akmhoque31d1d4b2014-05-05 22:08:14 -0500762}
763
764void
765Lsdb::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;
akmhoque674b0b12014-05-20 14:33:28 -0500769 _LOG_DEBUG("Interest recevied for LSA(name): " << intName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500770 string chkString("LSA");
771 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
772 chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500773 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500774 std::string interestedLsType;
775 uint64_t interestedLsSeqNo;
776 ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
777 interest.getName().size() - lsaPosition - 3);
akmhoque157b0a42014-05-13 00:26:37 -0500778 interestedLsType = intName[-2].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500779 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;
akmhoque157b0a42014-05-13 00:26:37 -0500784 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500785 processInterestForNameLsa(interest,
786 origRouter.append(interestedLsType),
787 interestedLsSeqNo);
788 return;
789 }
akmhoque157b0a42014-05-13 00:26:37 -0500790 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500791 processInterestForAdjacencyLsa(interest,
792 origRouter.append(interestedLsType),
793 interestedLsSeqNo);
794 return;
795 }
akmhoque157b0a42014-05-13 00:26:37 -0500796 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500797 processInterestForCoordinateLsa(interest,
798 origRouter.append(interestedLsType),
799 interestedLsSeqNo);
800 return;
801 }
akmhoque157b0a42014-05-13 00:26:37 -0500802 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500803 cout << "Unrecognized LSA Type :(" << endl;
804 }
805 }
806}
807
808void
809Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
810 const ndn::Name& lsaKey,
811 uint32_t interestedlsSeqNo)
812{
813 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500814 if (nameLsa != 0) {
815 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500816 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500817 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500818 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);
akmhoque674b0b12014-05-20 14:33:28 -0500823 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500824 m_nlsr.getNlsrFace().put(data);
825 }
826 }
827}
828
829void
830Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
831 const ndn::Name& lsaKey,
832 uint32_t interestedlsSeqNo)
833{
834 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500835 if (adjLsa != 0) {
836 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500837 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500838 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500839 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);
akmhoque674b0b12014-05-20 14:33:28 -0500844 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500845 m_nlsr.getNlsrFace().put(data);
846 }
847 }
848}
849
850void
851Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
852 const ndn::Name& lsaKey,
853 uint32_t interestedlsSeqNo)
854{
855 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500856 if (corLsa != 0) {
857 if (corLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500858 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500859 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500860 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);
akmhoque674b0b12014-05-20 14:33:28 -0500865 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500866 m_nlsr.getNlsrFace().put(data);
867 }
868 }
869}
870
871void
872Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data)
873{
874 const ndn::Name& dataName = data.getName();
akmhoque674b0b12014-05-20 14:33:28 -0500875 std::cout << "Data received for LSA(name): " << dataName << std::endl;
876 _LOG_DEBUG("Data received for LSA(name): " << dataName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500877 string dataContent(reinterpret_cast<const char*>(data.getContent().value()));
878 string chkString("LSA");
879 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500880 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500881 std::string interestedLsType;
882 uint64_t interestedLsSeqNo;
883 ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
884 dataName.size() - lsaPosition - 4);
akmhoque157b0a42014-05-13 00:26:37 -0500885 interestedLsType = dataName[-3].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500886 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;
akmhoque157b0a42014-05-13 00:26:37 -0500890 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500891 processContentNameLsa(origRouter.append(interestedLsType),
892 interestedLsSeqNo, dataContent);
893 return;
894 }
akmhoque157b0a42014-05-13 00:26:37 -0500895 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500896 processContentAdjacencyLsa(origRouter.append(interestedLsType),
897 interestedLsSeqNo, dataContent);
898 return;
899 }
akmhoque157b0a42014-05-13 00:26:37 -0500900 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500901 processContentCoordinateLsa(origRouter.append(interestedLsType),
902 interestedLsSeqNo, dataContent);
903 return;
904 }
akmhoque157b0a42014-05-13 00:26:37 -0500905 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500906 cout << "Unrecognized LSA Type :(" << endl;
907 }
908 }
909}
910
911void
912Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
913 uint32_t lsSeqNo, std::string& dataContent)
914{
akmhoque157b0a42014-05-13 00:26:37 -0500915 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500916 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500917 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500918 installNameLsa(nameLsa);
919 }
akmhoque157b0a42014-05-13 00:26:37 -0500920 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500921 std::cout << "LSA data decoding error :(" << std::endl;
922 }
923 }
924}
925
926void
927Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
928 uint32_t lsSeqNo, std::string& dataContent)
929{
akmhoque157b0a42014-05-13 00:26:37 -0500930 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500931 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500932 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500933 installAdjLsa(adjLsa);
934 }
akmhoque157b0a42014-05-13 00:26:37 -0500935 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500936 std::cout << "LSA data decoding error :(" << std::endl;
937 }
938 }
939}
940
941void
942Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
943 uint32_t lsSeqNo, std::string& dataContent)
944{
akmhoque157b0a42014-05-13 00:26:37 -0500945 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500946 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500947 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500948 installCoordinateLsa(corLsa);
949 }
akmhoque157b0a42014-05-13 00:26:37 -0500950 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500951 std::cout << "LSA data decoding error :(" << std::endl;
952 }
953 }
954}
955
956void
akmhoque06986672014-05-27 13:55:53 -0500957Lsdb::processInterestTimedOut(const ndn::Interest& interest, uint32_t timeoutCount)
akmhoque31d1d4b2014-05-05 22:08:14 -0500958{
959 const ndn::Name& interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500960 _LOG_DEBUG("Interest timed out for LSA(name): " << interestName);
akmhoque06986672014-05-27 13:55:53 -0500961 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 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500968}
969
akmhoquec7a79b22014-05-26 08:06:19 -0500970ndn::time::system_clock::TimePoint
971Lsdb::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}
akmhoque31d1d4b2014-05-05 22:08:14 -0500978
979void
akmhoque53353462014-04-22 08:43:45 -0500980Lsdb::printAdjLsdb()
981{
982 cout << "---------------Adj LSDB-------------------" << endl;
983 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500984 it != m_adjLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500985 cout << (*it) << endl;
986 }
987}
988
989//-----utility function -----
990bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500991Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500992{
akmhoque157b0a42014-05-13 00:26:37 -0500993 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -0500994 return doesNameLsaExist(key);
995 }
akmhoque157b0a42014-05-13 00:26:37 -0500996 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -0500997 return doesAdjLsaExist(key);
998 }
akmhoque157b0a42014-05-13 00:26:37 -0500999 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -05001000 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -05001001 }
1002 return false;
1003}
1004
1005}//namespace nlsr
1006