blob: 4e77f394e17750ca38b7a07fce2fde44ebe2cffc [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include<iostream>
2#include<cstdlib>
3
4
5
6#include "nlsr.hpp"
7#include "nlsr_im.hpp"
8#include "nlsr_dm.hpp"
9#include "nlsr_tokenizer.hpp"
10#include "nlsr_lsdb.hpp"
11
akmhoqueb1710aa2014-02-19 17:13:36 -060012namespace nlsr {
13
akmhoque298385a2014-02-13 14:13:09 -060014using namespace std;
15using namespace ndn;
16
17void
akmhoque1a481092014-02-19 16:34:22 -060018interestManager::processInterest( Nlsr& pnlsr,
akmhoque298385a2014-02-13 14:13:09 -060019 const ndn::Name &name,
20 const ndn::Interest &interest)
21{
22
23 cout << "<< I: " << interest << endl;
24 string intName=interest.getName().toUri();
25 cout << "Interest Received for Name: "<< intName <<endl;
26 nlsrTokenizer nt(intName,"/");
27 string chkString("info");
28 if( nt.doesTokenExist(chkString) ){
29 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
30 cout <<"Neighbor: " << nbr <<endl;
31 processInterestInfo(pnlsr,nbr,interest);
32 }
33
34 //Data data(ndn::Name(interest->getName()).append("testApp").appendVersion());
35 //data.setFreshnessPeriod(1000); // 10 sec
36 //data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY"));
37 //pnlsr.getKeyChain().sign(data);
38 //cout << ">> D: " << data << endl;
39 //pnlsr.getNlsrFace().put(data);
40}
41
42void
akmhoque1a481092014-02-19 16:34:22 -060043interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
akmhoque298385a2014-02-13 14:13:09 -060044 const ndn::Interest &interest)
45{
46 if ( pnlsr.getAdl().isNeighbor(neighbor) )
47 {
48 Data data(ndn::Name(interest.getName()).appendVersion());
49 data.setFreshnessPeriod(1000); // 10 sec
50 data.setContent((const uint8_t*)"info", sizeof("info"));
51 pnlsr.getKeyChain().sign(data);
52 cout << ">> D: " << data << endl;
53 pnlsr.getNlsrFace().put(data);
54
55 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
56 if ( status == 0 )
57 {
58 string intName=neighbor +"/"+"info"+
59 pnlsr.getConfParameter().getRouterPrefix();
60 expressInterest( pnlsr,intName,2,
61 pnlsr.getConfParameter().getInterestResendTime());
62 }
63 }
64}
65
66void
akmhoque1a481092014-02-19 16:34:22 -060067interestManager::processInterestTimedOut(Nlsr& pnlsr,
akmhoque298385a2014-02-13 14:13:09 -060068 const ndn::Interest &interest)
69{
70 cout << "Timed out interest : " << interest.getName().toUri() << endl;
71 string intName= interest.getName().toUri();
72 nlsrTokenizer nt(intName,"/");
73 string chkString("info");
74 if( nt.doesTokenExist(chkString) ){
75 string nbr="/" + nt.getFirstToken()
76 +nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
77 processInterestTimedOutInfo( pnlsr , nbr , interest);
78 }
79
80}
81
82void
akmhoque1a481092014-02-19 16:34:22 -060083interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
akmhoque298385a2014-02-13 14:13:09 -060084 const ndn::Interest &interest)
85{
86 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
87 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
88 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
89 cout<<"Neighbor: "<< neighbor << endl;
90 cout<<"Status: "<< status << endl;
91 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
92
93 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
94 {
95 string intName=neighbor +"/"+"info"+
96 pnlsr.getConfParameter().getRouterPrefix();
97 expressInterest( pnlsr,intName,2,
98 pnlsr.getConfParameter().getInterestResendTime());
99 }
100 else if ( (status == 1) &&
101 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
102 {
103 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
104 pnlsr.incrementAdjBuildCount();
105 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
106 {
107 pnlsr.setIsBuildAdjLsaSheduled(1);
108 // event here
109 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
110 ndn::bind(&Lsdb::scheduledAdjLsaBuild,pnlsr.getLsdb(),
111 boost::ref(pnlsr)));
112 }
113 }
114
115}
116
117void
akmhoque1a481092014-02-19 16:34:22 -0600118interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
akmhoque298385a2014-02-13 14:13:09 -0600119 int scope, int seconds)
120{
121 Interest i((ndn::Name(interestNamePrefix)));
122 //i.setScope(scope);
123 i.setInterestLifetime(seconds*1000);
124 i.setMustBeFresh(true);
125
126 pnlsr.getNlsrFace().expressInterest(i,
127 ndn::func_lib::bind(&DataManager::processContent,
128 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2),
129 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
130 this,boost::ref(pnlsr),_1));
131}
132
133
134void
akmhoque1a481092014-02-19 16:34:22 -0600135interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
akmhoque298385a2014-02-13 14:13:09 -0600136{
137 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
138 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end();++it)
139 {
140 string adjName=(*it).getAdjacentName()+"/"+"info"+
141 pnlsr.getConfParameter().getRouterPrefix();
142 expressInterest( pnlsr,adjName,2,pnlsr.getConfParameter().getInterestResendTime());
143 }
144
145 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
146
147}
148
149void
akmhoque1a481092014-02-19 16:34:22 -0600150interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
akmhoque298385a2014-02-13 14:13:09 -0600151{
akmhoque85d88332014-02-17 21:11:21 -0600152 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
akmhoque298385a2014-02-13 14:13:09 -0600153 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
154 boost::ref(pnlsr),seconds));
155}
156
akmhoqueb1710aa2014-02-19 17:13:36 -0600157
158} //namespace nlsr