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