blob: 7cdd4b6ef53e09d318db12a9c82fe8e11e541a51 [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
akmhoquefdbddb12014-05-02 18:35:19 -050011Lsdb::cancelScheduleLsaExpiringEvent(Nlsr& pnlsr, ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050012{
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{
akmhoquefdbddb12014-05-02 18:35:19 -050026 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix(),
27 1,
28 pnlsr.getSequencingManager().getNameLsaSeq() + 1,
29 pnlsr.getConfParameter().getRouterDeadInterval(),
30 pnlsr.getNamePrefixList());
31 pnlsr.getSequencingManager().increaseNameLsaSeq();
akmhoque53353462014-04-22 08:43:45 -050032 return installNameLsa(pnlsr, nameLsa);
33}
34
akmhoqueb6450b12014-04-24 00:01:03 -050035NameLsa*
36Lsdb::findNameLsa(const string key)
akmhoque53353462014-04-22 08:43:45 -050037{
38 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
39 m_nameLsdb.end(),
40 bind(nameLsaCompareByKey, _1, key));
41 if (it != m_nameLsdb.end())
42 {
akmhoqueb6450b12014-04-24 00:01:03 -050043 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050044 }
akmhoqueb6450b12014-04-24 00:01:03 -050045 return 0;
akmhoque53353462014-04-22 08:43:45 -050046}
47
48bool
49Lsdb::isNameLsaNew(string key, uint64_t seqNo)
50{
akmhoqueb6450b12014-04-24 00:01:03 -050051 NameLsa* nameLsaCheck = findNameLsa(key);
52 if (nameLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -050053 {
akmhoqueb6450b12014-04-24 00:01:03 -050054 if (nameLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -050055 {
56 return true;
57 }
58 else
59 {
60 return false;
61 }
62 }
63 return true;
64}
65
66ndn::EventId
67Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
68{
69 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
70 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
71 this, boost::ref(pnlsr), key, seqNo));
72}
73
74bool
75Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa& nlsa)
76{
77 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -050078 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
79 if (chkNameLsa == 0)
akmhoque53353462014-04-22 08:43:45 -050080 {
81 addNameLsa(nlsa);
82 nlsa.writeLog();
83 printNameLsdb();
84 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
85 {
akmhoquefdbddb12014-05-02 18:35:19 -050086 pnlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
87 nlsa.getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -050088 std::list<string> nameList = nlsa.getNpl().getNameList();
89 for (std::list<string>::iterator it = nameList.begin(); it != nameList.end();
90 it++)
91 {
92 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
93 {
akmhoquefdbddb12014-05-02 18:35:19 -050094 pnlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -050095 }
96 }
97 }
98 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
99 {
100 timeToExpire = nlsa.getLifeTime();
101 }
102 nlsa.setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
103 nlsa.getKey(),
104 nlsa.getLsSeqNo(),
105 timeToExpire));
106 }
107 else
108 {
akmhoqueb6450b12014-04-24 00:01:03 -0500109 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500110 {
akmhoqueb6450b12014-04-24 00:01:03 -0500111 chkNameLsa->writeLog();
112 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
113 chkNameLsa->setLifeTime(nlsa.getLifeTime());
114 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500115 nlsa.getNpl().sort();
116 std::list<string> nameToAdd;
117 std::set_difference(nlsa.getNpl().getNameList().begin(),
118 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500119 chkNameLsa->getNpl().getNameList().begin(),
120 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500121 std::inserter(nameToAdd, nameToAdd.begin()));
122 for (std::list<string>::iterator it = nameToAdd.begin(); it != nameToAdd.end();
123 ++it)
124 {
akmhoqueb6450b12014-04-24 00:01:03 -0500125 chkNameLsa->addName((*it));
akmhoque53353462014-04-22 08:43:45 -0500126 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
127 {
128 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
129 {
akmhoquefdbddb12014-05-02 18:35:19 -0500130 pnlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500131 }
132 }
133 }
134 std::list<string> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500135 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
136 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500137 nlsa.getNpl().getNameList().begin(),
138 nlsa.getNpl().getNameList().end(),
139 std::inserter(nameToRemove, nameToRemove.begin()));
140 for (std::list<string>::iterator it = nameToRemove.begin();
141 it != nameToRemove.end(); ++it)
142 {
akmhoqueb6450b12014-04-24 00:01:03 -0500143 chkNameLsa->removeName((*it));
akmhoque53353462014-04-22 08:43:45 -0500144 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
145 {
146 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
147 {
akmhoquefdbddb12014-05-02 18:35:19 -0500148 pnlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500149 }
150 }
151 }
152 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
153 {
154 timeToExpire = nlsa.getLifeTime();
155 }
156 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoqueb6450b12014-04-24 00:01:03 -0500157 chkNameLsa->getExpiringEventId());
158 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
159 nlsa.getKey(),
160 nlsa.getLsSeqNo(),
161 timeToExpire));
162 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500163 }
164 }
165 return true;
166}
167
168bool
169Lsdb::addNameLsa(NameLsa& nlsa)
170{
171 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
172 m_nameLsdb.end(),
173 bind(nameLsaCompareByKey, _1,
174 nlsa.getKey()));
175 if (it == m_nameLsdb.end())
176 {
177 m_nameLsdb.push_back(nlsa);
178 return true;
179 }
180 return false;
181}
182
183bool
184Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
185{
186 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
187 m_nameLsdb.end(),
188 bind(nameLsaCompareByKey, _1, key));
189 if (it != m_nameLsdb.end())
190 {
191 (*it).writeLog();
192 if ((*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
193 {
akmhoquefdbddb12014-05-02 18:35:19 -0500194 pnlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
195 (*it).getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500196 for (std::list<string>::iterator nit = (*it).getNpl().getNameList().begin();
197 nit != (*it).getNpl().getNameList().end(); ++nit)
198 {
199 if ((*nit) != pnlsr.getConfParameter().getRouterPrefix())
200 {
akmhoquefdbddb12014-05-02 18:35:19 -0500201 pnlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500202 }
203 }
204 }
205 m_nameLsdb.erase(it);
206 return true;
207 }
208 return false;
209}
210
211bool
212Lsdb::doesNameLsaExist(string key)
213{
214 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
215 m_nameLsdb.end(),
216 bind(nameLsaCompareByKey, _1, key));
217 if (it == m_nameLsdb.end())
218 {
219 return false;
220 }
221 return true;
222}
223
224void
225Lsdb::printNameLsdb()
226{
227 cout << "---------------Name LSDB-------------------" << endl;
228 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
229 it != m_nameLsdb.end() ; it++)
230 {
231 cout << (*it) << endl;
232 }
233}
234
235// Cor LSA and LSDB related Functions start here
236
237
238static bool
akmhoqueb6450b12014-04-24 00:01:03 -0500239corLsaCompareByKey(const CoordinateLsa& clsa, const string& key)
akmhoque53353462014-04-22 08:43:45 -0500240{
241 return clsa.getKey() == key;
242}
243
244bool
akmhoqueb6450b12014-04-24 00:01:03 -0500245Lsdb::buildAndInstallOwnCoordinateLsa(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500246{
akmhoquefdbddb12014-05-02 18:35:19 -0500247 CoordinateLsa corLsa(pnlsr.getConfParameter().getRouterPrefix(),
248 3,
249 pnlsr.getSequencingManager().getCorLsaSeq() + 1,
250 pnlsr.getConfParameter().getRouterDeadInterval(),
251 pnlsr.getConfParameter().getCorR(),
252 pnlsr.getConfParameter().getCorTheta());
253 pnlsr.getSequencingManager().increaseCorLsaSeq();
akmhoqueb6450b12014-04-24 00:01:03 -0500254 installCoordinateLsa(pnlsr, corLsa);
akmhoque53353462014-04-22 08:43:45 -0500255 return true;
256}
257
akmhoqueb6450b12014-04-24 00:01:03 -0500258CoordinateLsa*
259Lsdb::findCoordinateLsa(const string& key)
akmhoque53353462014-04-22 08:43:45 -0500260{
akmhoqueb6450b12014-04-24 00:01:03 -0500261 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
262 m_corLsdb.end(),
263 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500264 if (it != m_corLsdb.end())
265 {
akmhoqueb6450b12014-04-24 00:01:03 -0500266 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500267 }
akmhoqueb6450b12014-04-24 00:01:03 -0500268 return 0;
akmhoque53353462014-04-22 08:43:45 -0500269}
270
271bool
akmhoqueb6450b12014-04-24 00:01:03 -0500272Lsdb::isCoordinateLsaNew(const string& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500273{
akmhoqueb6450b12014-04-24 00:01:03 -0500274 CoordinateLsa* clsa = findCoordinateLsa(key);
275 if (clsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500276 {
akmhoqueb6450b12014-04-24 00:01:03 -0500277 if (clsa->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500278 {
279 return true;
280 }
281 else
282 {
283 return false;
284 }
285 }
286 return true;
287}
288
289ndn::EventId
akmhoqueb6450b12014-04-24 00:01:03 -0500290Lsdb::scheduleCoordinateLsaExpiration(Nlsr& pnlsr, const string& key, int seqNo,
291 int expTime)
akmhoque53353462014-04-22 08:43:45 -0500292{
293 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
akmhoqueb6450b12014-04-24 00:01:03 -0500294 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
akmhoque53353462014-04-22 08:43:45 -0500295 this, boost::ref(pnlsr),
296 key, seqNo));
297}
298
299bool
akmhoqueb6450b12014-04-24 00:01:03 -0500300Lsdb::installCoordinateLsa(Nlsr& pnlsr, CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500301{
302 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500303 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
304 if (chkCorLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500305 {
akmhoqueb6450b12014-04-24 00:01:03 -0500306 addCoordinateLsa(clsa);
akmhoque53353462014-04-22 08:43:45 -0500307 printCorLsdb(); //debugging purpose
308 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
309 {
akmhoquefdbddb12014-05-02 18:35:19 -0500310 pnlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
311 clsa.getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500312 }
313 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
314 {
315 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
316 }
317 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
318 {
319 timeToExpire = clsa.getLifeTime();
320 }
akmhoqueb6450b12014-04-24 00:01:03 -0500321 scheduleCoordinateLsaExpiration(pnlsr, clsa.getKey(),
322 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500323 }
324 else
325 {
akmhoqueb6450b12014-04-24 00:01:03 -0500326 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500327 {
akmhoqueb6450b12014-04-24 00:01:03 -0500328 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
329 chkCorLsa->setLifeTime(clsa.getLifeTime());
akmhoquefdbddb12014-05-02 18:35:19 -0500330 if (!chkCorLsa->isEqualContent(clsa))
akmhoque53353462014-04-22 08:43:45 -0500331 {
akmhoqueb6450b12014-04-24 00:01:03 -0500332 chkCorLsa->setCorRadius(clsa.getCorRadius());
333 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque53353462014-04-22 08:43:45 -0500334 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
335 {
336 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
337 }
338 }
339 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
340 {
341 timeToExpire = clsa.getLifeTime();
342 }
343 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoqueb6450b12014-04-24 00:01:03 -0500344 chkCorLsa->getExpiringEventId());
345 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(pnlsr,
346 clsa.getKey(),
347 clsa.getLsSeqNo(),
348 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500349 }
350 }
351 return true;
352}
353
354bool
akmhoqueb6450b12014-04-24 00:01:03 -0500355Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500356{
akmhoqueb6450b12014-04-24 00:01:03 -0500357 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
358 m_corLsdb.end(),
359 bind(corLsaCompareByKey, _1,
360 clsa.getKey()));
akmhoque53353462014-04-22 08:43:45 -0500361 if (it == m_corLsdb.end())
362 {
363 m_corLsdb.push_back(clsa);
364 return true;
365 }
366 return false;
367}
368
369bool
akmhoqueb6450b12014-04-24 00:01:03 -0500370Lsdb::removeCoordinateLsa(Nlsr& pnlsr, const string& key)
akmhoque53353462014-04-22 08:43:45 -0500371{
akmhoqueb6450b12014-04-24 00:01:03 -0500372 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
373 m_corLsdb.end(),
374 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500375 if (it != m_corLsdb.end())
376 {
377 if ((*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
378 {
akmhoquefdbddb12014-05-02 18:35:19 -0500379 pnlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
380 (*it).getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500381 }
382 m_corLsdb.erase(it);
383 return true;
384 }
385 return false;
386}
387
388bool
akmhoqueb6450b12014-04-24 00:01:03 -0500389Lsdb::doesCoordinateLsaExist(const string& key)
akmhoque53353462014-04-22 08:43:45 -0500390{
akmhoqueb6450b12014-04-24 00:01:03 -0500391 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
392 m_corLsdb.end(),
393 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500394 if (it == m_corLsdb.end())
395 {
396 return false;
397 }
398 return true;
399}
400
401void
402Lsdb::printCorLsdb() //debugging
403{
404 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500405 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque53353462014-04-22 08:43:45 -0500406 it != m_corLsdb.end() ; it++)
407 {
408 cout << (*it) << endl;
409 }
410}
411
412
413// Adj LSA and LSDB related function starts here
414
415static bool
416adjLsaCompareByKey(AdjLsa& alsa, string& key)
417{
418 return alsa.getKey() == key;
419}
420
421
422void
423Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
424{
425 cout << "scheduledAdjLsaBuild Called" << endl;
426 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoquec8a10f72014-04-25 18:42:55 -0500427 if (pnlsr.getAdjacencyList().isAdjLsaBuildable(pnlsr))
akmhoque53353462014-04-22 08:43:45 -0500428 {
429 int adjBuildCount = pnlsr.getAdjBuildCount();
430 if (adjBuildCount > 0)
431 {
akmhoquec8a10f72014-04-25 18:42:55 -0500432 if (pnlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0)
akmhoque53353462014-04-22 08:43:45 -0500433 {
434 buildAndInstallOwnAdjLsa(pnlsr);
435 }
436 else
437 {
438 string key = pnlsr.getConfParameter().getRouterPrefix() + "/2";
439 removeAdjLsa(pnlsr, key);
440 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
441 }
442 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount() - adjBuildCount);
443 }
444 }
445 else
446 {
447 pnlsr.setIsBuildAdjLsaSheduled(1);
448 int schedulingTime = pnlsr.getConfParameter().getInterestRetryNumber() *
449 pnlsr.getConfParameter().getInterestResendTime();
450 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
451 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
452 pnlsr.getLsdb(), boost::ref(pnlsr)));
453 }
454}
455
456
457bool
458Lsdb::addAdjLsa(AdjLsa& alsa)
459{
460 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
461 m_adjLsdb.end(),
462 bind(adjLsaCompareByKey, _1,
463 alsa.getKey()));
464 if (it == m_adjLsdb.end())
465 {
466 m_adjLsdb.push_back(alsa);
467 return true;
468 }
469 return false;
470}
471
akmhoqueb6450b12014-04-24 00:01:03 -0500472AdjLsa*
473Lsdb::findAdjLsa(const string key)
akmhoque53353462014-04-22 08:43:45 -0500474{
475 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
476 m_adjLsdb.end(),
477 bind(adjLsaCompareByKey, _1, key));
478 if (it != m_adjLsdb.end())
479 {
akmhoqueb6450b12014-04-24 00:01:03 -0500480 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500481 }
akmhoqueb6450b12014-04-24 00:01:03 -0500482 return 0;
akmhoque53353462014-04-22 08:43:45 -0500483}
484
485
486bool
487Lsdb::isAdjLsaNew(string key, uint64_t seqNo)
488{
akmhoqueb6450b12014-04-24 00:01:03 -0500489 AdjLsa* adjLsaCheck = findAdjLsa(key);
490 if (adjLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500491 {
akmhoqueb6450b12014-04-24 00:01:03 -0500492 if (adjLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500493 {
494 return true;
495 }
496 else
497 {
498 return false;
499 }
500 }
501 return true;
502}
503
504
505ndn::EventId
506Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
507{
508 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
509 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
510 this, boost::ref(pnlsr),
511 key, seqNo));
512}
513
514bool
515Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa& alsa)
516{
517 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500518 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
519 if (chkAdjLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500520 {
521 addAdjLsa(alsa);
522 alsa.addNptEntries(pnlsr);
523 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
524 if (alsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
525 {
526 timeToExpire = alsa.getLifeTime();
527 }
528 scheduleAdjLsaExpiration(pnlsr, alsa.getKey(),
529 alsa.getLsSeqNo(), timeToExpire);
530 }
531 else
532 {
akmhoqueb6450b12014-04-24 00:01:03 -0500533 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500534 {
akmhoqueb6450b12014-04-24 00:01:03 -0500535 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
536 chkAdjLsa->setLifeTime(alsa.getLifeTime());
akmhoquefdbddb12014-05-02 18:35:19 -0500537 if (!chkAdjLsa->isEqualContent(alsa))
akmhoque53353462014-04-22 08:43:45 -0500538 {
akmhoqueb6450b12014-04-24 00:01:03 -0500539 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500540 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque53353462014-04-22 08:43:45 -0500541 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
542 }
543 if (alsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
544 {
545 timeToExpire = alsa.getLifeTime();
546 }
akmhoqueb6450b12014-04-24 00:01:03 -0500547 cancelScheduleLsaExpiringEvent(pnlsr, chkAdjLsa->getExpiringEventId());
548 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
549 alsa.getKey(),
550 alsa.getLsSeqNo(),
551 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500552 }
553 }
554 return true;
555}
556
557bool
558Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
559{
akmhoquefdbddb12014-05-02 18:35:19 -0500560 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix(),
561 2,
562 pnlsr.getSequencingManager().getAdjLsaSeq() + 1,
563 pnlsr.getConfParameter().getRouterDeadInterval(),
564 pnlsr.getAdjacencyList().getNumOfActiveNeighbor(),
565 pnlsr.getAdjacencyList());
566 pnlsr.getSequencingManager().increaseAdjLsaSeq();
567 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix() +
568 pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500569 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
570 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500571 return pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
572}
573
574bool
575Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
576{
577 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
578 m_adjLsdb.end(),
579 bind(adjLsaCompareByKey, _1, key));
580 if (it != m_adjLsdb.end())
581 {
582 (*it).removeNptEntries(pnlsr);
583 m_adjLsdb.erase(it);
584 return true;
585 }
586 return false;
587}
588
589bool
590Lsdb::doesAdjLsaExist(string key)
591{
592 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
593 m_adjLsdb.end(),
594 bind(adjLsaCompareByKey, _1, key));
595 if (it == m_adjLsdb.end())
596 {
597 return false;
598 }
599 return true;
600}
601
602std::list<AdjLsa>&
603Lsdb::getAdjLsdb()
604{
605 return m_adjLsdb;
606}
607
608void
609Lsdb::setLsaRefreshTime(int lrt)
610{
611 m_lsaRefreshTime = lrt;
612}
613
614void
615Lsdb::setThisRouterPrefix(string trp)
616{
617 m_thisRouterPrefix = trp;
618}
619
620void
621Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, uint64_t seqNo)
622{
623 cout << "Lsdb::exprireOrRefreshNameLsa Called " << endl;
624 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500625 NameLsa* chkNameLsa = findNameLsa(lsaKey);
626 if (chkNameLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500627 {
akmhoqueb6450b12014-04-24 00:01:03 -0500628 cout << " LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << endl;
629 if (chkNameLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500630 {
akmhoqueb6450b12014-04-24 00:01:03 -0500631 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500632 {
akmhoqueb6450b12014-04-24 00:01:03 -0500633 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500634 cout << "Own Name LSA, so refreshing name LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500635 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoquec8a10f72014-04-25 18:42:55 -0500636 pnlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoqueb6450b12014-04-24 00:01:03 -0500637 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500638 // schedule refreshing event again
639 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
640 chkNameLsa->getKey(),
641 chkNameLsa->getLsSeqNo(),
642 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500643 // publish routing update
644 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
645 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500646 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
647 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500648 }
649 else
650 {
651 cout << "Other's Name LSA, so removing form LSDB" << endl;
652 removeNameLsa(pnlsr, lsaKey);
653 }
654 }
655 }
656}
657
658void
659Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, uint64_t seqNo)
660{
661 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
662 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500663 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
664 if (chkAdjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500665 {
akmhoqueb6450b12014-04-24 00:01:03 -0500666 cout << " LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
667 if (chkAdjLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500668 {
akmhoqueb6450b12014-04-24 00:01:03 -0500669 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500670 {
671 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500672 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoquec8a10f72014-04-25 18:42:55 -0500673 pnlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500674 // schedule refreshing event again
675 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
676 chkAdjLsa->getKey(),
677 chkAdjLsa->getLsSeqNo(),
678 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500679 // publish routing update
680 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
681 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500682 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
683 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500684 }
685 else
686 {
687 cout << "Other's Adj LSA, so removing form LSDB" << endl;
688 removeAdjLsa(pnlsr, lsaKey);
689 }
690 // schedule Routing table calculaiton
691 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
692 }
693 }
694}
695
696void
akmhoqueb6450b12014-04-24 00:01:03 -0500697Lsdb::exprireOrRefreshCoordinateLsa(Nlsr& pnlsr, const string& lsaKey,
698 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500699{
700 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
701 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500702 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
703 if (chkCorLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500704 {
akmhoqueb6450b12014-04-24 00:01:03 -0500705 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
706 if (chkCorLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500707 {
akmhoqueb6450b12014-04-24 00:01:03 -0500708 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500709 {
710 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500711 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoquec8a10f72014-04-25 18:42:55 -0500712 pnlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500713 // schedule refreshing event again
714 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(pnlsr,
715 chkCorLsa->getKey(),
716 chkCorLsa->getLsSeqNo(),
717 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500718 // publish routing update
719 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
720 + pnlsr.getConfParameter().getRouterPrefix();
akmhoquec8a10f72014-04-25 18:42:55 -0500721 pnlsr.getSyncLogicHandler().publishRoutingUpdate(pnlsr.getSequencingManager(),
722 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500723 }
724 else
725 {
726 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500727 removeCoordinateLsa(pnlsr, lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500728 }
729 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
730 {
731 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
732 }
733 }
734 }
735}
736
737
738void
739Lsdb::printAdjLsdb()
740{
741 cout << "---------------Adj LSDB-------------------" << endl;
742 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
743 it != m_adjLsdb.end() ; it++)
744 {
745 cout << (*it) << endl;
746 }
747}
748
749//-----utility function -----
750bool
751Lsdb::doesLsaExist(string key, int lsType)
752{
753 if (lsType == 1)
754 {
755 return doesNameLsaExist(key);
756 }
757 else if (lsType == 2)
758 {
759 return doesAdjLsaExist(key);
760 }
761 else if (lsType == 3)
762 {
akmhoqueb6450b12014-04-24 00:01:03 -0500763 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500764 }
765 return false;
766}
767
768}//namespace nlsr
769