blob: a567aab0c7fc5babdba52b16c50d56f68556548c [file] [log] [blame]
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, The University of Memphis,
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"
26
27namespace nlsr {
28
29INIT_LOGGER(LsaSegmentStorage);
30
31LsaSegmentStorage::LsaSegmentStorage(ndn::Scheduler& scheduler,
32 const ndn::time::seconds lsaDeletionTimepoint)
33 : m_scheduler(scheduler)
34 , m_lsaDeletionTimepoint(lsaDeletionTimepoint)
35{
36}
37
38void
39LsaSegmentStorage::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
40{
41 fetcher.afterSegmentValidated.connect(std::bind(&LsaSegmentStorage::afterFetcherSignalEmitted,
42 this, _1));
43}
44
45const ndn::Data*
46LsaSegmentStorage::getLsaSegment(const ndn::Interest& interest)
47{
48 ndn::Name lsaSegmentsKey = interest.getName();
49
50 // If this the first interest then it does not contain the segment number,
51 // so need to append zero segment component at the end to match with the data
52 if (lsaSegmentsKey.size() > 0) {
53 if (!lsaSegmentsKey.get(-1).isSegment()) {
54 lsaSegmentsKey.appendSegment(0);
55 }
56
57 auto it = m_lsaSegments.find(lsaSegmentsKey);
58 if (it == m_lsaSegments.end()) {
59 NLSR_LOG_TRACE("Data for interest: " << interest.getName() << " cannot be found in the lsa storage");
60
61 return nullptr;
62 }
63 else {
64 NLSR_LOG_TRACE("Data for interest: " << interest.getName() << " is in the storage.");
65 return &(it->second);
66 }
67 }
68 else {
69 NLSR_LOG_ERROR("Received interest has empty name.");
70 return nullptr;
71 }
72}
73
74void
75LsaSegmentStorage::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
76{
77 NLSR_LOG_TRACE("Received a LSA segment: " << lsaSegment.getName());
78
79 // lsaSegmentName is /<router-prefix>/<LS type>/<sequence no.>/<version no.>/<segment no.>
80 auto lsaSegmentName = lsaSegment.getName();
81
82 if (lsaSegmentName.size() > 0) {
83 // lsaSegmentsKey is /<router-prefix>/<LS type>/<sequence no.>/<segment no.>
84 ndn::Name lsaSegmentsKey(lsaSegmentName.getPrefix(lsaSegmentName.size() - 2));
85 lsaSegmentsKey.append(lsaSegmentName.get(-1));
86
87 // No need to store same LSA multiple time
88 if (m_lsaSegments.find(lsaSegmentsKey) == m_lsaSegments.end()) {
89 NLSR_LOG_TRACE("Received LSA segment is new. Storing it in the storage.\n"
90 << " LSA data name: " << lsaSegmentName);
91
92 // Delete the same LSA with lower sequence number
93 deleteOldLsas(lsaSegmentName);
94
95 m_lsaSegments[lsaSegmentsKey] = lsaSegment;
96 }
97 else {
98 NLSR_LOG_TRACE("The received segment is already in the storage.");
99 }
100
101 // schedule the segment deletion
102 scheduleLsaSegmentDeletion(lsaSegmentsKey);
103 }
104 else {
105 NLSR_LOG_ERROR("The received LSA segment has empty name.");
106 }
107}
108
109void
110LsaSegmentStorage::deleteOldLsas(const ndn::Name& newLsaName)
111{
112 auto newLsaKey = newLsaName.getPrefix(newLsaName.size() - 3);
113 auto newSeqNo = newLsaName.get(-3).toNumber();
114
115 std::vector<decltype(m_lsaSegments)::key_type> lsaToDelete;
116
117 for (auto& segment : m_lsaSegments) {
118 ndn::Name segmentKey = segment.first;
119 auto oldSeqNo = segmentKey.get(-2).toNumber();
120 auto existingLsaKey = segmentKey.getPrefix(segmentKey.size() - 2);
121
122 if (newLsaKey == existingLsaKey) {
123 if (newSeqNo > oldSeqNo) { // in the key the second last component is the sequence number
124 NLSR_LOG_TRACE("Outdated LSA: " << segmentKey << " with seq no: " <<
125 oldSeqNo << " is deleted.");
126 lsaToDelete.push_back(segmentKey);
127 }
128 }
129 }
130
131 for (auto& segmentKey : lsaToDelete) {
132 m_lsaSegments.erase(segmentKey);
133 }
134}
135
136void
137LsaSegmentStorage::scheduleLsaSegmentDeletion(const ndn::Name& lsaSegmentsKey)
138{
139 m_scheduler.scheduleEvent(m_lsaDeletionTimepoint,
140 [&, this] {
141 m_lsaSegments.erase(lsaSegmentsKey);
142 });
143}
144
145void
146LsaSegmentStorage::insertSegment(const ndn::Name& segmentKey,
147 const ndn::Data& segmentValue)
148{
149 m_lsaSegments[segmentKey] = segmentValue;
150}
151
152void
153LsaSegmentStorage::deleteSegment(const ndn::Name& segmentKey)
154{
155 m_lsaSegments.erase(segmentKey);
156}
157
158} // namespace nlsr