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