akmhoque | 95b7c8c | 2014-01-31 15:53: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 | |
| 11 | using namespace std; |
| 12 | using namespace ndn; |
| 13 | |
| 14 | void |
| 15 | interestManager::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; |
| 21 | Data data(ndn::Name(interest->getName()).append("testApp").appendVersion()); |
| 22 | data.setFreshnessPeriod(1000); // 10 sec |
| 23 | data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY")); |
| 24 | pnlsr.kChain.sign(data); |
| 25 | cout << ">> D: " << data << endl; |
| 26 | pnlsr.nlsrFace.put(data); |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | interestManager::processInterestTimedOut(nlsr& pnlsr, |
| 31 | const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest) |
| 32 | { |
| 33 | cout << "Timed out interest : " << interest->getName().toUri() << endl; |
| 34 | string intName= interest->getName().toUri(); |
| 35 | cout << intName <<endl; |
| 36 | nlsrTokenizer nt(intName,"/"); |
| 37 | string chkString("info"); |
| 38 | if( nt.doesTokenExist(chkString) ){ |
| 39 | string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1); |
| 40 | cout<<"Neighbor :"<<nbr<<endl; |
| 41 | } |
| 42 | |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | interestManager::expressInterest(nlsr& pnlsr,const string& interestNamePrefix, |
| 47 | int scope, int seconds) |
| 48 | { |
| 49 | Interest i((ndn::Name(interestNamePrefix))); |
| 50 | //i.setScope(scope); |
| 51 | i.setInterestLifetime(seconds*1000); |
| 52 | i.setMustBeFresh(true); |
| 53 | |
| 54 | pnlsr.nlsrFace.expressInterest(i, |
| 55 | ndn::func_lib::bind(&DataManager::processContent, |
| 56 | &pnlsr.dm, boost::ref(pnlsr),_1, _2), |
| 57 | ndn::func_lib::bind(&interestManager::processInterestTimedOut, |
| 58 | this,boost::ref(pnlsr),_1)); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | void |
| 63 | interestManager::sendScheduledInfoInterest(nlsr& pnlsr, int seconds) |
| 64 | { |
| 65 | std::list<Adjacent> adjList=pnlsr.adl.getAdjList(); |
| 66 | for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end();++it) |
| 67 | { |
| 68 | string adjName=(*it).getAdjacentName()+"/"+"info"+ |
| 69 | pnlsr.confParam.getRouterPrefix(); |
| 70 | expressInterest( pnlsr,adjName,2,pnlsr.confParam.getInterestResendTime()); |
| 71 | } |
| 72 | |
| 73 | scheduleInfoInterest(pnlsr, pnlsr.confParam.getInfoInterestInterval()); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | interestManager::scheduleInfoInterest(nlsr& pnlsr, int seconds) |
| 79 | { |
| 80 | pnlsr.scheduler.scheduleEvent(ndn::time::seconds(seconds), |
| 81 | ndn::bind(&interestManager::sendScheduledInfoInterest, this, |
| 82 | boost::ref(pnlsr),seconds)); |
| 83 | } |
| 84 | |