blob: e7d6f2d2c093c8055c64148c825a554d81681d7a [file] [log] [blame]
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
3 * Copyright (c) 2014-2019, The University of Memphis,
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00004 * 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 Gawande8c6d8c82018-02-28 21:41:31 -060026#include "conf-parameter.hpp"
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000027
28namespace nlsr {
29
30INIT_LOGGER(LsaSegmentStorage);
31
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060032LsaSegmentStorage::LsaSegmentStorage(ndn::Scheduler& scheduler)
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000033 : m_scheduler(scheduler)
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000034{
35}
36
37void
38LsaSegmentStorage::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
39{
40 fetcher.afterSegmentValidated.connect(std::bind(&LsaSegmentStorage::afterFetcherSignalEmitted,
41 this, _1));
42}
43
44const ndn::Data*
45LsaSegmentStorage::getLsaSegment(const ndn::Interest& interest)
46{
47 ndn::Name lsaSegmentsKey = interest.getName();
48
Ashlesh Gawande304bebf2018-02-07 17:10:35 -060049 // If this is the first interest then it does not contain the segment number,
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000050 // 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
73void
74LsaSegmentStorage::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 Gawandee8d8bd52018-08-09 17:18:51 -050088 NLSR_LOG_TRACE("Received LSA segment is new. Storing it in the storage.");
89 NLSR_LOG_TRACE("LSA data segment's name: " << lsaSegmentName);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000090
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 Gawande8c6d8c82018-02-28 21:41:31 -0600100 ndn::time::seconds expirationTime(LSA_REFRESH_TIME_DEFAULT);
101
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000102 // schedule the segment deletion
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600103 scheduleLsaSegmentDeletion(lsaSegmentsKey, expirationTime);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000104 }
105 else {
106 NLSR_LOG_ERROR("The received LSA segment has empty name.");
107 }
108}
109
110void
111LsaSegmentStorage::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 Gawande8c6d8c82018-02-28 21:41:31 -0600118 for (const auto& segment : m_lsaSegments) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000119 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 Gawande8c6d8c82018-02-28 21:41:31 -0600132 for (const auto& segmentKey : lsaToDelete) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000133 m_lsaSegments.erase(segmentKey);
134 }
135}
136
137void
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600138LsaSegmentStorage::scheduleLsaSegmentDeletion(const ndn::Name& lsaSegmentsKey,
139 ndn::time::seconds expirationTime)
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000140{
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -0600141 NLSR_LOG_TRACE("Scheduling LSA segment deletion for "
142 << lsaSegmentsKey << " in: " << expirationTime);
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400143 m_scheduler.schedule(expirationTime,
144 [lsaSegmentsKey, this] { m_lsaSegments.erase(lsaSegmentsKey); });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000145}
146
Ashlesh Gawande304bebf2018-02-07 17:10:35 -0600147} // namespace nlsr