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