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