akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame^] | 1 | #include "nlsr.hpp" |
| 2 | #include "sync-logic-handler.hpp" |
| 3 | #include "security/key-manager.hpp" |
| 4 | #include "utility/tokenizer.hpp" |
| 5 | |
| 6 | |
| 7 | namespace nlsr { |
| 8 | |
| 9 | void |
| 10 | SyncLogicHandler::createSyncSocket(Nlsr& pnlsr) |
| 11 | { |
| 12 | std::cout << "Creating Sync socket ......" << std::endl; |
| 13 | std::cout << "Sync prefix: " << m_syncPrefix.toUri() << std::endl; |
| 14 | m_syncSocket = make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, |
| 15 | m_syncFace, |
| 16 | bind(&SyncLogicHandler::nsyncUpdateCallBack, this, |
| 17 | _1, _2, boost::ref(pnlsr)), |
| 18 | bind(&SyncLogicHandler::nsyncRemoveCallBack, this, |
| 19 | _1, boost::ref(pnlsr))); |
| 20 | } |
| 21 | |
| 22 | void |
| 23 | SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v, |
| 24 | Sync::SyncSocket* socket, Nlsr& pnlsr) |
| 25 | { |
| 26 | std::cout << "nsyncUpdateCallBack called ...." << std::endl; |
| 27 | int n = v.size(); |
| 28 | for (int i = 0; i < n; i++) |
| 29 | { |
| 30 | std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() << |
| 31 | endl; |
| 32 | processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr) |
| 38 | { |
| 39 | std::cout << "nsyncRemoveCallBack called ...." << std::endl; |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | SyncLogicHandler::removeRouterFromSyncing(string& routerPrefix) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | SyncLogicHandler::processUpdateFromSync(std::string updateName, |
| 49 | uint64_t seqNo, Nlsr& pnlsr) |
| 50 | { |
| 51 | Tokenizer nt(updateName, "/"); |
| 52 | string chkString("LSA"); |
| 53 | if (nt.doesTokenExist(chkString)) |
| 54 | { |
| 55 | //process LSA Update here |
| 56 | string routerName = nt.getTokenString(nt.getTokenPosition(chkString) + 1); |
| 57 | processRoutingUpdateFromSync(routerName, seqNo, pnlsr); |
| 58 | } |
| 59 | chkString = "keys"; |
| 60 | if (nt.doesTokenExist(chkString)) |
| 61 | { |
| 62 | //process keys update here |
| 63 | std::string certName = nt.getTokenString(0); |
| 64 | processKeysUpdateFromSync(certName, seqNo, pnlsr); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | SyncLogicHandler::processRoutingUpdateFromSync(std::string routerName, |
| 70 | uint64_t seqNo, Nlsr& pnlsr) |
| 71 | { |
| 72 | if (routerName != pnlsr.getConfParameter().getRouterPrefix()) |
| 73 | { |
| 74 | SequencingManager sm(seqNo); |
| 75 | std::cout << sm; |
| 76 | std::cout << "Router Name: " << routerName << endl; |
| 77 | if (pnlsr.getLsdb().isNameLsaNew(routerName + "/1", sm.getNameLsaSeq())) |
| 78 | { |
| 79 | std::cout << "Updated Name LSA. Need to fetch it" << std::endl; |
| 80 | string lsaPrefix = |
| 81 | pnlsr.getConfParameter().getChronosyncLsaPrefix() + |
| 82 | routerName + "/1/" + |
| 83 | boost::lexical_cast<std::string>(sm.getNameLsaSeq()); |
| 84 | pnlsr.getIm().expressInterest(pnlsr, lsaPrefix, 3, |
| 85 | pnlsr.getConfParameter().getInterestResendTime()); |
| 86 | } |
| 87 | if (pnlsr.getLsdb().isAdjLsaNew(routerName + "/2", sm.getAdjLsaSeq())) |
| 88 | { |
| 89 | std::cout << "Updated Adj LSA. Need to fetch it" << std::endl; |
| 90 | string lsaPrefix = |
| 91 | pnlsr.getConfParameter().getChronosyncLsaPrefix() + |
| 92 | routerName + "/2/" + |
| 93 | boost::lexical_cast<std::string>(sm.getAdjLsaSeq()); |
| 94 | pnlsr.getIm().expressInterest(pnlsr, lsaPrefix, 3, |
| 95 | pnlsr.getConfParameter().getInterestResendTime()); |
| 96 | } |
| 97 | if (pnlsr.getLsdb().isCorLsaNew(routerName + "/3", sm.getCorLsaSeq())) |
| 98 | { |
| 99 | std::cout << "Updated Cor LSA. Need to fetch it" << std::endl; |
| 100 | string lsaPrefix = |
| 101 | pnlsr.getConfParameter().getChronosyncLsaPrefix() + |
| 102 | routerName + "/3/" + |
| 103 | boost::lexical_cast<std::string>(sm.getCorLsaSeq()); |
| 104 | pnlsr.getIm().expressInterest(pnlsr, lsaPrefix, 3, |
| 105 | pnlsr.getConfParameter().getInterestResendTime()); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | SyncLogicHandler::processKeysUpdateFromSync(std::string certName, |
| 112 | uint64_t seqNo, Nlsr& pnlsr) |
| 113 | { |
| 114 | std::cout << "Cert Name: " << certName << std::endl; |
| 115 | if (pnlsr.getKeyManager().isNewCertificate(certName, seqNo)) |
| 116 | { |
| 117 | string certNamePrefix = certName + "/" + |
| 118 | boost::lexical_cast<string>(seqNo); |
| 119 | pnlsr.getIm().expressInterest(pnlsr, certNamePrefix, 3, |
| 120 | pnlsr.getConfParameter().getInterestResendTime()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm, |
| 126 | string updatePrefix) |
| 127 | { |
| 128 | sm.writeSeqNoToFile(); |
| 129 | publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo()); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | SyncLogicHandler::publishKeyUpdate(KeyManager& km) |
| 134 | { |
| 135 | publishSyncUpdate(km.getProcessCertName().toUri(), km.getCertSeqNo()); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | SyncLogicHandler::publishIdentityUpdate(string identityName) |
| 140 | { |
| 141 | publishSyncUpdate(identityName, 0); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | SyncLogicHandler::publishSyncUpdate(string updatePrefix, uint64_t seqNo) |
| 146 | { |
| 147 | std::cout << "Publishing Sync Update ......" << std::endl; |
| 148 | std::cout << "Update in prefix: " << updatePrefix << std::endl; |
| 149 | std::cout << "Seq No: " << seqNo << std::endl; |
| 150 | ndn::Name updateName(updatePrefix); |
| 151 | string data("NoData"); |
| 152 | m_syncSocket->publishData(updateName, 0, data.c_str(), data.size(), 1000, |
| 153 | seqNo); |
| 154 | } |
| 155 | |
| 156 | }//namespace nlsr |