Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 11c3638 | 2024-01-19 00:23:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2024 University of California, Los Angeles |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync 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, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync 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 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Sonu Mishra | 0dadc57 | 2016-12-12 23:59:41 -0800 | [diff] [blame] | 23 | * @author Sonu Mishra <https://www.linkedin.com/in/mishrasonu> |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "logic.hpp" |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 27 | #include "detail/bzip2-helper.hpp" |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/util/backports.hpp> |
Davide Pesavento | e7dc776 | 2020-12-22 20:47:52 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/util/exception.hpp> |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/logger.hpp> |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 32 | #include <ndn-cxx/util/string-helper.hpp> |
| 33 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 34 | NDN_LOG_INIT(sync.Logic); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 36 | #define CHRONO_LOG_DBG(v) NDN_LOG_DEBUG("Instance" << m_instanceId << ": " << v) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 37 | |
| 38 | namespace chronosync { |
| 39 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 40 | const std::vector<uint8_t> EMPTY_DIGEST{ |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 41 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, |
| 42 | 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
| 43 | 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 44 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 45 | }; |
| 46 | |
Davide Pesavento | 55492d5 | 2022-09-19 18:38:02 -0400 | [diff] [blame] | 47 | const name::Component RESET_COMPONENT{"reset"}; |
| 48 | const name::Component RECOVERY_COMPONENT{"recovery"}; |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 49 | const size_t NDNLP_EXPECTED_OVERHEAD = 20; |
| 50 | |
| 51 | /** |
| 52 | * Get maximum packet limit |
| 53 | * |
| 54 | * By default, it returns `ndn::MAX_NDN_PACKET_SIZE`. |
| 55 | * The returned value can be customized using the environment variable `CHRONOSYNC_MAX_PACKET_SIZE`, |
| 56 | * but the returned value will be at least 500 and no more than `ndn::MAX_NDN_PACKET_SIZE`. |
| 57 | */ |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 58 | #ifndef CHRONOSYNC_WITH_TESTS |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 59 | static |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 60 | #endif // CHRONOSYNC_WITH_TESTS |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 61 | size_t |
| 62 | getMaxPacketLimit() |
| 63 | { |
| 64 | static size_t limit = 0; |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 65 | #ifndef CHRONOSYNC_WITH_TESTS |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 66 | if (limit != 0) { |
| 67 | return limit; |
| 68 | } |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 69 | #endif // CHRONOSYNC_WITH_TESTS |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 70 | |
| 71 | if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) { |
| 72 | try { |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 73 | limit = std::clamp<size_t>(boost::lexical_cast<size_t>(getenv("CHRONOSYNC_MAX_PACKET_SIZE")), |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 74 | 500, ndn::MAX_NDN_PACKET_SIZE); |
| 75 | } |
| 76 | catch (const boost::bad_lexical_cast&) { |
| 77 | limit = ndn::MAX_NDN_PACKET_SIZE; |
| 78 | } |
| 79 | } |
| 80 | else { |
| 81 | limit = ndn::MAX_NDN_PACKET_SIZE; |
| 82 | } |
| 83 | |
| 84 | return limit; |
| 85 | } |
| 86 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 87 | Logic::Logic(ndn::Face& face, |
| 88 | const Name& syncPrefix, |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 89 | const Name& defaultUserPrefix, |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 90 | const UpdateCallback& onUpdate, |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 91 | const Name& defaultSigningId, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 92 | std::shared_ptr<Validator> validator, |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 93 | const time::steady_clock::duration& resetTimer, |
| 94 | const time::steady_clock::duration& cancelResetTimer, |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 95 | const time::milliseconds& resetInterestLifetime, |
| 96 | const time::milliseconds& syncInterestLifetime, |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 97 | const time::milliseconds& syncReplyFreshness, |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 98 | const time::milliseconds& recoveryInterestLifetime, |
| 99 | const name::Component& session) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 100 | : m_face(face) |
| 101 | , m_syncPrefix(syncPrefix) |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 102 | , m_syncReset(Name(syncPrefix).append("reset")) |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 103 | , m_defaultUserPrefix(defaultUserPrefix) |
Davide Pesavento | d45f888 | 2023-11-11 17:32:23 -0500 | [diff] [blame] | 104 | , m_interestTable(m_face.getIoContext()) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 105 | , m_isInReset(false) |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 106 | , m_needPeriodReset(resetTimer > time::nanoseconds::zero()) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 107 | , m_onUpdate(onUpdate) |
Davide Pesavento | d45f888 | 2023-11-11 17:32:23 -0500 | [diff] [blame] | 108 | , m_scheduler(m_face.getIoContext()) |
Ashlesh Gawande | 9a306fe | 2019-01-04 11:38:18 -0600 | [diff] [blame] | 109 | , m_rng(ndn::random::getRandomNumberEngine()) |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 110 | , m_rangeUniformRandom(100, 500) |
| 111 | , m_reexpressionJitter(100, 500) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 112 | , m_resetTimer(resetTimer) |
| 113 | , m_cancelResetTimer(cancelResetTimer) |
| 114 | , m_resetInterestLifetime(resetInterestLifetime) |
| 115 | , m_syncInterestLifetime(syncInterestLifetime) |
| 116 | , m_syncReplyFreshness(syncReplyFreshness) |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 117 | , m_recoveryInterestLifetime(recoveryInterestLifetime) |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 118 | , m_validator(std::move(validator)) |
Alexander Afanasyev | 90587b8 | 2018-02-11 20:36:53 -0500 | [diff] [blame] | 119 | , m_instanceId(s_instanceCounter++) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 120 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 121 | CHRONO_LOG_DBG(">> Logic::Logic"); |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 122 | addUserNode(m_defaultUserPrefix, defaultSigningId, session, false); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 123 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 124 | CHRONO_LOG_DBG("Listen to: " << m_syncPrefix); |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 125 | m_syncRegisteredPrefix = m_face.setInterestFilter( |
| 126 | ndn::InterestFilter(m_syncPrefix).allowLoopback(false), |
| 127 | bind(&Logic::onSyncInterest, this, _1, _2), |
| 128 | bind(&Logic::onSyncRegisterFailed, this, _1, _2)); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 129 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 130 | sendSyncInterest(); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 131 | CHRONO_LOG_DBG("<< Logic::Logic"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | Logic::~Logic() |
| 135 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 136 | CHRONO_LOG_DBG(">> Logic::~Logic"); |
Yingdi Yu | 9d5679a | 2015-02-01 00:17:58 -0800 | [diff] [blame] | 137 | m_interestTable.clear(); |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 138 | m_scheduler.cancelAllEvents(); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 139 | CHRONO_LOG_DBG("<< Logic::~Logic"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 143 | Logic::reset(bool isOnInterest) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 144 | { |
| 145 | m_isInReset = true; |
| 146 | |
| 147 | m_state.reset(); |
| 148 | m_log.clear(); |
| 149 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 150 | if (!isOnInterest) |
| 151 | sendResetInterest(); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 152 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 153 | sendSyncInterest(); |
| 154 | |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 155 | m_delayedInterestProcessingId = m_scheduler.schedule(m_cancelResetTimer, [this] { cancelReset(); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 159 | Logic::setDefaultUserPrefix(const Name& defaultUserPrefix) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 160 | { |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 161 | if (defaultUserPrefix != EMPTY_NAME) { |
| 162 | if (m_nodeList.find(defaultUserPrefix) != m_nodeList.end()) { |
| 163 | m_defaultUserPrefix = defaultUserPrefix; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 169 | Logic::addUserNode(const Name& userPrefix, const Name& signingId, const name::Component& session, bool shouldSendReset) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 170 | { |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 171 | if (userPrefix == EMPTY_NAME) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 172 | return; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 173 | if (m_defaultUserPrefix == EMPTY_NAME) { |
| 174 | m_defaultUserPrefix = userPrefix; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 175 | } |
| 176 | if (m_nodeList.find(userPrefix) == m_nodeList.end()) { |
| 177 | m_nodeList[userPrefix].userPrefix = userPrefix; |
| 178 | m_nodeList[userPrefix].signingId = signingId; |
| 179 | Name sessionName = userPrefix; |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 180 | if (!session.empty()) { |
| 181 | sessionName.append(session); |
| 182 | } |
| 183 | else { |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 184 | sessionName.appendNumber(time::toUnixTimestamp(time::system_clock::now()).count()); |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 185 | } |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 186 | m_nodeList[userPrefix].sessionName = sessionName; |
| 187 | m_nodeList[userPrefix].seqNo = 0; |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 188 | reset(!shouldSendReset); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 191 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 192 | void |
| 193 | Logic::removeUserNode(const Name& userPrefix) |
| 194 | { |
| 195 | auto userNode = m_nodeList.find(userPrefix); |
| 196 | if (userNode != m_nodeList.end()) { |
| 197 | m_nodeList.erase(userNode); |
| 198 | if (m_defaultUserPrefix == userPrefix) { |
| 199 | if (!m_nodeList.empty()) { |
| 200 | m_defaultUserPrefix = m_nodeList.begin()->second.userPrefix; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 201 | } |
| 202 | else { |
| 203 | m_defaultUserPrefix = EMPTY_NAME; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 204 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 205 | } |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 206 | reset(false); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 209 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 210 | const Name& |
| 211 | Logic::getSessionName(Name prefix) |
| 212 | { |
| 213 | if (prefix == EMPTY_NAME) |
| 214 | prefix = m_defaultUserPrefix; |
Davide Pesavento | e7dc776 | 2020-12-22 20:47:52 -0500 | [diff] [blame] | 215 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 216 | auto node = m_nodeList.find(prefix); |
| 217 | if (node != m_nodeList.end()) |
| 218 | return node->second.sessionName; |
Davide Pesavento | e7dc776 | 2020-12-22 20:47:52 -0500 | [diff] [blame] | 219 | |
| 220 | NDN_THROW(Error("Nonexistent node: " + prefix.toUri())); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 221 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 222 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 223 | const SeqNo& |
| 224 | Logic::getSeqNo(Name prefix) |
| 225 | { |
| 226 | if (prefix == EMPTY_NAME) |
| 227 | prefix = m_defaultUserPrefix; |
Davide Pesavento | e7dc776 | 2020-12-22 20:47:52 -0500 | [diff] [blame] | 228 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 229 | auto node = m_nodeList.find(prefix); |
| 230 | if (node != m_nodeList.end()) |
| 231 | return node->second.seqNo; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 232 | |
Davide Pesavento | e7dc776 | 2020-12-22 20:47:52 -0500 | [diff] [blame] | 233 | NDN_THROW(Error("Nonexistent node: " + prefix.toUri())); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 237 | Logic::updateSeqNo(const SeqNo& seqNo, const Name& updatePrefix) |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 238 | { |
| 239 | Name prefix; |
| 240 | if (updatePrefix == EMPTY_NAME) { |
| 241 | if (m_defaultUserPrefix == EMPTY_NAME) |
| 242 | return; |
| 243 | prefix = m_defaultUserPrefix; |
| 244 | } |
| 245 | else |
| 246 | prefix = updatePrefix; |
| 247 | |
| 248 | auto it = m_nodeList.find(prefix); |
| 249 | if (it != m_nodeList.end()) { |
| 250 | NodeInfo& node = it->second; |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 251 | CHRONO_LOG_DBG(">> Logic::updateSeqNo"); |
| 252 | CHRONO_LOG_DBG("seqNo: " << seqNo << " m_seqNo: " << node.seqNo); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 253 | if (seqNo < node.seqNo || seqNo == 0) |
| 254 | return; |
| 255 | |
| 256 | node.seqNo = seqNo; |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 257 | CHRONO_LOG_DBG("updateSeqNo: m_seqNo " << node.seqNo); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 258 | |
| 259 | if (!m_isInReset) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 260 | CHRONO_LOG_DBG("updateSeqNo: not in Reset"); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 261 | ConstBufferPtr previousRoot = m_state.getRootDigest(); |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 262 | printDigest(previousRoot); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 263 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 264 | auto [isInserted, isUpdated, oldSeq] = m_state.update(node.sessionName, node.seqNo); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 265 | CHRONO_LOG_DBG("Insert: " << std::boolalpha << isInserted); |
| 266 | CHRONO_LOG_DBG("Updated: " << std::boolalpha << isUpdated); |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 267 | (void)oldSeq; // silence "unused variable" warning with gcc 7 |
| 268 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 269 | if (isInserted || isUpdated) { |
| 270 | DiffStatePtr commit = make_shared<DiffState>(); |
| 271 | commit->update(node.sessionName, node.seqNo); |
| 272 | commit->setRootDigest(m_state.getRootDigest()); |
| 273 | insertToDiffLog(commit, previousRoot); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 274 | satisfyPendingSyncInterests(prefix, commit); |
| 275 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | ConstBufferPtr |
| 281 | Logic::getRootDigest() const |
| 282 | { |
| 283 | return m_state.getRootDigest(); |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | Logic::printState(std::ostream& os) const |
| 288 | { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 289 | for (const auto& leaf : m_state.getLeaves()) { |
| 290 | os << *leaf << "\n"; |
| 291 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | std::set<Name> |
| 295 | Logic::getSessionNames() const |
| 296 | { |
| 297 | std::set<Name> sessionNames; |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 298 | for (const auto& leaf : m_state.getLeaves()) { |
| 299 | sessionNames.insert(leaf->getSessionName()); |
| 300 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 301 | return sessionNames; |
| 302 | } |
| 303 | |
| 304 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 305 | Logic::onSyncInterest(const Name&, const Interest& interest) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 306 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 307 | CHRONO_LOG_DBG(">> Logic::onSyncInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 308 | Name name = interest.getName(); |
| 309 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 310 | CHRONO_LOG_DBG("InterestName: " << name); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 311 | |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 312 | if (name.size() >= 1 && RESET_COMPONENT == name.get(-1)) { |
| 313 | processResetInterest(interest); |
| 314 | } |
| 315 | else if (name.size() >= 2 && RECOVERY_COMPONENT == name.get(-2)) { |
| 316 | processRecoveryInterest(interest); |
| 317 | } |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 318 | else { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 319 | processSyncInterest(interest); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 320 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 321 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 322 | CHRONO_LOG_DBG("<< Logic::onSyncInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void |
| 326 | Logic::onSyncRegisterFailed(const Name& prefix, const std::string& msg) |
| 327 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 328 | CHRONO_LOG_DBG(">> Logic::onSyncRegisterFailed"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 332 | Logic::onSyncData(const Interest&, const Data& data) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 333 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 334 | CHRONO_LOG_DBG(">> Logic::onSyncData"); |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 335 | if (m_validator != nullptr) |
| 336 | m_validator->validate(data, |
| 337 | bind(&Logic::onSyncDataValidated, this, _1), |
| 338 | bind(&Logic::onSyncDataValidationFailed, this, _1)); |
| 339 | else |
| 340 | onSyncDataValidated(data); |
Sonu Mishra | f42aa2c | 2017-01-22 18:47:33 -0800 | [diff] [blame] | 341 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 342 | CHRONO_LOG_DBG("<< Logic::onSyncData"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 346 | Logic::onResetData(const Interest&, const Data&) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 347 | { |
| 348 | // This should not happened, drop the received data. |
| 349 | } |
| 350 | |
| 351 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 352 | Logic::onSyncNack(const Interest&, const ndn::lp::Nack& nack) |
Ashlesh Gawande | a1ad604 | 2019-10-07 15:56:11 -0500 | [diff] [blame] | 353 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 354 | CHRONO_LOG_DBG(">> Logic::onSyncNack"); |
Ashlesh Gawande | a1ad604 | 2019-10-07 15:56:11 -0500 | [diff] [blame] | 355 | if (nack.getReason() == ndn::lp::NackReason::NO_ROUTE) { |
| 356 | auto after = ndn::time::milliseconds(m_reexpressionJitter(m_rng)); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 357 | CHRONO_LOG_DBG("Schedule sync interest after: " << after); |
Ashlesh Gawande | a1ad604 | 2019-10-07 15:56:11 -0500 | [diff] [blame] | 358 | m_scheduler.schedule(after, [this] { sendSyncInterest(); }); |
| 359 | } |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 360 | CHRONO_LOG_DBG("<< Logic::onSyncNack"); |
Ashlesh Gawande | a1ad604 | 2019-10-07 15:56:11 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 364 | Logic::onSyncTimeout(const Interest& interest) |
| 365 | { |
| 366 | // It is OK. Others will handle the time out situation. |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 367 | CHRONO_LOG_DBG(">> Logic::onSyncTimeout"); |
| 368 | CHRONO_LOG_DBG("Interest: " << interest.getName()); |
| 369 | CHRONO_LOG_DBG("<< Logic::onSyncTimeout"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 373 | Logic::onSyncDataValidationFailed(const Data&) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 374 | { |
| 375 | // SyncReply cannot be validated. |
| 376 | } |
| 377 | |
| 378 | void |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 379 | Logic::onSyncDataValidated(const Data& data) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 380 | { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 381 | const auto& name = data.getName(); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 382 | ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size()); |
| 383 | |
Alexander Afanasyev | fcbf81d | 2018-02-19 10:25:46 -0500 | [diff] [blame] | 384 | try { |
| 385 | auto contentBuffer = bzip2::decompress(reinterpret_cast<const char*>(data.getContent().value()), |
| 386 | data.getContent().value_size()); |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 387 | processSyncData(name, digest, Block(std::move(contentBuffer))); |
Alexander Afanasyev | fcbf81d | 2018-02-19 10:25:46 -0500 | [diff] [blame] | 388 | } |
| 389 | catch (const std::ios_base::failure& error) { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 390 | NDN_LOG_WARN("Error decompressing content of " << name << " (" << error.what() << ")"); |
Alexander Afanasyev | fcbf81d | 2018-02-19 10:25:46 -0500 | [diff] [blame] | 391 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 395 | Logic::processSyncInterest(const Interest& interest, bool isTimedProcessing/*=false*/) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 396 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 397 | CHRONO_LOG_DBG(">> Logic::processSyncInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 398 | |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 399 | const auto& name = interest.getName(); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 400 | ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size()); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 401 | ConstBufferPtr rootDigest = m_state.getRootDigest(); |
| 402 | |
| 403 | // If the digest of the incoming interest is the same as root digest |
| 404 | // Put the interest into InterestTable |
| 405 | if (*rootDigest == *digest) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 406 | CHRONO_LOG_DBG("Oh, we are in the same state"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 407 | m_interestTable.insert(interest, digest, false); |
| 408 | |
| 409 | if (!m_isInReset) |
| 410 | return; |
| 411 | |
| 412 | if (!isTimedProcessing) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 413 | CHRONO_LOG_DBG("Non timed processing in reset"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 414 | // Still in reset, our own seq has not been put into state yet |
| 415 | // Do not hurry, some others may be also resetting and may send their reply |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 416 | time::milliseconds after(m_rangeUniformRandom(m_rng)); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 417 | CHRONO_LOG_DBG("After: " << after); |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 418 | m_delayedInterestProcessingId = m_scheduler.schedule(after, |
| 419 | [=] { processSyncInterest(interest, true); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 420 | } |
| 421 | else { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 422 | CHRONO_LOG_DBG("Timed processing in reset"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 423 | // Now we can get out of reset state by putting our own stuff into m_state. |
| 424 | cancelReset(); |
| 425 | } |
| 426 | |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | // If the digest of incoming interest is an "empty" digest |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 431 | if (*digest == EMPTY_DIGEST) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 432 | CHRONO_LOG_DBG("Poor guy, he knows nothing"); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 433 | sendSyncData(m_defaultUserPrefix, name, m_state); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 437 | auto stateIter = m_log.find(digest); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 438 | // If the digest of incoming interest can be found from the log |
| 439 | if (stateIter != m_log.end()) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 440 | CHRONO_LOG_DBG("It is ok, you are so close"); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 441 | sendSyncData(m_defaultUserPrefix, name, *(*stateIter)->diff()); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
| 445 | if (!isTimedProcessing) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 446 | CHRONO_LOG_DBG("Let's wait, just wait for a while"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 447 | // Do not hurry, some incoming SyncReplies may help us to recognize the digest |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 448 | m_interestTable.insert(interest, digest, true); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 449 | |
| 450 | m_delayedInterestProcessingId = |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 451 | m_scheduler.schedule(time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 452 | [=] { processSyncInterest(interest, true); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 453 | } |
| 454 | else { |
| 455 | // OK, nobody is helping us, just tell the truth. |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 456 | CHRONO_LOG_DBG("OK, nobody is helping us, let us try to recover"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 457 | m_interestTable.erase(digest); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 458 | sendRecoveryInterest(digest); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 461 | CHRONO_LOG_DBG("<< Logic::processSyncInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame] | 465 | Logic::processResetInterest(const Interest&) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 466 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 467 | CHRONO_LOG_DBG(">> Logic::processResetInterest"); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 468 | reset(true); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame] | 472 | Logic::processSyncData(const Name&, ConstBufferPtr digest, const Block& syncReply) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 473 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 474 | CHRONO_LOG_DBG(">> Logic::processSyncData"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 475 | DiffStatePtr commit = make_shared<DiffState>(); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 476 | ConstBufferPtr previousRoot = m_state.getRootDigest(); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 477 | |
| 478 | try { |
| 479 | m_interestTable.erase(digest); // Remove satisfied interest from PIT |
| 480 | |
| 481 | State reply; |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 482 | reply.wireDecode(syncReply); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 483 | |
| 484 | std::vector<MissingDataInfo> v; |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame] | 485 | for (const auto& leaf : reply.getLeaves().get<ordered>()) { |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 486 | BOOST_ASSERT(leaf != nullptr); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 487 | |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 488 | const Name& info = leaf->getSessionName(); |
| 489 | SeqNo seq = leaf->getSeq(); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 490 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 491 | auto [isInserted, isUpdated, oldSeq] = m_state.update(info, seq); |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 492 | if (isInserted || isUpdated) { |
| 493 | commit->update(info, seq); |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 494 | oldSeq++; |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame] | 495 | v.push_back({info, oldSeq, seq}); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 496 | } |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 497 | } |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 498 | |
| 499 | if (!v.empty()) { |
| 500 | m_onUpdate(v); |
| 501 | |
| 502 | commit->setRootDigest(m_state.getRootDigest()); |
| 503 | insertToDiffLog(commit, previousRoot); |
| 504 | } |
| 505 | else { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 506 | CHRONO_LOG_DBG("What? nothing new"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 509 | catch (const State::Error&) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 510 | CHRONO_LOG_DBG("Something really fishy happened during state decoding"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 511 | commit.reset(); |
| 512 | return; |
| 513 | } |
| 514 | |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 515 | if (static_cast<bool>(commit) && !commit->getLeaves().empty()) { |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 516 | // state changed and it is safe to express a new interest |
Junxiao Shi | c490212 | 2019-02-08 15:13:50 -0700 | [diff] [blame] | 517 | auto after = time::milliseconds(m_reexpressionJitter(m_rng)); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 518 | CHRONO_LOG_DBG("Reschedule sync interest after: " << after); |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 519 | m_reexpressingInterestId = m_scheduler.schedule(after, [this] { sendSyncInterest(); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 524 | Logic::satisfyPendingSyncInterests(const Name& updatedPrefix, ConstDiffStatePtr commit) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 525 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 526 | CHRONO_LOG_DBG(">> Logic::satisfyPendingSyncInterests"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 527 | try { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 528 | CHRONO_LOG_DBG("InterestTable size: " << m_interestTable.size()); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 529 | auto it = m_interestTable.begin(); |
| 530 | while (it != m_interestTable.end()) { |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 531 | ConstUnsatisfiedInterestPtr request = *it; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 532 | ++it; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 533 | if (request->isUnknown) |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 534 | sendSyncData(updatedPrefix, request->interest.getName(), m_state); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 535 | else |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 536 | sendSyncData(updatedPrefix, request->interest.getName(), *commit); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 537 | } |
| 538 | m_interestTable.clear(); |
| 539 | } |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 540 | catch (const InterestTable::Error&) { |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 541 | // ok. not really an error |
| 542 | } |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 543 | CHRONO_LOG_DBG("<< Logic::satisfyPendingSyncInterests"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 547 | Logic::insertToDiffLog(DiffStatePtr commit, ConstBufferPtr previousRoot) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 548 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 549 | CHRONO_LOG_DBG(">> Logic::insertToDiffLog"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 550 | // Connect to the history |
| 551 | if (!m_log.empty()) |
| 552 | (*m_log.find(previousRoot))->setNext(commit); |
| 553 | |
| 554 | // Insert the commit |
| 555 | m_log.erase(commit->getRootDigest()); |
| 556 | m_log.insert(commit); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 557 | CHRONO_LOG_DBG("<< Logic::insertToDiffLog"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void |
| 561 | Logic::sendResetInterest() |
| 562 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 563 | CHRONO_LOG_DBG(">> Logic::sendResetInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 564 | |
| 565 | if (m_needPeriodReset) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 566 | CHRONO_LOG_DBG("Need Period Reset"); |
| 567 | CHRONO_LOG_DBG("ResetTimer: " << m_resetTimer); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 568 | |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 569 | m_resetInterestId = m_scheduler.schedule(m_resetTimer + time::milliseconds(m_reexpressionJitter(m_rng)), |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 570 | [this] { sendResetInterest(); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | Interest interest(m_syncReset); |
| 574 | interest.setMustBeFresh(true); |
| 575 | interest.setInterestLifetime(m_resetInterestLifetime); |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 576 | |
| 577 | // Assigning to m_pendingResetInterest cancels the previous reset Interest. |
| 578 | // This is harmless since no Data is expected. |
| 579 | m_pendingResetInterest = m_face.expressInterest(interest, |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 580 | bind(&Logic::onResetData, this, _1, _2), |
| 581 | bind(&Logic::onSyncTimeout, this, _1), // Nack |
| 582 | bind(&Logic::onSyncTimeout, this, _1)); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 583 | CHRONO_LOG_DBG("<< Logic::sendResetInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void |
| 587 | Logic::sendSyncInterest() |
| 588 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 589 | CHRONO_LOG_DBG(">> Logic::sendSyncInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 590 | |
| 591 | Name interestName; |
| 592 | interestName.append(m_syncPrefix) |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 593 | .append(name::Component(*m_state.getRootDigest())); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 594 | |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 595 | m_pendingSyncInterestName = interestName; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 596 | |
Davide Pesavento | 11c3638 | 2024-01-19 00:23:02 -0500 | [diff] [blame] | 597 | #ifndef NDEBUG |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 598 | printDigest(m_state.getRootDigest()); |
| 599 | #endif |
| 600 | |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 601 | m_reexpressingInterestId = m_scheduler.schedule(m_syncInterestLifetime / 2 + |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 602 | time::milliseconds(m_reexpressionJitter(m_rng)), |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 603 | [this] { sendSyncInterest(); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 604 | |
| 605 | Interest interest(interestName); |
| 606 | interest.setMustBeFresh(true); |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 607 | interest.setCanBePrefix(true); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 608 | interest.setInterestLifetime(m_syncInterestLifetime); |
| 609 | |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 610 | m_pendingSyncInterest = m_face.expressInterest(interest, |
| 611 | bind(&Logic::onSyncData, this, _1, _2), |
Ashlesh Gawande | a1ad604 | 2019-10-07 15:56:11 -0500 | [diff] [blame] | 612 | bind(&Logic::onSyncNack, this, _1, _2), |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 613 | bind(&Logic::onSyncTimeout, this, _1)); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 614 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 615 | CHRONO_LOG_DBG("Send interest: " << interest.getName()); |
| 616 | CHRONO_LOG_DBG("<< Logic::sendSyncInterest"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | void |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 620 | Logic::trimState(State& partialState, const State& state, size_t nExcludedStates) |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 621 | { |
| 622 | partialState.reset(); |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 623 | |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 624 | std::vector<ConstLeafPtr> leaves; |
Davide Pesavento | d6a0956 | 2020-04-02 02:48:46 -0400 | [diff] [blame] | 625 | for (const auto& leaf : state.getLeaves()) { |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 626 | leaves.push_back(leaf); |
| 627 | } |
| 628 | |
| 629 | std::shuffle(leaves.begin(), leaves.end(), m_rng); |
| 630 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 631 | size_t statesToEncode = leaves.size() - std::min(leaves.size() - 1, nExcludedStates); |
Davide Pesavento | d6a0956 | 2020-04-02 02:48:46 -0400 | [diff] [blame] | 632 | for (const auto& leaf : leaves) { |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 633 | if (statesToEncode == 0) { |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 634 | break; |
| 635 | } |
Davide Pesavento | d6a0956 | 2020-04-02 02:48:46 -0400 | [diff] [blame] | 636 | partialState.update(leaf->getSessionName(), leaf->getSeq()); |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 637 | --statesToEncode; |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 641 | Data |
| 642 | Logic::encodeSyncReply(const Name& nodePrefix, const Name& name, const State& state) |
| 643 | { |
| 644 | Data syncReply(name); |
| 645 | syncReply.setFreshnessPeriod(m_syncReplyFreshness); |
| 646 | |
| 647 | auto finalizeReply = [this, &nodePrefix, &syncReply] (const State& state) { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 648 | auto contentBuffer = bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().data()), |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 649 | state.wireEncode().size()); |
| 650 | syncReply.setContent(contentBuffer); |
| 651 | |
| 652 | if (m_nodeList[nodePrefix].signingId.empty()) |
| 653 | m_keyChain.sign(syncReply); |
| 654 | else |
| 655 | m_keyChain.sign(syncReply, security::signingByIdentity(m_nodeList[nodePrefix].signingId)); |
| 656 | }; |
| 657 | |
| 658 | finalizeReply(state); |
| 659 | |
| 660 | size_t nExcludedStates = 1; |
| 661 | while (syncReply.wireEncode().size() > getMaxPacketLimit() - NDNLP_EXPECTED_OVERHEAD) { |
| 662 | if (nExcludedStates == 1) { |
| 663 | // To show this debug message only once |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 664 | NDN_LOG_DEBUG("Sync reply size exceeded maximum packet limit (" |
| 665 | << (getMaxPacketLimit() - NDNLP_EXPECTED_OVERHEAD) << ")"); |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 666 | } |
| 667 | State partialState; |
| 668 | trimState(partialState, state, nExcludedStates); |
| 669 | finalizeReply(partialState); |
| 670 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 671 | BOOST_ASSERT(!state.getLeaves().empty()); |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 672 | nExcludedStates *= 2; |
| 673 | } |
| 674 | |
| 675 | return syncReply; |
| 676 | } |
| 677 | |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 678 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 679 | Logic::sendSyncData(const Name& nodePrefix, const Name& name, const State& state) |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 680 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 681 | CHRONO_LOG_DBG(">> Logic::sendSyncData"); |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 682 | if (m_nodeList.find(nodePrefix) == m_nodeList.end()) |
| 683 | return; |
| 684 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 685 | m_face.put(encodeSyncReply(nodePrefix, name, state)); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 686 | |
| 687 | // checking if our own interest got satisfied |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 688 | if (m_pendingSyncInterestName == name) { |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 689 | // remove outstanding interest |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 690 | m_pendingSyncInterest.cancel(); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 691 | |
| 692 | // re-schedule sending Sync interest |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 693 | time::milliseconds after(m_reexpressionJitter(m_rng)); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 694 | CHRONO_LOG_DBG("Satisfy our own interest"); |
| 695 | CHRONO_LOG_DBG("Reschedule sync interest after " << after); |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 696 | m_reexpressingInterestId = m_scheduler.schedule(after, [this] { sendSyncInterest(); }); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 697 | } |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 698 | CHRONO_LOG_DBG("<< Logic::sendSyncData"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | void |
| 702 | Logic::cancelReset() |
| 703 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 704 | CHRONO_LOG_DBG(">> Logic::cancelReset"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 705 | if (!m_isInReset) |
| 706 | return; |
| 707 | |
| 708 | m_isInReset = false; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 709 | for (const auto& node : m_nodeList) { |
| 710 | updateSeqNo(node.second.seqNo, node.first); |
| 711 | } |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 712 | CHRONO_LOG_DBG("<< Logic::cancelReset"); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 716 | Logic::printDigest(const ConstBufferPtr& digest) const |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 717 | { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 718 | CHRONO_LOG_DBG("Hash: " << ndn::toHex(*digest, false)); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 721 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 722 | Logic::sendRecoveryInterest(ConstBufferPtr digest) |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 723 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 724 | CHRONO_LOG_DBG(">> Logic::sendRecoveryInterest"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 725 | |
| 726 | Name interestName; |
| 727 | interestName.append(m_syncPrefix) |
| 728 | .append(RECOVERY_COMPONENT) |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 729 | .append(name::Component(*digest)); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 730 | |
| 731 | Interest interest(interestName); |
| 732 | interest.setMustBeFresh(true); |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 733 | interest.setCanBePrefix(true); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 734 | interest.setInterestLifetime(m_recoveryInterestLifetime); |
| 735 | |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 736 | m_pendingRecoveryInterests[interestName[-1].toUri()] = m_face.expressInterest(interest, |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 737 | bind(&Logic::onRecoveryData, this, _1, _2), |
| 738 | bind(&Logic::onRecoveryTimeout, this, _1), // Nack |
| 739 | bind(&Logic::onRecoveryTimeout, this, _1)); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 740 | CHRONO_LOG_DBG("interest: " << interest.getName()); |
| 741 | CHRONO_LOG_DBG("<< Logic::sendRecoveryInterest"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void |
| 745 | Logic::processRecoveryInterest(const Interest& interest) |
| 746 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 747 | CHRONO_LOG_DBG(">> Logic::processRecoveryInterest"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 748 | |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 749 | const auto& name = interest.getName(); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 750 | ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size()); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 751 | ConstBufferPtr rootDigest = m_state.getRootDigest(); |
| 752 | |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 753 | auto stateIter = m_log.find(digest); |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 754 | if (stateIter != m_log.end() || *digest == EMPTY_DIGEST || *rootDigest == *digest) { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 755 | CHRONO_LOG_DBG("I can help you recover"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 756 | sendSyncData(m_defaultUserPrefix, name, m_state); |
| 757 | return; |
| 758 | } |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 759 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 760 | CHRONO_LOG_DBG("<< Logic::processRecoveryInterest"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 764 | Logic::onRecoveryData(const Interest& interest, const Data& data) |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 765 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 766 | CHRONO_LOG_DBG(">> Logic::onRecoveryData"); |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 767 | m_pendingRecoveryInterests.erase(interest.getName()[-1].toUri()); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 768 | onSyncDataValidated(data); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 769 | CHRONO_LOG_DBG("<< Logic::onRecoveryData"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void |
| 773 | Logic::onRecoveryTimeout(const Interest& interest) |
| 774 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 775 | CHRONO_LOG_DBG(">> Logic::onRecoveryTimeout"); |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 776 | m_pendingRecoveryInterests.erase(interest.getName()[-1].toUri()); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 777 | CHRONO_LOG_DBG("Interest: " << interest.getName()); |
| 778 | CHRONO_LOG_DBG("<< Logic::onRecoveryTimeout"); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 781 | } // namespace chronosync |