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