blob: 9a2afabe7511ea5c9a7341714299ddc6cce72142 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <utility>
akmhoque157b0a42014-05-13 00:26:37 -05003
akmhoque53353462014-04-22 08:43:45 -05004#include "lsdb.hpp"
5#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -05006#include "conf-parameter.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -05007#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05008#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -05009
10namespace nlsr {
11
akmhoque674b0b12014-05-20 14:33:28 -050012INIT_LOGGER("Lsdb");
13
akmhoque53353462014-04-22 08:43:45 -050014using namespace std;
15
16void
akmhoque31d1d4b2014-05-05 22:08:14 -050017Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050018{
akmhoque31d1d4b2014-05-05 22:08:14 -050019 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050020}
21
22static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050023nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050024{
25 return nlsa1.getKey() == key;
26}
27
28
29bool
akmhoque31d1d4b2014-05-05 22:08:14 -050030Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050031{
akmhoque31d1d4b2014-05-05 22:08:14 -050032 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
33 "name",
34 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -050035 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -050036 m_nlsr.getNamePrefixList());
37 m_nlsr.getSequencingManager().increaseNameLsaSeq();
38 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050039}
40
akmhoqueb6450b12014-04-24 00:01:03 -050041NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050042Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050043{
44 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
45 m_nameLsdb.end(),
46 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -050047 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -050048 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050049 }
akmhoqueb6450b12014-04-24 00:01:03 -050050 return 0;
akmhoque53353462014-04-22 08:43:45 -050051}
52
53bool
akmhoque31d1d4b2014-05-05 22:08:14 -050054Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050055{
akmhoqueb6450b12014-04-24 00:01:03 -050056 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -050057 if (nameLsaCheck != 0) {
58 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -050059 return true;
60 }
akmhoque157b0a42014-05-13 00:26:37 -050061 else {
akmhoque53353462014-04-22 08:43:45 -050062 return false;
63 }
64 }
65 return true;
66}
67
68ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -050069Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
70 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050071{
akmhoquec7a79b22014-05-26 08:06:19 -050072 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -050073 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
74 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050075}
76
77bool
akmhoque31d1d4b2014-05-05 22:08:14 -050078Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -050079{
akmhoquec7a79b22014-05-26 08:06:19 -050080 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -050081 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -050082 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -050083 addNameLsa(nlsa);
akmhoque674b0b12014-05-20 14:33:28 -050084 _LOG_DEBUG("New Name LSA. Adding to LSDB");
akmhoque53353462014-04-22 08:43:45 -050085 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -050086
akmhoque53353462014-04-22 08:43:45 -050087 printNameLsdb();
akmhoque157b0a42014-05-13 00:26:37 -050088 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050089 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
90 nlsa.getOrigRouter());
91 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
92 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -050093 it++) {
94 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050095 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -050096 }
97 }
98 }
akmhoque157b0a42014-05-13 00:26:37 -050099 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500100 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
101 ndn::time::system_clock::now();
102 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500103 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500105 nlsa.getLsSeqNo(),
106 timeToExpire));
107 }
akmhoque157b0a42014-05-13 00:26:37 -0500108 else {
109 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500110 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
111 _LOG_DEBUG("Old Name LSA ");
akmhoqueb6450b12014-04-24 00:01:03 -0500112 chkNameLsa->writeLog();
113 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500114 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500115 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500116 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500118 std::set_difference(nlsa.getNpl().getNameList().begin(),
119 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500120 chkNameLsa->getNpl().getNameList().begin(),
121 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500122 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500123 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500124 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500125 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500126 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
127 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500129 }
130 }
131 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500132 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500133 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
134 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500135 nlsa.getNpl().getNameList().begin(),
136 nlsa.getNpl().getNameList().end(),
137 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500138 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500139 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500140 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500141 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
142 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500144 }
145 }
146 }
akmhoque157b0a42014-05-13 00:26:37 -0500147 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500148 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
149 ndn::time::system_clock::now();
150 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500151 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500152 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
153 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500154 nlsa.getLsSeqNo(),
155 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500156 _LOG_DEBUG("Updated Name LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500157 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500158 }
159 }
160 return true;
161}
162
163bool
164Lsdb::addNameLsa(NameLsa& nlsa)
165{
166 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
167 m_nameLsdb.end(),
168 bind(nameLsaCompareByKey, _1,
169 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500170 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500171 m_nameLsdb.push_back(nlsa);
172 return true;
173 }
174 return false;
175}
176
177bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500178Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500179{
180 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
181 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500182 ndn::bind(nameLsaCompareByKey, _1, key));
183 if (it != m_nameLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500184 _LOG_DEBUG("Removing Name LSA");
akmhoque53353462014-04-22 08:43:45 -0500185 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500186 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500187 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
189 (*it).getOrigRouter());
190 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500191 nit != (*it).getNpl().getNameList().end(); ++nit) {
192 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500193 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500194 }
195 }
196 }
197 m_nameLsdb.erase(it);
198 return true;
199 }
200 return false;
201}
202
203bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500204Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500205{
206 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
207 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500208 ndn::bind(nameLsaCompareByKey, _1, key));
209 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500210 return false;
211 }
212 return true;
213}
214
215void
216Lsdb::printNameLsdb()
217{
218 cout << "---------------Name LSDB-------------------" << endl;
219 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500220 it != m_nameLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500221 cout << (*it) << endl;
222 }
223}
224
225// Cor LSA and LSDB related Functions start here
226
227
228static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500229corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500230{
231 return clsa.getKey() == key;
232}
233
234bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500235Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500236{
akmhoque31d1d4b2014-05-05 22:08:14 -0500237 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
238 "coordinate",
239 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500240 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 m_nlsr.getConfParameter().getCorR(),
242 m_nlsr.getConfParameter().getCorTheta());
243 m_nlsr.getSequencingManager().increaseCorLsaSeq();
244 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500245 return true;
246}
247
akmhoqueb6450b12014-04-24 00:01:03 -0500248CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500249Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500250{
akmhoqueb6450b12014-04-24 00:01:03 -0500251 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
252 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500253 ndn::bind(corLsaCompareByKey, _1, key));
254 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500255 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500256 }
akmhoqueb6450b12014-04-24 00:01:03 -0500257 return 0;
akmhoque53353462014-04-22 08:43:45 -0500258}
259
260bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500261Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500262{
akmhoqueb6450b12014-04-24 00:01:03 -0500263 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500264 if (clsa != 0) {
265 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500266 return true;
267 }
akmhoque157b0a42014-05-13 00:26:37 -0500268 else {
akmhoque53353462014-04-22 08:43:45 -0500269 return false;
270 }
271 }
272 return true;
273}
274
275ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500276Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500277 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500278{
akmhoquec7a79b22014-05-26 08:06:19 -0500279 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -0500280 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
281 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500282}
283
284bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500285Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500286{
akmhoquec7a79b22014-05-26 08:06:19 -0500287 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500288 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500289 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500290 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
291 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500292 addCoordinateLsa(clsa);
akmhoque157b0a42014-05-13 00:26:37 -0500293 //debugging purpose
294 printCorLsdb();
295 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500296 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
297 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500298 }
akmhoque157b0a42014-05-13 00:26:37 -0500299 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500300 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500301 }
akmhoque157b0a42014-05-13 00:26:37 -0500302 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500303 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
304 ndn::time::system_clock::now();
305 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500306 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500307 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500308 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500309 }
akmhoque157b0a42014-05-13 00:26:37 -0500310 else {
311 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500312 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
313 _LOG_DEBUG("Old Coordinate LSA");
314 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500315 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500316 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500317 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500318 chkCorLsa->setCorRadius(clsa.getCorRadius());
319 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500320 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500321 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500322 }
323 }
akmhoque157b0a42014-05-13 00:26:37 -0500324 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500325 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
326 ndn::time::system_clock::now();
327 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500328 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500329 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
330 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500331 clsa.getLsSeqNo(),
332 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500333 _LOG_DEBUG("Updated Coordinate LSA");
334 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500335 }
336 }
337 return true;
338}
339
340bool
akmhoqueb6450b12014-04-24 00:01:03 -0500341Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500342{
akmhoqueb6450b12014-04-24 00:01:03 -0500343 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
344 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500345 ndn::bind(corLsaCompareByKey, _1,
346 clsa.getKey()));
347 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500348 m_corLsdb.push_back(clsa);
349 return true;
350 }
351 return false;
352}
353
354bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500355Lsdb::removeCoordinateLsa(const ndn::Name& key)
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(),
akmhoque157b0a42014-05-13 00:26:37 -0500359 ndn::bind(corLsaCompareByKey,
360 _1, key));
361 if (it != m_corLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500362 _LOG_DEBUG("Removing Coordinate LSA");
363 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500364 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500365 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500366 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
367 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500368 }
369 m_corLsdb.erase(it);
370 return true;
371 }
372 return false;
373}
374
375bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500376Lsdb::doesCoordinateLsaExist(const ndn::Name& 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(),
akmhoque157b0a42014-05-13 00:26:37 -0500380 ndn::bind(corLsaCompareByKey,
381 _1, key));
382 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500383 return false;
384 }
385 return true;
386}
387
akmhoque157b0a42014-05-13 00:26:37 -0500388//debugging
akmhoque53353462014-04-22 08:43:45 -0500389void
akmhoque157b0a42014-05-13 00:26:37 -0500390Lsdb::printCorLsdb()
akmhoque53353462014-04-22 08:43:45 -0500391{
392 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500393 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500394 it != m_corLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500395 cout << (*it) << endl;
396 }
397}
398
399
400// Adj LSA and LSDB related function starts here
401
402static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500403adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500404{
405 return alsa.getKey() == key;
406}
407
408
409void
akmhoque31d1d4b2014-05-05 22:08:14 -0500410Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500411{
akmhoque674b0b12014-05-20 14:33:28 -0500412 std::cout << "scheduledAdjLsaBuild Called" << endl;
413 _LOG_DEBUG("scheduledAdjLsaBuild Called");
414 m_nlsr.setIsBuildAdjLsaSheduled(false);
akmhoque157b0a42014-05-13 00:26:37 -0500415 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500416 int adjBuildCount = m_nlsr.getAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500417 if (adjBuildCount > 0) {
418 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500419 _LOG_DEBUG("Building and installing Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500420 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500421 }
akmhoque157b0a42014-05-13 00:26:37 -0500422 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500423 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
424 key.append("adjacency");
425 removeAdjLsa(key);
426 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500427 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500428 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500429 }
430 }
akmhoque157b0a42014-05-13 00:26:37 -0500431 else {
akmhoque674b0b12014-05-20 14:33:28 -0500432 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500433 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
434 m_nlsr.getConfParameter().getInterestResendTime();
435 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
436 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
437 this));
akmhoque53353462014-04-22 08:43:45 -0500438 }
439}
440
441
442bool
443Lsdb::addAdjLsa(AdjLsa& alsa)
444{
445 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
446 m_adjLsdb.end(),
447 bind(adjLsaCompareByKey, _1,
448 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500449 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500450 m_adjLsdb.push_back(alsa);
451 return true;
452 }
453 return false;
454}
455
akmhoqueb6450b12014-04-24 00:01:03 -0500456AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500457Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500458{
459 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
460 m_adjLsdb.end(),
461 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500462 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500463 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500464 }
akmhoqueb6450b12014-04-24 00:01:03 -0500465 return 0;
akmhoque53353462014-04-22 08:43:45 -0500466}
467
468
469bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500470Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500471{
akmhoqueb6450b12014-04-24 00:01:03 -0500472 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500473 if (adjLsaCheck != 0) {
474 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500475 return true;
476 }
akmhoque157b0a42014-05-13 00:26:37 -0500477 else {
akmhoque53353462014-04-22 08:43:45 -0500478 return false;
479 }
480 }
481 return true;
482}
483
484
485ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500486Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
487 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500488{
akmhoquec7a79b22014-05-26 08:06:19 -0500489 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -0500490 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
491 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500492}
493
494bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500495Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500496{
akmhoquec7a79b22014-05-26 08:06:19 -0500497 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500498 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500499 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500500 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
501 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500502 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500503 alsa.addNptEntries(m_nlsr);
504 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500505 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500506 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
507 ndn::time::system_clock::now();
508 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500509 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500510 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500511 alsa.getLsSeqNo(), timeToExpire);
512 }
akmhoque157b0a42014-05-13 00:26:37 -0500513 else {
514 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500515 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
516 _LOG_DEBUG("Old Adj LSA");
517 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500518 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500519 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500520 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500521 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500522 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500523 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500524 }
akmhoque157b0a42014-05-13 00:26:37 -0500525 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500526 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
527 ndn::time::system_clock::now();
528 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500529 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500530 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
531 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500532 alsa.getLsSeqNo(),
533 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500534 _LOG_DEBUG("Updated Adj LSA");
535 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500536 }
537 }
538 return true;
539}
540
541bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500542Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500543{
akmhoque31d1d4b2014-05-05 22:08:14 -0500544 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
545 "adjacency",
546 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500547 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500548 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
549 m_nlsr.getAdjacencyList());
550 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
551 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500552 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500553 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
554 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
555 lsaPrefix);
556 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500557}
558
559bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500560Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500561{
562 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
563 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500564 ndn::bind(adjLsaCompareByKey, _1, key));
565 if (it != m_adjLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500566 _LOG_DEBUG("Removing Adj LSA");
567 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500568 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500569 m_adjLsdb.erase(it);
570 return true;
571 }
572 return false;
573}
574
575bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500576Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500577{
578 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
579 m_adjLsdb.end(),
580 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500581 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500582 return false;
583 }
584 return true;
585}
586
587std::list<AdjLsa>&
588Lsdb::getAdjLsdb()
589{
590 return m_adjLsdb;
591}
592
593void
594Lsdb::setLsaRefreshTime(int lrt)
595{
596 m_lsaRefreshTime = lrt;
597}
598
599void
600Lsdb::setThisRouterPrefix(string trp)
601{
602 m_thisRouterPrefix = trp;
603}
604
605void
akmhoque31d1d4b2014-05-05 22:08:14 -0500606Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500607{
akmhoque674b0b12014-05-20 14:33:28 -0500608 std::cout << "Lsdb::exprireOrRefreshNameLsa Called " << std::endl;
609 std::cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << std::endl;
610 _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called");
611 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500612 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500613 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500614 std::cout << "LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << std::endl;
615 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500616 if (chkNameLsa->getLsSeqNo() == seqNo) {
617 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoqueb6450b12014-04-24 00:01:03 -0500618 chkNameLsa->writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500619 std::cout << "Own Name LSA, so refreshing name LSA" << std::endl;
620 _LOG_DEBUG("Own Name LSA, so refreshing name LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500621 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500622 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500623 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500624 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500625 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500626 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500627 chkNameLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500628 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500629 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500630 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500631 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
632 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
633 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500634 }
akmhoque157b0a42014-05-13 00:26:37 -0500635 else {
akmhoque674b0b12014-05-20 14:33:28 -0500636 std::cout << "Other's Name LSA, so removing form LSDB" << std::endl;
637 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500638 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500639 }
640 }
641 }
642}
643
644void
akmhoque31d1d4b2014-05-05 22:08:14 -0500645Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500646{
647 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
648 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500649 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
650 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500651 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500652 if (chkAdjLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500653 cout << "LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
654 _LOG_DEBUG("LSA Exists with seq no: ");
akmhoque157b0a42014-05-13 00:26:37 -0500655 if (chkAdjLsa->getLsSeqNo() == seqNo) {
656 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500657 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500658 _LOG_DEBUG("Own Adj LSA, so refreshing Adj LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500659 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500660 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500661 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoquefdbddb12014-05-02 18:35:19 -0500662 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500663 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500664 chkAdjLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500665 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500666 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500667 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500668 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
669 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
670 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500671 }
akmhoque157b0a42014-05-13 00:26:37 -0500672 else {
akmhoque53353462014-04-22 08:43:45 -0500673 cout << "Other's Adj LSA, so removing form LSDB" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500674 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500675 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500676 }
677 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500678 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500679 }
680 }
681}
682
683void
akmhoque31d1d4b2014-05-05 22:08:14 -0500684Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500685 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500686{
687 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
688 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500689 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
690 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500691 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500692 if (chkCorLsa != 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500693 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500694 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500695 if (chkCorLsa->getLsSeqNo() == seqNo) {
696 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500697 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500698 _LOG_DEBUG("Own Cor LSA, so refreshing Cor LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500699 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500700 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500701 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoquefdbddb12014-05-02 18:35:19 -0500702 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500703 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
704 chkCorLsa->getKey(),
705 chkCorLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500706 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500707 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500708 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500709 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
710 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
711 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500712 }
akmhoque157b0a42014-05-13 00:26:37 -0500713 else {
akmhoque53353462014-04-22 08:43:45 -0500714 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500715 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500716 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500717 }
akmhoque157b0a42014-05-13 00:26:37 -0500718 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500719 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500720 }
721 }
722 }
723}
724
725
726void
akmhoque31d1d4b2014-05-05 22:08:14 -0500727Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime)
728{
729 std::cout << "Expressing Interest :" << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500730 _LOG_DEBUG("Expressing Interest for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500731 ndn::Interest interest(interestName);
732 interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
733 interest.setMustBeFresh(true);
734 m_nlsr.getNlsrFace().expressInterest(interest,
735 ndn::bind(&Lsdb::processContent,
736 this, _1, _2),
737 ndn::bind(&Lsdb::processInterestTimedOut,
738 this, _1));
739}
740
741void
742Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
743{
744 const ndn::Name& intName(interest.getName());
745 std::cout << "Interest recevied for LSA: " << intName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500746 _LOG_DEBUG("Interest recevied for LSA(name): " << intName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500747 string chkString("LSA");
748 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
749 chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500750 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500751 std::string interestedLsType;
752 uint64_t interestedLsSeqNo;
753 ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
754 interest.getName().size() - lsaPosition - 3);
akmhoque157b0a42014-05-13 00:26:37 -0500755 interestedLsType = intName[-2].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500756 interestedLsSeqNo = intName[-1].toNumber();
757 std::cout << "Router Name: " << origRouter << std::endl;
758 std::cout << "Ls Type : " << interestedLsType << std::endl;
759 std::cout << "Ls Seq : " << interestedLsSeqNo << endl;
760 std::cout << "Ls Type: " << interestedLsType << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500761 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500762 processInterestForNameLsa(interest,
763 origRouter.append(interestedLsType),
764 interestedLsSeqNo);
765 return;
766 }
akmhoque157b0a42014-05-13 00:26:37 -0500767 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500768 processInterestForAdjacencyLsa(interest,
769 origRouter.append(interestedLsType),
770 interestedLsSeqNo);
771 return;
772 }
akmhoque157b0a42014-05-13 00:26:37 -0500773 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500774 processInterestForCoordinateLsa(interest,
775 origRouter.append(interestedLsType),
776 interestedLsSeqNo);
777 return;
778 }
akmhoque157b0a42014-05-13 00:26:37 -0500779 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500780 cout << "Unrecognized LSA Type :(" << endl;
781 }
782 }
783}
784
785void
786Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
787 const ndn::Name& lsaKey,
788 uint32_t interestedlsSeqNo)
789{
790 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500791 if (nameLsa != 0) {
792 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500793 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500794 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500795 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
796 std::string content = nameLsa->getData();
797 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
798 content.size());
799 m_keyChain.sign(data);
akmhoque674b0b12014-05-20 14:33:28 -0500800 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500801 m_nlsr.getNlsrFace().put(data);
802 }
803 }
804}
805
806void
807Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
808 const ndn::Name& lsaKey,
809 uint32_t interestedlsSeqNo)
810{
811 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500812 if (adjLsa != 0) {
813 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500814 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500815 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500816 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
817 std::string content = adjLsa->getData();
818 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
819 content.size());
820 m_keyChain.sign(data);
akmhoque674b0b12014-05-20 14:33:28 -0500821 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500822 m_nlsr.getNlsrFace().put(data);
823 }
824 }
825}
826
827void
828Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
829 const ndn::Name& lsaKey,
830 uint32_t interestedlsSeqNo)
831{
832 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500833 if (corLsa != 0) {
834 if (corLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500835 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500836 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500837 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
838 std::string content = corLsa->getData();
839 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
840 content.size());
841 m_keyChain.sign(data);
akmhoque674b0b12014-05-20 14:33:28 -0500842 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500843 m_nlsr.getNlsrFace().put(data);
844 }
845 }
846}
847
848void
849Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data)
850{
851 const ndn::Name& dataName = data.getName();
akmhoque674b0b12014-05-20 14:33:28 -0500852 std::cout << "Data received for LSA(name): " << dataName << std::endl;
853 _LOG_DEBUG("Data received for LSA(name): " << dataName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500854 string dataContent(reinterpret_cast<const char*>(data.getContent().value()));
855 string chkString("LSA");
856 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500857 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500858 std::string interestedLsType;
859 uint64_t interestedLsSeqNo;
860 ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
861 dataName.size() - lsaPosition - 4);
akmhoque157b0a42014-05-13 00:26:37 -0500862 interestedLsType = dataName[-3].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500863 interestedLsSeqNo = dataName[-2].toNumber();
864 std::cout << "Ls Type : " << interestedLsType << std::endl;
865 std::cout << "Ls Seq : " << interestedLsSeqNo << std::endl;
866 std::cout << "Ls Type: " << interestedLsType << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500867 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500868 processContentNameLsa(origRouter.append(interestedLsType),
869 interestedLsSeqNo, dataContent);
870 return;
871 }
akmhoque157b0a42014-05-13 00:26:37 -0500872 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500873 processContentAdjacencyLsa(origRouter.append(interestedLsType),
874 interestedLsSeqNo, dataContent);
875 return;
876 }
akmhoque157b0a42014-05-13 00:26:37 -0500877 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500878 processContentCoordinateLsa(origRouter.append(interestedLsType),
879 interestedLsSeqNo, dataContent);
880 return;
881 }
akmhoque157b0a42014-05-13 00:26:37 -0500882 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500883 cout << "Unrecognized LSA Type :(" << endl;
884 }
885 }
886}
887
888void
889Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
890 uint32_t lsSeqNo, std::string& dataContent)
891{
akmhoque157b0a42014-05-13 00:26:37 -0500892 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500893 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500894 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500895 installNameLsa(nameLsa);
896 }
akmhoque157b0a42014-05-13 00:26:37 -0500897 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500898 std::cout << "LSA data decoding error :(" << std::endl;
899 }
900 }
901}
902
903void
904Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
905 uint32_t lsSeqNo, std::string& dataContent)
906{
akmhoque157b0a42014-05-13 00:26:37 -0500907 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500908 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500909 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500910 installAdjLsa(adjLsa);
911 }
akmhoque157b0a42014-05-13 00:26:37 -0500912 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500913 std::cout << "LSA data decoding error :(" << std::endl;
914 }
915 }
916}
917
918void
919Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
920 uint32_t lsSeqNo, std::string& dataContent)
921{
akmhoque157b0a42014-05-13 00:26:37 -0500922 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500923 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500924 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500925 installCoordinateLsa(corLsa);
926 }
akmhoque157b0a42014-05-13 00:26:37 -0500927 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500928 std::cout << "LSA data decoding error :(" << std::endl;
929 }
930 }
931}
932
933void
934Lsdb::processInterestTimedOut(const ndn::Interest& interest)
935{
936 const ndn::Name& interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500937 cout << "Interest timed out for LSA(name): " << interestName << endl;
938 _LOG_DEBUG("Interest timed out for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500939}
940
akmhoquec7a79b22014-05-26 08:06:19 -0500941ndn::time::system_clock::TimePoint
942Lsdb::getLsaExpirationTimePoint()
943{
944 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
945 expirationTimePoint = expirationTimePoint +
946 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
947 return expirationTimePoint;
948}
akmhoque31d1d4b2014-05-05 22:08:14 -0500949
950void
akmhoque53353462014-04-22 08:43:45 -0500951Lsdb::printAdjLsdb()
952{
953 cout << "---------------Adj LSDB-------------------" << endl;
954 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500955 it != m_adjLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500956 cout << (*it) << endl;
957 }
958}
959
960//-----utility function -----
961bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500962Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500963{
akmhoque157b0a42014-05-13 00:26:37 -0500964 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -0500965 return doesNameLsaExist(key);
966 }
akmhoque157b0a42014-05-13 00:26:37 -0500967 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -0500968 return doesAdjLsaExist(key);
969 }
akmhoque157b0a42014-05-13 00:26:37 -0500970 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -0500971 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500972 }
973 return false;
974}
975
976}//namespace nlsr
977