Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- 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> |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 35 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 36 | |
| 37 | namespace psync { |
| 38 | |
| 39 | // Name has to be different than PendingEntryInfo |
| 40 | // used in partial-producer otherwise get strange segmentation-faults |
| 41 | // when partial producer is destructed |
| 42 | struct PendingEntryInfoFull |
| 43 | { |
| 44 | IBLT iblt; |
| 45 | ndn::util::scheduler::ScopedEventId expirationEvent; |
| 46 | }; |
| 47 | |
| 48 | typedef std::function<void(const std::vector<MissingDataInfo>&)> UpdateCallback; |
| 49 | |
| 50 | const ndn::time::milliseconds SYNC_INTEREST_LIFTIME = 1_s; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 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 | */ |
| 61 | class FullProducer : public ProducerBase |
| 62 | { |
| 63 | public: |
| 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 | |
| 101 | private: |
| 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 | |
| 112 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 113 | /** |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 114 | * @brief Process sync interest from other parties |
| 115 | * |
| 116 | * Get differences b/w our IBF and IBF in the sync interest. |
| 117 | * If we cannot get the differences successfully then send an application nack. |
| 118 | * |
| 119 | * If we have some things in our IBF that the other side does not have, reply with the content or |
| 120 | * If no. of different items is greater than threshold or equals zero then send a nack. |
| 121 | * Otherwise add the sync interest into a map with interest name as key and PendingEntryInfoFull |
| 122 | * as value. |
| 123 | * |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 124 | * @param prefixName prefix for sync group which we registered |
| 125 | * @param interest the interest we got |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 126 | */ |
| 127 | void |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 128 | onSyncInterest(const ndn::Name& prefixName, const ndn::Interest& interest); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 129 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 130 | private: |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 131 | /** |
| 132 | * @brief Send sync data |
| 133 | * |
| 134 | * Check if the data will satisfy our own pending interest, |
| 135 | * remove it first if it does, and then renew the sync interest |
| 136 | * Otherwise just send the data |
| 137 | * |
| 138 | * @param name name to be set as data name |
| 139 | * @param block the content of the data |
| 140 | */ |
| 141 | void |
| 142 | sendSyncData(const ndn::Name& name, const ndn::Block& block); |
| 143 | |
| 144 | /** |
| 145 | * @brief Process sync data |
| 146 | * |
| 147 | * Call deletePendingInterests to delete any pending sync interest with |
| 148 | * interest name would have been satisfied once NFD got the data. |
| 149 | * |
| 150 | * For each prefix/seq in data content check that we don't already have the |
| 151 | * prefix/seq and updateSeq(prefix, seq) |
| 152 | * |
| 153 | * Notify the application about the updates |
| 154 | * sendSyncInterest because the last one was satisfied by the incoming data |
| 155 | * |
| 156 | * @param interest interest for which we got the data |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 157 | * @param bufferPtr sync data content |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 158 | */ |
| 159 | void |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 160 | onSyncData(const ndn::Interest& interest, const ndn::ConstBufferPtr& bufferPtr); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * @brief Satisfy pending sync interests |
| 164 | * |
| 165 | * For pending sync interests SI, if IBF of SI has any difference from our own IBF: |
| 166 | * send data back. |
| 167 | * If we can't decode differences from the stored IBF, then delete it. |
| 168 | */ |
| 169 | void |
| 170 | satisfyPendingInterests(); |
| 171 | |
| 172 | /** |
| 173 | * @brief Delete pending sync interests that match given name |
| 174 | * |
| 175 | */ |
| 176 | void |
| 177 | deletePendingInterests(const ndn::Name& interestName); |
| 178 | |
| 179 | /** |
| 180 | * @brief Check if hash(prefix + 1) is in negative |
| 181 | * |
| 182 | * Sometimes what happens is that interest from other side |
| 183 | * gets to us before the data |
| 184 | */ |
| 185 | bool |
| 186 | isFutureHash(const ndn::Name& prefix, const std::set<uint32_t>& negative); |
| 187 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 188 | private: |
| 189 | std::map <ndn::Name, PendingEntryInfoFull> m_pendingEntries; |
| 190 | |
| 191 | ndn::time::milliseconds m_syncInterestLifetime; |
| 192 | |
| 193 | UpdateCallback m_onUpdate; |
| 194 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 195 | ndn::util::scheduler::ScopedEventId m_scheduledSyncInterestId; |
| 196 | |
| 197 | std::uniform_int_distribution<> m_jitter; |
| 198 | |
| 199 | ndn::Name m_outstandingInterestName; |
| 200 | |
| 201 | const ndn::RegisteredPrefixId* m_registerPrefixId; |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame^] | 202 | |
| 203 | std::shared_ptr<ndn::util::SegmentFetcher> m_fetcher; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | } // namespace psync |
| 207 | |
| 208 | #endif // PSYNC_FULL_PRODUCER_HPP |