Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 44ec207 | 2022-03-07 13:32:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2022 University of California, Los Angeles |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -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> |
| 23 | */ |
| 24 | |
| 25 | #include "socket.hpp" |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/util/logger.hpp> |
| 28 | |
| 29 | NDN_LOG_INIT(sync.Socket); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 31 | namespace chronosync { |
| 32 | |
| 33 | Socket::Socket(const Name& syncPrefix, |
| 34 | const Name& userPrefix, |
| 35 | ndn::Face& face, |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 36 | const UpdateCallback& updateCallback, |
| 37 | const Name& signingId, |
Ashlesh Gawande | aae853c | 2018-02-06 17:28:50 -0600 | [diff] [blame] | 38 | std::shared_ptr<Validator> validator, |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 39 | const time::milliseconds& syncInterestLifetime, |
| 40 | const name::Component& session) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 41 | : m_userPrefix(userPrefix) |
| 42 | , m_face(face) |
Ashlesh Gawande | aae853c | 2018-02-06 17:28:50 -0600 | [diff] [blame] | 43 | , m_logic(face, syncPrefix, userPrefix, updateCallback, Logic::DEFAULT_NAME, Logic::DEFAULT_VALIDATOR, |
| 44 | Logic::DEFAULT_RESET_TIMER, Logic::DEFAULT_CANCEL_RESET_TIMER, Logic::DEFAULT_RESET_INTEREST_LIFETIME, |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 45 | syncInterestLifetime, Logic::DEFAULT_SYNC_REPLY_FRESHNESS, Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME, |
| 46 | session) |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 47 | , m_signingId(signingId) |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame^] | 48 | , m_validator(std::move(validator)) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 90587b8 | 2018-02-11 20:36:53 -0500 | [diff] [blame] | 50 | NDN_LOG_DEBUG(">> Socket::Socket"); |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame^] | 51 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 52 | if (m_userPrefix != DEFAULT_NAME) |
| 53 | m_registeredPrefixList[m_userPrefix] = |
| 54 | m_face.setInterestFilter(m_userPrefix, |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 55 | [this] (auto&&... args) { onInterest(std::forward<decltype(args)>(args)...); }, |
| 56 | [] (auto&&...) {}); |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame^] | 57 | |
Alexander Afanasyev | 90587b8 | 2018-02-11 20:36:53 -0500 | [diff] [blame] | 58 | NDN_LOG_DEBUG("<< Socket::Socket"); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 61 | Socket::~Socket() |
| 62 | { |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 63 | for (auto& itr : m_registeredPrefixList) { |
| 64 | itr.second.unregister(); |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 65 | } |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 68 | void |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 69 | Socket::addSyncNode(const Name& prefix, const Name& signingId, const name::Component& session) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 70 | { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 71 | if (prefix == DEFAULT_NAME) |
| 72 | return; |
| 73 | |
Alexander Afanasyev | 90587b8 | 2018-02-11 20:36:53 -0500 | [diff] [blame] | 74 | NDN_LOG_DEBUG(">> addSyncNode"); |
| 75 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 76 | auto itr = m_registeredPrefixList.find(prefix); |
| 77 | if (itr != m_registeredPrefixList.end()) |
| 78 | return; |
| 79 | |
| 80 | if (m_userPrefix == DEFAULT_NAME) |
| 81 | m_userPrefix = prefix; |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 82 | m_logic.addUserNode(prefix, signingId, session); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 83 | m_registeredPrefixList[prefix] = |
| 84 | m_face.setInterestFilter(prefix, |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 85 | [this] (auto&&... args) { onInterest(std::forward<decltype(args)>(args)...); }, |
| 86 | [] (auto&&...) {}); |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 87 | |
Alexander Afanasyev | 90587b8 | 2018-02-11 20:36:53 -0500 | [diff] [blame] | 88 | NDN_LOG_DEBUG("<< addSyncNode"); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 92 | Socket::removeSyncNode(const Name& prefix) |
| 93 | { |
| 94 | if (prefix == DEFAULT_NAME) |
| 95 | return; |
| 96 | |
| 97 | auto itr = m_registeredPrefixList.find(prefix); |
| 98 | if (itr != m_registeredPrefixList.end()) { |
Junxiao Shi | 8e4e76d | 2019-02-08 15:25:08 -0700 | [diff] [blame] | 99 | itr->second.unregister(); |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 100 | m_registeredPrefixList.erase(itr); |
| 101 | } |
| 102 | |
| 103 | m_ims.erase(prefix); |
| 104 | m_logic.removeUserNode(prefix); |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 108 | Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness, |
| 109 | const Name& prefix) |
| 110 | { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 111 | publishData(ndn::makeBinaryBlock(ndn::tlv::Content, {buf, len}), freshness, prefix); |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 115 | Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness, |
| 116 | const uint64_t& seqNo, const Name& prefix) |
| 117 | { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 118 | publishData(ndn::makeBinaryBlock(ndn::tlv::Content, {buf, len}), freshness, seqNo, prefix); |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 122 | Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 123 | const Name& prefix) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 124 | { |
Davide Pesavento | 44ec207 | 2022-03-07 13:32:04 -0500 | [diff] [blame] | 125 | auto data = std::make_shared<Data>(); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 126 | data->setContent(content); |
| 127 | data->setFreshnessPeriod(freshness); |
| 128 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 129 | SeqNo newSeq = m_logic.getSeqNo(prefix) + 1; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 130 | Name dataName; |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 131 | dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 132 | data->setName(dataName); |
| 133 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 134 | if (m_signingId.empty()) |
| 135 | m_keyChain.sign(*data); |
| 136 | else |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 137 | m_keyChain.sign(*data, security::signingByIdentity(m_signingId)); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 138 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 139 | m_ims.insert(*data); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 140 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 141 | m_logic.updateSeqNo(newSeq, prefix); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 145 | Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 146 | const uint64_t& seqNo, const Name& prefix) |
| 147 | { |
Davide Pesavento | 44ec207 | 2022-03-07 13:32:04 -0500 | [diff] [blame] | 148 | auto data = std::make_shared<Data>(); |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 149 | data->setContent(content); |
| 150 | data->setFreshnessPeriod(freshness); |
| 151 | |
| 152 | SeqNo newSeq = seqNo; |
| 153 | Name dataName; |
| 154 | dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq); |
| 155 | data->setName(dataName); |
| 156 | |
| 157 | if (m_signingId.empty()) |
| 158 | m_keyChain.sign(*data); |
| 159 | else |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 160 | m_keyChain.sign(*data, security::signingByIdentity(m_signingId)); |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 161 | |
| 162 | m_ims.insert(*data); |
| 163 | |
| 164 | m_logic.updateSeqNo(newSeq, prefix); |
| 165 | } |
| 166 | |
| 167 | void |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 168 | Socket::fetchData(const Name& sessionName, const SeqNo& seqNo, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 169 | const DataValidatedCallback& dataCallback, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 170 | int nRetries) |
| 171 | { |
| 172 | Name interestName; |
| 173 | interestName.append(sessionName).appendNumber(seqNo); |
| 174 | |
| 175 | Interest interest(interestName); |
| 176 | interest.setMustBeFresh(true); |
| 177 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 178 | DataValidationErrorCallback failureCallback = |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 179 | [this] (auto&&... args) { onDataValidationFailed(std::forward<decltype(args)>(args)...); }; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 180 | |
| 181 | m_face.expressInterest(interest, |
| 182 | bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback), |
| 183 | bind(&Socket::onDataTimeout, this, _1, nRetries, |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 184 | dataCallback, failureCallback), // Nack |
| 185 | bind(&Socket::onDataTimeout, this, _1, nRetries, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 186 | dataCallback, failureCallback)); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | Socket::fetchData(const Name& sessionName, const SeqNo& seqNo, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 191 | const DataValidatedCallback& dataCallback, |
| 192 | const DataValidationErrorCallback& failureCallback, |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 193 | const ndn::TimeoutCallback& onTimeout, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 194 | int nRetries) |
| 195 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 196 | NDN_LOG_DEBUG(">> Socket::fetchData"); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 197 | Name interestName; |
| 198 | interestName.append(sessionName).appendNumber(seqNo); |
| 199 | |
| 200 | Interest interest(interestName); |
| 201 | interest.setMustBeFresh(true); |
| 202 | |
| 203 | m_face.expressInterest(interest, |
| 204 | bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback), |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 205 | bind(onTimeout, _1), // Nack |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 206 | onTimeout); |
| 207 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 208 | NDN_LOG_DEBUG("<< Socket::fetchData"); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 212 | Socket::onInterest(const Name&, const Interest& interest) |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 213 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 214 | auto data = m_ims.find(interest); |
Alexander Afanasyev | 90587b8 | 2018-02-11 20:36:53 -0500 | [diff] [blame] | 215 | if (data != nullptr) { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 216 | m_face.put(*data); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 221 | Socket::onData(const Interest&, const Data& data, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 222 | const DataValidatedCallback& onValidated, |
| 223 | const DataValidationErrorCallback& onFailed) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 224 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 225 | NDN_LOG_DEBUG("Socket::onData"); |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 226 | |
| 227 | if (static_cast<bool>(m_validator)) |
| 228 | m_validator->validate(data, onValidated, onFailed); |
| 229 | else |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 230 | onValidated(data); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
| 234 | Socket::onDataTimeout(const Interest& interest, int nRetries, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 235 | const DataValidatedCallback& onValidated, |
| 236 | const DataValidationErrorCallback& onFailed) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 237 | { |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 238 | NDN_LOG_DEBUG("Socket::onDataTimeout"); |
| 239 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 240 | if (nRetries <= 0) |
| 241 | return; |
| 242 | |
Ashlesh Gawande | 8dfa63c | 2017-07-31 17:01:00 -0500 | [diff] [blame] | 243 | Interest newNonceInterest(interest); |
| 244 | newNonceInterest.refreshNonce(); |
| 245 | |
| 246 | m_face.expressInterest(newNonceInterest, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 247 | bind(&Socket::onData, this, _1, _2, onValidated, onFailed), |
| 248 | bind(&Socket::onDataTimeout, this, _1, nRetries - 1, |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 249 | onValidated, onFailed), // Nack |
| 250 | bind(&Socket::onDataTimeout, this, _1, nRetries - 1, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 251 | onValidated, onFailed)); |
| 252 | } |
| 253 | |
| 254 | void |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 255 | Socket::onDataValidationFailed(const Data&, const ValidationError&) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 256 | { |
| 257 | } |
| 258 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 259 | ConstBufferPtr |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 260 | Socket::getRootDigest() const |
| 261 | { |
| 262 | return m_logic.getRootDigest(); |
| 263 | } |
| 264 | |
| 265 | } // namespace chronosync |