blob: 9aca2e51ca8fe590835062be9df6e8b72df63fb5 [file] [log] [blame]
Yingdi Yu31ad44c2014-08-28 14:55:42 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Junxiao Shi8e4e76d2019-02-08 15:25:08 -07003 * Copyright (c) 2012-2019 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"
26#include "logger.hpp"
27
Alexander Afanasyev36eb3ed2017-01-11 12:35:58 -080028INIT_LOGGER(Socket);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070029
Yingdi Yu31ad44c2014-08-28 14:55:42 -070030namespace chronosync {
31
Yingdi Yucd339022014-11-05 17:51:19 -080032const ndn::Name Socket::DEFAULT_NAME;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080033const ndn::Name Socket::DEFAULT_PREFIX;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050034const std::shared_ptr<Validator> Socket::DEFAULT_VALIDATOR;
Yingdi Yucd339022014-11-05 17:51:19 -080035
Yingdi Yu31ad44c2014-08-28 14:55:42 -070036Socket::Socket(const Name& syncPrefix,
37 const Name& userPrefix,
38 ndn::Face& face,
Yingdi Yucd339022014-11-05 17:51:19 -080039 const UpdateCallback& updateCallback,
40 const Name& signingId,
Ashlesh Gawandeaae853c2018-02-06 17:28:50 -060041 std::shared_ptr<Validator> validator,
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050042 const time::milliseconds& syncInterestLifetime,
43 const name::Component& session)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070044 : m_userPrefix(userPrefix)
45 , m_face(face)
Ashlesh Gawandeaae853c2018-02-06 17:28:50 -060046 , 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 Afanasyevbf5bc6c2018-02-19 11:26:09 -050048 syncInterestLifetime, Logic::DEFAULT_SYNC_REPLY_FRESHNESS, Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME,
49 session)
Yingdi Yucd339022014-11-05 17:51:19 -080050 , m_signingId(signingId)
51 , m_validator(validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070052{
Alexander Afanasyev90587b82018-02-11 20:36:53 -050053 NDN_LOG_DEBUG(">> Socket::Socket");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080054 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 Afanasyev90587b82018-02-11 20:36:53 -050059 NDN_LOG_DEBUG("<< Socket::Socket");
Yingdi Yu31ad44c2014-08-28 14:55:42 -070060}
61
Qiuhan Ding03b9de32015-01-30 14:03:12 -080062Socket::~Socket()
63{
Junxiao Shi8e4e76d2019-02-08 15:25:08 -070064 for (auto& itr : m_registeredPrefixList) {
65 itr.second.unregister();
Qiuhan Ding03b9de32015-01-30 14:03:12 -080066 }
Qiuhan Ding03b9de32015-01-30 14:03:12 -080067}
68
Yingdi Yu31ad44c2014-08-28 14:55:42 -070069void
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050070Socket::addSyncNode(const Name& prefix, const Name& signingId, const name::Component& session)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070071{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080072 if (prefix == DEFAULT_NAME)
73 return;
74
Alexander Afanasyev90587b82018-02-11 20:36:53 -050075 NDN_LOG_DEBUG(">> addSyncNode");
76
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080077 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;
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -050083 m_logic.addUserNode(prefix, signingId, session);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080084 m_registeredPrefixList[prefix] =
85 m_face.setInterestFilter(prefix,
86 bind(&Socket::onInterest, this, _1, _2),
87 [] (const Name& prefix, const std::string& msg) {});
Alexander Afanasyev90587b82018-02-11 20:36:53 -050088 NDN_LOG_DEBUG("<< addSyncNode");
Yingdi Yu31ad44c2014-08-28 14:55:42 -070089}
90
91void
Qiuhan Ding03b9de32015-01-30 14:03:12 -080092Socket::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 Shi8e4e76d2019-02-08 15:25:08 -070099 itr->second.unregister();
Qiuhan Ding03b9de32015-01-30 14:03:12 -0800100 m_registeredPrefixList.erase(itr);
101 }
102
103 m_ims.erase(prefix);
104 m_logic.removeUserNode(prefix);
Qiuhan Ding03b9de32015-01-30 14:03:12 -0800105}
106
107void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800108Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
109 const Name& prefix)
110{
Junxiao Shi60d25882016-07-23 01:41:30 +0000111 publishData(ndn::encoding::makeBinaryBlock(ndn::tlv::Content, buf, len), freshness, prefix);
Qiuhan Dinge246b622014-12-03 21:57:48 -0800112}
113
114void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500115Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
116 const uint64_t& seqNo, const Name& prefix)
117{
118 publishData(ndn::encoding::makeBinaryBlock(ndn::tlv::Content, buf, len), freshness, seqNo, prefix);
119}
120
121void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800122Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
123 const Name& prefix)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700124{
125 shared_ptr<Data> data = make_shared<Data>();
126 data->setContent(content);
127 data->setFreshnessPeriod(freshness);
128
Qiuhan Dinge246b622014-12-03 21:57:48 -0800129 SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700130 Name dataName;
Qiuhan Dinge246b622014-12-03 21:57:48 -0800131 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700132 data->setName(dataName);
133
Yingdi Yucd339022014-11-05 17:51:19 -0800134 if (m_signingId.empty())
135 m_keyChain.sign(*data);
136 else
Ashlesh Gawande687cf922017-05-30 15:04:16 -0500137 m_keyChain.sign(*data, security::signingByIdentity(m_signingId));
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700138
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800139 m_ims.insert(*data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700140
Qiuhan Dinge246b622014-12-03 21:57:48 -0800141 m_logic.updateSeqNo(newSeq, prefix);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700142}
143
144void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500145Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
146 const uint64_t& seqNo, const Name& prefix)
147{
148 shared_ptr<Data> data = make_shared<Data>();
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 Gawande687cf922017-05-30 15:04:16 -0500160 m_keyChain.sign(*data, security::signingByIdentity(m_signingId));
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500161
162 m_ims.insert(*data);
163
164 m_logic.updateSeqNo(newSeq, prefix);
165}
166
167void
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700168Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500169 const DataValidatedCallback& dataCallback,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700170 int nRetries)
171{
172 Name interestName;
173 interestName.append(sessionName).appendNumber(seqNo);
174
175 Interest interest(interestName);
176 interest.setMustBeFresh(true);
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500177 interest.setCanBePrefix(false);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700178
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500179 DataValidationErrorCallback failureCallback =
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700180 bind(&Socket::onDataValidationFailed, this, _1, _2);
181
182 m_face.expressInterest(interest,
183 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
184 bind(&Socket::onDataTimeout, this, _1, nRetries,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800185 dataCallback, failureCallback), // Nack
186 bind(&Socket::onDataTimeout, this, _1, nRetries,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700187 dataCallback, failureCallback));
188}
189
190void
191Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500192 const DataValidatedCallback& dataCallback,
193 const DataValidationErrorCallback& failureCallback,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800194 const ndn::TimeoutCallback& onTimeout,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700195 int nRetries)
196{
197 _LOG_DEBUG(">> Socket::fetchData");
198 Name interestName;
199 interestName.append(sessionName).appendNumber(seqNo);
200
201 Interest interest(interestName);
202 interest.setMustBeFresh(true);
Ashlesh Gawande1d1092d2018-08-03 14:36:49 -0500203 interest.setCanBePrefix(false);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700204
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
210 _LOG_DEBUG("<< Socket::fetchData");
211}
212
213void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800214Socket::onInterest(const Name& prefix, const Interest& interest)
215{
Alexander Afanasyev90587b82018-02-11 20:36:53 -0500216 shared_ptr<const Data> data = m_ims.find(interest);
217 if (data != nullptr) {
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800218 m_face.put(*data);
219 }
220}
221
222void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800223Socket::onData(const Interest& 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{
227 _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{
240 _LOG_DEBUG("Socket::onDataTimeout");
241 if (nRetries <= 0)
242 return;
243
Ashlesh Gawande8dfa63c2017-07-31 17:01:00 -0500244 Interest newNonceInterest(interest);
245 newNonceInterest.refreshNonce();
246
247 m_face.expressInterest(newNonceInterest,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700248 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
249 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800250 onValidated, onFailed), // Nack
251 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700252 onValidated, onFailed));
253}
254
255void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500256Socket::onDataValidationFailed(const Data& data,
257 const ValidationError& error)
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