blob: a96cecbe2871a021546cb5d133fcb6195e5bdcc3 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <utility>
3#include "lsdb.hpp"
4#include "nlsr.hpp"
5
6namespace nlsr {
7
8using namespace std;
9
10void
11Lsdb::cancelScheduleLsaExpiringEvent(Nlsr& pnlsr, EventId eid)
12{
13 pnlsr.getScheduler().cancelEvent(eid);
14}
15
16static bool
akmhoqueb6450b12014-04-24 00:01:03 -050017nameLsaCompareByKey(const NameLsa& nlsa1, const string& key)
akmhoque53353462014-04-22 08:43:45 -050018{
19 return nlsa1.getKey() == key;
20}
21
22
23bool
24Lsdb::buildAndInstallOwnNameLsa(Nlsr& pnlsr)
25{
26 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
27 , 1
akmhoquec8a10f72014-04-25 18:42:55 -050028 , pnlsr.getSequencingManager().getNameLsaSeq() + 1
akmhoque53353462014-04-22 08:43:45 -050029 , pnlsr.getConfParameter().getRouterDeadInterval()
akmhoquec8a10f72014-04-25 18:42:55 -050030 , pnlsr.getNamePrefixList());
31 pnlsr.getSequencingManager().setNameLsaSeq(
32 pnlsr.getSequencingManager().getNameLsaSeq() + 1);
akmhoque53353462014-04-22 08:43:45 -050033 return installNameLsa(pnlsr, nameLsa);
34}
35
akmhoqueb6450b12014-04-24 00:01:03 -050036NameLsa*
37Lsdb::findNameLsa(const string key)
akmhoque53353462014-04-22 08:43:45 -050038{
39 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
40 m_nameLsdb.end(),
41 bind(nameLsaCompareByKey, _1, key));
42 if (it != m_nameLsdb.end())
43 {
akmhoqueb6450b12014-04-24 00:01:03 -050044 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050045 }
akmhoqueb6450b12014-04-24 00:01:03 -050046 return 0;
akmhoque53353462014-04-22 08:43:45 -050047}
48
49bool
50Lsdb::isNameLsaNew(string key, uint64_t seqNo)
51{
akmhoqueb6450b12014-04-24 00:01:03 -050052 NameLsa* nameLsaCheck = findNameLsa(key);
53 if (nameLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -050054 {
akmhoqueb6450b12014-04-24 00:01:03 -050055 if (nameLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -050056 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63 }
64 return true;
65}
66
67ndn::EventId
68Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
69{
70 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
71 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
72 this, boost::ref(pnlsr), key, seqNo));
73}
74
75bool
76Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa& nlsa)
77{
78 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -050079 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
80 if (chkNameLsa == 0)
akmhoque53353462014-04-22 08:43:45 -050081 {
82 addNameLsa(nlsa);
83 nlsa.writeLog();
84 printNameLsdb();
85 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
86 {
akmhoquec8a10f72014-04-25 18:42:55 -050087 pnlsr.getNamePrefixTable().addNpteByDestName(nlsa.getOrigRouter(),
88 nlsa.getOrigRouter(),
89 pnlsr);
akmhoque53353462014-04-22 08:43:45 -050090 std::list<string> nameList = nlsa.getNpl().getNameList();
91 for (std::list<string>::iterator it = nameList.begin(); it != nameList.end();
92 it++)
93 {
94 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
95 {
akmhoquec8a10f72014-04-25 18:42:55 -050096 pnlsr.getNamePrefixTable().addNpteByDestName((*it), nlsa.getOrigRouter(),
97 pnlsr);
akmhoque53353462014-04-22 08:43:45 -050098 }
99 }
100 }
101 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
102 {
103 timeToExpire = nlsa.getLifeTime();
104 }
105 nlsa.setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
106 nlsa.getKey(),
107 nlsa.getLsSeqNo(),
108 timeToExpire));
109 }
110 else
111 {
akmhoqueb6450b12014-04-24 00:01:03 -0500112 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500113 {
akmhoqueb6450b12014-04-24 00:01:03 -0500114 chkNameLsa->writeLog();
115 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
116 chkNameLsa->setLifeTime(nlsa.getLifeTime());
117 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500118 nlsa.getNpl().sort();
119 std::list<string> nameToAdd;
120 std::set_difference(nlsa.getNpl().getNameList().begin(),
121 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500122 chkNameLsa->getNpl().getNameList().begin(),
123 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500124 std::inserter(nameToAdd, nameToAdd.begin()));
125 for (std::list<string>::iterator it = nameToAdd.begin(); it != nameToAdd.end();
126 ++it)
127 {
akmhoqueb6450b12014-04-24 00:01:03 -0500128 chkNameLsa->addName((*it));
akmhoque53353462014-04-22 08:43:45 -0500129 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
130 {
131 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
132 {
akmhoquec8a10f72014-04-25 18:42:55 -0500133 pnlsr.getNamePrefixTable().addNpteByDestName((*it), nlsa.getOrigRouter(),
134 pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500135 }
136 }
137 }
138 std::list<string> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500139 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
140 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500141 nlsa.getNpl().getNameList().begin(),
142 nlsa.getNpl().getNameList().end(),
143 std::inserter(nameToRemove, nameToRemove.begin()));
144 for (std::list<string>::iterator it = nameToRemove.begin();
145 it != nameToRemove.end(); ++it)
146 {
akmhoqueb6450b12014-04-24 00:01:03 -0500147 chkNameLsa->removeName((*it));
akmhoque53353462014-04-22 08:43:45 -0500148 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
149 {
150 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
151 {
akmhoquec8a10f72014-04-25 18:42:55 -0500152 pnlsr.getNamePrefixTable().removeNpte((*it), nlsa.getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500153 }
154 }
155 }
156 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
157 {
158 timeToExpire = nlsa.getLifeTime();
159 }
160 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoqueb6450b12014-04-24 00:01:03 -0500161 chkNameLsa->getExpiringEventId());
162 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
163 nlsa.getKey(),
164 nlsa.getLsSeqNo(),
165 timeToExpire));
166 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500167 }
168 }
169 return true;
170}
171
172bool
173Lsdb::addNameLsa(NameLsa& nlsa)
174{
175 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
176 m_nameLsdb.end(),
177 bind(nameLsaCompareByKey, _1,
178 nlsa.getKey()));
179 if (it == m_nameLsdb.end())
180 {
181 m_nameLsdb.push_back(nlsa);
182 return true;
183 }
184 return false;
185}
186
187bool
188Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
189{
190 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
191 m_nameLsdb.end(),
192 bind(nameLsaCompareByKey, _1, key));
193 if (it != m_nameLsdb.end())
194 {
195 (*it).writeLog();
196 if ((*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
197 {
akmhoquec8a10f72014-04-25 18:42:55 -0500198 pnlsr.getNamePrefixTable().removeNpte((*it).getOrigRouter(),
199 (*it).getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500200 for (std::list<string>::iterator nit = (*it).getNpl().getNameList().begin();
201 nit != (*it).getNpl().getNameList().end(); ++nit)
202 {
203 if ((*nit) != pnlsr.getConfParameter().getRouterPrefix())
204 {
akmhoquec8a10f72014-04-25 18:42:55 -0500205 pnlsr.getNamePrefixTable().removeNpte((*nit), (*it).getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500206 }
207 }
208 }
209 m_nameLsdb.erase(it);
210 return true;
211 }
212 return false;
213}
214
215bool
216Lsdb::doesNameLsaExist(string key)
217{
218 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
219 m_nameLsdb.end(),
220 bind(nameLsaCompareByKey, _1, key));
221 if (it == m_nameLsdb.end())
222 {
223 return false;
224 }
225 return true;
226}
227
228void
229Lsdb::printNameLsdb()
230{
231 cout << "---------------Name LSDB-------------------" << endl;
232 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
233 it != m_nameLsdb.end() ; it++)
234 {
235 cout << (*it) << endl;
236 }
237}
238
239// Cor LSA and LSDB related Functions start here
240
241
242static bool
akmhoqueb6450b12014-04-24 00:01:03 -0500243corLsaCompareByKey(const CoordinateLsa& clsa, const string& key)
akmhoque53353462014-04-22 08:43:45 -0500244{
245 return clsa.getKey() == key;
246}
247
248bool
akmhoqueb6450b12014-04-24 00:01:03 -0500249Lsdb::buildAndInstallOwnCoordinateLsa(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500250{
akmhoqueb6450b12014-04-24 00:01:03 -0500251 CoordinateLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
252 , 3
akmhoquec8a10f72014-04-25 18:42:55 -0500253 , pnlsr.getSequencingManager().getCorLsaSeq() + 1
akmhoqueb6450b12014-04-24 00:01:03 -0500254 , pnlsr.getConfParameter().getRouterDeadInterval()
255 , pnlsr.getConfParameter().getCorR()
256 , pnlsr.getConfParameter().getCorTheta());
akmhoquec8a10f72014-04-25 18:42:55 -0500257 pnlsr.getSequencingManager().setCorLsaSeq(
258 pnlsr.getSequencingManager().getCorLsaSeq() + 1);
akmhoqueb6450b12014-04-24 00:01:03 -0500259 installCoordinateLsa(pnlsr, corLsa);
akmhoque53353462014-04-22 08:43:45 -0500260 return true;
261}
262
akmhoqueb6450b12014-04-24 00:01:03 -0500263CoordinateLsa*
264Lsdb::findCoordinateLsa(const string& key)
akmhoque53353462014-04-22 08:43:45 -0500265{
akmhoqueb6450b12014-04-24 00:01:03 -0500266 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
267 m_corLsdb.end(),
268 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500269 if (it != m_corLsdb.end())
270 {
akmhoqueb6450b12014-04-24 00:01:03 -0500271 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500272 }
akmhoqueb6450b12014-04-24 00:01:03 -0500273 return 0;
akmhoque53353462014-04-22 08:43:45 -0500274}
275
276bool
akmhoqueb6450b12014-04-24 00:01:03 -0500277Lsdb::isCoordinateLsaNew(const string& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500278{
akmhoqueb6450b12014-04-24 00:01:03 -0500279 CoordinateLsa* clsa = findCoordinateLsa(key);
280 if (clsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500281 {
akmhoqueb6450b12014-04-24 00:01:03 -0500282 if (clsa->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500283 {
284 return true;
285 }
286 else
287 {
288 return false;
289 }
290 }
291 return true;
292}
293
294ndn::EventId
akmhoqueb6450b12014-04-24 00:01:03 -0500295Lsdb::scheduleCoordinateLsaExpiration(Nlsr& pnlsr, const string& key, int seqNo,
296 int expTime)
akmhoque53353462014-04-22 08:43:45 -0500297{
298 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
akmhoqueb6450b12014-04-24 00:01:03 -0500299 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
akmhoque53353462014-04-22 08:43:45 -0500300 this, boost::ref(pnlsr),
301 key, seqNo));
302}
303
304bool
akmhoqueb6450b12014-04-24 00:01:03 -0500305Lsdb::installCoordinateLsa(Nlsr& pnlsr, CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500306{
307 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500308 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
309 if (chkCorLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500310 {
akmhoqueb6450b12014-04-24 00:01:03 -0500311 addCoordinateLsa(clsa);
akmhoque53353462014-04-22 08:43:45 -0500312 printCorLsdb(); //debugging purpose
313 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
314 {
akmhoquec8a10f72014-04-25 18:42:55 -0500315 pnlsr.getNamePrefixTable().addNpteByDestName(clsa.getOrigRouter(),
316 clsa.getOrigRouter(),
317 pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500318 }
319 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
320 {
321 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
322 }
323 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
324 {
325 timeToExpire = clsa.getLifeTime();
326 }
akmhoqueb6450b12014-04-24 00:01:03 -0500327 scheduleCoordinateLsaExpiration(pnlsr, clsa.getKey(),
328 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500329 }
330 else
331 {
akmhoqueb6450b12014-04-24 00:01:03 -0500332 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500333 {
akmhoqueb6450b12014-04-24 00:01:03 -0500334 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
335 chkCorLsa->setLifeTime(clsa.getLifeTime());
336 if (!chkCorLsa->isEqual(clsa))
akmhoque53353462014-04-22 08:43:45 -0500337 {
akmhoqueb6450b12014-04-24 00:01:03 -0500338 chkCorLsa->setCorRadius(clsa.getCorRadius());
339 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque53353462014-04-22 08:43:45 -0500340 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
341 {
342 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
343 }
344 }
345 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
346 {
347 timeToExpire = clsa.getLifeTime();
348 }
349 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoqueb6450b12014-04-24 00:01:03 -0500350 chkCorLsa->getExpiringEventId());
351 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(pnlsr,
352 clsa.getKey(),
353 clsa.getLsSeqNo(),
354 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500355 }
356 }
357 return true;
358}
359
360bool
akmhoqueb6450b12014-04-24 00:01:03 -0500361Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500362{
akmhoqueb6450b12014-04-24 00:01:03 -0500363 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
364 m_corLsdb.end(),
365 bind(corLsaCompareByKey, _1,
366 clsa.getKey()));
akmhoque53353462014-04-22 08:43:45 -0500367 if (it == m_corLsdb.end())
368 {
369 m_corLsdb.push_back(clsa);
370 return true;
371 }
372 return false;
373}
374
375bool
akmhoqueb6450b12014-04-24 00:01:03 -0500376Lsdb::removeCoordinateLsa(Nlsr& pnlsr, const string& key)
akmhoque53353462014-04-22 08:43:45 -0500377{
akmhoqueb6450b12014-04-24 00:01:03 -0500378 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
379 m_corLsdb.end(),
380 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500381 if (it != m_corLsdb.end())
382 {
383 if ((*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
384 {
akmhoquec8a10f72014-04-25 18:42:55 -0500385 pnlsr.getNamePrefixTable().removeNpte((*it).getOrigRouter(),
386 (*it).getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500387 }
388 m_corLsdb.erase(it);
389 return true;
390 }
391 return false;
392}
393
394bool
akmhoqueb6450b12014-04-24 00:01:03 -0500395Lsdb::doesCoordinateLsaExist(const string& key)
akmhoque53353462014-04-22 08:43:45 -0500396{
akmhoqueb6450b12014-04-24 00:01:03 -0500397 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
398 m_corLsdb.end(),
399 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500400 if (it == m_corLsdb.end())
401 {
402 return false;
403 }
404 return true;
405}
406
407void
408Lsdb::printCorLsdb() //debugging
409{
410 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500411 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque53353462014-04-22 08:43:45 -0500412 it != m_corLsdb.end() ; it++)
413 {
414 cout << (*it) << endl;
415 }
416}
417
418
419// Adj LSA and LSDB related function starts here
420
421static bool
422adjLsaCompareByKey(AdjLsa& alsa, string& key)
423{
424 return alsa.getKey() == key;
425}
426
427
428void
429Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
430{
431 cout << "scheduledAdjLsaBuild Called" << endl;
432 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoquec8a10f72014-04-25 18:42:55 -0500433 if (pnlsr.getAdjacencyList().isAdjLsaBuildable(pnlsr))
akmhoque53353462014-04-22 08:43:45 -0500434 {
435 int adjBuildCount = pnlsr.getAdjBuildCount();
436 if (adjBuildCount > 0)
437 {
akmhoquec8a10f72014-04-25 18:42:55 -0500438 if (pnlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0)
akmhoque53353462014-04-22 08:43:45 -0500439 {
440 buildAndInstallOwnAdjLsa(pnlsr);
441 }
442 else
443 {
444 string key = pnlsr.getConfParameter().getRouterPrefix() + "/2";
445 removeAdjLsa(pnlsr, key);
446 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
447 }
448 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount() - adjBuildCount);
449 }
450 }
451 else
452 {
453 pnlsr.setIsBuildAdjLsaSheduled(1);
454 int schedulingTime = pnlsr.getConfParameter().getInterestRetryNumber() *
455 pnlsr.getConfParameter().getInterestResendTime();
456 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
457 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
458 pnlsr.getLsdb(), boost::ref(pnlsr)));
459 }
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()));
470 if (it == m_adjLsdb.end())
471 {
472 m_adjLsdb.push_back(alsa);
473 return true;
474 }
475 return false;
476}
477
akmhoqueb6450b12014-04-24 00:01:03 -0500478AdjLsa*
479Lsdb::findAdjLsa(const string 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));
484 if (it != m_adjLsdb.end())
485 {
akmhoqueb6450b12014-04-24 00:01:03 -0500486 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500487 }
akmhoqueb6450b12014-04-24 00:01:03 -0500488 return 0;
akmhoque53353462014-04-22 08:43:45 -0500489}
490
491
492bool
493Lsdb::isAdjLsaNew(string key, uint64_t seqNo)
494{
akmhoqueb6450b12014-04-24 00:01:03 -0500495 AdjLsa* adjLsaCheck = findAdjLsa(key);
496 if (adjLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500497 {
akmhoqueb6450b12014-04-24 00:01:03 -0500498 if (adjLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500499 {
500 return true;
501 }
502 else
503 {
504 return false;
505 }
506 }
507 return true;
508}
509
510
511ndn::EventId
512Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
513{
514 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
515 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
516 this, boost::ref(pnlsr),
517 key, seqNo));
518}
519
520bool
521Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa& alsa)
522{
523 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500524 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
525 if (chkAdjLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500526 {
527 addAdjLsa(alsa);
528 alsa.addNptEntries(pnlsr);
529 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
530 if (alsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
531 {
532 timeToExpire = alsa.getLifeTime();
533 }
534 scheduleAdjLsaExpiration(pnlsr, alsa.getKey(),
535 alsa.getLsSeqNo(), timeToExpire);
536 }
537 else
538 {
akmhoqueb6450b12014-04-24 00:01:03 -0500539 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500540 {
akmhoqueb6450b12014-04-24 00:01:03 -0500541 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
542 chkAdjLsa->setLifeTime(alsa.getLifeTime());
543 if (!chkAdjLsa->isEqual(alsa))
akmhoque53353462014-04-22 08:43:45 -0500544 {
akmhoqueb6450b12014-04-24 00:01:03 -0500545 chkAdjLsa->getAdl().reset();
546 chkAdjLsa->getAdl().addAdjacentsFromAdl(alsa.getAdl());
akmhoque53353462014-04-22 08:43:45 -0500547 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
548 }
549 if (alsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
550 {
551 timeToExpire = alsa.getLifeTime();
552 }
akmhoqueb6450b12014-04-24 00:01:03 -0500553 cancelScheduleLsaExpiringEvent(pnlsr, chkAdjLsa->getExpiringEventId());
554 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
555 alsa.getKey(),
556 alsa.getLsSeqNo(),
557 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500558 }
559 }
560 return true;
561}
562
563bool
564Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
565{
566 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
567 , 2
akmhoquec8a10f72014-04-25 18:42:55 -0500568 , pnlsr.getSequencingManager().getAdjLsaSeq() + 1
akmhoque53353462014-04-22 08:43:45 -0500569 , pnlsr.getConfParameter().getRouterDeadInterval()
akmhoquec8a10f72014-04-25 18:42:55 -0500570 , pnlsr.getAdjacencyList().getNumOfActiveNeighbor()
571 , pnlsr.getAdjacencyList());
572 pnlsr.getSequencingManager().setAdjLsaSeq(
573 pnlsr.getSequencingManager().getAdjLsaSeq() + 1);
akmhoque53353462014-04-22 08:43:45 -0500574 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
575 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500576 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
577 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500578 return pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
579}
580
581bool
582Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
583{
584 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
585 m_adjLsdb.end(),
586 bind(adjLsaCompareByKey, _1, key));
587 if (it != m_adjLsdb.end())
588 {
589 (*it).removeNptEntries(pnlsr);
590 m_adjLsdb.erase(it);
591 return true;
592 }
593 return false;
594}
595
596bool
597Lsdb::doesAdjLsaExist(string key)
598{
599 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
600 m_adjLsdb.end(),
601 bind(adjLsaCompareByKey, _1, key));
602 if (it == m_adjLsdb.end())
603 {
604 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
628Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, uint64_t seqNo)
629{
630 cout << "Lsdb::exprireOrRefreshNameLsa Called " << endl;
631 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500632 NameLsa* chkNameLsa = findNameLsa(lsaKey);
633 if (chkNameLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500634 {
akmhoqueb6450b12014-04-24 00:01:03 -0500635 cout << " LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << endl;
636 if (chkNameLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500637 {
akmhoqueb6450b12014-04-24 00:01:03 -0500638 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500639 {
akmhoqueb6450b12014-04-24 00:01:03 -0500640 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500641 cout << "Own Name LSA, so refreshing name LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500642 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoquec8a10f72014-04-25 18:42:55 -0500643 pnlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoqueb6450b12014-04-24 00:01:03 -0500644 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500645 // publish routing update
646 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
647 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500648 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
649 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500650 }
651 else
652 {
653 cout << "Other's Name LSA, so removing form LSDB" << endl;
654 removeNameLsa(pnlsr, lsaKey);
655 }
656 }
657 }
658}
659
660void
661Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, uint64_t seqNo)
662{
663 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
664 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500665 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
666 if (chkAdjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500667 {
akmhoqueb6450b12014-04-24 00:01:03 -0500668 cout << " LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
669 if (chkAdjLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500670 {
akmhoqueb6450b12014-04-24 00:01:03 -0500671 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500672 {
673 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500674 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoquec8a10f72014-04-25 18:42:55 -0500675 pnlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoque53353462014-04-22 08:43:45 -0500676 // publish routing update
677 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
678 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500679 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
680 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500681 }
682 else
683 {
684 cout << "Other's Adj LSA, so removing form LSDB" << endl;
685 removeAdjLsa(pnlsr, lsaKey);
686 }
687 // schedule Routing table calculaiton
688 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
689 }
690 }
691}
692
693void
akmhoqueb6450b12014-04-24 00:01:03 -0500694Lsdb::exprireOrRefreshCoordinateLsa(Nlsr& pnlsr, const string& lsaKey,
695 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500696{
697 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
698 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500699 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
700 if (chkCorLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500701 {
akmhoqueb6450b12014-04-24 00:01:03 -0500702 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
703 if (chkCorLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500704 {
akmhoqueb6450b12014-04-24 00:01:03 -0500705 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500706 {
707 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500708 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoquec8a10f72014-04-25 18:42:55 -0500709 pnlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoque53353462014-04-22 08:43:45 -0500710 // publish routing update
711 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
712 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500713 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
714 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500715 }
716 else
717 {
718 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500719 removeCoordinateLsa(pnlsr, lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500720 }
721 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
722 {
723 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
724 }
725 }
726 }
727}
728
729
730void
731Lsdb::printAdjLsdb()
732{
733 cout << "---------------Adj LSDB-------------------" << endl;
734 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
735 it != m_adjLsdb.end() ; it++)
736 {
737 cout << (*it) << endl;
738 }
739}
740
741//-----utility function -----
742bool
743Lsdb::doesLsaExist(string key, int lsType)
744{
745 if (lsType == 1)
746 {
747 return doesNameLsaExist(key);
748 }
749 else if (lsType == 2)
750 {
751 return doesAdjLsaExist(key);
752 }
753 else if (lsType == 3)
754 {
akmhoqueb6450b12014-04-24 00:01:03 -0500755 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500756 }
757 return false;
758}
759
760}//namespace nlsr
761