blob: 301828ba6b7a827f10c45c8772e76d95c42b6177 [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 "consumer.hpp"
21#include "detail/state.hpp"
22
23#include <ndn-cxx/util/logger.hpp>
24
25#include <boost/algorithm/string.hpp>
26
27namespace psync {
28
29NDN_LOG_INIT(psync.Consumer);
30
31Consumer::Consumer(const ndn::Name& syncPrefix,
32 ndn::Face& face,
33 const ReceiveHelloCallback& onReceiveHelloData,
34 const UpdateCallback& onUpdate,
35 unsigned int count,
36 double false_positive = 0.001,
37 ndn::time::milliseconds helloInterestLifetime,
38 ndn::time::milliseconds syncInterestLifetime)
39 : m_face(face)
40 , m_scheduler(m_face.getIoService())
41 , m_syncPrefix(syncPrefix)
42 , m_helloInterestPrefix(ndn::Name(m_syncPrefix).append("hello"))
43 , m_syncInterestPrefix(ndn::Name(m_syncPrefix).append("sync"))
44 , m_onReceiveHelloData(onReceiveHelloData)
45 , m_onUpdate(onUpdate)
46 , m_bloomFilter(count, false_positive)
47 , m_helloInterestLifetime(helloInterestLifetime)
48 , m_syncInterestLifetime(syncInterestLifetime)
49 , m_rng(std::random_device{}())
50 , m_rangeUniformRandom(100, 500)
51{
52}
53
54bool
55Consumer::addSubscription(const ndn::Name& prefix)
56{
57 auto it = m_prefixes.insert(std::pair<ndn::Name, uint64_t>(prefix, 0));
58 if (!it.second) {
59 return false;
60 }
61 m_subscriptionList.insert(prefix);
62 m_bloomFilter.insert(prefix.toUri());
63 return true;
64}
65
66void
67Consumer::sendHelloInterest()
68{
69 ndn::Interest helloInterest(m_helloInterestPrefix);
70 helloInterest.setInterestLifetime(m_helloInterestLifetime);
71 helloInterest.setCanBePrefix(true);
72 helloInterest.setMustBeFresh(true);
73
74 NDN_LOG_DEBUG("Send Hello Interest " << helloInterest);
75
76 m_face.expressInterest(helloInterest,
77 std::bind(&Consumer::onHelloData, this, _1, _2),
78 std::bind(&Consumer::onNackForHello, this, _1, _2),
79 std::bind(&Consumer::onHelloTimeout, this, _1));
80}
81
82void
83Consumer::onHelloData(const ndn::Interest& interest, const ndn::Data& data)
84{
85 ndn::Name helloDataName = data.getName();
86
87 NDN_LOG_DEBUG("On Hello Data");
88
89 // Extract IBF from name which is the last element in hello data's name
90 m_iblt = helloDataName.getSubName(helloDataName.size()-1, 1);
91
92 NDN_LOG_TRACE("m_iblt: " << std::hash<std::string>{}(m_iblt.toUri()));
93
94 State state(data.getContent());
Ashlesh Gawandea9296472018-08-04 08:21:39 -050095 std::vector<MissingDataInfo> updates;
96 std::vector<ndn::Name> availableSubscriptions;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050097
Ashlesh Gawandea9296472018-08-04 08:21:39 -050098 for (const auto& content : state.getContent()) {
99 ndn::Name prefix = content.getPrefix(-1);
100 uint64_t seq = content.get(content.size()-1).toNumber();
101 if (m_prefixes.find(prefix) == m_prefixes.end()) {
102 // In case this the first prefix ever received via hello data,
103 // add it to the available subscriptions
104 availableSubscriptions.push_back(prefix);
105 }
106 else if (seq > m_prefixes[prefix]) {
107 // Else this is not the first time we have seen this prefix,
108 // we must let application know that there is missing data
109 // (scenario: application nack triggers another hello interest)
110 updates.push_back(MissingDataInfo{prefix, m_prefixes[prefix] + 1, seq});
111 m_prefixes[prefix] = seq;
112 }
113 }
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500114
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500115 NDN_LOG_DEBUG("Hello Data: " << state);
116
117 m_onReceiveHelloData(availableSubscriptions);
118
119 if (!updates.empty()) {
120 NDN_LOG_DEBUG("Updating application with missed updates");
121 m_onUpdate(updates);
122 }
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500123}
124
125void
126Consumer::sendSyncInterest()
127{
128 BOOST_ASSERT(!m_iblt.empty());
129
130 ndn::Name syncInterestName(m_syncInterestPrefix);
131
132 // Append subscription list
133 m_bloomFilter.appendToName(syncInterestName);
134
135 // Append IBF received in hello/sync data
136 syncInterestName.append(m_iblt);
137
138 ndn::Interest syncInterest(syncInterestName);
139 syncInterest.setInterestLifetime(m_syncInterestLifetime);
140 syncInterest.setCanBePrefix(true);
141 syncInterest.setMustBeFresh(true);
142
143 NDN_LOG_DEBUG("sendSyncInterest, nonce: " << syncInterest.getNonce() <<
144 " hash: " << std::hash<std::string>{}(syncInterest.getName().toUri()));
145
146 // Remove last pending interest before sending a new one
147 if (m_outstandingInterestId != nullptr) {
148 m_face.removePendingInterest(m_outstandingInterestId);
149 m_outstandingInterestId = nullptr;
150 }
151
152 m_outstandingInterestId = m_face.expressInterest(syncInterest,
153 std::bind(&Consumer::onSyncData, this, _1, _2),
154 std::bind(&Consumer::onNackForSync, this, _1, _2),
155 std::bind(&Consumer::onSyncTimeout, this, _1));
156}
157
158void
159Consumer::onSyncData(const ndn::Interest& interest, const ndn::Data& data)
160{
161 ndn::Name syncDataName = data.getName();
162
163 // Extract IBF from sync data name which is the last component
164 m_iblt = syncDataName.getSubName(syncDataName.size()-1, 1);
165
166 if (data.getContentType() == ndn::tlv::ContentType_Nack) {
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500167 NDN_LOG_DEBUG("Received application Nack from producer, send hello again");
168 sendHelloInterest();
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500169 return;
170 }
171
172 State state(data.getContent());
173 std::vector <MissingDataInfo> updates;
174
175 for (const auto& content : state.getContent()) {
176 NDN_LOG_DEBUG(content);
177 ndn::Name prefix = content.getPrefix(-1);
178 uint64_t seq = content.get(content.size()-1).toNumber();
179 if (m_prefixes.find(prefix) == m_prefixes.end() || seq > m_prefixes[prefix]) {
180 // If this is just the next seq number then we had already informed the consumer about
181 // the previous sequence number and hence seq low and seq high should be equal to current seq
182 updates.push_back(MissingDataInfo{prefix, m_prefixes[prefix] + 1, seq});
183 m_prefixes[prefix] = seq;
184 }
185 // Else updates will be empty and consumer will not be notified.
186 }
187
188 NDN_LOG_DEBUG("Sync Data: " << state);
189
190 if (!updates.empty()) {
191 m_onUpdate(updates);
192 }
193
194 sendSyncInterest();
195}
196
197void
198Consumer::onHelloTimeout(const ndn::Interest& interest)
199{
200 NDN_LOG_DEBUG("on hello timeout");
201 this->sendHelloInterest();
202}
203
204void
205Consumer::onSyncTimeout(const ndn::Interest& interest)
206{
207 NDN_LOG_DEBUG("on sync timeout " << interest.getNonce());
208
209 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
210 m_scheduler.scheduleEvent(after, [this] { sendSyncInterest(); });
211}
212
213void
214Consumer::onNackForHello(const ndn::Interest& interest, const ndn::lp::Nack& nack)
215{
216 NDN_LOG_DEBUG("received Nack with reason " << nack.getReason() <<
217 " for interest " << interest << std::endl);
218
219 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
220 m_scheduler.scheduleEvent(after, [this] { sendHelloInterest(); });
221}
222
223void
224Consumer::onNackForSync(const ndn::Interest& interest, const ndn::lp::Nack& nack)
225{
226 NDN_LOG_DEBUG("received Nack with reason " << nack.getReason() <<
227 " for interest " << interest << std::endl);
228
229 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
230 m_scheduler.scheduleEvent(after, [this] { sendSyncInterest(); });
231}
232
233} // namespace psync