blob: a29fac9a9c5440141d96c222c296e1b443855cdd [file] [log] [blame]
akmhoque95b7c8c2014-01-31 15:53: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
11using namespace std;
12using namespace ndn;
13
14void
15interestManager::processInterest( nlsr& pnlsr,
16 const ptr_lib::shared_ptr<const Name> &name,
17 const ptr_lib::shared_ptr<const Interest> &interest)
18{
19
20 cout << "<< I: " << *interest << endl;
akmhoquea8cd6b92014-01-31 20:13:26 -060021 string intName=interest->getName().toUri();
22 cout << "Interest Received for Name: "<< intName <<endl;
23 nlsrTokenizer nt(intName,"/");
24 string chkString("info");
25 if( nt.doesTokenExist(chkString) ){
26 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
27 cout <<"Neighbor: " << nbr <<endl;
28 processInterestInfo(pnlsr,nbr,interest);
29 }
30
31 //Data data(ndn::Name(interest->getName()).append("testApp").appendVersion());
32 //data.setFreshnessPeriod(1000); // 10 sec
33 //data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY"));
34 //pnlsr.getKeyChain().sign(data);
35 //cout << ">> D: " << data << endl;
36 //pnlsr.getNlsrFace().put(data);
37}
38
39void
40interestManager::processInterestInfo(nlsr& pnlsr, string& neighbor,
41 const ptr_lib::shared_ptr<const Interest> &interest)
42{
43 if ( pnlsr.getAdl().isNeighbor(neighbor) )
44 {
45 Data data(ndn::Name(interest->getName()).appendVersion());
46 data.setFreshnessPeriod(1000); // 10 sec
47 data.setContent((const uint8_t*)"info", sizeof("info"));
48 pnlsr.getKeyChain().sign(data);
49 cout << ">> D: " << data << endl;
50 pnlsr.getNlsrFace().put(data);
51
52 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
53 if ( status == 0 )
54 {
55 string intName=neighbor +"/"+"info"+
56 pnlsr.getConfParameter().getRouterPrefix();
57 expressInterest( pnlsr,intName,2,
58 pnlsr.getConfParameter().getInterestResendTime());
59 }
60 }
akmhoque95b7c8c2014-01-31 15:53:09 -060061}
62
63void
64interestManager::processInterestTimedOut(nlsr& pnlsr,
65 const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest)
66{
67 cout << "Timed out interest : " << interest->getName().toUri() << endl;
68 string intName= interest->getName().toUri();
akmhoque95b7c8c2014-01-31 15:53:09 -060069 nlsrTokenizer nt(intName,"/");
70 string chkString("info");
71 if( nt.doesTokenExist(chkString) ){
akmhoquea8cd6b92014-01-31 20:13:26 -060072 string nbr="/" + nt.getFirstToken()
73 +nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
74 processInterestTimedOutInfo( pnlsr , nbr , interest);
akmhoque95b7c8c2014-01-31 15:53:09 -060075 }
76
77}
78
79void
akmhoquea8cd6b92014-01-31 20:13:26 -060080interestManager::processInterestTimedOutInfo(nlsr& pnlsr, string& neighbor,
81 const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest)
82{
83 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
84 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
85 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
86 cout<<"Neighbor: "<< neighbor << endl;
87 cout<<"Status: "<< status << endl;
88 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
89
90 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
91 {
92 string intName=neighbor +"/"+"info"+
93 pnlsr.getConfParameter().getRouterPrefix();
94 expressInterest( pnlsr,intName,2,
95 pnlsr.getConfParameter().getInterestResendTime());
96 }
97 else if ( (status == 1) &&
98 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
99 {
100 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
101 // schedule event for building adjacency LSA
102 }
103
104}
105
106void
akmhoque95b7c8c2014-01-31 15:53:09 -0600107interestManager::expressInterest(nlsr& pnlsr,const string& interestNamePrefix,
108 int scope, int seconds)
109{
110 Interest i((ndn::Name(interestNamePrefix)));
111 //i.setScope(scope);
112 i.setInterestLifetime(seconds*1000);
113 i.setMustBeFresh(true);
114
akmhoquea8cd6b92014-01-31 20:13:26 -0600115 pnlsr.getNlsrFace().expressInterest(i,
akmhoque95b7c8c2014-01-31 15:53:09 -0600116 ndn::func_lib::bind(&DataManager::processContent,
akmhoquea8cd6b92014-01-31 20:13:26 -0600117 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2),
akmhoque95b7c8c2014-01-31 15:53:09 -0600118 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
119 this,boost::ref(pnlsr),_1));
120}
121
122
123void
124interestManager::sendScheduledInfoInterest(nlsr& pnlsr, int seconds)
125{
akmhoquea8cd6b92014-01-31 20:13:26 -0600126 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
akmhoque95b7c8c2014-01-31 15:53:09 -0600127 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end();++it)
128 {
129 string adjName=(*it).getAdjacentName()+"/"+"info"+
akmhoquea8cd6b92014-01-31 20:13:26 -0600130 pnlsr.getConfParameter().getRouterPrefix();
131 expressInterest( pnlsr,adjName,2,pnlsr.getConfParameter().getInterestResendTime());
akmhoque95b7c8c2014-01-31 15:53:09 -0600132 }
133
akmhoquea8cd6b92014-01-31 20:13:26 -0600134 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
akmhoque95b7c8c2014-01-31 15:53:09 -0600135
136}
137
138void
139interestManager::scheduleInfoInterest(nlsr& pnlsr, int seconds)
140{
akmhoquea8cd6b92014-01-31 20:13:26 -0600141 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
akmhoque95b7c8c2014-01-31 15:53:09 -0600142 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
143 boost::ref(pnlsr),seconds));
144}
145