blob: 545acea0dd64af3ea2d4dedf23122177b874622b [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());
95
96 NDN_LOG_DEBUG("Content: " << state);
97
98 m_onReceiveHelloData(state.getContent());
99}
100
101void
102Consumer::sendSyncInterest()
103{
104 BOOST_ASSERT(!m_iblt.empty());
105
106 ndn::Name syncInterestName(m_syncInterestPrefix);
107
108 // Append subscription list
109 m_bloomFilter.appendToName(syncInterestName);
110
111 // Append IBF received in hello/sync data
112 syncInterestName.append(m_iblt);
113
114 ndn::Interest syncInterest(syncInterestName);
115 syncInterest.setInterestLifetime(m_syncInterestLifetime);
116 syncInterest.setCanBePrefix(true);
117 syncInterest.setMustBeFresh(true);
118
119 NDN_LOG_DEBUG("sendSyncInterest, nonce: " << syncInterest.getNonce() <<
120 " hash: " << std::hash<std::string>{}(syncInterest.getName().toUri()));
121
122 // Remove last pending interest before sending a new one
123 if (m_outstandingInterestId != nullptr) {
124 m_face.removePendingInterest(m_outstandingInterestId);
125 m_outstandingInterestId = nullptr;
126 }
127
128 m_outstandingInterestId = m_face.expressInterest(syncInterest,
129 std::bind(&Consumer::onSyncData, this, _1, _2),
130 std::bind(&Consumer::onNackForSync, this, _1, _2),
131 std::bind(&Consumer::onSyncTimeout, this, _1));
132}
133
134void
135Consumer::onSyncData(const ndn::Interest& interest, const ndn::Data& data)
136{
137 ndn::Name syncDataName = data.getName();
138
139 // Extract IBF from sync data name which is the last component
140 m_iblt = syncDataName.getSubName(syncDataName.size()-1, 1);
141
142 if (data.getContentType() == ndn::tlv::ContentType_Nack) {
143 NDN_LOG_DEBUG("Received application Nack from producer, renew sync interest");
144 sendSyncInterest();
145 return;
146 }
147
148 State state(data.getContent());
149 std::vector <MissingDataInfo> updates;
150
151 for (const auto& content : state.getContent()) {
152 NDN_LOG_DEBUG(content);
153 ndn::Name prefix = content.getPrefix(-1);
154 uint64_t seq = content.get(content.size()-1).toNumber();
155 if (m_prefixes.find(prefix) == m_prefixes.end() || seq > m_prefixes[prefix]) {
156 // If this is just the next seq number then we had already informed the consumer about
157 // the previous sequence number and hence seq low and seq high should be equal to current seq
158 updates.push_back(MissingDataInfo{prefix, m_prefixes[prefix] + 1, seq});
159 m_prefixes[prefix] = seq;
160 }
161 // Else updates will be empty and consumer will not be notified.
162 }
163
164 NDN_LOG_DEBUG("Sync Data: " << state);
165
166 if (!updates.empty()) {
167 m_onUpdate(updates);
168 }
169
170 sendSyncInterest();
171}
172
173void
174Consumer::onHelloTimeout(const ndn::Interest& interest)
175{
176 NDN_LOG_DEBUG("on hello timeout");
177 this->sendHelloInterest();
178}
179
180void
181Consumer::onSyncTimeout(const ndn::Interest& interest)
182{
183 NDN_LOG_DEBUG("on sync timeout " << interest.getNonce());
184
185 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
186 m_scheduler.scheduleEvent(after, [this] { sendSyncInterest(); });
187}
188
189void
190Consumer::onNackForHello(const ndn::Interest& interest, const ndn::lp::Nack& nack)
191{
192 NDN_LOG_DEBUG("received Nack with reason " << nack.getReason() <<
193 " for interest " << interest << std::endl);
194
195 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
196 m_scheduler.scheduleEvent(after, [this] { sendHelloInterest(); });
197}
198
199void
200Consumer::onNackForSync(const ndn::Interest& interest, const ndn::lp::Nack& nack)
201{
202 NDN_LOG_DEBUG("received Nack with reason " << nack.getReason() <<
203 " for interest " << interest << std::endl);
204
205 ndn::time::milliseconds after(m_rangeUniformRandom(m_rng));
206 m_scheduler.scheduleEvent(after, [this] { sendSyncInterest(); });
207}
208
209} // namespace psync