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 | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 17 | m_syncSocket = make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, |
| 18 | m_syncFace, |
| 19 | bind(&SyncLogicHandler::nsyncUpdateCallBack, this, |
| 20 | _1, _2, boost::ref(pnlsr)), |
| 21 | bind(&SyncLogicHandler::nsyncRemoveCallBack, this, |
| 22 | _1, boost::ref(pnlsr))); |
| 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(); |
| 31 | for (int32_t i = 0; i < n; i++) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | { |
| 33 | std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() << |
| 34 | endl; |
| 35 | processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr) |
| 41 | { |
| 42 | std::cout << "nsyncRemoveCallBack called ...." << std::endl; |
| 43 | } |
| 44 | |
| 45 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 46 | SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 51 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | uint64_t seqNo, Nlsr& pnlsr) |
| 53 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 54 | //const ndn::Name name(updateName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | string chkString("LSA"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 56 | int32_t lasPosition = util::getNameComponentPosition(updateName, chkString); |
| 57 | if (lasPosition >= 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 59 | ndn::Name routerName = updateName.getSubName(lasPosition + 1); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | processRoutingUpdateFromSync(routerName, seqNo, pnlsr); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 61 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 66 | SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | uint64_t seqNo, Nlsr& pnlsr) |
| 68 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 69 | ndn::Name rName = routerName; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | if (routerName != pnlsr.getConfParameter().getRouterPrefix()) |
| 71 | { |
| 72 | SequencingManager sm(seqNo); |
| 73 | std::cout << sm; |
| 74 | std::cout << "Router Name: " << routerName << endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 75 | try |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 77 | if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) |
| 78 | { |
| 79 | std::cout << "Updated Name LSA. Need to fetch it" << std::endl; |
| 80 | ndn::Name interestName(pnlsr.getConfParameter().getChronosyncLsaPrefix()); |
| 81 | interestName.append(routerName); |
| 82 | interestName.append("name"); |
| 83 | interestName.appendNumber(sm.getNameLsaSeq()); |
| 84 | pnlsr.getLsdb().expressInterest(interestName, |
| 85 | pnlsr.getConfParameter().getInterestResendTime()); |
| 86 | } |
| 87 | if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"), |
| 88 | sm.getAdjLsaSeq())) |
| 89 | { |
| 90 | std::cout << "Updated Adj LSA. Need to fetch it" << std::endl; |
| 91 | ndn::Name interestName(pnlsr.getConfParameter().getChronosyncLsaPrefix()); |
| 92 | interestName.append(routerName); |
| 93 | interestName.append("adjacency"); |
| 94 | interestName.appendNumber(sm.getAdjLsaSeq()); |
| 95 | pnlsr.getLsdb().expressInterest(interestName, |
| 96 | pnlsr.getConfParameter().getInterestResendTime()); |
| 97 | } |
| 98 | if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"), |
| 99 | sm.getCorLsaSeq())) |
| 100 | { |
| 101 | std::cout << "Updated Cor LSA. Need to fetch it" << std::endl; |
| 102 | ndn::Name interestName(pnlsr.getConfParameter().getChronosyncLsaPrefix()); |
| 103 | interestName.append(routerName); |
| 104 | interestName.append("coordinate"); |
| 105 | interestName.appendNumber(sm.getCorLsaSeq()); |
| 106 | pnlsr.getLsdb().expressInterest(interestName, |
| 107 | pnlsr.getConfParameter().getInterestResendTime()); |
| 108 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 109 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 110 | catch (std::exception& e) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | { |
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; |
| 133 | ndn::Name updateName(updatePrefix); |
| 134 | string data("NoData"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame^] | 135 | m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), |
| 136 | 1000, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | seqNo); |
| 138 | } |
| 139 | |
| 140 | }//namespace nlsr |