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