akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include "nlsr.hpp" |
| 2 | #include "sync-logic-handler.hpp" |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 3 | #include "utility/name-helper.hpp" |
| 4 | #include "lsa.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 5 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 6 | |
| 7 | namespace nlsr { |
| 8 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 9 | INIT_LOGGER("SyncLogicHandler"); |
| 10 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 11 | using namespace ndn; |
| 12 | using namespace std; |
| 13 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 14 | void |
| 15 | SyncLogicHandler::createSyncSocket(Nlsr& pnlsr) |
| 16 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 17 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 18 | std::cout << "Creating Sync socket ......" << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 19 | std::cout << "Sync prefix: " << m_syncPrefix << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 20 | m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, |
| 21 | m_syncFace, |
| 22 | ndn::bind(&SyncLogicHandler::nsyncUpdateCallBack, |
| 23 | this, _1, _2, ndn::ref(pnlsr)), |
| 24 | ndn::bind(&SyncLogicHandler::nsyncRemoveCallBack, |
| 25 | this, _1, ndn::ref(pnlsr))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void |
| 29 | SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v, |
| 30 | Sync::SyncSocket* socket, Nlsr& pnlsr) |
| 31 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 32 | _LOG_DEBUG("nsyncUpdateCallBack called ...."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | std::cout << "nsyncUpdateCallBack called ...." << std::endl; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 34 | int32_t n = v.size(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 35 | for (int32_t i = 0; i < n; i++){ |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 36 | _LOG_DEBUG("Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() << |
| 38 | endl; |
| 39 | processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr) |
| 45 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 46 | _LOG_DEBUG("nsyncRemoveCallBack called ...."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | std::cout << "nsyncRemoveCallBack called ...." << std::endl; |
| 48 | } |
| 49 | |
| 50 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 51 | SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 56 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | uint64_t seqNo, Nlsr& pnlsr) |
| 58 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | string chkString("LSA"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 60 | int32_t lasPosition = util::getNameComponentPosition(updateName, chkString); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 61 | if (lasPosition >= 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 62 | ndn::Name routerName = updateName.getSubName(lasPosition + 1); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | processRoutingUpdateFromSync(routerName, seqNo, pnlsr); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 64 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 69 | SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | uint64_t seqNo, Nlsr& pnlsr) |
| 71 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 72 | ndn::Name rName = routerName; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 73 | if (routerName != pnlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | SequencingManager sm(seqNo); |
| 75 | std::cout << sm; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 76 | //std::cout << "Router Name: " << routerName << endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 77 | try { |
| 78 | if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 79 | std::cout << "Updated Name LSA. Need to fetch it" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 80 | _LOG_DEBUG("Updated Name LSA. Need to fetch it"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 82 | interestName.append(routerName); |
| 83 | interestName.append("name"); |
| 84 | interestName.appendNumber(sm.getNameLsaSeq()); |
| 85 | pnlsr.getLsdb().expressInterest(interestName, |
| 86 | pnlsr.getConfParameter().getInterestResendTime()); |
| 87 | } |
| 88 | if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 89 | sm.getAdjLsaSeq())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 90 | std::cout << "Updated Adj LSA. Need to fetch it" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 91 | _LOG_DEBUG("Updated Adj LSA. Need to fetch it"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 92 | ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 93 | interestName.append(routerName); |
| 94 | interestName.append("adjacency"); |
| 95 | interestName.appendNumber(sm.getAdjLsaSeq()); |
| 96 | pnlsr.getLsdb().expressInterest(interestName, |
| 97 | pnlsr.getConfParameter().getInterestResendTime()); |
| 98 | } |
| 99 | if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | sm.getCorLsaSeq())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 101 | std::cout << "Updated Cor LSA. Need to fetch it" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 102 | _LOG_DEBUG("Updated Cor LSA. Need to fetch it"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 103 | ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 104 | interestName.append(routerName); |
| 105 | interestName.append("coordinate"); |
| 106 | interestName.appendNumber(sm.getCorLsaSeq()); |
| 107 | pnlsr.getLsdb().expressInterest(interestName, |
| 108 | pnlsr.getConfParameter().getInterestResendTime()); |
| 109 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 110 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 111 | catch (std::exception& e) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 112 | std::cerr << e.what() << std::endl; |
| 113 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | void |
| 119 | SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 120 | const ndn::Name& updatePrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | { |
| 122 | sm.writeSeqNoToFile(); |
| 123 | publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo()); |
| 124 | } |
| 125 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 126 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 127 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, |
| 128 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | { |
| 130 | std::cout << "Publishing Sync Update ......" << std::endl; |
| 131 | std::cout << "Update in prefix: " << updatePrefix << std::endl; |
| 132 | std::cout << "Seq No: " << seqNo << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 133 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | ndn::Name updateName(updatePrefix); |
| 135 | string data("NoData"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 136 | m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), |
| 137 | 1000, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | seqNo); |
| 139 | } |
| 140 | |
| 141 | }//namespace nlsr |