blob: b22b4458aeebd992418b3c48872a15ddfb0db855 [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
akmhoque5a44dd42014-03-12 18:11:32 -050017 using namespace std;
18 using namespace ndn;
akmhoqueba094742014-02-28 11:47:21 -060019
akmhoque5a44dd42014-03-12 18:11:32 -050020 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) )
akmhoqueba094742014-02-28 11:47:21 -060031 {
akmhoque5a44dd42014-03-12 18:11:32 -050032 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
33 cout <<"Neighbor: " << nbr <<endl;
34 processInterestInfo(pnlsr,nbr,interest);
akmhoqueba094742014-02-28 11:47:21 -060035 }
akmhoque5a44dd42014-03-12 18:11:32 -050036 chkString="LSA";
37 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060038 {
akmhoque5a44dd42014-03-12 18:11:32 -050039 processInterestLsa(pnlsr,interest);
akmhoqueba094742014-02-28 11:47:21 -060040 }
akmhoque5a44dd42014-03-12 18:11:32 -050041 chkString="keys";
42 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060043 {
akmhoque5a44dd42014-03-12 18:11:32 -050044 processInterestKeys(pnlsr,interest);
akmhoqueba094742014-02-28 11:47:21 -060045 }
akmhoque5a44dd42014-03-12 18:11:32 -050046 //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 }
akmhoqueba094742014-02-28 11:47:21 -060053
akmhoque5a44dd42014-03-12 18:11:32 -050054 void
55 interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
56 const ndn::Interest &interest)
57 {
58 if ( pnlsr.getAdl().isNeighbor(neighbor) )
akmhoqueba094742014-02-28 11:47:21 -060059 {
akmhoque5a44dd42014-03-12 18:11:32 -050060 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 }
akmhoqueba094742014-02-28 11:47:21 -060074 }
akmhoque5a44dd42014-03-12 18:11:32 -050075 }
akmhoqueba094742014-02-28 11:47:21 -060076
akmhoque5a44dd42014-03-12 18:11:32 -050077 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
akmhoqueba094742014-02-28 11:47:21 -060093 {
akmhoque5a44dd42014-03-12 18:11:32 -050094 interestedLsType=boost::lexical_cast<uint8_t>(lsTypeString);
95 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeqString);
akmhoqueba094742014-02-28 11:47:21 -060096 }
akmhoque5a44dd42014-03-12 18:11:32 -050097 catch(std::exception &e)
akmhoqueba094742014-02-28 11:47:21 -060098 {
akmhoque5a44dd42014-03-12 18:11:32 -050099 return;
akmhoqueba094742014-02-28 11:47:21 -0600100 }
akmhoque5a44dd42014-03-12 18:11:32 -0500101 cout <<"Ls Type: "<<interestedLsType<<endl;
102 if( lsTypeString == "1" ) //Name Lsa
akmhoqueba094742014-02-28 11:47:21 -0600103 {
akmhoque5a44dd42014-03-12 18:11:32 -0500104 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 {
akmhoqueba094742014-02-28 11:47:21 -0600133 Data data(ndn::Name(interest.getName()).appendVersion());
134 data.setFreshnessPeriod(1000); // 10 sec
akmhoque5a44dd42014-03-12 18:11:32 -0500135 string content=nameLsa.first.getNameLsaData();
136 data.setContent((const uint8_t*)content.c_str(),content.size());
akmhoqueba094742014-02-28 11:47:21 -0600137 pnlsr.getKeyManager().signData(data);
akmhoque5a44dd42014-03-12 18:11:32 -0500138 cout << ">> D: " << data << endl;
akmhoqueba094742014-02-28 11:47:21 -0600139 pnlsr.getNlsrFace()->put(data);
akmhoque5a44dd42014-03-12 18:11:32 -0500140 }
akmhoqueba094742014-02-28 11:47:21 -0600141 }
akmhoque5a44dd42014-03-12 18:11:32 -0500142 }
akmhoqueba094742014-02-28 11:47:21 -0600143
akmhoque5a44dd42014-03-12 18:11:32 -0500144 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 )
akmhoqueba094742014-02-28 11:47:21 -0600151 {
akmhoque5a44dd42014-03-12 18:11:32 -0500152 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 }
akmhoqueba094742014-02-28 11:47:21 -0600162 }
akmhoque5a44dd42014-03-12 18:11:32 -0500163 }
akmhoqueba094742014-02-28 11:47:21 -0600164
akmhoque5a44dd42014-03-12 18:11:32 -0500165 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 )
akmhoqueba094742014-02-28 11:47:21 -0600172 {
akmhoque5a44dd42014-03-12 18:11:32 -0500173 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 }
akmhoqueba094742014-02-28 11:47:21 -0600183 }
akmhoque5a44dd42014-03-12 18:11:32 -0500184 }
akmhoqueba094742014-02-28 11:47:21 -0600185
akmhoque5a44dd42014-03-12 18:11:32 -0500186 void
187 interestManager::processInterestKeys(Nlsr& pnlsr,const ndn::Interest &interest)
188 {
189 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(
195 nt.getTokenNumber()-1));
196 cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl;
197 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert=
198 pnlsr.getKeyManager().getCertificateFromStore(certName,seqNum);
199 if( chkCert.second )
akmhoqueba094742014-02-28 11:47:21 -0600200 {
akmhoque5a44dd42014-03-12 18:11:32 -0500201 Data data(ndn::Name(interest.getName()).appendVersion());
202 data.setFreshnessPeriod(1000); //10 sec
203 data.setContent(chkCert.first->wireEncode());
204 pnlsr.getKeyManager().signData(data);
205 pnlsr.getNlsrFace()->put(data);
akmhoqueba094742014-02-28 11:47:21 -0600206 }
akmhoque5a44dd42014-03-12 18:11:32 -0500207 //std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert=
208 /*
209 ndn::shared_ptr<ndn::IdentityCertificate> cert=pnlsr.getKeyManager().getCertificate();
210 Data data(ndn::Name(interest.getName()).appendVersion());
211 data.setFreshnessPeriod(1000); // 10 sec
212 data.setContent(cert->wireEncode());
213 pnlsr.getKeyManager().signData(data);
214 //std::ofstream outFile("data_sent");
215 //ndn::io::save(data,outFile,ndn::io::NO_ENCODING);
216 pnlsr.getNlsrFace()->put(data);
217 */
218 }
akmhoqueba094742014-02-28 11:47:21 -0600219
akmhoque5a44dd42014-03-12 18:11:32 -0500220
221 void
222 interestManager::processInterestTimedOut(Nlsr& pnlsr,
223 const ndn::Interest &interest)
224 {
225 cout << "Timed out interest : " << interest.getName().toUri() << endl;
226 string intName= interest.getName().toUri();
227 nlsrTokenizer nt(intName,"/");
228 string chkString("info");
229 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -0600230 {
akmhoque5a44dd42014-03-12 18:11:32 -0500231 string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
232 processInterestTimedOutInfo( pnlsr , nbr , interest);
akmhoqueba094742014-02-28 11:47:21 -0600233 }
akmhoque5a44dd42014-03-12 18:11:32 -0500234 chkString="LSA";
235 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -0600236 {
akmhoque5a44dd42014-03-12 18:11:32 -0500237 processInterestTimedOutLsa(pnlsr, interest);
akmhoqueba094742014-02-28 11:47:21 -0600238 }
akmhoque5a44dd42014-03-12 18:11:32 -0500239 }
akmhoqueba094742014-02-28 11:47:21 -0600240
akmhoque5a44dd42014-03-12 18:11:32 -0500241 void
242 interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
243 const ndn::Interest &interest)
244 {
245 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
246 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
247 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
248 cout<<"Neighbor: "<< neighbor << endl;
249 cout<<"Status: "<< status << endl;
250 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
251 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
akmhoqueba094742014-02-28 11:47:21 -0600252 {
akmhoque5a44dd42014-03-12 18:11:32 -0500253 string intName=neighbor +"/"+"info"+
254 pnlsr.getConfParameter().getRouterPrefix();
255 expressInterest( pnlsr,intName,2,
256 pnlsr.getConfParameter().getInterestResendTime());
akmhoqueba094742014-02-28 11:47:21 -0600257 }
akmhoque5a44dd42014-03-12 18:11:32 -0500258 else if ( (status == 1) &&
259 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
260 {
261 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
262 pnlsr.incrementAdjBuildCount();
263 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
264 {
265 pnlsr.setIsBuildAdjLsaSheduled(1);
266 // event here
267 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
268 ndn::bind(&Lsdb::scheduledAdjLsaBuild,&pnlsr.getLsdb(),
269 boost::ref(pnlsr)));
270 }
271 }
272 }
273
274 void
275 interestManager::processInterestTimedOutLsa(Nlsr& pnlsr,
276 const ndn::Interest &interest)
277 {
278 }
279
280 void
281 interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
282 int scope, int seconds)
283 {
284 cout<<"Expressing Interest :"<<interestNamePrefix<<endl;
285 ndn::Interest i((ndn::Name(interestNamePrefix)));
286 //i.setScope(scope);
287 i.setInterestLifetime(seconds*1000);
288 i.setMustBeFresh(true);
289 pnlsr.getNlsrFace()->expressInterest(i,
290 ndn::func_lib::bind(&DataManager::processContent,
291 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2,boost::ref(*this)),
292 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
293 this,boost::ref(pnlsr),_1));
294 }
295
296
297 void
298 interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
299 {
300 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
301 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
302 {
303 string adjName=(*it).getAdjacentName()+"/"+"info"+
304 pnlsr.getConfParameter().getRouterPrefix();
305 expressInterest( pnlsr,adjName,2,
306 pnlsr.getConfParameter().getInterestResendTime());
307 }
308 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
309 }
310
311 void
312 interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
313 {
314 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
315 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
316 boost::ref(pnlsr),seconds));
317 }
akmhoqueba094742014-02-28 11:47:21 -0600318
319
320} //namespace nlsr