Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2020 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 | 4d3a2e0 | 2017-01-18 20:27:51 -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 | #ifndef CHRONOSYNC_LOGIC_HPP |
| 27 | #define CHRONOSYNC_LOGIC_HPP |
| 28 | |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 29 | #include "diff-state-container.hpp" |
| 30 | #include "interest-table.hpp" |
| 31 | |
Ashlesh Gawande | 9a306fe | 2019-01-04 11:38:18 -0600 | [diff] [blame] | 32 | #include <ndn-cxx/util/random.hpp> |
| 33 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 34 | #include <boost/archive/iterators/dataflow_exception.hpp> |
| 35 | #include <boost/archive/iterators/transform_width.hpp> |
| 36 | #include <boost/assert.hpp> |
| 37 | #include <boost/iterator/transform_iterator.hpp> |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 38 | #include <boost/throw_exception.hpp> |
| 39 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 40 | #include <memory> |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 41 | #include <unordered_map> |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 42 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 43 | namespace chronosync { |
| 44 | |
| 45 | /** |
| 46 | * @brief The missing sequence numbers for a session |
| 47 | * |
| 48 | * This class is used to notify the clients of Logic |
| 49 | * the details of state changes. |
| 50 | * |
| 51 | * Instances of this class is usually used as elements of some containers |
| 52 | * such as std::vector, thus it is copyable. |
| 53 | */ |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 54 | class NodeInfo |
| 55 | { |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 56 | public: |
| 57 | Name userPrefix; |
| 58 | Name signingId; |
| 59 | Name sessionName; |
| 60 | SeqNo seqNo; |
| 61 | }; |
| 62 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 63 | class MissingDataInfo |
| 64 | { |
| 65 | public: |
| 66 | /// @brief session name |
| 67 | Name session; |
| 68 | /// @brief the lowest one of missing sequence numbers |
| 69 | SeqNo low; |
| 70 | /// @brief the highest one of missing sequence numbers |
| 71 | SeqNo high; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * @brief The callback function to handle state updates |
| 76 | * |
| 77 | * The parameter is a set of MissingDataInfo, of which each corresponds to |
| 78 | * a session that has changed its state. |
| 79 | */ |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 80 | using UpdateCallback = function<void(const std::vector<MissingDataInfo>&)>; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * @brief Logic of ChronoSync |
| 84 | */ |
| 85 | class Logic : noncopyable |
| 86 | { |
| 87 | public: |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 88 | class Error : public std::runtime_error |
| 89 | { |
| 90 | public: |
| 91 | explicit |
| 92 | Error(const std::string& what) |
| 93 | : std::runtime_error(what) |
| 94 | { |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | public: |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 99 | static const time::steady_clock::Duration DEFAULT_RESET_TIMER; |
| 100 | static const time::steady_clock::Duration DEFAULT_CANCEL_RESET_TIMER; |
| 101 | static const time::milliseconds DEFAULT_RESET_INTEREST_LIFETIME; |
| 102 | static const time::milliseconds DEFAULT_SYNC_INTEREST_LIFETIME; |
| 103 | static const time::milliseconds DEFAULT_SYNC_REPLY_FRESHNESS; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 104 | static const time::milliseconds DEFAULT_RECOVERY_INTEREST_LIFETIME; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * @brief Constructor |
| 108 | * |
Yingdi Yu | 9d5679a | 2015-02-01 00:17:58 -0800 | [diff] [blame] | 109 | * @param face The face used to communication, will be shutdown in destructor |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 110 | * @param syncPrefix The prefix of the sync group |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 111 | * @param defaultUserPrefix The prefix of the first user added to this session |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 112 | * @param onUpdate The callback function to handle state updates |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 113 | * @param defaultSigningId The signing Id of the default user |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 114 | * @param validator The validator for packet validation |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 115 | * @param resetTimer The timer to periodically send Reset Interest |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 116 | * @param cancelResetTimer The timer to exit from Reset state |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 117 | * @param resetInterestLifetime The lifetime of Reset Interest |
| 118 | * @param syncInterestLifetime The lifetime of sync interest |
| 119 | * @param syncReplyFreshness The FreshnessPeriod of sync reply |
| 120 | * @param recoveryInterestLifetime The lifetime of recovery interest |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 121 | * @param session Manually defined session ID |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 122 | */ |
| 123 | Logic(ndn::Face& face, |
| 124 | const Name& syncPrefix, |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 125 | const Name& defaultUserPrefix, |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 126 | const UpdateCallback& onUpdate, |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 127 | const Name& defaultSigningId = DEFAULT_NAME, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 128 | std::shared_ptr<Validator> validator = DEFAULT_VALIDATOR, |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 129 | const time::steady_clock::Duration& resetTimer = DEFAULT_RESET_TIMER, |
| 130 | const time::steady_clock::Duration& cancelResetTimer = DEFAULT_CANCEL_RESET_TIMER, |
| 131 | const time::milliseconds& resetInterestLifetime = DEFAULT_RESET_INTEREST_LIFETIME, |
| 132 | const time::milliseconds& syncInterestLifetime = DEFAULT_SYNC_INTEREST_LIFETIME, |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 133 | const time::milliseconds& syncReplyFreshness = DEFAULT_SYNC_REPLY_FRESHNESS, |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 134 | const time::milliseconds& recoveryInterestLifetime = DEFAULT_RECOVERY_INTEREST_LIFETIME, |
Ashlesh Gawande | db862e0 | 2018-03-30 17:15:08 -0500 | [diff] [blame] | 135 | const name::Component& session = name::Component()); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 136 | |
| 137 | ~Logic(); |
| 138 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 139 | /** |
| 140 | * @brief Reset the sync tree (and restart synchronization again) |
| 141 | * |
| 142 | * @param isOnInterest a flag that tells whether the reset is called by reset interest. |
| 143 | */ |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 144 | void |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 145 | reset(bool isOnInterest = false); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * @brief Set user prefix |
| 149 | * |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 150 | * This method will also change the default user and signing Id of that user. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 151 | * |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 152 | * @param defaultUserPrefix The prefix of user. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 153 | */ |
| 154 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 155 | setDefaultUserPrefix(const Name& defaultUserPrefix); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 156 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 157 | /// @brief Get the name of default user. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 158 | const Name& |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 159 | getDefaultUserPrefix() const |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 160 | { |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 161 | return m_defaultUserPrefix; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 164 | /** |
| 165 | * @brief Add user node into the local session. |
| 166 | * |
| 167 | * This method also reset after adding |
| 168 | * |
| 169 | * @param userPrefix prefix of the added node |
| 170 | * @param signingId signing Id of the added node |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 171 | * @param session manually defined session ID |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 172 | * @param shouldSendReset enable/disable sending the reset interest |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 173 | */ |
| 174 | void |
Ashlesh Gawande | db862e0 | 2018-03-30 17:15:08 -0500 | [diff] [blame] | 175 | addUserNode(const Name& userPrefix, const Name& signingId = DEFAULT_NAME, |
Davide Pesavento | d7de2d4 | 2019-08-01 20:55:50 -0400 | [diff] [blame] | 176 | const name::Component& session = name::Component(), bool shouldSendReset = true); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 177 | |
| 178 | /// @brief remove the node from the local session |
| 179 | void |
| 180 | removeUserNode(const Name& userPrefix); |
| 181 | |
| 182 | /** |
| 183 | * @brief Get the name of the local session. |
| 184 | * |
| 185 | * This method gets the session name according to prefix, if prefix is not specified, |
| 186 | * it returns the session name of default user. |
| 187 | * |
| 188 | * @param prefix prefix of the node |
| 189 | */ |
| 190 | const Name& |
| 191 | getSessionName(Name prefix = EMPTY_NAME); |
| 192 | |
| 193 | /** |
| 194 | * @brief Get current seqNo of the local session. |
| 195 | * |
| 196 | * This method gets the seqNo according to prefix, if prefix is not specified, |
| 197 | * it returns the seqNo of default user. |
| 198 | * |
| 199 | * @param prefix prefix of the node |
| 200 | */ |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 201 | const SeqNo& |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 202 | getSeqNo(Name prefix = EMPTY_NAME); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * @brief Update the seqNo of the local session |
| 206 | * |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 207 | * The method updates the existing seqNo with the supplied seqNo and prefix. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 208 | * |
| 209 | * @param seq The new seqNo. |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 210 | * @param updatePrefix The prefix of node to update. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 211 | */ |
| 212 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 213 | updateSeqNo(const SeqNo& seq, const Name& updatePrefix = EMPTY_NAME); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 214 | |
| 215 | /// @brief Get root digest of current sync tree |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 216 | ConstBufferPtr |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 217 | getRootDigest() const; |
| 218 | |
| 219 | /// @brief Get the name of all sessions |
| 220 | std::set<Name> |
| 221 | getSessionNames() const; |
| 222 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 223 | CHRONOSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 224 | void |
| 225 | printState(std::ostream& os) const; |
| 226 | |
| 227 | ndn::Scheduler& |
| 228 | getScheduler() |
| 229 | { |
| 230 | return m_scheduler; |
| 231 | } |
| 232 | |
| 233 | State& |
| 234 | getState() |
| 235 | { |
| 236 | return m_state; |
| 237 | } |
| 238 | |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 239 | /// Create a subset @p partialState excluding @p nExcludedStates from @p state |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 240 | void |
Alexander Afanasyev | 6ee98ff | 2018-02-13 19:12:28 -0500 | [diff] [blame] | 241 | trimState(State& partialState, const State& state, size_t excludedStates); |
| 242 | |
| 243 | Data |
| 244 | encodeSyncReply(const Name& nodePrefix, const Name& name, const State& state); |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 245 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 246 | private: |
| 247 | /** |
| 248 | * @brief Callback to handle Sync Interest |
| 249 | * |
| 250 | * This method checks whether an incoming interest is a normal one or a reset |
| 251 | * and dispatches the incoming interest to corresponding processing methods. |
| 252 | * |
| 253 | * @param prefix The prefix of the sync group. |
| 254 | * @param interest The incoming sync interest. |
| 255 | */ |
| 256 | void |
| 257 | onSyncInterest(const Name& prefix, const Interest& interest); |
| 258 | |
| 259 | /** |
| 260 | * @brief Callback to handle Sync prefix registration failure |
| 261 | * |
| 262 | * This method does nothing for now. |
| 263 | * |
| 264 | * @param prefix The prefix of the sync group. |
| 265 | * @param msg The error message. |
| 266 | */ |
| 267 | void |
| 268 | onSyncRegisterFailed(const Name& prefix, const std::string& msg); |
| 269 | |
| 270 | /** |
| 271 | * @brief Callback to handle Sync Reply |
| 272 | * |
| 273 | * This method calls validator to validate Sync Reply. |
| 274 | * For now, validation is disabled, Logic::onSyncDataValidated is called |
| 275 | * directly. |
| 276 | * |
| 277 | * @param interest The Sync Interest |
| 278 | * @param data The reply to the Sync Interest |
| 279 | */ |
| 280 | void |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 281 | onSyncData(const Interest& interest, const Data& data); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 282 | |
| 283 | /** |
| 284 | * @brief Callback to handle reply to Reset Interest. |
| 285 | * |
| 286 | * This method does nothing, since reply to Reset Interest is not useful for now. |
| 287 | * |
| 288 | * @param interest The Reset Interest |
| 289 | * @param data The reply to the Reset Interest |
| 290 | */ |
| 291 | void |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 292 | onResetData(const Interest& interest, const Data& data); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
Ashlesh Gawande | a1ad604 | 2019-10-07 15:56:11 -0500 | [diff] [blame] | 295 | * @brief Callback to handle Sync Interest Nack |
| 296 | * |
| 297 | * This method checks whether the Nack is of type NoRoute |
| 298 | * and schedules a sync interest in m_reexpressionJitter |
| 299 | * |
| 300 | * @param interest The sync interest for which the Nack happened |
| 301 | * @param nack The incoming Nack |
| 302 | */ |
| 303 | void |
| 304 | onSyncNack(const Interest& interest, const ndn::lp::Nack& nack); |
| 305 | |
| 306 | /** |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 307 | * @brief Callback to handle Sync Interest timeout. |
| 308 | * |
| 309 | * This method does nothing, since Logic per se handles timeout explicitly. |
| 310 | * |
| 311 | * @param interest The Sync Interest |
| 312 | */ |
| 313 | void |
| 314 | onSyncTimeout(const Interest& interest); |
| 315 | |
| 316 | /** |
| 317 | * @brief Callback to invalid Sync Reply. |
| 318 | * |
| 319 | * This method does nothing but drops the invalid reply. |
| 320 | * |
| 321 | * @param data The invalid Sync Reply |
| 322 | */ |
| 323 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 324 | onSyncDataValidationFailed(const Data& data); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * @brief Callback to valid Sync Reply. |
| 328 | * |
| 329 | * This method simply passes the valid reply to processSyncData. |
| 330 | * |
| 331 | * @param data The valid Sync Reply. |
| 332 | */ |
| 333 | void |
Ashlesh Gawande | 1d1092d | 2018-08-03 14:36:49 -0500 | [diff] [blame] | 334 | onSyncDataValidated(const Data& data); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * @brief Process normal Sync Interest |
| 338 | * |
| 339 | * This method extracts the digest from the incoming Sync Interest, |
| 340 | * compares it against current local digest, and process the Sync |
| 341 | * Interest according to the comparison result. See docs/design.rst |
| 342 | * for more details. |
| 343 | * |
| 344 | * @param interest The incoming interest |
| 345 | * @param isTimedProcessing True if the interest needs an immediate reply, |
| 346 | * otherwise hold the interest for a while before |
| 347 | * making a reply (to avoid unnecessary recovery) |
| 348 | */ |
| 349 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 350 | processSyncInterest(const Interest& interest, bool isTimedProcessing = false); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * @brief Process reset Sync Interest |
| 354 | * |
| 355 | * This method simply call Logic::reset() |
| 356 | * |
| 357 | * @param interest The incoming interest. |
| 358 | */ |
| 359 | void |
| 360 | processResetInterest(const Interest& interest); |
| 361 | |
| 362 | /** |
| 363 | * @brief Process Sync Reply. |
| 364 | * |
| 365 | * This method extracts state update information from Sync Reply and applies |
| 366 | * it to the Sync Tree and re-express Sync Interest. |
| 367 | * |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 368 | * @param name The data name of the Sync Reply. |
| 369 | * @param digest The digest in the data name. |
| 370 | * @param syncReply The content of the Sync Reply. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 371 | */ |
| 372 | void |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 373 | processSyncData(const Name& name, ConstBufferPtr digest, const Block& syncReply); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * @brief Insert state diff into log |
| 377 | * |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 378 | * @param diff The diff. |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 379 | * @param previousRoot The root digest before state changes. |
| 380 | */ |
| 381 | void |
Davide Pesavento | 2741644 | 2020-01-23 23:10:24 -0500 | [diff] [blame] | 382 | insertToDiffLog(DiffStatePtr diff, ConstBufferPtr previousRoot); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 383 | |
| 384 | /** |
| 385 | * @brief Reply to all pending Sync Interests with a particular commit (or diff) |
| 386 | * |
| 387 | * @param commit The diff. |
| 388 | */ |
| 389 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 390 | satisfyPendingSyncInterests(const Name& updatedPrefix, ConstDiffStatePtr commit); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 391 | |
| 392 | /// @brief Helper method to send normal Sync Interest |
| 393 | void |
| 394 | sendSyncInterest(); |
| 395 | |
| 396 | /// @brief Helper method to send reset Sync Interest |
| 397 | void |
| 398 | sendResetInterest(); |
| 399 | |
| 400 | /// @brief Helper method to send Sync Reply |
| 401 | void |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 402 | sendSyncData(const Name& nodePrefix, const Name& name, const State& state); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * @brief Unset reset status |
| 406 | * |
| 407 | * By invoking this method, one can add its own state into the Sync Tree, thus |
| 408 | * jumping out of the reset status |
| 409 | */ |
| 410 | void |
| 411 | cancelReset(); |
| 412 | |
| 413 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 414 | printDigest(ConstBufferPtr digest); |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 415 | |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 416 | /** |
| 417 | * @brief Helper method to send Recovery Interest |
| 418 | * |
| 419 | * @param digest The digest to be included in the recovery interest |
| 420 | */ |
| 421 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 422 | sendRecoveryInterest(ConstBufferPtr digest); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * @brief Process Recovery Interest |
| 426 | * |
| 427 | * This method extracts the digest from the incoming Recovery Interest. |
| 428 | * If it recognizes this incoming digest, then it sends its full state |
| 429 | * as reply. |
| 430 | * |
| 431 | * @param interest The incoming interest |
| 432 | */ |
| 433 | void |
| 434 | processRecoveryInterest(const Interest& interest); |
| 435 | |
| 436 | /** |
| 437 | * @brief Callback to handle Recovery Reply |
| 438 | * |
| 439 | * This method calls Logic::onSyncDataValidated directly. |
| 440 | * |
| 441 | * @param interest The Recovery Interest |
| 442 | * @param data The reply to the Recovery Interest |
| 443 | */ |
| 444 | void |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 445 | onRecoveryData(const Interest& interest, const Data& data); |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 446 | |
| 447 | /** |
| 448 | * @brief Callback to handle Recovery Interest timeout. |
| 449 | * |
| 450 | * This method does nothing. |
| 451 | * |
| 452 | * @param interest The Recovery Interest |
| 453 | */ |
| 454 | void |
| 455 | onRecoveryTimeout(const Interest& interest); |
| 456 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 457 | public: |
| 458 | static const ndn::Name DEFAULT_NAME; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 459 | static const ndn::Name EMPTY_NAME; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 460 | static const std::shared_ptr<Validator> DEFAULT_VALIDATOR; |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 461 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 462 | private: |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 463 | using NodeList = std::unordered_map<ndn::Name, NodeInfo>; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 464 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 465 | static const ConstBufferPtr EMPTY_DIGEST; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 466 | static const ndn::name::Component RESET_COMPONENT; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 467 | static const ndn::name::Component RECOVERY_COMPONENT; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 468 | |
| 469 | // Communication |
| 470 | ndn::Face& m_face; |
| 471 | Name m_syncPrefix; |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 472 | ndn::ScopedRegisteredPrefixHandle m_syncRegisteredPrefix; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 473 | Name m_syncReset; |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 474 | Name m_defaultUserPrefix; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 475 | |
| 476 | // State |
Qiuhan Ding | 8c095fd | 2014-11-19 17:38:32 -0800 | [diff] [blame] | 477 | NodeList m_nodeList; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 478 | State m_state; |
| 479 | DiffStateContainer m_log; |
| 480 | InterestTable m_interestTable; |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 481 | Name m_pendingSyncInterestName; |
| 482 | ndn::ScopedPendingInterestHandle m_pendingSyncInterest; |
| 483 | ndn::ScopedPendingInterestHandle m_pendingResetInterest; |
| 484 | std::map<std::string, ndn::ScopedPendingInterestHandle> m_pendingRecoveryInterests; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 485 | bool m_isInReset; |
| 486 | bool m_needPeriodReset; |
| 487 | |
| 488 | // Callback |
| 489 | UpdateCallback m_onUpdate; |
| 490 | |
| 491 | // Event |
Davide Pesavento | 0d837e3 | 2019-03-16 22:31:14 -0400 | [diff] [blame] | 492 | ndn::Scheduler m_scheduler; |
| 493 | ndn::scheduler::ScopedEventId m_delayedInterestProcessingId; |
| 494 | ndn::scheduler::ScopedEventId m_reexpressingInterestId; |
| 495 | ndn::scheduler::ScopedEventId m_resetInterestId; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 496 | |
| 497 | // Timer |
Ashlesh Gawande | 9a306fe | 2019-01-04 11:38:18 -0600 | [diff] [blame] | 498 | ndn::random::RandomNumberEngine& m_rng; |
Ashlesh Gawande | 4a9ecd5 | 2018-02-06 14:36:19 -0600 | [diff] [blame] | 499 | std::uniform_int_distribution<> m_rangeUniformRandom; |
| 500 | std::uniform_int_distribution<> m_reexpressionJitter; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 501 | /// @brief Timer to send next reset 0 for no reset |
| 502 | time::steady_clock::Duration m_resetTimer; |
| 503 | /// @brief Timer to cancel reset state |
| 504 | time::steady_clock::Duration m_cancelResetTimer; |
| 505 | /// @brief Lifetime of reset interest |
| 506 | time::milliseconds m_resetInterestLifetime; |
| 507 | /// @brief Lifetime of sync interest |
| 508 | time::milliseconds m_syncInterestLifetime; |
| 509 | /// @brief FreshnessPeriod of SyncReply |
| 510 | time::milliseconds m_syncReplyFreshness; |
Sonu Mishra | 4d3a2e0 | 2017-01-18 20:27:51 -0800 | [diff] [blame] | 511 | /// @brief Lifetime of recovery interest |
| 512 | time::milliseconds m_recoveryInterestLifetime; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 513 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 514 | // Security |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 515 | ndn::KeyChain m_keyChain; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 516 | std::shared_ptr<Validator> m_validator; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 517 | |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 518 | int m_instanceId; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 519 | static int s_instanceCounter; |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 520 | }; |
| 521 | |
Alexander Afanasyev | 8903629 | 2018-02-13 17:19:50 -0500 | [diff] [blame] | 522 | #ifdef CHRONOSYNC_HAVE_TESTS |
| 523 | size_t |
| 524 | getMaxPacketLimit(); |
| 525 | #endif // CHRONOSYNC_HAVE_TESTS |
Yingdi Yu | f7ede41 | 2014-08-30 20:37:52 -0700 | [diff] [blame] | 526 | |
| 527 | } // namespace chronosync |
| 528 | |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 529 | #endif // CHRONOSYNC_LOGIC_HPP |