blob: 29c0fcdbac92865ae2d651ab1727d414d86a3cbb [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
akmhoque1fd8c1e2014-02-19 19:41:49 -060012namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060013{
14
akmhoque1fd8c1e2014-02-19 19:41:49 -060015 using namespace std;
16 using namespace ndn;
akmhoque298385a2014-02-13 14:13:09 -060017
akmhoque1fd8c1e2014-02-19 19:41:49 -060018 void
19 interestManager::processInterest( Nlsr& pnlsr,
20 const ndn::Name &name,
21 const ndn::Interest &interest)
22 {
akmhoque298385a2014-02-13 14:13:09 -060023
akmhoque1fd8c1e2014-02-19 19:41:49 -060024 cout << "<< I: " << interest << endl;
25 string intName=interest.getName().toUri();
26 cout << "Interest Received for Name: "<< intName <<endl;
27 nlsrTokenizer 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 }
akmhoque298385a2014-02-13 14:13:09 -060035
akmhoque1fd8c1e2014-02-19 19:41:49 -060036 //Data data(ndn::Name(interest->getName()).append("testApp").appendVersion());
37 //data.setFreshnessPeriod(1000); // 10 sec
38 //data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY"));
39 //pnlsr.getKeyChain().sign(data);
40 //cout << ">> D: " << data << endl;
41 //pnlsr.getNlsrFace().put(data);
42 }
akmhoque298385a2014-02-13 14:13:09 -060043
akmhoque1fd8c1e2014-02-19 19:41:49 -060044 void
45 interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
46 const ndn::Interest &interest)
47 {
48 if ( pnlsr.getAdl().isNeighbor(neighbor) )
49 {
50 Data data(ndn::Name(interest.getName()).appendVersion());
51 data.setFreshnessPeriod(1000); // 10 sec
52 data.setContent((const uint8_t*)"info", sizeof("info"));
53 pnlsr.getKeyChain().sign(data);
54 cout << ">> D: " << data << endl;
55 pnlsr.getNlsrFace().put(data);
akmhoque298385a2014-02-13 14:13:09 -060056
akmhoque1fd8c1e2014-02-19 19:41:49 -060057 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
58 if ( status == 0 )
59 {
60 string intName=neighbor +"/"+"info"+
61 pnlsr.getConfParameter().getRouterPrefix();
62 expressInterest( pnlsr,intName,2,
63 pnlsr.getConfParameter().getInterestResendTime());
64 }
65 }
66 }
akmhoque298385a2014-02-13 14:13:09 -060067
akmhoque1fd8c1e2014-02-19 19:41:49 -060068 void
69 interestManager::processInterestTimedOut(Nlsr& pnlsr,
70 const ndn::Interest &interest)
71 {
72 cout << "Timed out interest : " << interest.getName().toUri() << endl;
73 string intName= interest.getName().toUri();
74 nlsrTokenizer nt(intName,"/");
75 string chkString("info");
76 if( nt.doesTokenExist(chkString) )
77 {
78 string nbr="/" + nt.getFirstToken()
79 +nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
80 processInterestTimedOutInfo( pnlsr , nbr , interest);
81 }
akmhoque298385a2014-02-13 14:13:09 -060082
akmhoque1fd8c1e2014-02-19 19:41:49 -060083 }
akmhoque298385a2014-02-13 14:13:09 -060084
akmhoque1fd8c1e2014-02-19 19:41:49 -060085 void
86 interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
87 const ndn::Interest &interest)
88 {
89 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
90 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
91 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
92 cout<<"Neighbor: "<< neighbor << endl;
93 cout<<"Status: "<< status << endl;
94 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
95
96 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
97 {
98 string intName=neighbor +"/"+"info"+
99 pnlsr.getConfParameter().getRouterPrefix();
100 expressInterest( pnlsr,intName,2,
101 pnlsr.getConfParameter().getInterestResendTime());
102 }
103 else if ( (status == 1) &&
104 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
105 {
106 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
107 pnlsr.incrementAdjBuildCount();
108 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
109 {
110 pnlsr.setIsBuildAdjLsaSheduled(1);
111 // event here
112 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
113 ndn::bind(&Lsdb::scheduledAdjLsaBuild,pnlsr.getLsdb(),
114 boost::ref(pnlsr)));
115 }
116 }
117
118 }
119
120 void
121 interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
122 int scope, int seconds)
123 {
124 Interest i((ndn::Name(interestNamePrefix)));
125 //i.setScope(scope);
126 i.setInterestLifetime(seconds*1000);
127 i.setMustBeFresh(true);
128
129 pnlsr.getNlsrFace().expressInterest(i,
130 ndn::func_lib::bind(&DataManager::processContent,
131 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2),
132 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
akmhoque298385a2014-02-13 14:13:09 -0600133 this,boost::ref(pnlsr),_1));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600134 }
akmhoque298385a2014-02-13 14:13:09 -0600135
136
akmhoque1fd8c1e2014-02-19 19:41:49 -0600137 void
138 interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
139 {
140 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
141 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
142 {
143 string adjName=(*it).getAdjacentName()+"/"+"info"+
144 pnlsr.getConfParameter().getRouterPrefix();
145 expressInterest( pnlsr,adjName,2,
146 pnlsr.getConfParameter().getInterestResendTime());
147 }
akmhoque298385a2014-02-13 14:13:09 -0600148
akmhoque1fd8c1e2014-02-19 19:41:49 -0600149 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
akmhoque298385a2014-02-13 14:13:09 -0600150
akmhoque1fd8c1e2014-02-19 19:41:49 -0600151 }
akmhoque298385a2014-02-13 14:13:09 -0600152
akmhoque1fd8c1e2014-02-19 19:41:49 -0600153 void
154 interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
155 {
156 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
157 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
158 boost::ref(pnlsr),seconds));
159 }
akmhoque298385a2014-02-13 14:13:09 -0600160
akmhoqueb1710aa2014-02-19 17:13:36 -0600161
162} //namespace nlsr