blob: f27f355b5084f4dd940d4824c6991305db3f88d1 [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 Gawandedeb73f82018-08-09 11:08:02 -050098 NDN_LOG_DEBUG("Hello Data: " << state);
99
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500100 for (const auto& content : state.getContent()) {
101 ndn::Name prefix = content.getPrefix(-1);
102 uint64_t seq = content.get(content.size()-1).toNumber();
Ashlesh Gawandedeb73f82018-08-09 11:08:02 -0500103 // If consumer is subscribed then prefix must already be present in
104 // m_prefixes (see addSubscription). So [] operator is safe to use.
105 if (isSubscribed(prefix) && seq > m_prefixes[prefix]) {
106 // In case we are behind on this prefix and consumer is subscribed to it
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500107 updates.push_back(MissingDataInfo{prefix, m_prefixes[prefix] + 1, seq});
108 m_prefixes[prefix] = seq;
109 }
Ashlesh Gawandedeb73f82018-08-09 11:08:02 -0500110 availableSubscriptions.push_back(prefix);
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500111 }
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500112
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500113 m_onReceiveHelloData(availableSubscriptions);
114
115 if (!updates.empty()) {
116 NDN_LOG_DEBUG("Updating application with missed updates");
117 m_onUpdate(updates);
118 }
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500119}
120
121void
122Consumer::sendSyncInterest()
123{
124 BOOST_ASSERT(!m_iblt.empty());
125
126 ndn::Name syncInterestName(m_syncInterestPrefix);
127
128 // Append subscription list
129 m_bloomFilter.appendToName(syncInterestName);
130
131 // Append IBF received in hello/sync data
132 syncInterestName.append(m_iblt);
133
134 ndn::Interest syncInterest(syncInterestName);
135 syncInterest.setInterestLifetime(m_syncInterestLifetime);
136 syncInterest.setCanBePrefix(true);
137 syncInterest.setMustBeFresh(true);
138
139 NDN_LOG_DEBUG("sendSyncInterest, nonce: " << syncInterest.getNonce() <<
140 " hash: " << std::hash<std::string>{}(syncInterest.getName().toUri()));
141
142 // Remove last pending interest before sending a new one
143 if (m_outstandingInterestId != nullptr) {
144 m_face.removePendingInterest(m_outstandingInterestId);
145 m_outstandingInterestId = nullptr;
146 }
147
148 m_outstandingInterestId = m_face.expressInterest(syncInterest,
149 std::bind(&Consumer::onSyncData, this, _1, _2),
150 std::bind(&Consumer::onNackForSync, this, _1, _2),
151 std::bind(&Consumer::onSyncTimeout, this, _1));
152}
153
154void
155Consumer::onSyncData(const ndn::Interest& interest, const ndn::Data& data)
156{
157 ndn::Name syncDataName = data.getName();
158
159 // Extract IBF from sync data name which is the last component
160 m_iblt = syncDataName.getSubName(syncDataName.size()-1, 1);
161
162 if (data.getContentType() == ndn::tlv::ContentType_Nack) {
Ashlesh Gawandea9296472018-08-04 08:21:39 -0500163 NDN_LOG_DEBUG("Received application Nack from producer, send hello again");
164 sendHelloInterest();
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500165 return;
166 }
167
168 State state(data.getContent());
169 std::vector <MissingDataInfo> updates;
170
171 for (const auto& content : state.getContent()) {
172 NDN_LOG_DEBUG(content);
173 ndn::Name prefix = content.getPrefix(-1);
174 uint64_t seq = content.get(content.size()-1).toNumber();
175 if (m_prefixes.find(prefix) == m_prefixes.end() || seq > m_prefixes[prefix]) {
176 // If this is just the next seq number then we had already informed the consumer about
177 // the previous sequence number and hence seq low and seq high should be equal to current seq
178 updates.push_back(MissingDataInfo{prefix, m_prefixes[prefix] + 1, seq});
179 m_prefixes[prefix] = seq;
180 }
181 // Else updates will be empty and consumer will not be notified.
182 }
183
184 NDN_LOG_DEBUG("Sync Data: " << state);
185
186 if (!updates.empty()) {
187 m_onUpdate(updates);
188 }
189
190 sendSyncInterest();
191}
192
193void
194Consumer::onHelloTimeout(const ndn::Interest& interest)
195{
196 NDN_LOG_DEBUG("on hello timeout");
197 this->sendHelloInterest();
198}
199
200void
201Consumer::onSyncTimeout(const ndn::Interest& interest)
202{
203 NDN_LOG_DEBUG("on sync timeout " << interest.getNonce());
204
205 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
206 m_scheduler.scheduleEvent(after, [this] { sendSyncInterest(); });
207}
208
209void
210Consumer::onNackForHello(const ndn::Interest& interest, const ndn::lp::Nack& nack)
211{
212 NDN_LOG_DEBUG("received Nack with reason " << nack.getReason() <<
213 " for interest " << interest << std::endl);
214
215 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
216 m_scheduler.scheduleEvent(after, [this] { sendHelloInterest(); });
217}
218
219void
220Consumer::onNackForSync(const ndn::Interest& interest, const ndn::lp::Nack& nack)
221{
222 NDN_LOG_DEBUG("received Nack with reason " << nack.getReason() <<
223 " for interest " << interest << std::endl);
224
225 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
226 m_scheduler.scheduleEvent(after, [this] { sendSyncInterest(); });
227}
228
229} // namespace psync