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