blob: 6ec9c4173eaafc42c2e1344df2b57fafb7185cb8 [file] [log] [blame]
Yingdi Yu31ad44c2014-08-28 14:55:42 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawandeaae853c2018-02-06 17:28:50 -06003 * Copyright (c) 2012-2018 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,
42 const time::milliseconds& syncInterestLifetime)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070043 : m_userPrefix(userPrefix)
44 , m_face(face)
Ashlesh Gawandeaae853c2018-02-06 17:28:50 -060045 , 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 Yucd339022014-11-05 17:51:19 -080048 , m_signingId(signingId)
49 , m_validator(validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070050{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080051 if (m_userPrefix != DEFAULT_NAME)
52 m_registeredPrefixList[m_userPrefix] =
53 m_face.setInterestFilter(m_userPrefix,
54 bind(&Socket::onInterest, this, _1, _2),
55 [] (const Name& prefix, const std::string& msg) {});
Yingdi Yu31ad44c2014-08-28 14:55:42 -070056}
57
Qiuhan Ding03b9de32015-01-30 14:03:12 -080058Socket::~Socket()
59{
60 for(const auto& itr : m_registeredPrefixList) {
61 if (static_cast<bool>(itr.second))
62 m_face.unsetInterestFilter(itr.second);
63 }
64 m_ims.erase("/");
65}
66
Yingdi Yu31ad44c2014-08-28 14:55:42 -070067void
Qiuhan Dinge246b622014-12-03 21:57:48 -080068Socket::addSyncNode(const Name& prefix, const Name& signingId)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070069{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080070 if (prefix == DEFAULT_NAME)
71 return;
72
73 auto itr = m_registeredPrefixList.find(prefix);
74 if (itr != m_registeredPrefixList.end())
75 return;
76
77 if (m_userPrefix == DEFAULT_NAME)
78 m_userPrefix = prefix;
Qiuhan Dinge246b622014-12-03 21:57:48 -080079 m_logic.addUserNode(prefix, signingId);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080080 m_registeredPrefixList[prefix] =
81 m_face.setInterestFilter(prefix,
82 bind(&Socket::onInterest, this, _1, _2),
83 [] (const Name& prefix, const std::string& msg) {});
Yingdi Yu31ad44c2014-08-28 14:55:42 -070084}
85
86void
Qiuhan Ding03b9de32015-01-30 14:03:12 -080087Socket::removeSyncNode(const Name& prefix)
88{
89 if (prefix == DEFAULT_NAME)
90 return;
91
92 auto itr = m_registeredPrefixList.find(prefix);
93 if (itr != m_registeredPrefixList.end()) {
94 if (static_cast<bool>(itr->second))
95 m_face.unsetInterestFilter(itr->second);
96 m_registeredPrefixList.erase(itr);
97 }
98
99 m_ims.erase(prefix);
100 m_logic.removeUserNode(prefix);
101
102}
103
104void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800105Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
106 const Name& prefix)
107{
Junxiao Shi60d25882016-07-23 01:41:30 +0000108 publishData(ndn::encoding::makeBinaryBlock(ndn::tlv::Content, buf, len), freshness, prefix);
Qiuhan Dinge246b622014-12-03 21:57:48 -0800109}
110
111void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500112Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
113 const uint64_t& seqNo, const Name& prefix)
114{
115 publishData(ndn::encoding::makeBinaryBlock(ndn::tlv::Content, buf, len), freshness, seqNo, prefix);
116}
117
118void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800119Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
120 const Name& prefix)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700121{
122 shared_ptr<Data> data = make_shared<Data>();
123 data->setContent(content);
124 data->setFreshnessPeriod(freshness);
125
Qiuhan Dinge246b622014-12-03 21:57:48 -0800126 SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700127 Name dataName;
Qiuhan Dinge246b622014-12-03 21:57:48 -0800128 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700129 data->setName(dataName);
130
Yingdi Yucd339022014-11-05 17:51:19 -0800131 if (m_signingId.empty())
132 m_keyChain.sign(*data);
133 else
Ashlesh Gawande687cf922017-05-30 15:04:16 -0500134 m_keyChain.sign(*data, security::signingByIdentity(m_signingId));
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700135
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800136 m_ims.insert(*data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700137
Qiuhan Dinge246b622014-12-03 21:57:48 -0800138 m_logic.updateSeqNo(newSeq, prefix);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700139}
140
141void
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500142Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
143 const uint64_t& seqNo, const Name& prefix)
144{
145 shared_ptr<Data> data = make_shared<Data>();
146 data->setContent(content);
147 data->setFreshnessPeriod(freshness);
148
149 SeqNo newSeq = seqNo;
150 Name dataName;
151 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
152 data->setName(dataName);
153
154 if (m_signingId.empty())
155 m_keyChain.sign(*data);
156 else
Ashlesh Gawande687cf922017-05-30 15:04:16 -0500157 m_keyChain.sign(*data, security::signingByIdentity(m_signingId));
Ashlesh Gawande8d1347a2017-04-03 19:10:28 -0500158
159 m_ims.insert(*data);
160
161 m_logic.updateSeqNo(newSeq, prefix);
162}
163
164void
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700165Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500166 const DataValidatedCallback& dataCallback,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700167 int nRetries)
168{
169 Name interestName;
170 interestName.append(sessionName).appendNumber(seqNo);
171
172 Interest interest(interestName);
173 interest.setMustBeFresh(true);
174
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500175 DataValidationErrorCallback failureCallback =
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700176 bind(&Socket::onDataValidationFailed, this, _1, _2);
177
178 m_face.expressInterest(interest,
179 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
180 bind(&Socket::onDataTimeout, this, _1, nRetries,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800181 dataCallback, failureCallback), // Nack
182 bind(&Socket::onDataTimeout, this, _1, nRetries,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700183 dataCallback, failureCallback));
184}
185
186void
187Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500188 const DataValidatedCallback& dataCallback,
189 const DataValidationErrorCallback& failureCallback,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800190 const ndn::TimeoutCallback& onTimeout,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700191 int nRetries)
192{
193 _LOG_DEBUG(">> Socket::fetchData");
194 Name interestName;
195 interestName.append(sessionName).appendNumber(seqNo);
196
197 Interest interest(interestName);
198 interest.setMustBeFresh(true);
199
200 m_face.expressInterest(interest,
201 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800202 bind(onTimeout, _1), // Nack
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700203 onTimeout);
204
205 _LOG_DEBUG("<< Socket::fetchData");
206}
207
208void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800209Socket::onInterest(const Name& prefix, const Interest& interest)
210{
211 shared_ptr<const Data>data = m_ims.find(interest);
212 if (static_cast<bool>(data)) {
213 m_face.put(*data);
214 }
215}
216
217void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800218Socket::onData(const Interest& interest, const Data& data,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500219 const DataValidatedCallback& onValidated,
220 const DataValidationErrorCallback& onFailed)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700221{
222 _LOG_DEBUG("Socket::onData");
Yingdi Yucd339022014-11-05 17:51:19 -0800223
224 if (static_cast<bool>(m_validator))
225 m_validator->validate(data, onValidated, onFailed);
226 else
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500227 onValidated(data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700228}
229
230void
231Socket::onDataTimeout(const Interest& interest, int nRetries,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500232 const DataValidatedCallback& onValidated,
233 const DataValidationErrorCallback& onFailed)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700234{
235 _LOG_DEBUG("Socket::onDataTimeout");
236 if (nRetries <= 0)
237 return;
238
Ashlesh Gawande8dfa63c2017-07-31 17:01:00 -0500239 Interest newNonceInterest(interest);
240 newNonceInterest.refreshNonce();
241
242 m_face.expressInterest(newNonceInterest,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700243 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
244 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800245 onValidated, onFailed), // Nack
246 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700247 onValidated, onFailed));
248}
249
250void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500251Socket::onDataValidationFailed(const Data& data,
252 const ValidationError& error)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700253{
254}
255
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500256ConstBufferPtr
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700257Socket::getRootDigest() const
258{
259 return m_logic.getRootDigest();
260}
261
262} // namespace chronosync