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