blob: 6ae0131601e854ae11f45f59b3b5410ef2e915cc [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"
akmhoquecd552472014-02-01 21:22:16 -060010#include "nlsr_lsdb.hpp"
akmhoque95b7c8c2014-01-31 15:53:09 -060011
12using namespace std;
13using namespace ndn;
14
15void
16interestManager::processInterest( nlsr& pnlsr,
akmhoque9d19a492014-02-11 19:44:29 -060017 const ndn::Name &name,
18 const ndn::Interest &interest)
akmhoque95b7c8c2014-01-31 15:53:09 -060019{
20
akmhoque9d19a492014-02-11 19:44:29 -060021 cout << "<< I: " << interest << endl;
22 string intName=interest.getName().toUri();
akmhoquea8cd6b92014-01-31 20:13:26 -060023 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,
akmhoque9d19a492014-02-11 19:44:29 -060042 const ndn::Interest &interest)
akmhoquea8cd6b92014-01-31 20:13:26 -060043{
44 if ( pnlsr.getAdl().isNeighbor(neighbor) )
45 {
akmhoque9d19a492014-02-11 19:44:29 -060046 Data data(ndn::Name(interest.getName()).appendVersion());
akmhoquea8cd6b92014-01-31 20:13:26 -060047 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 }
akmhoque95b7c8c2014-01-31 15:53:09 -060062}
63
64void
65interestManager::processInterestTimedOut(nlsr& pnlsr,
akmhoque9d19a492014-02-11 19:44:29 -060066 const ndn::Interest &interest)
akmhoque95b7c8c2014-01-31 15:53:09 -060067{
akmhoque9d19a492014-02-11 19:44:29 -060068 cout << "Timed out interest : " << interest.getName().toUri() << endl;
69 string intName= interest.getName().toUri();
akmhoque95b7c8c2014-01-31 15:53:09 -060070 nlsrTokenizer nt(intName,"/");
71 string chkString("info");
72 if( nt.doesTokenExist(chkString) ){
akmhoquea8cd6b92014-01-31 20:13:26 -060073 string nbr="/" + nt.getFirstToken()
74 +nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
75 processInterestTimedOutInfo( pnlsr , nbr , interest);
akmhoque95b7c8c2014-01-31 15:53:09 -060076 }
77
78}
79
80void
akmhoquea8cd6b92014-01-31 20:13:26 -060081interestManager::processInterestTimedOutInfo(nlsr& pnlsr, string& neighbor,
akmhoque9d19a492014-02-11 19:44:29 -060082 const ndn::Interest &interest)
akmhoquea8cd6b92014-01-31 20:13:26 -060083{
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);
akmhoquecd552472014-02-01 21:22:16 -0600102 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 }
akmhoquea8cd6b92014-01-31 20:13:26 -0600111 }
112
113}
114
115void
akmhoque95b7c8c2014-01-31 15:53:09 -0600116interestManager::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
akmhoquea8cd6b92014-01-31 20:13:26 -0600124 pnlsr.getNlsrFace().expressInterest(i,
akmhoque95b7c8c2014-01-31 15:53:09 -0600125 ndn::func_lib::bind(&DataManager::processContent,
akmhoquea8cd6b92014-01-31 20:13:26 -0600126 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2),
akmhoque95b7c8c2014-01-31 15:53:09 -0600127 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
128 this,boost::ref(pnlsr),_1));
129}
130
131
132void
133interestManager::sendScheduledInfoInterest(nlsr& pnlsr, int seconds)
134{
akmhoquea8cd6b92014-01-31 20:13:26 -0600135 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
akmhoque95b7c8c2014-01-31 15:53:09 -0600136 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end();++it)
137 {
138 string adjName=(*it).getAdjacentName()+"/"+"info"+
akmhoquea8cd6b92014-01-31 20:13:26 -0600139 pnlsr.getConfParameter().getRouterPrefix();
140 expressInterest( pnlsr,adjName,2,pnlsr.getConfParameter().getInterestResendTime());
akmhoque95b7c8c2014-01-31 15:53:09 -0600141 }
142
akmhoquea8cd6b92014-01-31 20:13:26 -0600143 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
akmhoque95b7c8c2014-01-31 15:53:09 -0600144
145}
146
147void
148interestManager::scheduleInfoInterest(nlsr& pnlsr, int seconds)
149{
akmhoquea8cd6b92014-01-31 20:13:26 -0600150 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
akmhoque95b7c8c2014-01-31 15:53:09 -0600151 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
152 boost::ref(pnlsr),seconds));
153}
154