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