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