blob: bcf699ee84f38841260aece3cd0f9a9e846a92e4 [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 NLSR (Named-data Link State Routing).
6 * See AUTHORS.md for complete list of NLSR authors and contributors.
7 *
8 * NLSR 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 * NLSR 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 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#ifndef PSYNC_FULL_PRODUCER_HPP
21#define PSYNC_FULL_PRODUCER_HPP
22
23#include "producer-base.hpp"
24#include "detail/state.hpp"
25
26#include <map>
27#include <unordered_set>
28#include <random>
29
30#include <ndn-cxx/face.hpp>
31#include <ndn-cxx/util/scheduler.hpp>
32#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
33#include <ndn-cxx/util/time.hpp>
34#include <ndn-cxx/security/key-chain.hpp>
35
36namespace psync {
37
38// Name has to be different than PendingEntryInfo
39// used in partial-producer otherwise get strange segmentation-faults
40// when partial producer is destructed
41struct PendingEntryInfoFull
42{
43 IBLT iblt;
44 ndn::util::scheduler::ScopedEventId expirationEvent;
45};
46
47typedef std::function<void(const std::vector<MissingDataInfo>&)> UpdateCallback;
48
49const ndn::time::milliseconds SYNC_INTEREST_LIFTIME = 1_s;
50const ndn::Name RECOVERY_PREFIX("recovery");
51
52/**
53 * @brief Full sync logic to synchronize with other nodes
54 * where all nodes wants to get all names prefixes synced.
55 *
56 * Application should call publishName whenever it wants to
57 * let consumers know that new data is available for the userPrefix.
58 * Multiple userPrefixes can be added by using addUserNode.
59 * Currently, fetching and publishing of data needs to be handled by the application.
60 */
61class FullProducer : public ProducerBase
62{
63public:
64 /**
65 * @brief constructor
66 *
67 * Registers syncPrefix in NFD and sends a sync interest
68 *
69 * @param expectedNumEntries expected entries in IBF
70 * @param face application's face
71 * @param syncPrefix The prefix of the sync group
72 * @param userPrefix The prefix of the first user in the group
73 * @param onUpdateCallBack The call back to be called when there is new data
74 * @param syncInterestLifetime lifetime of the sync interest
75 * @param syncReplyFreshness freshness of sync data
76 */
77 FullProducer(size_t expectedNumEntries,
78 ndn::Face& face,
79 const ndn::Name& syncPrefix,
80 const ndn::Name& userPrefix,
81 const UpdateCallback& onUpdateCallBack,
82 ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFTIME,
83 ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS);
84
85 ~FullProducer();
86
87 /**
88 * @brief Publish name to let others know
89 *
90 * addUserNode needs to be called before this to add the prefix
91 * if not already added via the constructor.
92 * If seq is null then the seq of prefix is incremented by 1 else
93 * the supplied sequence is set in the IBF.
94 *
95 * @param prefix the prefix to be updated
96 * @param seq the sequence number of the prefix
97 */
98 void
99 publishName(const ndn::Name& prefix, ndn::optional<uint64_t> seq = ndn::nullopt);
100
101private:
102 /**
103 * @brief Send sync interest for full synchronization
104 *
105 * Forms the interest name: /<sync-prefix>/<own-IBF>
106 * Cancels any pending sync interest we sent earlier on the face
107 * Sends the sync interest
108 */
109 void
110 sendSyncInterest();
111
112PUBLIC_WITH_TESTS_ELSE_PRIVATE:
113 /**
114 * @brief Process interest from other parties
115 *
116 * Determine whether this is a sync interest or recovery interest
117 * and dispatch to onSyncInterest or onRecoveryInterest respectively.
118 *
119 * @param prefixName prefix for sync group which we registered
120 * @param interest the interest we got
121 */
122 void
123 onInterest(const ndn::Name& prefixName, const ndn::Interest& interest);
124
125private:
126 /**
127 * @brief Process sync interest from other parties
128 *
129 * Get differences b/w our IBF and IBF in the sync interest.
130 * If we cannot get the differences successfully then send an application nack.
131 *
132 * If we have some things in our IBF that the other side does not have, reply with the content or
133 * If no. of different items is greater than threshold or equals zero then send a nack.
134 * Otherwise add the sync interest into a map with interest name as key and PendingEntryInfoFull
135 * as value.
136 *
137 * @param interest the sync interest we got
138 */
139 void
140 onSyncInterest(const ndn::Interest& interest);
141
142 /**
143 * @brief Publish our entire state so that requester can catch up.
144 *
145 * @param interest the recovery interest we got
146 */
147 void
148 onRecoveryInterest(const ndn::Interest& interest);
149
150 /**
151 * @brief Send sync data
152 *
153 * Check if the data will satisfy our own pending interest,
154 * remove it first if it does, and then renew the sync interest
155 * Otherwise just send the data
156 *
157 * @param name name to be set as data name
158 * @param block the content of the data
159 */
160 void
161 sendSyncData(const ndn::Name& name, const ndn::Block& block);
162
163 /**
164 * @brief Process sync data
165 *
166 * Call deletePendingInterests to delete any pending sync interest with
167 * interest name would have been satisfied once NFD got the data.
168 *
169 * For each prefix/seq in data content check that we don't already have the
170 * prefix/seq and updateSeq(prefix, seq)
171 *
172 * Notify the application about the updates
173 * sendSyncInterest because the last one was satisfied by the incoming data
174 *
175 * @param interest interest for which we got the data
176 * @param data the data packet we got
177 */
178 void
179 onSyncData(const ndn::Interest& interest, const ndn::Data& data);
180
181 /**
182 * @brief Satisfy pending sync interests
183 *
184 * For pending sync interests SI, if IBF of SI has any difference from our own IBF:
185 * send data back.
186 * If we can't decode differences from the stored IBF, then delete it.
187 */
188 void
189 satisfyPendingInterests();
190
191 /**
192 * @brief Delete pending sync interests that match given name
193 *
194 */
195 void
196 deletePendingInterests(const ndn::Name& interestName);
197
198 /**
199 * @brief Check if hash(prefix + 1) is in negative
200 *
201 * Sometimes what happens is that interest from other side
202 * gets to us before the data
203 */
204 bool
205 isFutureHash(const ndn::Name& prefix, const std::set<uint32_t>& negative);
206
207 /**
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500208 * @brief Send recovery interest using segment fetcher
209 *
210 * Recovery data is expected go over max packet size
211 * Appends the RECOVERY_PREFIX to the given interest
212 */
213 void
214 sendRecoveryInterest(const ndn::Interest& interest);
215
216private:
217 std::map <ndn::Name, PendingEntryInfoFull> m_pendingEntries;
218
219 ndn::time::milliseconds m_syncInterestLifetime;
220
221 UpdateCallback m_onUpdate;
222
223 const ndn::PendingInterestId* m_outstandingInterestId;
224
225 ndn::util::scheduler::ScopedEventId m_scheduledSyncInterestId;
226
227 std::uniform_int_distribution<> m_jitter;
228
229 ndn::Name m_outstandingInterestName;
230
231 const ndn::RegisteredPrefixId* m_registerPrefixId;
232};
233
234} // namespace psync
235
236#endif // PSYNC_FULL_PRODUCER_HPP