33 : m_scheduler(scheduler)
40 fetcher.afterSegmentValidated.connect(std::bind(&LsaSegmentStorage::afterFetcherSignalEmitted,
47 ndn::Name lsaSegmentsKey = interest.getName();
51 if (lsaSegmentsKey.size() > 0) {
52 if (!lsaSegmentsKey.get(-1).isSegment()) {
53 lsaSegmentsKey.appendSegment(0);
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");
63 NLSR_LOG_TRACE(
"Data for interest: " << interest.getName() <<
" is in the storage.");
74 LsaSegmentStorage::afterFetcherSignalEmitted(
const ndn::Data& lsaSegment)
76 NLSR_LOG_TRACE(
"Received a LSA segment: " << lsaSegment.getName());
79 auto lsaSegmentName = lsaSegment.getName();
81 if (lsaSegmentName.size() > 0) {
83 ndn::Name lsaSegmentsKey(lsaSegmentName.getPrefix(lsaSegmentName.size() - 2));
84 lsaSegmentsKey.append(lsaSegmentName.get(-1));
87 if (m_lsaSegments.find(lsaSegmentsKey) == m_lsaSegments.end()) {
88 NLSR_LOG_TRACE(
"Received LSA segment is new. Storing it in the storage.\n" 89 <<
" LSA data name: " << lsaSegmentName);
92 deleteOldLsas(lsaSegmentName);
94 m_lsaSegments[lsaSegmentsKey] = lsaSegment;
100 std::string content(reinterpret_cast<const char*>(lsaSegment.getContent().value()),
101 lsaSegment.getContent().value_size());
105 std::vector<std::string> options;
106 boost::split(options, content, boost::is_any_of(
"|"));
109 expirationTime = ndn::time::duration_cast<ndn::time::seconds>
110 (ndn::time::fromIsoString(options.at(3)) - ndn::time::system_clock::now());
111 }
catch (
const std::exception& e) {
112 NLSR_LOG_ERROR(
"Cannot extract expiration time from LSA content: " << e.what());
116 scheduleLsaSegmentDeletion(lsaSegmentsKey, expirationTime);
124 LsaSegmentStorage::deleteOldLsas(
const ndn::Name& newLsaName)
126 auto newLsaKey = newLsaName.getPrefix(newLsaName.size() - 3);
127 auto newSeqNo = newLsaName.get(-3).toNumber();
129 std::vector<decltype(m_lsaSegments)::key_type> lsaToDelete;
131 for (
const auto& segment : m_lsaSegments) {
132 ndn::Name segmentKey = segment.first;
133 auto oldSeqNo = segmentKey.get(-2).toNumber();
134 auto existingLsaKey = segmentKey.getPrefix(segmentKey.size() - 2);
136 if (newLsaKey == existingLsaKey) {
137 if (newSeqNo > oldSeqNo) {
138 NLSR_LOG_TRACE(
"Outdated LSA: " << segmentKey <<
" with seq no: " <<
139 oldSeqNo <<
" is deleted.");
140 lsaToDelete.push_back(segmentKey);
145 for (
const auto& segmentKey : lsaToDelete) {
146 m_lsaSegments.erase(segmentKey);
151 LsaSegmentStorage::scheduleLsaSegmentDeletion(
const ndn::Name& lsaSegmentsKey,
152 ndn::time::seconds expirationTime)
155 << lsaSegmentsKey <<
" in: " << expirationTime);
156 m_scheduler.scheduleEvent(expirationTime,
157 [lsaSegmentsKey,
this] {
158 m_lsaSegments.erase(lsaSegmentsKey);
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
void connectToFetcher(ndn::util::SegmentFetcher &fetcher)
Get connected to the signal emitted by SegmentFetcher.
const ndn::Data * getLsaSegment(const ndn::Interest &interest)
Returns an LSA segment for an interest from LsaSegmentStorage.
#define NLSR_LOG_ERROR(x)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
LsaSegmentStorage(ndn::Scheduler &scheduler)
#define NLSR_LOG_TRACE(x)