blob: edcfec7f67b7c58ec035a8881414f2ab488d3c7b [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <cstdlib>
3
4
akmhoquec8a10f72014-04-25 18:42:55 -05005#include <ndn-cxx/security/identity-certificate.hpp>
6#include <ndn-cxx/util/io.hpp>
akmhoque53353462014-04-22 08:43:45 -05007
8#include "nlsr.hpp"
9#include "interest-manager.hpp"
10#include "data-manager.hpp"
11#include "utility/tokenizer.hpp"
12#include "lsdb.hpp"
13
14namespace nlsr {
15
16using namespace std;
17using namespace ndn;
18
19void
akmhoqueb6450b12014-04-24 00:01:03 -050020InterestManager::processInterest(const ndn::Name& name,
akmhoque53353462014-04-22 08:43:45 -050021 const ndn::Interest& interest)
22{
23 cout << "<< I: " << interest << endl;
24 string intName = interest.getName().toUri();
25 cout << "Interest Received for Name: " << intName << endl;
26 Tokenizer nt(intName, "/");
27 string chkString("info");
28 if (nt.doesTokenExist(chkString))
29 {
30 string nbr = nt.getTokenString(nt.getTokenPosition(chkString) + 1);
31 cout << "Neighbor: " << nbr << endl;
akmhoqueb6450b12014-04-24 00:01:03 -050032 processInterestInfo(nbr, interest);
akmhoque53353462014-04-22 08:43:45 -050033 }
34 chkString = "LSA";
35 if (nt.doesTokenExist(chkString))
36 {
akmhoqueb6450b12014-04-24 00:01:03 -050037 processInterestLsa(interest);
akmhoque53353462014-04-22 08:43:45 -050038 }
39 chkString = "keys";
40 if (nt.doesTokenExist(chkString))
41 {
akmhoqueb6450b12014-04-24 00:01:03 -050042 processInterestKeys(interest);
akmhoque53353462014-04-22 08:43:45 -050043 }
44}
45
46void
akmhoqueb6450b12014-04-24 00:01:03 -050047InterestManager::processInterestInfo(const string& neighbor,
akmhoque53353462014-04-22 08:43:45 -050048 const ndn::Interest& interest)
49{
akmhoquec8a10f72014-04-25 18:42:55 -050050 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor))
akmhoque53353462014-04-22 08:43:45 -050051 {
52 Data data(ndn::Name(interest.getName()).appendVersion());
53 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
54 data.setContent((const uint8_t*)"info", sizeof("info"));
Yingdi Yu0c217822014-04-24 14:22:42 -070055 // m_nlsr.getKeyManager().signData(data);
56 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -050057 cout << ">> D: " << data << endl;
akmhoqueb6450b12014-04-24 00:01:03 -050058 m_nlsr.getNlsrFace()->put(data);
akmhoquec8a10f72014-04-25 18:42:55 -050059 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque53353462014-04-22 08:43:45 -050060 if (status == 0)
61 {
62 string intName = neighbor + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -050063 m_nlsr.getConfParameter().getRouterPrefix();
64 expressInterest(intName, 2,
65 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -050066 }
67 }
68}
69
70void
akmhoqueb6450b12014-04-24 00:01:03 -050071InterestManager::processInterestLsa(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -050072{
73 string intName = interest.getName().toUri();
74 Tokenizer nt(intName, "/");
75 string chkString("LSA");
76 string origRouter = nt.getTokenString(nt.getTokenPosition(chkString) + 1,
77 nt.getTokenNumber() - 3);
78 string lsTypeString = nt.getToken(nt.getTokenNumber() - 2);
79 string lsSeqString = nt.getToken(nt.getTokenNumber() - 1);
80 std::cout << "Router Name: " << origRouter << std::endl;
81 std::cout << "Ls Type : " << lsTypeString << std::endl;
82 std::cout << "Ls Seq : " << lsSeqString << endl;
83 uint8_t interestedLsType;
84 uint32_t interestedLsSeqNo;
85 try
86 {
87 interestedLsType = boost::lexical_cast<uint8_t>(lsTypeString);
88 interestedLsSeqNo = boost::lexical_cast<uint32_t>(lsSeqString);
89 }
90 catch (std::exception& e)
91 {
92 return;
93 }
94 std::cout << "Ls Type: " << interestedLsType << std::endl;
95 if (lsTypeString == "1") //Name Lsa
96 {
akmhoqueb6450b12014-04-24 00:01:03 -050097 processInterestForNameLsa(interest,
akmhoque53353462014-04-22 08:43:45 -050098 origRouter + "/" + lsTypeString, interestedLsSeqNo);
99 }
100 else if (lsTypeString == "2") //Adj Lsa
101 {
akmhoqueb6450b12014-04-24 00:01:03 -0500102 processInterestForAdjLsa(interest,
akmhoque53353462014-04-22 08:43:45 -0500103 origRouter + "/" + lsTypeString, interestedLsSeqNo);
104 }
105 else if (lsTypeString == "3") //Cor Lsa
106 {
akmhoqueb6450b12014-04-24 00:01:03 -0500107 processInterestForCorLsa(interest,
akmhoque53353462014-04-22 08:43:45 -0500108 origRouter + "/" + lsTypeString, interestedLsSeqNo);
109 }
110 else
111 {
112 cout << "Unrecognized LSA Type :(" << endl;
113 }
114}
115
116void
akmhoqueb6450b12014-04-24 00:01:03 -0500117InterestManager::processInterestForNameLsa(const ndn::Interest& interest,
118 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500119{
akmhoqueb6450b12014-04-24 00:01:03 -0500120 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
121 if (nameLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500122 {
akmhoqueb6450b12014-04-24 00:01:03 -0500123 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500124 {
125 Data data(ndn::Name(interest.getName()).appendVersion());
126 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500127 string content = nameLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500128 data.setContent((const uint8_t*)content.c_str(), content.size());
Yingdi Yu0c217822014-04-24 14:22:42 -0700129 // m_nlsr.getKeyManager().signData(data);
130 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -0500131 std::cout << ">> D: " << data << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500132 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500133 }
134 }
135}
136
137void
akmhoqueb6450b12014-04-24 00:01:03 -0500138InterestManager::processInterestForAdjLsa(const ndn::Interest& interest,
139 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500140{
akmhoqueb6450b12014-04-24 00:01:03 -0500141 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
142 if (adjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500143 {
akmhoqueb6450b12014-04-24 00:01:03 -0500144 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500145 {
146 Data data(ndn::Name(interest.getName()).appendVersion());
147 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500148 string content = adjLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500149 data.setContent((const uint8_t*)content.c_str(), content.size());
Yingdi Yu0c217822014-04-24 14:22:42 -0700150 // m_nlsr.getKeyManager().signData(data);
151 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -0500152 std::cout << ">> D: " << data << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500153 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500154 }
155 }
156}
157
158void
akmhoqueb6450b12014-04-24 00:01:03 -0500159InterestManager::processInterestForCorLsa(const ndn::Interest& interest,
160 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500161{
akmhoqueb6450b12014-04-24 00:01:03 -0500162 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
163 if (corLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500164 {
akmhoqueb6450b12014-04-24 00:01:03 -0500165 if (corLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500166 {
167 Data data(ndn::Name(interest.getName()).appendVersion());
168 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500169 string content = corLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500170 data.setContent((const uint8_t*)content.c_str(), content.size());
Yingdi Yu0c217822014-04-24 14:22:42 -0700171 // m_nlsr.getKeyManager().signData(data);
172 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -0500173 std::cout << ">> D: " << data << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500174 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500175 }
176 }
177}
178
179void
akmhoqueb6450b12014-04-24 00:01:03 -0500180InterestManager::processInterestKeys(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500181{
182 std::cout << "processInterestKeys called " << std::endl;
Yingdi Yu0c217822014-04-24 14:22:42 -0700183 // string intName = interest.getName().toUri();
184 // std::cout << "Interest Name for Key: " << intName << std::endl;
185 // Tokenizer nt(intName, "/");
186 // std::string chkString("ID-CERT");
187 // std::string certName;
188 // uint32_t seqNum;
189 // ndn::Name dataName;
190 // std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert;
191 // if (nt.getTokenPosition(chkString) == nt.getTokenNumber() - 1)
192 // {
193 // certName = nt.getTokenString(0, nt.getTokenNumber() - 1);
194 // cout << "Cert Name: " << certName << std::endl;
195 // chkCert = m_nlsr.getKeyManager().getCertificateFromStore(certName);
196 // }
197 // else
198 // {
199 // certName = nt.getTokenString(0, nt.getTokenNumber() - 2);
200 // seqNum = boost::lexical_cast<uint32_t>(nt.getToken(nt.getTokenNumber() - 1));
201 // std::cout << "Cert Name: " << certName << " Seq Num: " << seqNum << std::endl;
202 // chkCert = m_nlsr.getKeyManager().getCertificateFromStore(certName, seqNum);
203 // }
204 // if (chkCert.second)
205 // {
206 // if (nt.getTokenPosition(chkString) == nt.getTokenNumber() - 1)
207 // {
208 // std::string dn;
209 // dataName = ndn::Name(interest.getName()).appendVersion();
210 // std::pair<uint32_t, bool> seqChk =
211 // m_nlsr.getKeyManager().getCertificateSeqNum(certName);
212 // if (seqChk.second)
213 // {
214 // dn = dataName.toUri() + "/" + boost::lexical_cast<std::string>(seqChk.first);
215 // dataName = ndn::Name(dn);
216 // }
217 // else
218 // {
219 // dn = dataName.toUri() + "/" + boost::lexical_cast<std::string>(10);
220 // dataName = ndn::Name(dn);
221 // }
222 // }
223 // else
224 // {
225 // dataName = ndn::Name(interest.getName());
226 // }
227 // Data data(dataName.appendVersion());
228 // data.setFreshnessPeriod(time::seconds(10)); //10 sec
229 // data.setContent(chkCert.first->wireEncode());
230 // m_nlsr.getKeyManager().signData(data);
231 // m_nlsr.getNlsrFace()->put(data);
232 // }
akmhoque53353462014-04-22 08:43:45 -0500233}
234
235
236void
akmhoqueb6450b12014-04-24 00:01:03 -0500237InterestManager::processInterestTimedOut(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500238{
239 std::cout << "Timed out interest : " << interest.getName().toUri() << std::endl;
240 string intName = interest.getName().toUri();
241 Tokenizer nt(intName, "/");
242 string chkString("info");
243 if (nt.doesTokenExist(chkString))
244 {
245 string nbr = nt.getTokenString(0, nt.getTokenPosition(chkString) - 1);
akmhoqueb6450b12014-04-24 00:01:03 -0500246 processInterestTimedOutInfo(nbr , interest);
akmhoque53353462014-04-22 08:43:45 -0500247 }
248 chkString = "LSA";
249 if (nt.doesTokenExist(chkString))
250 {
akmhoqueb6450b12014-04-24 00:01:03 -0500251 processInterestTimedOutLsa(interest);
akmhoque53353462014-04-22 08:43:45 -0500252 }
253}
254
255void
akmhoqueb6450b12014-04-24 00:01:03 -0500256InterestManager::processInterestTimedOutInfo(const string& neighbor,
akmhoque53353462014-04-22 08:43:45 -0500257 const ndn::Interest& interest)
258{
akmhoquec8a10f72014-04-25 18:42:55 -0500259 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
260 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
261 int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
262 neighbor);
akmhoque53353462014-04-22 08:43:45 -0500263 std::cout << "Neighbor: " << neighbor << std::endl;
264 std::cout << "Status: " << status << std::endl;
265 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500266 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber()))
akmhoque53353462014-04-22 08:43:45 -0500267 {
268 string intName = neighbor + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -0500269 m_nlsr.getConfParameter().getRouterPrefix();
270 expressInterest(intName, 2,
271 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500272 }
273 else if ((status == 1) &&
akmhoqueb6450b12014-04-24 00:01:03 -0500274 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber()))
akmhoque53353462014-04-22 08:43:45 -0500275 {
akmhoquec8a10f72014-04-25 18:42:55 -0500276 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
akmhoqueb6450b12014-04-24 00:01:03 -0500277 m_nlsr.incrementAdjBuildCount();
278 if (m_nlsr.getIsBuildAdjLsaSheduled() == 0)
akmhoque53353462014-04-22 08:43:45 -0500279 {
akmhoqueb6450b12014-04-24 00:01:03 -0500280 m_nlsr.setIsBuildAdjLsaSheduled(1);
akmhoque53353462014-04-22 08:43:45 -0500281 // event here
akmhoqueb6450b12014-04-24 00:01:03 -0500282 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
283 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
284 &m_nlsr.getLsdb(),
285 boost::ref(m_nlsr)));
akmhoque53353462014-04-22 08:43:45 -0500286 }
287 }
288}
289
290void
akmhoqueb6450b12014-04-24 00:01:03 -0500291InterestManager::processInterestTimedOutLsa(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500292{
293}
294
295void
akmhoqueb6450b12014-04-24 00:01:03 -0500296InterestManager::expressInterest(const string& interestNamePrefix,
akmhoque53353462014-04-22 08:43:45 -0500297 int scope, int seconds)
298{
299 std::cout << "Expressing Interest :" << interestNamePrefix << std::endl;
300 ndn::Interest i((ndn::Name(interestNamePrefix)));
301 i.setInterestLifetime(time::seconds(seconds));
302 i.setMustBeFresh(true);
akmhoqueb6450b12014-04-24 00:01:03 -0500303 m_nlsr.getNlsrFace()->expressInterest(i,
304 ndn::bind(&DataManager::processContent,
akmhoquec8a10f72014-04-25 18:42:55 -0500305 &m_nlsr.getDataManager(),
akmhoqueb6450b12014-04-24 00:01:03 -0500306 _1, _2, boost::ref(*this)),
307 ndn::bind(&InterestManager::processInterestTimedOut,
308 this, _1));
akmhoque53353462014-04-22 08:43:45 -0500309}
310
311
312void
akmhoqueb6450b12014-04-24 00:01:03 -0500313InterestManager::sendScheduledInfoInterest(int seconds)
akmhoque53353462014-04-22 08:43:45 -0500314{
akmhoquec8a10f72014-04-25 18:42:55 -0500315 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500316 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
317 ++it)
318 {
319 string adjName = (*it).getName() + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -0500320 m_nlsr.getConfParameter().getRouterPrefix();
321 expressInterest(adjName, 2,
322 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500323 }
akmhoqueb6450b12014-04-24 00:01:03 -0500324 scheduleInfoInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
akmhoque53353462014-04-22 08:43:45 -0500325}
326
327void
akmhoqueb6450b12014-04-24 00:01:03 -0500328InterestManager::scheduleInfoInterest(int seconds)
akmhoque53353462014-04-22 08:43:45 -0500329{
akmhoqueb6450b12014-04-24 00:01:03 -0500330 EventId eid = m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
331 ndn::bind(&InterestManager::sendScheduledInfoInterest, this,
332 seconds));
akmhoque53353462014-04-22 08:43:45 -0500333}
334
335
336} //namespace nlsr