akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 1 | #include "nlsr.hpp" |
| 2 | #include "lsdb.hpp" |
| 3 | #include "hello-protocol.hpp" |
| 4 | #include "utility/name-helper.hpp" |
| 5 | |
| 6 | namespace nlsr { |
| 7 | |
| 8 | void |
| 9 | HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds) |
| 10 | { |
| 11 | std::cout << "Expressing Interest :" << interestName << std::endl; |
| 12 | ndn::Interest i(interestName); |
| 13 | i.setInterestLifetime(ndn::time::seconds(seconds)); |
| 14 | i.setMustBeFresh(true); |
| 15 | m_nlsr.getNlsrFace().expressInterest(i, |
| 16 | ndn::bind(&HelloProtocol::processContent, |
| 17 | this, |
| 18 | _1, _2), |
| 19 | ndn::bind(&HelloProtocol::processInterestTimedOut, |
| 20 | this, _1)); |
| 21 | } |
| 22 | |
| 23 | void |
| 24 | HelloProtocol::sendScheduledInterest(uint32_t seconds) |
| 25 | { |
| 26 | std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList(); |
| 27 | for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end(); |
| 28 | ++it) |
| 29 | { |
| 30 | ndn::Name interestName = (*it).getName() ; |
| 31 | interestName.append("info"); |
| 32 | interestName.append(ndn::Name(m_nlsr.getConfParameter().getRouterPrefix())); |
| 33 | expressInterest(interestName, |
| 34 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 35 | } |
| 36 | scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval()); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | HelloProtocol::scheduleInterest(uint32_t seconds) |
| 41 | { |
| 42 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds), |
| 43 | ndn::bind(&HelloProtocol::sendScheduledInterest, |
| 44 | this, seconds)); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | HelloProtocol::processInterest(const ndn::Name& name, |
| 49 | const ndn::Interest& interest) |
| 50 | { |
| 51 | const ndn::Name interestName = interest.getName(); |
| 52 | std::cout << "Interest Received for Name: " << interestName << std::endl; |
| 53 | std::string chkString("info"); |
| 54 | int32_t infoPosition = util::getNameComponentPosition(interestName, chkString); |
| 55 | if (infoPosition < 0) |
| 56 | { |
| 57 | return; |
| 58 | } |
| 59 | ndn::Name neighbor = interestName.getSubName(infoPosition + 1); |
| 60 | std::cout << "Neighbor: " << neighbor << std::endl; |
| 61 | if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) |
| 62 | { |
| 63 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
| 64 | data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec |
| 65 | data.setContent((const uint8_t*)"info", sizeof("info")); |
| 66 | m_keyChain.sign(data); |
| 67 | std::cout << ">> D: " << data << std::endl; |
| 68 | m_nlsr.getNlsrFace().put(data); |
| 69 | int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 70 | if (status == 0) |
| 71 | { |
| 72 | ndn::Name interestName(neighbor); |
| 73 | interestName.append("info"); |
| 74 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 75 | expressInterest(interestName, |
| 76 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | HelloProtocol::processInterestTimedOut(const ndn::Interest& interest) |
| 83 | { |
| 84 | const ndn::Name interestName(interest.getName()); |
| 85 | std::cout << "Interest timed out for Name: " << interestName << std::endl; |
| 86 | std::string chkString("info"); |
| 87 | int32_t infoPosition = util::getNameComponentPosition(interestName, chkString); |
| 88 | if (infoPosition < 0) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | ndn::Name neighbor = interestName.getSubName(0, infoPosition); |
| 93 | std::cout << "Neighbor: " << neighbor << std::endl; |
| 94 | m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor); |
| 95 | int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 96 | uint32_t infoIntTimedOutCount = |
| 97 | m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor); |
| 98 | std::cout << "Neighbor: " << neighbor << std::endl; |
| 99 | std::cout << "Status: " << status << std::endl; |
| 100 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
| 101 | if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) |
| 102 | { |
| 103 | ndn::Name interestName(neighbor); |
| 104 | interestName.append("info"); |
| 105 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 106 | expressInterest(interestName, |
| 107 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 108 | } |
| 109 | else if ((status == 1) && |
| 110 | (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) |
| 111 | { |
| 112 | m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0); |
| 113 | m_nlsr.incrementAdjBuildCount(); |
| 114 | if (m_nlsr.getIsBuildAdjLsaSheduled() == 0) |
| 115 | { |
| 116 | m_nlsr.setIsBuildAdjLsaSheduled(1); |
| 117 | // event here |
| 118 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 119 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, |
| 120 | &m_nlsr.getLsdb())); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void |
| 127 | HelloProtocol::processContent(const ndn::Interest& interest, |
| 128 | const ndn::Data& data) |
| 129 | { |
| 130 | ndn::Name dataName = data.getName(); |
| 131 | std::cout << "Data received for name: " << dataName << std::endl; |
| 132 | std::string chkString("info"); |
| 133 | int32_t infoPosition = util::getNameComponentPosition(dataName, chkString); |
| 134 | if (infoPosition >= 0) |
| 135 | { |
| 136 | ndn::Name neighbor = dataName.getSubName(0, infoPosition); |
| 137 | int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 138 | int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount( |
| 139 | neighbor); |
| 140 | //debugging purpose start |
| 141 | std::cout << "Before Updates: " << std::endl; |
| 142 | std::cout << "Neighbor : " << neighbor << std::endl; |
| 143 | std::cout << "Status: " << oldStatus << std::endl; |
| 144 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
| 145 | //debugging purpose end |
| 146 | m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1); |
| 147 | m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0); |
| 148 | int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 149 | infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount( |
| 150 | neighbor); |
| 151 | //debugging purpose |
| 152 | std::cout << "After Updates: " << std::endl; |
| 153 | std::cout << "Neighbor : " << neighbor << std::endl; |
| 154 | std::cout << "Status: " << newStatus << std::endl; |
| 155 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
| 156 | //debugging purpose end |
| 157 | if ((oldStatus - newStatus) != 0) // change in Adjacency list |
| 158 | { |
| 159 | m_nlsr.incrementAdjBuildCount(); |
| 160 | /* Need to schedule event for Adjacency LSA building */ |
| 161 | if (m_nlsr.getIsBuildAdjLsaSheduled() == 0) |
| 162 | { |
| 163 | m_nlsr.setIsBuildAdjLsaSheduled(1); |
| 164 | // event here |
| 165 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 166 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, |
| 167 | boost::ref(m_nlsr.getLsdb()))); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | } //namespace nlsr |