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