Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, The University of Memphis, |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "lsa-segment-storage.hpp" |
| 23 | #include "logger.hpp" |
| 24 | #include "lsa.hpp" |
| 25 | #include "utility/name-helper.hpp" |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 26 | #include "conf-parameter.hpp" |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 27 | |
| 28 | namespace nlsr { |
| 29 | |
| 30 | INIT_LOGGER(LsaSegmentStorage); |
| 31 | |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 32 | LsaSegmentStorage::LsaSegmentStorage(ndn::Scheduler& scheduler) |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 33 | : m_scheduler(scheduler) |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | LsaSegmentStorage::connectToFetcher(ndn::util::SegmentFetcher& fetcher) |
| 39 | { |
| 40 | fetcher.afterSegmentValidated.connect(std::bind(&LsaSegmentStorage::afterFetcherSignalEmitted, |
| 41 | this, _1)); |
| 42 | } |
| 43 | |
| 44 | const ndn::Data* |
| 45 | LsaSegmentStorage::getLsaSegment(const ndn::Interest& interest) |
| 46 | { |
| 47 | ndn::Name lsaSegmentsKey = interest.getName(); |
| 48 | |
Ashlesh Gawande | 304bebf | 2018-02-07 17:10:35 -0600 | [diff] [blame] | 49 | // If this is the first interest then it does not contain the segment number, |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 50 | // so need to append zero segment component at the end to match with the data |
| 51 | if (lsaSegmentsKey.size() > 0) { |
| 52 | if (!lsaSegmentsKey.get(-1).isSegment()) { |
| 53 | lsaSegmentsKey.appendSegment(0); |
| 54 | } |
| 55 | |
| 56 | auto it = m_lsaSegments.find(lsaSegmentsKey); |
| 57 | if (it == m_lsaSegments.end()) { |
| 58 | NLSR_LOG_TRACE("Data for interest: " << interest.getName() << " cannot be found in the lsa storage"); |
| 59 | |
| 60 | return nullptr; |
| 61 | } |
| 62 | else { |
| 63 | NLSR_LOG_TRACE("Data for interest: " << interest.getName() << " is in the storage."); |
| 64 | return &(it->second); |
| 65 | } |
| 66 | } |
| 67 | else { |
| 68 | NLSR_LOG_ERROR("Received interest has empty name."); |
| 69 | return nullptr; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | LsaSegmentStorage::afterFetcherSignalEmitted(const ndn::Data& lsaSegment) |
| 75 | { |
| 76 | NLSR_LOG_TRACE("Received a LSA segment: " << lsaSegment.getName()); |
| 77 | |
| 78 | // lsaSegmentName is /<router-prefix>/<LS type>/<sequence no.>/<version no.>/<segment no.> |
| 79 | auto lsaSegmentName = lsaSegment.getName(); |
| 80 | |
| 81 | if (lsaSegmentName.size() > 0) { |
| 82 | // lsaSegmentsKey is /<router-prefix>/<LS type>/<sequence no.>/<segment no.> |
| 83 | ndn::Name lsaSegmentsKey(lsaSegmentName.getPrefix(lsaSegmentName.size() - 2)); |
| 84 | lsaSegmentsKey.append(lsaSegmentName.get(-1)); |
| 85 | |
| 86 | // No need to store same LSA multiple time |
| 87 | if (m_lsaSegments.find(lsaSegmentsKey) == m_lsaSegments.end()) { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 88 | NLSR_LOG_TRACE("Received LSA segment is new. Storing it in the storage."); |
| 89 | NLSR_LOG_TRACE("LSA data segment's name: " << lsaSegmentName); |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 90 | |
| 91 | // Delete the same LSA with lower sequence number |
| 92 | deleteOldLsas(lsaSegmentName); |
| 93 | |
| 94 | m_lsaSegments[lsaSegmentsKey] = lsaSegment; |
| 95 | } |
| 96 | else { |
| 97 | NLSR_LOG_TRACE("The received segment is already in the storage."); |
| 98 | } |
| 99 | |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 100 | ndn::time::seconds expirationTime(LSA_REFRESH_TIME_DEFAULT); |
| 101 | |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 102 | // schedule the segment deletion |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 103 | scheduleLsaSegmentDeletion(lsaSegmentsKey, expirationTime); |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 104 | } |
| 105 | else { |
| 106 | NLSR_LOG_ERROR("The received LSA segment has empty name."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | LsaSegmentStorage::deleteOldLsas(const ndn::Name& newLsaName) |
| 112 | { |
| 113 | auto newLsaKey = newLsaName.getPrefix(newLsaName.size() - 3); |
| 114 | auto newSeqNo = newLsaName.get(-3).toNumber(); |
| 115 | |
| 116 | std::vector<decltype(m_lsaSegments)::key_type> lsaToDelete; |
| 117 | |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 118 | for (const auto& segment : m_lsaSegments) { |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 119 | ndn::Name segmentKey = segment.first; |
| 120 | auto oldSeqNo = segmentKey.get(-2).toNumber(); |
| 121 | auto existingLsaKey = segmentKey.getPrefix(segmentKey.size() - 2); |
| 122 | |
| 123 | if (newLsaKey == existingLsaKey) { |
| 124 | if (newSeqNo > oldSeqNo) { // in the key the second last component is the sequence number |
| 125 | NLSR_LOG_TRACE("Outdated LSA: " << segmentKey << " with seq no: " << |
| 126 | oldSeqNo << " is deleted."); |
| 127 | lsaToDelete.push_back(segmentKey); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 132 | for (const auto& segmentKey : lsaToDelete) { |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 133 | m_lsaSegments.erase(segmentKey); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 138 | LsaSegmentStorage::scheduleLsaSegmentDeletion(const ndn::Name& lsaSegmentsKey, |
| 139 | ndn::time::seconds expirationTime) |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 140 | { |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 141 | NLSR_LOG_TRACE("Scheduling LSA segment deletion for " |
| 142 | << lsaSegmentsKey << " in: " << expirationTime); |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 143 | m_scheduler.schedule(expirationTime, |
| 144 | [lsaSegmentsKey, this] { m_lsaSegments.erase(lsaSegmentsKey); }); |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ashlesh Gawande | 304bebf | 2018-02-07 17:10:35 -0600 | [diff] [blame] | 147 | } // namespace nlsr |