blob: de551bb319d66354bad6098893c570f65c1b6874 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#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 "nlsr_im.hpp"
10#include "nlsr_dm.hpp"
11#include "utility/nlsr_tokenizer.hpp"
12#include "nlsr_lsdb.hpp"
13
14namespace nlsr
15{
16
17 using namespace std;
18 using namespace ndn;
19
20 void
21 interestManager::processInterest( Nlsr& pnlsr,
22 const ndn::Name &name,
23 const ndn::Interest &interest)
24 {
25 cout << "<< I: " << interest << endl;
26 string intName=interest.getName().toUri();
27 cout << "Interest Received for Name: "<< intName <<endl;
28 nlsrTokenizer nt(intName,"/");
29 string chkString("info");
30 if( nt.doesTokenExist(chkString) )
31 {
32 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
33 cout <<"Neighbor: " << nbr <<endl;
34 processInterestInfo(pnlsr,nbr,interest);
35 }
36 chkString="LSA";
37 if( nt.doesTokenExist(chkString) )
38 {
39 processInterestLsa(pnlsr,interest);
40 }
41 chkString="keys";
42 if( nt.doesTokenExist(chkString) )
43 {
44 processInterestKeys(pnlsr,interest);
45 }
46 //Data data(ndn::Name(interest->getName()).append("testApp").appendVersion());
47 //data.setFreshnessPeriod(1000); // 10 sec
48 //data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY"));
49 //pnlsr.getKeyChain().sign(data);
50 //cout << ">> D: " << data << endl;
51 //pnlsr.getNlsrFace().put(data);
52 }
53
54 void
55 interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
56 const ndn::Interest &interest)
57 {
58 if ( pnlsr.getAdl().isNeighbor(neighbor) )
59 {
60 Data data(ndn::Name(interest.getName()).appendVersion());
61 data.setFreshnessPeriod(1000); // 10 sec
62 data.setContent((const uint8_t*)"info", sizeof("info"));
63 pnlsr.getKeyManager().signData(data);
64 cout << ">> D: " << data << endl;
65 pnlsr.getNlsrFace()->put(data);
66 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
67 if ( status == 0 )
68 {
69 string intName=neighbor +"/"+"info"+
70 pnlsr.getConfParameter().getRouterPrefix();
71 expressInterest( pnlsr,intName,2,
72 pnlsr.getConfParameter().getInterestResendTime());
73 }
74 }
75 }
76
77 void
78 interestManager::processInterestLsa(Nlsr& pnlsr,const ndn::Interest &interest)
79 {
80 string intName=interest.getName().toUri();
81 nlsrTokenizer nt(intName,"/");
82 string chkString("LSA");
83 string origRouter=nt.getTokenString(nt.getTokenPosition(chkString)+1,
84 nt.getTokenNumber()-3);
85 string lsTypeString=nt.getToken(nt.getTokenNumber()-2);
86 string lsSeqString=nt.getToken(nt.getTokenNumber()-1);
87 cout<<"Router Name: "<<origRouter<<endl;
88 cout<<"Ls Type : "<<lsTypeString<<endl;
89 cout<<"Ls Seq : "<<lsSeqString<<endl;
90 uint8_t interestedLsType;
91 uint32_t interestedLsSeqNo;
92 try
93 {
94 interestedLsType=boost::lexical_cast<uint8_t>(lsTypeString);
95 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeqString);
96 }
97 catch(std::exception &e)
98 {
99 return;
100 }
101 cout <<"Ls Type: "<<interestedLsType<<endl;
102 if( lsTypeString == "1" ) //Name Lsa
103 {
104 processInterestForNameLsa(pnlsr, interest,
105 origRouter+"/"+lsTypeString, interestedLsSeqNo);
106 }
107 else if( lsTypeString == "2" ) //Adj Lsa
108 {
109 processInterestForAdjLsa(pnlsr, interest,
110 origRouter+"/"+lsTypeString, interestedLsSeqNo);
111 }
112 else if( lsTypeString == "3" ) //Cor Lsa
113 {
114 processInterestForCorLsa(pnlsr, interest,
115 origRouter+"/"+lsTypeString, interestedLsSeqNo);
116 }
117 else
118 {
119 cout<<"Unrecognized LSA Type :("<<endl;
120 }
121 }
122
123 void
124 interestManager::processInterestForNameLsa(Nlsr& pnlsr,
125 const ndn::Interest &interest,
126 string lsaKey, uint32_t interestedlsSeqNo)
127 {
128 std::pair<NameLsa&, bool> nameLsa=pnlsr.getLsdb().getNameLsa(lsaKey);
129 if( nameLsa.second )
130 {
131 if ( nameLsa.first.getLsSeqNo() >= interestedlsSeqNo )
132 {
133 Data data(ndn::Name(interest.getName()).appendVersion());
134 data.setFreshnessPeriod(1000); // 10 sec
135 string content=nameLsa.first.getNameLsaData();
136 data.setContent((const uint8_t*)content.c_str(),content.size());
137 pnlsr.getKeyManager().signData(data);
138 cout << ">> D: " << data << endl;
139 pnlsr.getNlsrFace()->put(data);
140 }
141 }
142 }
143
144 void
145 interestManager::processInterestForAdjLsa(Nlsr& pnlsr,
146 const ndn::Interest &interest,
147 string lsaKey, uint32_t interestedlsSeqNo)
148 {
149 std::pair<AdjLsa&, bool> adjLsa=pnlsr.getLsdb().getAdjLsa(lsaKey);
150 if( adjLsa.second )
151 {
152 if ( adjLsa.first.getLsSeqNo() >= interestedlsSeqNo )
153 {
154 Data data(ndn::Name(interest.getName()).appendVersion());
155 data.setFreshnessPeriod(1000); // 10 sec
156 string content=adjLsa.first.getAdjLsaData();
157 data.setContent((const uint8_t*)content.c_str(),content.size());
158 pnlsr.getKeyManager().signData(data);
159 cout << ">> D: " << data << endl;
160 pnlsr.getNlsrFace()->put(data);
161 }
162 }
163 }
164
165 void
166 interestManager::processInterestForCorLsa(Nlsr& pnlsr,
167 const ndn::Interest &interest,
168 string lsaKey, uint32_t interestedlsSeqNo)
169 {
170 std::pair<CorLsa&, bool> corLsa=pnlsr.getLsdb().getCorLsa(lsaKey);
171 if( corLsa.second )
172 {
173 if ( corLsa.first.getLsSeqNo() >= interestedlsSeqNo )
174 {
175 Data data(ndn::Name(interest.getName()).appendVersion());
176 data.setFreshnessPeriod(1000); // 10 sec
177 string content=corLsa.first.getCorLsaData();
178 data.setContent((const uint8_t*)content.c_str(),content.size());
179 pnlsr.getKeyManager().signData(data);
180 cout << ">> D: " << data << endl;
181 pnlsr.getNlsrFace()->put(data);
182 }
183 }
184 }
185
186 void
187 interestManager::processInterestKeys(Nlsr& pnlsr,const ndn::Interest &interest)
188 {
akmhoqueeb764c52014-03-11 16:01:09 -0500189 cout<<"processInterestKeys called "<<endl;
190 string intName=interest.getName().toUri();
191 cout<<"Interest Name for Key: "<<intName<<std::endl;
192 nlsrTokenizer nt(intName,"/");
193 std::string certName=nt.getTokenString(0,nt.getTokenNumber()-2);
194 uint32_t seqNum=boost::lexical_cast<uint32_t>(nt.getToken(nt.getTokenNumber()-1));
195 cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl;
196 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert=
197 pnlsr.getKeyManager().getCertificateFromStore(certName,seqNum);
198 if( chkCert.second )
199 {
200 Data data(ndn::Name(interest.getName()).appendVersion());
201 data.setFreshnessPeriod(1000); //10 sec
202 data.setContent(chkCert.first->wireEncode());
203 pnlsr.getKeyManager().signData(data);
204 pnlsr.getNlsrFace()->put(data);
205 }
206 //std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert=
207 /*
208 ndn::shared_ptr<ndn::IdentityCertificate> cert=pnlsr.getKeyManager().getCertificate();
akmhoqueba094742014-02-28 11:47:21 -0600209 Data data(ndn::Name(interest.getName()).appendVersion());
210 data.setFreshnessPeriod(1000); // 10 sec
211 data.setContent(cert->wireEncode());
212 pnlsr.getKeyManager().signData(data);
akmhoqueeb764c52014-03-11 16:01:09 -0500213 //std::ofstream outFile("data_sent");
214 //ndn::io::save(data,outFile,ndn::io::NO_ENCODING);
akmhoqueba094742014-02-28 11:47:21 -0600215 pnlsr.getNlsrFace()->put(data);
akmhoqueeb764c52014-03-11 16:01:09 -0500216 */
akmhoqueba094742014-02-28 11:47:21 -0600217 }
218
219
220 void
221 interestManager::processInterestTimedOut(Nlsr& pnlsr,
222 const ndn::Interest &interest)
223 {
224 cout << "Timed out interest : " << interest.getName().toUri() << endl;
225 string intName= interest.getName().toUri();
226 nlsrTokenizer nt(intName,"/");
227 string chkString("info");
228 if( nt.doesTokenExist(chkString) )
229 {
230 string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
231 processInterestTimedOutInfo( pnlsr , nbr , interest);
232 }
233 chkString="LSA";
234 if( nt.doesTokenExist(chkString) )
235 {
236 processInterestTimedOutLsa(pnlsr, interest);
237 }
238 }
239
240 void
241 interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
242 const ndn::Interest &interest)
243 {
244 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
245 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
246 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
247 cout<<"Neighbor: "<< neighbor << endl;
248 cout<<"Status: "<< status << endl;
249 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
250 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
251 {
252 string intName=neighbor +"/"+"info"+
253 pnlsr.getConfParameter().getRouterPrefix();
254 expressInterest( pnlsr,intName,2,
255 pnlsr.getConfParameter().getInterestResendTime());
256 }
257 else if ( (status == 1) &&
258 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
259 {
260 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
261 pnlsr.incrementAdjBuildCount();
262 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
263 {
264 pnlsr.setIsBuildAdjLsaSheduled(1);
265 // event here
266 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
267 ndn::bind(&Lsdb::scheduledAdjLsaBuild,&pnlsr.getLsdb(),
268 boost::ref(pnlsr)));
269 }
270 }
271 }
272
273 void
274 interestManager::processInterestTimedOutLsa(Nlsr& pnlsr,
275 const ndn::Interest &interest)
276 {
277 }
278
279 void
280 interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
281 int scope, int seconds)
282 {
283 cout<<"Expressing Interest :"<<interestNamePrefix<<endl;
284 ndn::Interest i((ndn::Name(interestNamePrefix)));
285 //i.setScope(scope);
286 i.setInterestLifetime(seconds*1000);
287 i.setMustBeFresh(true);
288 pnlsr.getNlsrFace()->expressInterest(i,
289 ndn::func_lib::bind(&DataManager::processContent,
290 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2,boost::ref(*this)),
291 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
292 this,boost::ref(pnlsr),_1));
293 }
294
295
296 void
297 interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
298 {
299 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
300 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
301 {
302 string adjName=(*it).getAdjacentName()+"/"+"info"+
303 pnlsr.getConfParameter().getRouterPrefix();
304 expressInterest( pnlsr,adjName,2,
305 pnlsr.getConfParameter().getInterestResendTime());
306 }
307 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
308 }
309
310 void
311 interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
312 {
313 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
314 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
315 boost::ref(pnlsr),seconds));
316 }
317
318
319} //namespace nlsr