blob: e2c8b66d3f2e4ef8b2054860e4cf8cab5b4ec7f4 [file] [log] [blame]
Yingdi Yu31ad44c2014-08-28 14:55:42 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento44ec2072022-03-07 13:32:04 -05003 * Copyright (c) 2012-2022 University of California, Los Angeles
Yingdi Yu31ad44c2014-08-28 14:55:42 -07004 *
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 Yu31ad44c2014-08-28 14:55:42 -070026
Davide Pesavento07684bc2021-02-07 20:09:28 -050027#include <ndn-cxx/util/logger.hpp>
28
29NDN_LOG_INIT(sync.Socket);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070030
Yingdi Yu31ad44c2014-08-28 14:55:42 -070031namespace chronosync {
32
Yingdi Yucd339022014-11-05 17:51:19 -080033const ndn::Name Socket::DEFAULT_NAME;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080034const ndn::Name Socket::DEFAULT_PREFIX;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050035const std::shared_ptr<Validator> Socket::DEFAULT_VALIDATOR;
Yingdi Yucd339022014-11-05 17:51:19 -080036
Yingdi Yu31ad44c2014-08-28 14:55:42 -070037Socket::Socket(const Name& syncPrefix,
38 const Name& userPrefix,
39 ndn::Face& face,
Yingdi Yucd339022014-11-05 17:51:19 -080040 const UpdateCallback& updateCallback,
41 const Name& signingId,
Ashlesh Gawandeaae853c2018-02-06 17:28:50 -060042 std::shared_ptr<Validator> validator,
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050043 const time::milliseconds& syncInterestLifetime,
44 const name::Component& session)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070045 : m_userPrefix(userPrefix)
46 , m_face(face)
Ashlesh Gawandeaae853c2018-02-06 17:28:50 -060047 , m_logic(face, syncPrefix, userPrefix, updateCallback, Logic::DEFAULT_NAME, Logic::DEFAULT_VALIDATOR,
48 Logic::DEFAULT_RESET_TIMER, Logic::DEFAULT_CANCEL_RESET_TIMER, Logic::DEFAULT_RESET_INTEREST_LIFETIME,
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050049 syncInterestLifetime, Logic::DEFAULT_SYNC_REPLY_FRESHNESS, Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME,
50 session)
Yingdi Yucd339022014-11-05 17:51:19 -080051 , m_signingId(signingId)
52 , m_validator(validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070053{
Alexander Afanasyev90587b82018-02-11 20:36:53 -050054 NDN_LOG_DEBUG(">> Socket::Socket");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080055 if (m_userPrefix != DEFAULT_NAME)
56 m_registeredPrefixList[m_userPrefix] =
57 m_face.setInterestFilter(m_userPrefix,
Davide Pesavento5f5101a2022-03-11 20:05:03 -050058 [this] (auto&&... args) { onInterest(std::forward<decltype(args)>(args)...); },
59 [] (auto&&...) {});
Alexander Afanasyev90587b82018-02-11 20:36:53 -050060 NDN_LOG_DEBUG("<< Socket::Socket");
Yingdi Yu31ad44c2014-08-28 14:55:42 -070061}
62
Qiuhan Ding03b9de32015-01-30 14:03:12 -080063Socket::~Socket()
64{
Junxiao Shi8e4e76d2019-02-08 15:25:08 -070065 for (auto& itr : m_registeredPrefixList) {
66 itr.second.unregister();
Qiuhan Ding03b9de32015-01-30 14:03:12 -080067 }
Qiuhan Ding03b9de32015-01-30 14:03:12 -080068}
69
Yingdi Yu31ad44c2014-08-28 14:55:42 -070070void
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050071Socket::addSyncNode(const Name& prefix, const Name& signingId, const name::Component& session)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070072{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080073 if (prefix == DEFAULT_NAME)
74 return;
75
Alexander Afanasyev90587b82018-02-11 20:36:53 -050076 NDN_LOG_DEBUG(">> addSyncNode");
77
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080078 auto itr = m_registeredPrefixList.find(prefix);
79 if (itr != m_registeredPrefixList.end())
80 return;
81
82 if (m_userPrefix == DEFAULT_NAME)
83 m_userPrefix = prefix;
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050084 m_logic.addUserNode(prefix, signingId, session);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080085 m_registeredPrefixList[prefix] =
86 m_face.setInterestFilter(prefix,
Davide Pesavento5f5101a2022-03-11 20:05:03 -050087 [this] (auto&&... args) { onInterest(std::forward<decltype(args)>(args)...); },
88 [] (auto&&...) {});
Davide Pesavento07684bc2021-02-07 20:09:28 -050089
Alexander Afanasyev90587b82018-02-11 20:36:53 -050090 NDN_LOG_DEBUG("<< addSyncNode");
Yingdi Yu31ad44c2014-08-28 14:55:42 -070091}
92
93void
Qiuhan Ding03b9de32015-01-30 14:03:12 -080094Socket::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()) {
Junxiao Shi8e4e76d2019-02-08 15:25:08 -0700101 itr->second.unregister();
Qiuhan Ding03b9de32015-01-30 14:03:12 -0800102 m_registeredPrefixList.erase(itr);
103 }
104
105 m_ims.erase(prefix);
106 m_logic.removeUserNode(prefix);
Qiuhan Ding03b9de32015-01-30 14:03:12 -0800107}
108
109void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800110Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
111 const Name& prefix)
112{
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500113 publishData(ndn::makeBinaryBlock(ndn::tlv::Content, {buf, len}), freshness, prefix);
Qiuhan Dinge246b622014-12-03 21:57:48 -0800114}
115
116void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500117Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
118 const uint64_t& seqNo, const Name& prefix)
119{
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500120 publishData(ndn::makeBinaryBlock(ndn::tlv::Content, {buf, len}), freshness, seqNo, prefix);
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500121}
122
123void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800124Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
125 const Name& prefix)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700126{
Davide Pesavento44ec2072022-03-07 13:32:04 -0500127 auto data = std::make_shared<Data>();
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700128 data->setContent(content);
129 data->setFreshnessPeriod(freshness);
130
Qiuhan Dinge246b622014-12-03 21:57:48 -0800131 SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700132 Name dataName;
Qiuhan Dinge246b622014-12-03 21:57:48 -0800133 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700134 data->setName(dataName);
135
Yingdi Yucd339022014-11-05 17:51:19 -0800136 if (m_signingId.empty())
137 m_keyChain.sign(*data);
138 else
Ashlesh Gawande687cf922017-05-30 15:04:16 -0500139 m_keyChain.sign(*data, security::signingByIdentity(m_signingId));
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700140
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800141 m_ims.insert(*data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700142
Qiuhan Dinge246b622014-12-03 21:57:48 -0800143 m_logic.updateSeqNo(newSeq, prefix);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700144}
145
146void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500147Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
148 const uint64_t& seqNo, const Name& prefix)
149{
Davide Pesavento44ec2072022-03-07 13:32:04 -0500150 auto data = std::make_shared<Data>();
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500151 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 Gawande687cf922017-05-30 15:04:16 -0500162 m_keyChain.sign(*data, security::signingByIdentity(m_signingId));
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500163
164 m_ims.insert(*data);
165
166 m_logic.updateSeqNo(newSeq, prefix);
167}
168
169void
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700170Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500171 const DataValidatedCallback& dataCallback,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700172 int nRetries)
173{
174 Name interestName;
175 interestName.append(sessionName).appendNumber(seqNo);
176
177 Interest interest(interestName);
178 interest.setMustBeFresh(true);
179
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500180 DataValidationErrorCallback failureCallback =
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500181 [this] (auto&&... args) { onDataValidationFailed(std::forward<decltype(args)>(args)...); };
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700182
183 m_face.expressInterest(interest,
184 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
185 bind(&Socket::onDataTimeout, this, _1, nRetries,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800186 dataCallback, failureCallback), // Nack
187 bind(&Socket::onDataTimeout, this, _1, nRetries,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700188 dataCallback, failureCallback));
189}
190
191void
192Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500193 const DataValidatedCallback& dataCallback,
194 const DataValidationErrorCallback& failureCallback,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800195 const ndn::TimeoutCallback& onTimeout,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700196 int nRetries)
197{
Davide Pesavento07684bc2021-02-07 20:09:28 -0500198 NDN_LOG_DEBUG(">> Socket::fetchData");
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700199 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 Afanasyeve9eda8a2017-03-09 14:40:03 -0800207 bind(onTimeout, _1), // Nack
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700208 onTimeout);
209
Davide Pesavento07684bc2021-02-07 20:09:28 -0500210 NDN_LOG_DEBUG("<< Socket::fetchData");
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700211}
212
213void
Davide Pesavento07684bc2021-02-07 20:09:28 -0500214Socket::onInterest(const Name&, const Interest& interest)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800215{
Davide Pesavento07684bc2021-02-07 20:09:28 -0500216 auto data = m_ims.find(interest);
Alexander Afanasyev90587b82018-02-11 20:36:53 -0500217 if (data != nullptr) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800218 m_face.put(*data);
219 }
220}
221
222void
Davide Pesavento07684bc2021-02-07 20:09:28 -0500223Socket::onData(const Interest&, const Data& data,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500224 const DataValidatedCallback& onValidated,
225 const DataValidationErrorCallback& onFailed)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700226{
Davide Pesavento07684bc2021-02-07 20:09:28 -0500227 NDN_LOG_DEBUG("Socket::onData");
Yingdi Yucd339022014-11-05 17:51:19 -0800228
229 if (static_cast<bool>(m_validator))
230 m_validator->validate(data, onValidated, onFailed);
231 else
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500232 onValidated(data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700233}
234
235void
236Socket::onDataTimeout(const Interest& interest, int nRetries,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500237 const DataValidatedCallback& onValidated,
238 const DataValidationErrorCallback& onFailed)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700239{
Davide Pesavento07684bc2021-02-07 20:09:28 -0500240 NDN_LOG_DEBUG("Socket::onDataTimeout");
241
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700242 if (nRetries <= 0)
243 return;
244
Ashlesh Gawande8dfa63c2017-07-31 17:01:00 -0500245 Interest newNonceInterest(interest);
246 newNonceInterest.refreshNonce();
247
248 m_face.expressInterest(newNonceInterest,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700249 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
250 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800251 onValidated, onFailed), // Nack
252 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700253 onValidated, onFailed));
254}
255
256void
Davide Pesavento5f5101a2022-03-11 20:05:03 -0500257Socket::onDataValidationFailed(const Data&, const ValidationError&)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700258{
259}
260
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500261ConstBufferPtr
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700262Socket::getRootDigest() const
263{
264 return m_logic.getRootDigest();
265}
266
267} // namespace chronosync