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