blob: b0d29f120a59f7f247fc70947ef6b3b0735578dd [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, The University of Memphis
4 *
5 * This file is part of PSync.
6 * See AUTHORS.md for complete list of PSync authors and contributors.
7 *
8 * PSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "partial-producer.hpp"
21#include "detail/state.hpp"
22
23#include <ndn-cxx/util/logger.hpp>
24
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050025#include <cstring>
26#include <limits>
27
28namespace psync {
29
30NDN_LOG_INIT(psync.PartialProducer);
31
32PartialProducer::PartialProducer(size_t expectedNumEntries,
33 ndn::Face& face,
34 const ndn::Name& syncPrefix,
35 const ndn::Name& userPrefix,
36 ndn::time::milliseconds syncReplyFreshness,
37 ndn::time::milliseconds helloReplyFreshness)
38 : ProducerBase(expectedNumEntries, face, syncPrefix,
39 userPrefix, syncReplyFreshness, helloReplyFreshness)
40{
41 m_registerPrefixId =
42 m_face.registerPrefix(m_syncPrefix,
43 [this] (const ndn::Name& syncPrefix) {
44 m_face.setInterestFilter(ndn::Name(m_syncPrefix).append("hello"),
45 std::bind(&PartialProducer::onHelloInterest, this, _1, _2));
46
47 m_face.setInterestFilter(ndn::Name(m_syncPrefix).append("sync"),
48 std::bind(&PartialProducer::onSyncInterest, this, _1, _2));
49 },
50 std::bind(&PartialProducer::onRegisterFailed, this, _1, _2));
51}
52
53PartialProducer::~PartialProducer()
54{
55 m_face.unregisterPrefix(m_registerPrefixId, nullptr, nullptr);
56}
57
58void
59PartialProducer::publishName(const ndn::Name& prefix, ndn::optional<uint64_t> seq)
60{
61 if (m_prefixes.find(prefix) == m_prefixes.end()) {
62 return;
63 }
64
65 uint64_t newSeq = seq.value_or(m_prefixes[prefix] + 1);
66
67 NDN_LOG_INFO("Publish: " << prefix << "/" << newSeq);
68
69 updateSeqNo(prefix, newSeq);
70
71 satisfyPendingSyncInterests(prefix);
72}
73
74void
75PartialProducer::onHelloInterest(const ndn::Name& prefix, const ndn::Interest& interest)
76{
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050077 // Last component or third last component (in case of interest with IBF and segment)
78 // needs to be hello
79 if (interest.getName().get(interest.getName().size()-1).toUri() != "hello" &&
80 interest.getName().get(interest.getName().size()-3).toUri() != "hello") {
81 return;
82 }
83
84 if (m_segmentPublisher.replyFromStore(interest.getName())) {
85 return;
86 }
87
88 NDN_LOG_DEBUG("Hello Interest Received, nonce: " << interest);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050089
90 State state;
91
Ashlesh Gawandea9296472018-08-04 08:21:39 -050092 for (const auto& prefix : m_prefixes) {
93 state.addContent(ndn::Name(prefix.first).appendNumber(prefix.second));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050094 }
95 NDN_LOG_DEBUG("sending content p: " << state);
96
97 ndn::Name helloDataName = prefix;
98 m_iblt.appendToName(helloDataName);
99
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500100 m_segmentPublisher.publish(interest.getName(), helloDataName,
101 state.wireEncode(), m_helloReplyFreshness);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500102}
103
104void
105PartialProducer::onSyncInterest(const ndn::Name& prefix, const ndn::Interest& interest)
106{
107 NDN_LOG_DEBUG("Sync Interest Received, nonce: " << interest.getNonce() <<
108 " hash: " << std::hash<std::string>{}(interest.getName().toUri()));
109
110 ndn::Name interestName = interest.getName();
111
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500112 if (interestName.get(interestName.size() - 5).toUri() != "sync" &&
113 interestName.get(interestName.size() - 7).toUri() != "sync") {
114 return;
115 }
116
117 if (m_segmentPublisher.replyFromStore(interest.getName())) {
118 return;
119 }
120
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500121 ndn::name::Component bfName, ibltName;
122 unsigned int projectedCount;
123 double falsePositiveProb;
124 try {
125 projectedCount = interestName.get(interestName.size()-4).toNumber();
126 falsePositiveProb = interestName.get(interestName.size()-3).toNumber()/1000.;
127 bfName = interestName.get(interestName.size()-2);
128
129 ibltName = interestName.get(interestName.size()-1);
130 }
131 catch (const std::exception& e) {
132 NDN_LOG_ERROR("Cannot extract bloom filter and IBF from sync interest: " << e.what());
133 NDN_LOG_ERROR("Format: /<syncPrefix>/sync/<BF-count>/<BF-false-positive-probability>/<BF>/<IBF>");
134 return;
135 }
136
137 BloomFilter bf;
138 IBLT iblt(m_expectedNumEntries);
139
140 try {
141 bf = BloomFilter(projectedCount, falsePositiveProb, bfName);
142 iblt.initialize(ibltName);
143 }
144 catch (const std::exception& e) {
145 NDN_LOG_WARN(e.what());
146 return;
147 }
148
149 // get the difference
150 IBLT diff = m_iblt - iblt;
151
152 // non-empty positive means we have some elements that the others don't
153 std::set<uint32_t> positive;
154 std::set<uint32_t> negative;
155
156 NDN_LOG_TRACE("Number elements in IBF: " << m_prefixes.size());
157
158 bool peel = diff.listEntries(positive, negative);
159
160 NDN_LOG_TRACE("Result of listEntries on the difference: " << peel);
161
162 if (!peel) {
163 NDN_LOG_DEBUG("Can't decode the difference, sending application Nack");
164 sendApplicationNack(interestName);
165 return;
166 }
167
168 // generate content for Sync reply
169 State state;
170 NDN_LOG_TRACE("Size of positive set " << positive.size());
171 NDN_LOG_TRACE("Size of negative set " << negative.size());
172 for (const auto& hash : positive) {
173 ndn::Name prefix = m_hash2prefix[hash];
174 if (bf.contains(prefix.toUri())) {
175 // generate data
176 state.addContent(ndn::Name(prefix).appendNumber(m_prefixes[prefix]));
177 NDN_LOG_DEBUG("Content: " << prefix << " " << std::to_string(m_prefixes[prefix]));
178 }
179 }
180
181 NDN_LOG_TRACE("m_threshold: " << m_threshold << " Total: " << positive.size() + negative.size());
182
183 if (positive.size() + negative.size() >= m_threshold || !state.getContent().empty()) {
184
185 // send back data
186 ndn::Name syncDataName = interestName;
187 m_iblt.appendToName(syncDataName);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500188
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500189 m_segmentPublisher.publish(interest.getName(), syncDataName,
190 state.wireEncode(), m_syncReplyFreshness);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500191 return;
192 }
193
194 ndn::util::scheduler::ScopedEventId scopedEventId(m_scheduler);
195 auto it = m_pendingEntries.emplace(interestName,
196 PendingEntryInfo{bf, iblt, std::move(scopedEventId)});
197
198 it.first->second.expirationEvent =
199 m_scheduler.scheduleEvent(interest.getInterestLifetime(),
200 [this, interest] {
201 NDN_LOG_TRACE("Erase Pending Interest " << interest.getNonce());
202 m_pendingEntries.erase(interest.getName());
203 });
204}
205
206void
207PartialProducer::satisfyPendingSyncInterests(const ndn::Name& prefix) {
208 NDN_LOG_TRACE("size of pending interest: " << m_pendingEntries.size());
209
210 for (auto it = m_pendingEntries.begin(); it != m_pendingEntries.end();) {
211 const PendingEntryInfo& entry = it->second;
212
213 IBLT diff = m_iblt - entry.iblt;
214 std::set<uint32_t> positive;
215 std::set<uint32_t> negative;
216
217 bool peel = diff.listEntries(positive, negative);
218
219 NDN_LOG_TRACE("Result of listEntries on the difference: " << peel);
220
221 NDN_LOG_TRACE("Number elements in IBF: " << m_prefixes.size());
222 NDN_LOG_TRACE("m_threshold: " << m_threshold << " Total: " << positive.size() + negative.size());
223
224 if (!peel) {
225 NDN_LOG_TRACE("Decoding of differences with stored IBF unsuccessful, deleting pending interest");
226 m_pendingEntries.erase(it++);
227 continue;
228 }
229
230 State state;
231 if (entry.bf.contains(prefix.toUri()) || positive.size() + negative.size() >= m_threshold) {
232 if (entry.bf.contains(prefix.toUri())) {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500233 state.addContent(ndn::Name(prefix).appendNumber(m_prefixes[prefix]));
234 NDN_LOG_DEBUG("sending sync content " << prefix << " " << std::to_string(m_prefixes[prefix]));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500235 }
236 else {
237 NDN_LOG_DEBUG("Sending with empty content to send latest IBF to consumer");
238 }
239
240 // generate sync data and cancel the event
241 ndn::Name syncDataName = it->first;
242 m_iblt.appendToName(syncDataName);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500243
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500244 m_segmentPublisher.publish(it->first, syncDataName,
245 state.wireEncode(), m_syncReplyFreshness);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500246
247 m_pendingEntries.erase(it++);
248 }
249 else {
250 ++it;
251 }
252 }
253}
254
255} // namespace psync