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