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