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