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