blob: 2e6d6d1ff38a690cb41105d218dd4de2b1659745 [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 {
akmhoque1fd8c1e2014-02-19 19:41:49 -060023 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 {
30 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
31 cout <<"Neighbor: " << nbr <<endl;
32 processInterestInfo(pnlsr,nbr,interest);
33 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060034 //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 }
akmhoque298385a2014-02-13 14:13:09 -060041
akmhoque1fd8c1e2014-02-19 19:41:49 -060042 void
43 interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
44 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"));
akmhoque66e66182014-02-21 17:56:03 -060051 pnlsr.getKeyManager().getKeyChain().sign(data);
akmhoque1fd8c1e2014-02-19 19:41:49 -060052 cout << ">> D: " << data << endl;
53 pnlsr.getNlsrFace().put(data);
akmhoque1fd8c1e2014-02-19 19:41:49 -060054 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
55 if ( status == 0 )
56 {
57 string intName=neighbor +"/"+"info"+
58 pnlsr.getConfParameter().getRouterPrefix();
59 expressInterest( pnlsr,intName,2,
60 pnlsr.getConfParameter().getInterestResendTime());
61 }
62 }
63 }
akmhoque298385a2014-02-13 14:13:09 -060064
akmhoque1fd8c1e2014-02-19 19:41:49 -060065 void
66 interestManager::processInterestTimedOut(Nlsr& pnlsr,
67 const ndn::Interest &interest)
68 {
69 cout << "Timed out interest : " << interest.getName().toUri() << endl;
70 string intName= interest.getName().toUri();
71 nlsrTokenizer nt(intName,"/");
72 string chkString("info");
73 if( nt.doesTokenExist(chkString) )
74 {
75 string nbr="/" + nt.getFirstToken()
76 +nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
77 processInterestTimedOutInfo( pnlsr , nbr , interest);
78 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060079 }
akmhoque298385a2014-02-13 14:13:09 -060080
akmhoque1fd8c1e2014-02-19 19:41:49 -060081 void
82 interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
83 const ndn::Interest &interest)
84 {
85 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
86 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
87 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
88 cout<<"Neighbor: "<< neighbor << endl;
89 cout<<"Status: "<< status << endl;
90 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -060091 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 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600112 }
113
114 void
115 interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
116 int scope, int seconds)
117 {
118 Interest i((ndn::Name(interestNamePrefix)));
119 //i.setScope(scope);
120 i.setInterestLifetime(seconds*1000);
121 i.setMustBeFresh(true);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600122 pnlsr.getNlsrFace().expressInterest(i,
123 ndn::func_lib::bind(&DataManager::processContent,
124 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2),
125 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
akmhoque298385a2014-02-13 14:13:09 -0600126 this,boost::ref(pnlsr),_1));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600127 }
akmhoque298385a2014-02-13 14:13:09 -0600128
129
akmhoque1fd8c1e2014-02-19 19:41:49 -0600130 void
131 interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
132 {
133 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
134 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
135 {
136 string adjName=(*it).getAdjacentName()+"/"+"info"+
137 pnlsr.getConfParameter().getRouterPrefix();
138 expressInterest( pnlsr,adjName,2,
139 pnlsr.getConfParameter().getInterestResendTime());
140 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600141 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600142 }
akmhoque298385a2014-02-13 14:13:09 -0600143
akmhoque1fd8c1e2014-02-19 19:41:49 -0600144 void
145 interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
146 {
147 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
148 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
149 boost::ref(pnlsr),seconds));
150 }
akmhoque298385a2014-02-13 14:13:09 -0600151
akmhoqueb1710aa2014-02-19 17:13:36 -0600152
153} //namespace nlsr