blob: a5eb2b2f74a9d8a8140f5629cf2ed41d2daa4e17 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <utility>
3#include "lsdb.hpp"
4#include "nlsr.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -05005#include "utility/name-helper.hpp"
akmhoque53353462014-04-22 08:43:45 -05006
7namespace nlsr {
8
9using namespace std;
10
11void
akmhoque31d1d4b2014-05-05 22:08:14 -050012Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050013{
akmhoque31d1d4b2014-05-05 22:08:14 -050014 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050015}
16
17static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050018nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050019{
20 return nlsa1.getKey() == key;
21}
22
23
24bool
akmhoque31d1d4b2014-05-05 22:08:14 -050025Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050026{
akmhoque31d1d4b2014-05-05 22:08:14 -050027 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
28 "name",
29 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
30 m_nlsr.getConfParameter().getRouterDeadInterval(),
31 m_nlsr.getNamePrefixList());
32 m_nlsr.getSequencingManager().increaseNameLsaSeq();
33 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050034}
35
akmhoqueb6450b12014-04-24 00:01:03 -050036NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050037Lsdb::findNameLsa(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -050050Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050051{
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
akmhoque31d1d4b2014-05-05 22:08:14 -050068Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
akmhoque53353462014-04-22 08:43:45 -050069{
akmhoque31d1d4b2014-05-05 22:08:14 -050070 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
71 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
72 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050073}
74
75bool
akmhoque31d1d4b2014-05-05 22:08:14 -050076Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -050077{
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();
akmhoque31d1d4b2014-05-05 22:08:14 -050085 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -050086 {
akmhoque31d1d4b2014-05-05 22:08:14 -050087 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
88 nlsa.getOrigRouter());
89 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
90 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque53353462014-04-22 08:43:45 -050091 it++)
92 {
akmhoque31d1d4b2014-05-05 22:08:14 -050093 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -050094 {
akmhoque31d1d4b2014-05-05 22:08:14 -050095 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -050096 }
97 }
98 }
akmhoque31d1d4b2014-05-05 22:08:14 -050099 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500100 {
101 timeToExpire = nlsa.getLifeTime();
102 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500104 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();
akmhoque31d1d4b2014-05-05 22:08:14 -0500116 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500117 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()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
123 it != nameToAdd.end();
akmhoque53353462014-04-22 08:43:45 -0500124 ++it)
125 {
akmhoqueb6450b12014-04-24 00:01:03 -0500126 chkNameLsa->addName((*it));
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500128 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500130 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500131 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500132 }
133 }
134 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500135 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500136 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
137 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500138 nlsa.getNpl().getNameList().begin(),
139 nlsa.getNpl().getNameList().end(),
140 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500141 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque53353462014-04-22 08:43:45 -0500142 it != nameToRemove.end(); ++it)
143 {
akmhoqueb6450b12014-04-24 00:01:03 -0500144 chkNameLsa->removeName((*it));
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500146 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500148 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500150 }
151 }
152 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500153 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500154 {
155 timeToExpire = nlsa.getLifeTime();
156 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500157 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
158 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500159 nlsa.getLsSeqNo(),
160 timeToExpire));
161 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500162 }
163 }
164 return true;
165}
166
167bool
168Lsdb::addNameLsa(NameLsa& nlsa)
169{
170 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
171 m_nameLsdb.end(),
172 bind(nameLsaCompareByKey, _1,
173 nlsa.getKey()));
174 if (it == m_nameLsdb.end())
175 {
176 m_nameLsdb.push_back(nlsa);
177 return true;
178 }
179 return false;
180}
181
182bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500183Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500184{
185 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
186 m_nameLsdb.end(),
187 bind(nameLsaCompareByKey, _1, key));
188 if (it != m_nameLsdb.end())
189 {
190 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 if ((*it).getOrigRouter() !=
192 m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500193 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500194 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
195 (*it).getOrigRouter());
196 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque53353462014-04-22 08:43:45 -0500197 nit != (*it).getNpl().getNameList().end(); ++nit)
198 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500199 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500200 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500201 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500202 }
203 }
204 }
205 m_nameLsdb.erase(it);
206 return true;
207 }
208 return false;
209}
210
211bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500212Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500213{
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
akmhoque31d1d4b2014-05-05 22:08:14 -0500239corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500240{
241 return clsa.getKey() == key;
242}
243
244bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500245Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500246{
akmhoque31d1d4b2014-05-05 22:08:14 -0500247 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
248 "coordinate",
249 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
250 m_nlsr.getConfParameter().getRouterDeadInterval(),
251 m_nlsr.getConfParameter().getCorR(),
252 m_nlsr.getConfParameter().getCorTheta());
253 m_nlsr.getSequencingManager().increaseCorLsaSeq();
254 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500255 return true;
256}
257
akmhoqueb6450b12014-04-24 00:01:03 -0500258CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500259Lsdb::findCoordinateLsa(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -0500272Lsdb::isCoordinateLsaNew(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -0500290Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoqueb6450b12014-04-24 00:01:03 -0500291 int expTime)
akmhoque53353462014-04-22 08:43:45 -0500292{
akmhoque31d1d4b2014-05-05 22:08:14 -0500293 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
294 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
295 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500296}
297
298bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500299Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500300{
301 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500302 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
303 if (chkCorLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500304 {
akmhoqueb6450b12014-04-24 00:01:03 -0500305 addCoordinateLsa(clsa);
akmhoque53353462014-04-22 08:43:45 -0500306 printCorLsdb(); //debugging purpose
akmhoque31d1d4b2014-05-05 22:08:14 -0500307 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500308 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500309 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
310 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500311 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500312 if (m_nlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
akmhoque53353462014-04-22 08:43:45 -0500313 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500314 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500315 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500316 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500317 {
318 timeToExpire = clsa.getLifeTime();
319 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500320 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500321 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500322 }
323 else
324 {
akmhoqueb6450b12014-04-24 00:01:03 -0500325 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500326 {
akmhoqueb6450b12014-04-24 00:01:03 -0500327 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
328 chkCorLsa->setLifeTime(clsa.getLifeTime());
akmhoquefdbddb12014-05-02 18:35:19 -0500329 if (!chkCorLsa->isEqualContent(clsa))
akmhoque53353462014-04-22 08:43:45 -0500330 {
akmhoqueb6450b12014-04-24 00:01:03 -0500331 chkCorLsa->setCorRadius(clsa.getCorRadius());
332 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque31d1d4b2014-05-05 22:08:14 -0500333 if (m_nlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
akmhoque53353462014-04-22 08:43:45 -0500334 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500335 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500336 }
337 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500338 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500339 {
340 timeToExpire = clsa.getLifeTime();
341 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500342 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
343 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500344 clsa.getLsSeqNo(),
345 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500346 }
347 }
348 return true;
349}
350
351bool
akmhoqueb6450b12014-04-24 00:01:03 -0500352Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500353{
akmhoqueb6450b12014-04-24 00:01:03 -0500354 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
355 m_corLsdb.end(),
356 bind(corLsaCompareByKey, _1,
357 clsa.getKey()));
akmhoque53353462014-04-22 08:43:45 -0500358 if (it == m_corLsdb.end())
359 {
360 m_corLsdb.push_back(clsa);
361 return true;
362 }
363 return false;
364}
365
366bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500367Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500368{
akmhoqueb6450b12014-04-24 00:01:03 -0500369 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
370 m_corLsdb.end(),
371 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500372 if (it != m_corLsdb.end())
373 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500374 if ((*it).getOrigRouter() !=
375 m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500376 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500377 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
378 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500379 }
380 m_corLsdb.erase(it);
381 return true;
382 }
383 return false;
384}
385
386bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500387Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500388{
akmhoqueb6450b12014-04-24 00:01:03 -0500389 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
390 m_corLsdb.end(),
391 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500392 if (it == m_corLsdb.end())
393 {
394 return false;
395 }
396 return true;
397}
398
399void
400Lsdb::printCorLsdb() //debugging
401{
402 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500403 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque53353462014-04-22 08:43:45 -0500404 it != m_corLsdb.end() ; it++)
405 {
406 cout << (*it) << endl;
407 }
408}
409
410
411// Adj LSA and LSDB related function starts here
412
413static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500414adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500415{
416 return alsa.getKey() == key;
417}
418
419
420void
akmhoque31d1d4b2014-05-05 22:08:14 -0500421Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500422{
423 cout << "scheduledAdjLsaBuild Called" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500424 m_nlsr.setIsBuildAdjLsaSheduled(0);
425 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr))
akmhoque53353462014-04-22 08:43:45 -0500426 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500427 int adjBuildCount = m_nlsr.getAdjBuildCount();
akmhoque53353462014-04-22 08:43:45 -0500428 if (adjBuildCount > 0)
429 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500430 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0)
akmhoque53353462014-04-22 08:43:45 -0500431 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500432 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500433 }
434 else
435 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500436 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
437 key.append("adjacency");
438 removeAdjLsa(key);
439 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500440 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500441 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500442 }
443 }
444 else
445 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500446 m_nlsr.setIsBuildAdjLsaSheduled(1);
447 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
448 m_nlsr.getConfParameter().getInterestResendTime();
449 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
450 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
451 this));
akmhoque53353462014-04-22 08:43:45 -0500452 }
453}
454
455
456bool
457Lsdb::addAdjLsa(AdjLsa& alsa)
458{
459 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
460 m_adjLsdb.end(),
461 bind(adjLsaCompareByKey, _1,
462 alsa.getKey()));
463 if (it == m_adjLsdb.end())
464 {
465 m_adjLsdb.push_back(alsa);
466 return true;
467 }
468 return false;
469}
470
akmhoqueb6450b12014-04-24 00:01:03 -0500471AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500472Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500473{
474 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
475 m_adjLsdb.end(),
476 bind(adjLsaCompareByKey, _1, key));
477 if (it != m_adjLsdb.end())
478 {
akmhoqueb6450b12014-04-24 00:01:03 -0500479 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500480 }
akmhoqueb6450b12014-04-24 00:01:03 -0500481 return 0;
akmhoque53353462014-04-22 08:43:45 -0500482}
483
484
485bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500486Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500487{
akmhoqueb6450b12014-04-24 00:01:03 -0500488 AdjLsa* adjLsaCheck = findAdjLsa(key);
489 if (adjLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500490 {
akmhoqueb6450b12014-04-24 00:01:03 -0500491 if (adjLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500492 {
493 return true;
494 }
495 else
496 {
497 return false;
498 }
499 }
500 return true;
501}
502
503
504ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500505Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
akmhoque53353462014-04-22 08:43:45 -0500506{
akmhoque31d1d4b2014-05-05 22:08:14 -0500507 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
508 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
509 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500510}
511
512bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500513Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500514{
515 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500516 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
517 if (chkAdjLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500518 {
519 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500520 alsa.addNptEntries(m_nlsr);
521 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
522 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500523 {
524 timeToExpire = alsa.getLifeTime();
525 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500526 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500527 alsa.getLsSeqNo(), timeToExpire);
528 }
529 else
530 {
akmhoqueb6450b12014-04-24 00:01:03 -0500531 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500532 {
akmhoqueb6450b12014-04-24 00:01:03 -0500533 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
534 chkAdjLsa->setLifeTime(alsa.getLifeTime());
akmhoquefdbddb12014-05-02 18:35:19 -0500535 if (!chkAdjLsa->isEqualContent(alsa))
akmhoque53353462014-04-22 08:43:45 -0500536 {
akmhoqueb6450b12014-04-24 00:01:03 -0500537 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500538 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500539 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500540 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500541 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix())
akmhoque53353462014-04-22 08:43:45 -0500542 {
543 timeToExpire = alsa.getLifeTime();
544 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500545 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
546 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500547 alsa.getLsSeqNo(),
548 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500549 }
550 }
551 return true;
552}
553
554bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500555Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500556{
akmhoque31d1d4b2014-05-05 22:08:14 -0500557 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
558 "adjacency",
559 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
560 m_nlsr.getConfParameter().getRouterDeadInterval(),
561 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
562 m_nlsr.getAdjacencyList());
563 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
564 // publish routing update
565 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
566 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
567 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
568 lsaPrefix);
569 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500570}
571
572bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500573Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500574{
575 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
576 m_adjLsdb.end(),
577 bind(adjLsaCompareByKey, _1, key));
578 if (it != m_adjLsdb.end())
579 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500580 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500581 m_adjLsdb.erase(it);
582 return true;
583 }
584 return false;
585}
586
587bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500588Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500589{
590 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
591 m_adjLsdb.end(),
592 bind(adjLsaCompareByKey, _1, key));
593 if (it == m_adjLsdb.end())
594 {
595 return false;
596 }
597 return true;
598}
599
600std::list<AdjLsa>&
601Lsdb::getAdjLsdb()
602{
603 return m_adjLsdb;
604}
605
606void
607Lsdb::setLsaRefreshTime(int lrt)
608{
609 m_lsaRefreshTime = lrt;
610}
611
612void
613Lsdb::setThisRouterPrefix(string trp)
614{
615 m_thisRouterPrefix = trp;
616}
617
618void
akmhoque31d1d4b2014-05-05 22:08:14 -0500619Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500620{
621 cout << "Lsdb::exprireOrRefreshNameLsa Called " << endl;
622 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500623 NameLsa* chkNameLsa = findNameLsa(lsaKey);
624 if (chkNameLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500625 {
akmhoqueb6450b12014-04-24 00:01:03 -0500626 cout << " LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << endl;
627 if (chkNameLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500628 {
akmhoqueb6450b12014-04-24 00:01:03 -0500629 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500630 {
akmhoqueb6450b12014-04-24 00:01:03 -0500631 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500632 cout << "Own Name LSA, so refreshing name LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500633 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500634 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoqueb6450b12014-04-24 00:01:03 -0500635 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500636 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500637 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500638 chkNameLsa->getLsSeqNo(),
639 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500640 // publish routing update
akmhoque31d1d4b2014-05-05 22:08:14 -0500641 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
642 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
643 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
644 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500645 }
646 else
647 {
648 cout << "Other's Name LSA, so removing form LSDB" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500649 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500650 }
651 }
652 }
653}
654
655void
akmhoque31d1d4b2014-05-05 22:08:14 -0500656Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500657{
658 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
659 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500660 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
661 if (chkAdjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500662 {
akmhoqueb6450b12014-04-24 00:01:03 -0500663 cout << " LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
664 if (chkAdjLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500665 {
akmhoqueb6450b12014-04-24 00:01:03 -0500666 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500667 {
668 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500669 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500670 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500671 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500672 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500673 chkAdjLsa->getLsSeqNo(),
674 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500675 // publish routing update
akmhoque31d1d4b2014-05-05 22:08:14 -0500676 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
677 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
678 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
679 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500680 }
681 else
682 {
683 cout << "Other's Adj LSA, so removing form LSDB" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500684 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500685 }
686 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500687 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500688 }
689 }
690}
691
692void
akmhoque31d1d4b2014-05-05 22:08:14 -0500693Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500694 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500695{
696 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
697 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500698 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
699 if (chkCorLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500700 {
akmhoqueb6450b12014-04-24 00:01:03 -0500701 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
702 if (chkCorLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500703 {
akmhoqueb6450b12014-04-24 00:01:03 -0500704 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500705 {
706 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500707 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500708 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500709 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500710 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
711 chkCorLsa->getKey(),
712 chkCorLsa->getLsSeqNo(),
713 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500714 // publish routing update
akmhoque31d1d4b2014-05-05 22:08:14 -0500715 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getChronosyncLsaPrefix();
716 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
717 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
718 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500719 }
720 else
721 {
722 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500723 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500724 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500725 if (m_nlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
akmhoque53353462014-04-22 08:43:45 -0500726 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500727 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500728 }
729 }
730 }
731}
732
733
734void
akmhoque31d1d4b2014-05-05 22:08:14 -0500735Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime)
736{
737 std::cout << "Expressing Interest :" << interestName << std::endl;
738 ndn::Interest interest(interestName);
739 interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
740 interest.setMustBeFresh(true);
741 m_nlsr.getNlsrFace().expressInterest(interest,
742 ndn::bind(&Lsdb::processContent,
743 this, _1, _2),
744 ndn::bind(&Lsdb::processInterestTimedOut,
745 this, _1));
746}
747
748void
749Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
750{
751 const ndn::Name& intName(interest.getName());
752 std::cout << "Interest recevied for LSA: " << intName << std::endl;
753 string chkString("LSA");
754 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
755 chkString);
756 if (lsaPosition >= 0)
757 {
758 std::string interestedLsType;
759 uint64_t interestedLsSeqNo;
760 ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
761 interest.getName().size() - lsaPosition - 3);
762 interestedLsType = intName[-2].toEscapedString();
763 interestedLsSeqNo = intName[-1].toNumber();
764 std::cout << "Router Name: " << origRouter << std::endl;
765 std::cout << "Ls Type : " << interestedLsType << std::endl;
766 std::cout << "Ls Seq : " << interestedLsSeqNo << endl;
767 std::cout << "Ls Type: " << interestedLsType << std::endl;
768 if (interestedLsType == "name")
769 {
770 processInterestForNameLsa(interest,
771 origRouter.append(interestedLsType),
772 interestedLsSeqNo);
773 return;
774 }
775 else if (interestedLsType == "adjacency")
776 {
777 processInterestForAdjacencyLsa(interest,
778 origRouter.append(interestedLsType),
779 interestedLsSeqNo);
780 return;
781 }
782 else if (interestedLsType == "coordinate")
783 {
784 processInterestForCoordinateLsa(interest,
785 origRouter.append(interestedLsType),
786 interestedLsSeqNo);
787 return;
788 }
789 else
790 {
791 cout << "Unrecognized LSA Type :(" << endl;
792 }
793 }
794}
795
796void
797Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
798 const ndn::Name& lsaKey,
799 uint32_t interestedlsSeqNo)
800{
801 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
802 if (nameLsa != 0)
803 {
804 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo)
805 {
806 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
807 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
808 std::string content = nameLsa->getData();
809 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
810 content.size());
811 m_keyChain.sign(data);
812 std::cout << ">> D: " << data << std::endl;
813 m_nlsr.getNlsrFace().put(data);
814 }
815 }
816}
817
818void
819Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
820 const ndn::Name& lsaKey,
821 uint32_t interestedlsSeqNo)
822{
823 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
824 if (adjLsa != 0)
825 {
826 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo)
827 {
828 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
829 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
830 std::string content = adjLsa->getData();
831 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
832 content.size());
833 m_keyChain.sign(data);
834 std::cout << ">> D: " << data << std::endl;
835 m_nlsr.getNlsrFace().put(data);
836 }
837 }
838}
839
840void
841Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
842 const ndn::Name& lsaKey,
843 uint32_t interestedlsSeqNo)
844{
845 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
846 if (corLsa != 0)
847 {
848 if (corLsa->getLsSeqNo() >= interestedlsSeqNo)
849 {
850 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
851 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
852 std::string content = corLsa->getData();
853 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
854 content.size());
855 m_keyChain.sign(data);
856 std::cout << ">> D: " << data << std::endl;
857 m_nlsr.getNlsrFace().put(data);
858 }
859 }
860}
861
862void
863Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data)
864{
865 const ndn::Name& dataName = data.getName();
866 std::cout << "Data received for name: " << dataName << std::endl;
867 string dataContent(reinterpret_cast<const char*>(data.getContent().value()));
868 string chkString("LSA");
869 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
870 if (lsaPosition >= 0)
871 {
872 std::string interestedLsType;
873 uint64_t interestedLsSeqNo;
874 ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
875 dataName.size() - lsaPosition - 4);
876 interestedLsType = dataName[-3].toEscapedString();
877 interestedLsSeqNo = dataName[-2].toNumber();
878 std::cout << "Ls Type : " << interestedLsType << std::endl;
879 std::cout << "Ls Seq : " << interestedLsSeqNo << std::endl;
880 std::cout << "Ls Type: " << interestedLsType << std::endl;
881 if (interestedLsType == "name")
882 {
883 processContentNameLsa(origRouter.append(interestedLsType),
884 interestedLsSeqNo, dataContent);
885 return;
886 }
887 else if (interestedLsType == "adjacency")
888 {
889 processContentAdjacencyLsa(origRouter.append(interestedLsType),
890 interestedLsSeqNo, dataContent);
891 return;
892 }
893 else if (interestedLsType == "coordinate")
894 {
895 processContentCoordinateLsa(origRouter.append(interestedLsType),
896 interestedLsSeqNo, dataContent);
897 return;
898 }
899 else
900 {
901 cout << "Unrecognized LSA Type :(" << endl;
902 }
903 }
904}
905
906void
907Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
908 uint32_t lsSeqNo, std::string& dataContent)
909{
910 if (isNameLsaNew(lsaKey, lsSeqNo))
911 {
912 NameLsa nameLsa;
913 if (nameLsa.initializeFromContent(dataContent))
914 {
915 installNameLsa(nameLsa);
916 }
917 else
918 {
919 std::cout << "LSA data decoding error :(" << std::endl;
920 }
921 }
922}
923
924void
925Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
926 uint32_t lsSeqNo, std::string& dataContent)
927{
928 if (isAdjLsaNew(lsaKey, lsSeqNo))
929 {
930 AdjLsa adjLsa;
931 if (adjLsa.initializeFromContent(dataContent))
932 {
933 installAdjLsa(adjLsa);
934 }
935 else
936 {
937 std::cout << "LSA data decoding error :(" << std::endl;
938 }
939 }
940}
941
942void
943Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
944 uint32_t lsSeqNo, std::string& dataContent)
945{
946 if (isCoordinateLsaNew(lsaKey, lsSeqNo))
947 {
948 CoordinateLsa corLsa;
949 if (corLsa.initializeFromContent(dataContent))
950 {
951 installCoordinateLsa(corLsa);
952 }
953 else
954 {
955 std::cout << "LSA data decoding error :(" << std::endl;
956 }
957 }
958}
959
960void
961Lsdb::processInterestTimedOut(const ndn::Interest& interest)
962{
963 const ndn::Name& interestName(interest.getName());
964 cout << "Interest timed out for Name: " << interestName << endl;
965}
966
967
968void
akmhoque53353462014-04-22 08:43:45 -0500969Lsdb::printAdjLsdb()
970{
971 cout << "---------------Adj LSDB-------------------" << endl;
972 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
973 it != m_adjLsdb.end() ; it++)
974 {
975 cout << (*it) << endl;
976 }
977}
978
979//-----utility function -----
980bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500981Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500982{
akmhoque31d1d4b2014-05-05 22:08:14 -0500983 if (lsType == "name")
akmhoque53353462014-04-22 08:43:45 -0500984 {
985 return doesNameLsaExist(key);
986 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500987 else if (lsType == "adjacency")
akmhoque53353462014-04-22 08:43:45 -0500988 {
989 return doesAdjLsaExist(key);
990 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500991 else if (lsType == "coordinate")
akmhoque53353462014-04-22 08:43:45 -0500992 {
akmhoqueb6450b12014-04-24 00:01:03 -0500993 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500994 }
995 return false;
996}
997
998}//namespace nlsr
999