akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #include<iostream> |
| 2 | #include<cstdlib> |
| 3 | |
| 4 | |
| 5 | #include <ndn-cpp-dev/security/identity-certificate.hpp> |
| 6 | #include <ndn-cpp-dev/util/io.hpp> |
| 7 | |
| 8 | #include "nlsr.hpp" |
| 9 | #include "nlsr_im.hpp" |
| 10 | #include "nlsr_dm.hpp" |
| 11 | #include "utility/nlsr_tokenizer.hpp" |
| 12 | #include "nlsr_lsdb.hpp" |
| 13 | |
| 14 | namespace nlsr |
| 15 | { |
| 16 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 17 | using namespace std; |
| 18 | using namespace ndn; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 19 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 20 | void |
| 21 | interestManager::processInterest( Nlsr& pnlsr, |
| 22 | const ndn::Name &name, |
| 23 | const ndn::Interest &interest) |
| 24 | { |
| 25 | cout << "<< I: " << interest << endl; |
| 26 | string intName=interest.getName().toUri(); |
| 27 | cout << "Interest Received for Name: "<< intName <<endl; |
| 28 | nlsrTokenizer nt(intName,"/"); |
| 29 | string chkString("info"); |
| 30 | if( nt.doesTokenExist(chkString) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 31 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 32 | string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1); |
| 33 | cout <<"Neighbor: " << nbr <<endl; |
| 34 | processInterestInfo(pnlsr,nbr,interest); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 35 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 36 | chkString="LSA"; |
| 37 | if( nt.doesTokenExist(chkString) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 38 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 39 | processInterestLsa(pnlsr,interest); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 40 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 41 | chkString="keys"; |
| 42 | if( nt.doesTokenExist(chkString) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 43 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 44 | processInterestKeys(pnlsr,interest); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 45 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 46 | //Data data(ndn::Name(interest->getName()).append("testApp").appendVersion()); |
| 47 | //data.setFreshnessPeriod(1000); // 10 sec |
| 48 | //data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY")); |
| 49 | //pnlsr.getKeyChain().sign(data); |
| 50 | //cout << ">> D: " << data << endl; |
| 51 | //pnlsr.getNlsrFace().put(data); |
| 52 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 53 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 54 | void |
| 55 | interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor, |
| 56 | const ndn::Interest &interest) |
| 57 | { |
| 58 | if ( pnlsr.getAdl().isNeighbor(neighbor) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 59 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 60 | Data data(ndn::Name(interest.getName()).appendVersion()); |
| 61 | data.setFreshnessPeriod(1000); // 10 sec |
| 62 | data.setContent((const uint8_t*)"info", sizeof("info")); |
| 63 | pnlsr.getKeyManager().signData(data); |
| 64 | cout << ">> D: " << data << endl; |
| 65 | pnlsr.getNlsrFace()->put(data); |
| 66 | int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor); |
| 67 | if ( status == 0 ) |
| 68 | { |
| 69 | string intName=neighbor +"/"+"info"+ |
| 70 | pnlsr.getConfParameter().getRouterPrefix(); |
| 71 | expressInterest( pnlsr,intName,2, |
| 72 | pnlsr.getConfParameter().getInterestResendTime()); |
| 73 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 74 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 75 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 76 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 77 | void |
| 78 | interestManager::processInterestLsa(Nlsr& pnlsr,const ndn::Interest &interest) |
| 79 | { |
| 80 | string intName=interest.getName().toUri(); |
| 81 | nlsrTokenizer nt(intName,"/"); |
| 82 | string chkString("LSA"); |
| 83 | string origRouter=nt.getTokenString(nt.getTokenPosition(chkString)+1, |
| 84 | nt.getTokenNumber()-3); |
| 85 | string lsTypeString=nt.getToken(nt.getTokenNumber()-2); |
| 86 | string lsSeqString=nt.getToken(nt.getTokenNumber()-1); |
| 87 | cout<<"Router Name: "<<origRouter<<endl; |
| 88 | cout<<"Ls Type : "<<lsTypeString<<endl; |
| 89 | cout<<"Ls Seq : "<<lsSeqString<<endl; |
| 90 | uint8_t interestedLsType; |
| 91 | uint32_t interestedLsSeqNo; |
| 92 | try |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 93 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 94 | interestedLsType=boost::lexical_cast<uint8_t>(lsTypeString); |
| 95 | interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeqString); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 96 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 97 | catch(std::exception &e) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 98 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 99 | return; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 100 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 101 | cout <<"Ls Type: "<<interestedLsType<<endl; |
| 102 | if( lsTypeString == "1" ) //Name Lsa |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 103 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 104 | processInterestForNameLsa(pnlsr, interest, |
| 105 | origRouter+"/"+lsTypeString, interestedLsSeqNo); |
| 106 | } |
| 107 | else if( lsTypeString == "2" ) //Adj Lsa |
| 108 | { |
| 109 | processInterestForAdjLsa(pnlsr, interest, |
| 110 | origRouter+"/"+lsTypeString, interestedLsSeqNo); |
| 111 | } |
| 112 | else if( lsTypeString == "3" ) //Cor Lsa |
| 113 | { |
| 114 | processInterestForCorLsa(pnlsr, interest, |
| 115 | origRouter+"/"+lsTypeString, interestedLsSeqNo); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | cout<<"Unrecognized LSA Type :("<<endl; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | interestManager::processInterestForNameLsa(Nlsr& pnlsr, |
| 125 | const ndn::Interest &interest, |
| 126 | string lsaKey, uint32_t interestedlsSeqNo) |
| 127 | { |
| 128 | std::pair<NameLsa&, bool> nameLsa=pnlsr.getLsdb().getNameLsa(lsaKey); |
| 129 | if( nameLsa.second ) |
| 130 | { |
| 131 | if ( nameLsa.first.getLsSeqNo() >= interestedlsSeqNo ) |
| 132 | { |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 133 | Data data(ndn::Name(interest.getName()).appendVersion()); |
| 134 | data.setFreshnessPeriod(1000); // 10 sec |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 135 | string content=nameLsa.first.getNameLsaData(); |
| 136 | data.setContent((const uint8_t*)content.c_str(),content.size()); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 137 | pnlsr.getKeyManager().signData(data); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 138 | cout << ">> D: " << data << endl; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 139 | pnlsr.getNlsrFace()->put(data); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 140 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 141 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 142 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 143 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 144 | void |
| 145 | interestManager::processInterestForAdjLsa(Nlsr& pnlsr, |
| 146 | const ndn::Interest &interest, |
| 147 | string lsaKey, uint32_t interestedlsSeqNo) |
| 148 | { |
| 149 | std::pair<AdjLsa&, bool> adjLsa=pnlsr.getLsdb().getAdjLsa(lsaKey); |
| 150 | if( adjLsa.second ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 151 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 152 | if ( adjLsa.first.getLsSeqNo() >= interestedlsSeqNo ) |
| 153 | { |
| 154 | Data data(ndn::Name(interest.getName()).appendVersion()); |
| 155 | data.setFreshnessPeriod(1000); // 10 sec |
| 156 | string content=adjLsa.first.getAdjLsaData(); |
| 157 | data.setContent((const uint8_t*)content.c_str(),content.size()); |
| 158 | pnlsr.getKeyManager().signData(data); |
| 159 | cout << ">> D: " << data << endl; |
| 160 | pnlsr.getNlsrFace()->put(data); |
| 161 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 162 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 163 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 164 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 165 | void |
| 166 | interestManager::processInterestForCorLsa(Nlsr& pnlsr, |
| 167 | const ndn::Interest &interest, |
| 168 | string lsaKey, uint32_t interestedlsSeqNo) |
| 169 | { |
| 170 | std::pair<CorLsa&, bool> corLsa=pnlsr.getLsdb().getCorLsa(lsaKey); |
| 171 | if( corLsa.second ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 172 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 173 | if ( corLsa.first.getLsSeqNo() >= interestedlsSeqNo ) |
| 174 | { |
| 175 | Data data(ndn::Name(interest.getName()).appendVersion()); |
| 176 | data.setFreshnessPeriod(1000); // 10 sec |
| 177 | string content=corLsa.first.getCorLsaData(); |
| 178 | data.setContent((const uint8_t*)content.c_str(),content.size()); |
| 179 | pnlsr.getKeyManager().signData(data); |
| 180 | cout << ">> D: " << data << endl; |
| 181 | pnlsr.getNlsrFace()->put(data); |
| 182 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 183 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 184 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 185 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 186 | void |
| 187 | interestManager::processInterestKeys(Nlsr& pnlsr,const ndn::Interest &interest) |
| 188 | { |
| 189 | cout<<"processInterestKeys called "<<endl; |
| 190 | string intName=interest.getName().toUri(); |
| 191 | cout<<"Interest Name for Key: "<<intName<<std::endl; |
| 192 | nlsrTokenizer nt(intName,"/"); |
| 193 | std::string certName=nt.getTokenString(0,nt.getTokenNumber()-2); |
| 194 | uint32_t seqNum=boost::lexical_cast<uint32_t>(nt.getToken( |
| 195 | nt.getTokenNumber()-1)); |
| 196 | cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl; |
| 197 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert= |
| 198 | pnlsr.getKeyManager().getCertificateFromStore(certName,seqNum); |
| 199 | if( chkCert.second ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 200 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 201 | Data data(ndn::Name(interest.getName()).appendVersion()); |
| 202 | data.setFreshnessPeriod(1000); //10 sec |
| 203 | data.setContent(chkCert.first->wireEncode()); |
| 204 | pnlsr.getKeyManager().signData(data); |
| 205 | pnlsr.getNlsrFace()->put(data); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 206 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 207 | //std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert= |
| 208 | /* |
| 209 | ndn::shared_ptr<ndn::IdentityCertificate> cert=pnlsr.getKeyManager().getCertificate(); |
| 210 | Data data(ndn::Name(interest.getName()).appendVersion()); |
| 211 | data.setFreshnessPeriod(1000); // 10 sec |
| 212 | data.setContent(cert->wireEncode()); |
| 213 | pnlsr.getKeyManager().signData(data); |
| 214 | //std::ofstream outFile("data_sent"); |
| 215 | //ndn::io::save(data,outFile,ndn::io::NO_ENCODING); |
| 216 | pnlsr.getNlsrFace()->put(data); |
| 217 | */ |
| 218 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 219 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 220 | |
| 221 | void |
| 222 | interestManager::processInterestTimedOut(Nlsr& pnlsr, |
| 223 | const ndn::Interest &interest) |
| 224 | { |
| 225 | cout << "Timed out interest : " << interest.getName().toUri() << endl; |
| 226 | string intName= interest.getName().toUri(); |
| 227 | nlsrTokenizer nt(intName,"/"); |
| 228 | string chkString("info"); |
| 229 | if( nt.doesTokenExist(chkString) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 230 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 231 | string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1); |
| 232 | processInterestTimedOutInfo( pnlsr , nbr , interest); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 233 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 234 | chkString="LSA"; |
| 235 | if( nt.doesTokenExist(chkString) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 236 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 237 | processInterestTimedOutLsa(pnlsr, interest); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 238 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 239 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 240 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 241 | void |
| 242 | interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor, |
| 243 | const ndn::Interest &interest) |
| 244 | { |
| 245 | pnlsr.getAdl().incrementTimedOutInterestCount(neighbor); |
| 246 | int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor); |
| 247 | int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor); |
| 248 | cout<<"Neighbor: "<< neighbor << endl; |
| 249 | cout<<"Status: "<< status << endl; |
| 250 | cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl; |
| 251 | if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber())) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 252 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 253 | string intName=neighbor +"/"+"info"+ |
| 254 | pnlsr.getConfParameter().getRouterPrefix(); |
| 255 | expressInterest( pnlsr,intName,2, |
| 256 | pnlsr.getConfParameter().getInterestResendTime()); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 257 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 258 | else if ( (status == 1) && |
| 259 | (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber())) |
| 260 | { |
| 261 | pnlsr.getAdl().setStatusOfNeighbor(neighbor,0); |
| 262 | pnlsr.incrementAdjBuildCount(); |
| 263 | if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 ) |
| 264 | { |
| 265 | pnlsr.setIsBuildAdjLsaSheduled(1); |
| 266 | // event here |
| 267 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 268 | ndn::bind(&Lsdb::scheduledAdjLsaBuild,&pnlsr.getLsdb(), |
| 269 | boost::ref(pnlsr))); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void |
| 275 | interestManager::processInterestTimedOutLsa(Nlsr& pnlsr, |
| 276 | const ndn::Interest &interest) |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix, |
| 282 | int scope, int seconds) |
| 283 | { |
| 284 | cout<<"Expressing Interest :"<<interestNamePrefix<<endl; |
| 285 | ndn::Interest i((ndn::Name(interestNamePrefix))); |
| 286 | //i.setScope(scope); |
| 287 | i.setInterestLifetime(seconds*1000); |
| 288 | i.setMustBeFresh(true); |
| 289 | pnlsr.getNlsrFace()->expressInterest(i, |
| 290 | ndn::func_lib::bind(&DataManager::processContent, |
| 291 | &pnlsr.getDm(), boost::ref(pnlsr),_1, _2,boost::ref(*this)), |
| 292 | ndn::func_lib::bind(&interestManager::processInterestTimedOut, |
| 293 | this,boost::ref(pnlsr),_1)); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | void |
| 298 | interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds) |
| 299 | { |
| 300 | std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList(); |
| 301 | for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it) |
| 302 | { |
| 303 | string adjName=(*it).getAdjacentName()+"/"+"info"+ |
| 304 | pnlsr.getConfParameter().getRouterPrefix(); |
| 305 | expressInterest( pnlsr,adjName,2, |
| 306 | pnlsr.getConfParameter().getInterestResendTime()); |
| 307 | } |
| 308 | scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval()); |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds) |
| 313 | { |
| 314 | EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds), |
| 315 | ndn::bind(&interestManager::sendScheduledInfoInterest, this, |
| 316 | boost::ref(pnlsr),seconds)); |
| 317 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 318 | |
| 319 | |
| 320 | } //namespace nlsr |