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