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