blob: 73a43e98fb3c6357cd9427f8cb6f31d04635008c [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <cstdlib>
3
4
5#include <ndn-cpp-dev/security/identity-certificate.hpp>
6#include <ndn-cpp-dev/util/io.hpp>
7
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
20InterestManager::processInterest(Nlsr& pnlsr,
21 const ndn::Name& name,
22 const ndn::Interest& interest)
23{
24 cout << "<< I: " << interest << endl;
25 string intName = interest.getName().toUri();
26 cout << "Interest Received for Name: " << intName << endl;
27 Tokenizer nt(intName, "/");
28 string chkString("info");
29 if (nt.doesTokenExist(chkString))
30 {
31 string nbr = nt.getTokenString(nt.getTokenPosition(chkString) + 1);
32 cout << "Neighbor: " << nbr << endl;
33 processInterestInfo(pnlsr, nbr, interest);
34 }
35 chkString = "LSA";
36 if (nt.doesTokenExist(chkString))
37 {
38 processInterestLsa(pnlsr, interest);
39 }
40 chkString = "keys";
41 if (nt.doesTokenExist(chkString))
42 {
43 processInterestKeys(pnlsr, interest);
44 }
45}
46
47void
48InterestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
49 const ndn::Interest& interest)
50{
51 if (pnlsr.getAdl().isNeighbor(neighbor))
52 {
53 Data data(ndn::Name(interest.getName()).appendVersion());
54 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
55 data.setContent((const uint8_t*)"info", sizeof("info"));
56 pnlsr.getKeyManager().signData(data);
57 cout << ">> D: " << data << endl;
58 pnlsr.getNlsrFace()->put(data);
59 int status = pnlsr.getAdl().getStatusOfNeighbor(neighbor);
60 if (status == 0)
61 {
62 string intName = neighbor + "/" + "info" +
63 pnlsr.getConfParameter().getRouterPrefix();
64 expressInterest(pnlsr, intName, 2,
65 pnlsr.getConfParameter().getInterestResendTime());
66 }
67 }
68}
69
70void
71InterestManager::processInterestLsa(Nlsr& pnlsr, const ndn::Interest& interest)
72{
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 {
97 processInterestForNameLsa(pnlsr, interest,
98 origRouter + "/" + lsTypeString, interestedLsSeqNo);
99 }
100 else if (lsTypeString == "2") //Adj Lsa
101 {
102 processInterestForAdjLsa(pnlsr, interest,
103 origRouter + "/" + lsTypeString, interestedLsSeqNo);
104 }
105 else if (lsTypeString == "3") //Cor Lsa
106 {
107 processInterestForCorLsa(pnlsr, interest,
108 origRouter + "/" + lsTypeString, interestedLsSeqNo);
109 }
110 else
111 {
112 cout << "Unrecognized LSA Type :(" << endl;
113 }
114}
115
116void
117InterestManager::processInterestForNameLsa(Nlsr& pnlsr,
118 const ndn::Interest& interest,
119 string lsaKey, uint32_t interestedlsSeqNo)
120{
121 std::pair<NameLsa&, bool> nameLsa = pnlsr.getLsdb().getNameLsa(lsaKey);
122 if (nameLsa.second)
123 {
124 if (nameLsa.first.getLsSeqNo() >= interestedlsSeqNo)
125 {
126 Data data(ndn::Name(interest.getName()).appendVersion());
127 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
128 string content = nameLsa.first.getData();
129 data.setContent((const uint8_t*)content.c_str(), content.size());
130 pnlsr.getKeyManager().signData(data);
131 std::cout << ">> D: " << data << std::endl;
132 pnlsr.getNlsrFace()->put(data);
133 }
134 }
135}
136
137void
138InterestManager::processInterestForAdjLsa(Nlsr& pnlsr,
139 const ndn::Interest& interest,
140 string lsaKey, uint32_t interestedlsSeqNo)
141{
142 std::pair<AdjLsa&, bool> adjLsa = pnlsr.getLsdb().getAdjLsa(lsaKey);
143 if (adjLsa.second)
144 {
145 if (adjLsa.first.getLsSeqNo() >= interestedlsSeqNo)
146 {
147 Data data(ndn::Name(interest.getName()).appendVersion());
148 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
149 string content = adjLsa.first.getData();
150 data.setContent((const uint8_t*)content.c_str(), content.size());
151 pnlsr.getKeyManager().signData(data);
152 std::cout << ">> D: " << data << std::endl;
153 pnlsr.getNlsrFace()->put(data);
154 }
155 }
156}
157
158void
159InterestManager::processInterestForCorLsa(Nlsr& pnlsr,
160 const ndn::Interest& interest,
161 string lsaKey, uint32_t interestedlsSeqNo)
162{
163 std::pair<CorLsa&, bool> corLsa = pnlsr.getLsdb().getCorLsa(lsaKey);
164 if (corLsa.second)
165 {
166 if (corLsa.first.getLsSeqNo() >= interestedlsSeqNo)
167 {
168 Data data(ndn::Name(interest.getName()).appendVersion());
169 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
170 string content = corLsa.first.getData();
171 data.setContent((const uint8_t*)content.c_str(), content.size());
172 pnlsr.getKeyManager().signData(data);
173 std::cout << ">> D: " << data << std::endl;
174 pnlsr.getNlsrFace()->put(data);
175 }
176 }
177}
178
179void
180InterestManager::processInterestKeys(Nlsr& pnlsr, const ndn::Interest& interest)
181{
182 std::cout << "processInterestKeys called " << std::endl;
183 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 = pnlsr.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 = pnlsr.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 pnlsr.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 }
224 else
225 {
226 dataName = ndn::Name(interest.getName());
227 }
228 Data data(dataName.appendVersion());
229 data.setFreshnessPeriod(time::seconds(10)); //10 sec
230 data.setContent(chkCert.first->wireEncode());
231 pnlsr.getKeyManager().signData(data);
232 pnlsr.getNlsrFace()->put(data);
233 }
234}
235
236
237void
238InterestManager::processInterestTimedOut(Nlsr& pnlsr,
239 const ndn::Interest& interest)
240{
241 std::cout << "Timed out interest : " << interest.getName().toUri() << std::endl;
242 string intName = interest.getName().toUri();
243 Tokenizer nt(intName, "/");
244 string chkString("info");
245 if (nt.doesTokenExist(chkString))
246 {
247 string nbr = nt.getTokenString(0, nt.getTokenPosition(chkString) - 1);
248 processInterestTimedOutInfo(pnlsr , nbr , interest);
249 }
250 chkString = "LSA";
251 if (nt.doesTokenExist(chkString))
252 {
253 processInterestTimedOutLsa(pnlsr, interest);
254 }
255}
256
257void
258InterestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
259 const ndn::Interest& interest)
260{
261 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
262 int status = pnlsr.getAdl().getStatusOfNeighbor(neighbor);
263 int infoIntTimedOutCount = pnlsr.getAdl().getTimedOutInterestCount(neighbor);
264 std::cout << "Neighbor: " << neighbor << std::endl;
265 std::cout << "Status: " << status << std::endl;
266 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
267 if ((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
268 {
269 string intName = neighbor + "/" + "info" +
270 pnlsr.getConfParameter().getRouterPrefix();
271 expressInterest(pnlsr, intName, 2,
272 pnlsr.getConfParameter().getInterestResendTime());
273 }
274 else if ((status == 1) &&
275 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
276 {
277 pnlsr.getAdl().setStatusOfNeighbor(neighbor, 0);
278 pnlsr.incrementAdjBuildCount();
279 if (pnlsr.getIsBuildAdjLsaSheduled() == 0)
280 {
281 pnlsr.setIsBuildAdjLsaSheduled(1);
282 // event here
283 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
284 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
285 &pnlsr.getLsdb(),
286 boost::ref(pnlsr)));
287 }
288 }
289}
290
291void
292InterestManager::processInterestTimedOutLsa(Nlsr& pnlsr,
293 const ndn::Interest& interest)
294{
295}
296
297void
298InterestManager::expressInterest(Nlsr& pnlsr, const string& interestNamePrefix,
299 int scope, int seconds)
300{
301 std::cout << "Expressing Interest :" << interestNamePrefix << std::endl;
302 ndn::Interest i((ndn::Name(interestNamePrefix)));
303 i.setInterestLifetime(time::seconds(seconds));
304 i.setMustBeFresh(true);
305 pnlsr.getNlsrFace()->expressInterest(i,
306 ndn::func_lib::bind(&DataManager::processContent,
307 &pnlsr.getDm(),
308 boost::ref(pnlsr), _1,
309 _2, boost::ref(*this)),
310 ndn::func_lib::bind(&InterestManager::processInterestTimedOut,
311 this, boost::ref(pnlsr), _1));
312}
313
314
315void
316InterestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
317{
318 std::list<Adjacent> adjList = pnlsr.getAdl().getAdjList();
319 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
320 ++it)
321 {
322 string adjName = (*it).getName() + "/" + "info" +
323 pnlsr.getConfParameter().getRouterPrefix();
324 expressInterest(pnlsr, adjName, 2,
325 pnlsr.getConfParameter().getInterestResendTime());
326 }
327 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
328}
329
330void
331InterestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
332{
333 EventId eid = pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
334 ndn::bind(&InterestManager::sendScheduledInfoInterest, this,
335 boost::ref(pnlsr), seconds));
336}
337
338
339} //namespace nlsr