akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * |
| 22 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include "nlsr.hpp" |
| 24 | #include "sync-logic-handler.hpp" |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 25 | #include "utility/name-helper.hpp" |
| 26 | #include "lsa.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 27 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
| 30 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | INIT_LOGGER("SyncLogicHandler"); |
| 32 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 33 | using namespace ndn; |
| 34 | using namespace std; |
| 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | void |
| 37 | SyncLogicHandler::createSyncSocket(Nlsr& pnlsr) |
| 38 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 39 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | std::cout << "Creating Sync socket ......" << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 41 | std::cout << "Sync prefix: " << m_syncPrefix << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 42 | m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, |
| 43 | m_syncFace, |
| 44 | ndn::bind(&SyncLogicHandler::nsyncUpdateCallBack, |
| 45 | this, _1, _2, ndn::ref(pnlsr)), |
| 46 | ndn::bind(&SyncLogicHandler::nsyncRemoveCallBack, |
| 47 | this, _1, ndn::ref(pnlsr))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v, |
| 52 | Sync::SyncSocket* socket, Nlsr& pnlsr) |
| 53 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 54 | _LOG_DEBUG("nsyncUpdateCallBack called ...."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | std::cout << "nsyncUpdateCallBack called ...." << std::endl; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 56 | int32_t n = v.size(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 57 | for (int32_t i = 0; i < n; i++){ |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 58 | _LOG_DEBUG("Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() << |
| 60 | endl; |
| 61 | processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr) |
| 67 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 68 | _LOG_DEBUG("nsyncRemoveCallBack called ...."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | std::cout << "nsyncRemoveCallBack called ...." << std::endl; |
| 70 | } |
| 71 | |
| 72 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 73 | SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
| 77 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 78 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | uint64_t seqNo, Nlsr& pnlsr) |
| 80 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | string chkString("LSA"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 82 | int32_t lasPosition = util::getNameComponentPosition(updateName, chkString); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 83 | if (lasPosition >= 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 84 | ndn::Name routerName = updateName.getSubName(lasPosition + 1); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | processRoutingUpdateFromSync(routerName, seqNo, pnlsr); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 86 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 91 | SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | uint64_t seqNo, Nlsr& pnlsr) |
| 93 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 94 | ndn::Name rName = routerName; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 95 | if (routerName != pnlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 96 | SequencingManager sm(seqNo); |
| 97 | std::cout << sm; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 98 | //std::cout << "Router Name: " << routerName << endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 99 | try { |
| 100 | if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 101 | std::cout << "Updated Name LSA. Need to fetch it" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 102 | _LOG_DEBUG("Updated Name 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("name"); |
| 106 | interestName.appendNumber(sm.getNameLsaSeq()); |
| 107 | pnlsr.getLsdb().expressInterest(interestName, |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 108 | pnlsr.getConfParameter().getInterestResendTime(), |
| 109 | 0); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 110 | } |
| 111 | if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 112 | sm.getAdjLsaSeq())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 113 | std::cout << "Updated Adj LSA. Need to fetch it" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 114 | _LOG_DEBUG("Updated Adj LSA. Need to fetch it"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 115 | ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 116 | interestName.append(routerName); |
| 117 | interestName.append("adjacency"); |
| 118 | interestName.appendNumber(sm.getAdjLsaSeq()); |
| 119 | pnlsr.getLsdb().expressInterest(interestName, |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 120 | pnlsr.getConfParameter().getInterestResendTime(), |
| 121 | 0); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 122 | } |
| 123 | if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 124 | sm.getCorLsaSeq())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 125 | std::cout << "Updated Cor LSA. Need to fetch it" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 126 | _LOG_DEBUG("Updated Cor LSA. Need to fetch it"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 127 | ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 128 | interestName.append(routerName); |
| 129 | interestName.append("coordinate"); |
| 130 | interestName.appendNumber(sm.getCorLsaSeq()); |
| 131 | pnlsr.getLsdb().expressInterest(interestName, |
akmhoque | 0698667 | 2014-05-27 13:55:53 -0500 | [diff] [blame] | 132 | pnlsr.getConfParameter().getInterestResendTime(), |
| 133 | 0); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 134 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 136 | catch (std::exception& e) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 137 | std::cerr << e.what() << std::endl; |
| 138 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | void |
| 144 | SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 145 | const ndn::Name& updatePrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 146 | { |
| 147 | sm.writeSeqNoToFile(); |
| 148 | publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo()); |
| 149 | } |
| 150 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 152 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, |
| 153 | uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | { |
| 155 | std::cout << "Publishing Sync Update ......" << std::endl; |
| 156 | std::cout << "Update in prefix: " << updatePrefix << std::endl; |
| 157 | std::cout << "Seq No: " << seqNo << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 158 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | ndn::Name updateName(updatePrefix); |
| 160 | string data("NoData"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 161 | m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), |
| 162 | 1000, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 163 | seqNo); |
| 164 | } |
| 165 | |
| 166 | }//namespace nlsr |