blob: 74952653d2063ba86e38aa74b362cfc785e8842d [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 {
189 cout<<" processInterestKeys called "<<endl;
190 ndn::shared_ptr<ndn::IdentityCertificate> cert=pnlsr.getKeyManager().getCertificate("dummy");
191 Data data(ndn::Name(interest.getName()).appendVersion());
192 data.setFreshnessPeriod(1000); // 10 sec
193 data.setContent(cert->wireEncode());
194 pnlsr.getKeyManager().signData(data);
195 std::ofstream outFile("data_sent");
196 ndn::io::save(data,outFile,ndn::io::NO_ENCODING);
197 pnlsr.getNlsrFace()->put(data);
198 }
199
200
201 void
202 interestManager::processInterestTimedOut(Nlsr& pnlsr,
203 const ndn::Interest &interest)
204 {
205 cout << "Timed out interest : " << interest.getName().toUri() << endl;
206 string intName= interest.getName().toUri();
207 nlsrTokenizer nt(intName,"/");
208 string chkString("info");
209 if( nt.doesTokenExist(chkString) )
210 {
211 string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
212 processInterestTimedOutInfo( pnlsr , nbr , interest);
213 }
214 chkString="LSA";
215 if( nt.doesTokenExist(chkString) )
216 {
217 processInterestTimedOutLsa(pnlsr, interest);
218 }
219 }
220
221 void
222 interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
223 const ndn::Interest &interest)
224 {
225 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
226 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
227 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
228 cout<<"Neighbor: "<< neighbor << endl;
229 cout<<"Status: "<< status << endl;
230 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
231 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
232 {
233 string intName=neighbor +"/"+"info"+
234 pnlsr.getConfParameter().getRouterPrefix();
235 expressInterest( pnlsr,intName,2,
236 pnlsr.getConfParameter().getInterestResendTime());
237 }
238 else if ( (status == 1) &&
239 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
240 {
241 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
242 pnlsr.incrementAdjBuildCount();
243 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
244 {
245 pnlsr.setIsBuildAdjLsaSheduled(1);
246 // event here
247 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
248 ndn::bind(&Lsdb::scheduledAdjLsaBuild,&pnlsr.getLsdb(),
249 boost::ref(pnlsr)));
250 }
251 }
252 }
253
254 void
255 interestManager::processInterestTimedOutLsa(Nlsr& pnlsr,
256 const ndn::Interest &interest)
257 {
258 }
259
260 void
261 interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
262 int scope, int seconds)
263 {
264 cout<<"Expressing Interest :"<<interestNamePrefix<<endl;
265 ndn::Interest i((ndn::Name(interestNamePrefix)));
266 //i.setScope(scope);
267 i.setInterestLifetime(seconds*1000);
268 i.setMustBeFresh(true);
269 pnlsr.getNlsrFace()->expressInterest(i,
270 ndn::func_lib::bind(&DataManager::processContent,
271 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2,boost::ref(*this)),
272 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
273 this,boost::ref(pnlsr),_1));
274 }
275
276
277 void
278 interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
279 {
280 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
281 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
282 {
283 string adjName=(*it).getAdjacentName()+"/"+"info"+
284 pnlsr.getConfParameter().getRouterPrefix();
285 expressInterest( pnlsr,adjName,2,
286 pnlsr.getConfParameter().getInterestResendTime());
287 }
288 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
289 }
290
291 void
292 interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
293 {
294 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
295 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
296 boost::ref(pnlsr),seconds));
297 }
298
299
300} //namespace nlsr