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