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