blob: dad11259d89d1e4a4ca5af4c1264e1dc42ba6d86 [file] [log] [blame]
Yingdi Yu31ad44c2014-08-28 14:55:42 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev36eb3ed2017-01-11 12:35:58 -08003 * Copyright (c) 2012-2017 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
30
31namespace 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;
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -080035const std::shared_ptr<ndn::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,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -080042 std::shared_ptr<ndn::Validator> validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070043 : m_userPrefix(userPrefix)
44 , m_face(face)
Yingdi Yucd339022014-11-05 17:51:19 -080045 , m_logic(face, syncPrefix, userPrefix, updateCallback)
46 , m_signingId(signingId)
47 , m_validator(validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070048{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080049 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 Yu31ad44c2014-08-28 14:55:42 -070054}
55
Qiuhan Ding03b9de32015-01-30 14:03:12 -080056Socket::~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 Yu31ad44c2014-08-28 14:55:42 -070065void
Qiuhan Dinge246b622014-12-03 21:57:48 -080066Socket::addSyncNode(const Name& prefix, const Name& signingId)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070067{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080068 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 Dinge246b622014-12-03 21:57:48 -080077 m_logic.addUserNode(prefix, signingId);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080078 m_registeredPrefixList[prefix] =
79 m_face.setInterestFilter(prefix,
80 bind(&Socket::onInterest, this, _1, _2),
81 [] (const Name& prefix, const std::string& msg) {});
Yingdi Yu31ad44c2014-08-28 14:55:42 -070082}
83
84void
Qiuhan Ding03b9de32015-01-30 14:03:12 -080085Socket::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
102void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800103Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
104 const Name& prefix)
105{
Junxiao Shi60d25882016-07-23 01:41:30 +0000106 publishData(ndn::encoding::makeBinaryBlock(ndn::tlv::Content, buf, len), freshness, prefix);
Qiuhan Dinge246b622014-12-03 21:57:48 -0800107}
108
109void
110Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
111 const Name& prefix)
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700112{
113 shared_ptr<Data> data = make_shared<Data>();
114 data->setContent(content);
115 data->setFreshnessPeriod(freshness);
116
Qiuhan Dinge246b622014-12-03 21:57:48 -0800117 SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700118 Name dataName;
Qiuhan Dinge246b622014-12-03 21:57:48 -0800119 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700120 data->setName(dataName);
121
Yingdi Yucd339022014-11-05 17:51:19 -0800122 if (m_signingId.empty())
123 m_keyChain.sign(*data);
124 else
125 m_keyChain.signByIdentity(*data, m_signingId);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700126
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800127 m_ims.insert(*data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700128
Qiuhan Dinge246b622014-12-03 21:57:48 -0800129 m_logic.updateSeqNo(newSeq, prefix);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700130}
131
132void
133Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
134 const ndn::OnDataValidated& dataCallback,
135 int nRetries)
136{
137 Name interestName;
138 interestName.append(sessionName).appendNumber(seqNo);
139
140 Interest interest(interestName);
141 interest.setMustBeFresh(true);
142
143 ndn::OnDataValidationFailed failureCallback =
144 bind(&Socket::onDataValidationFailed, this, _1, _2);
145
146 m_face.expressInterest(interest,
147 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
148 bind(&Socket::onDataTimeout, this, _1, nRetries,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800149 dataCallback, failureCallback), // Nack
150 bind(&Socket::onDataTimeout, this, _1, nRetries,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700151 dataCallback, failureCallback));
152}
153
154void
155Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
156 const ndn::OnDataValidated& dataCallback,
157 const ndn::OnDataValidationFailed& failureCallback,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800158 const ndn::TimeoutCallback& onTimeout,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700159 int nRetries)
160{
161 _LOG_DEBUG(">> Socket::fetchData");
162 Name interestName;
163 interestName.append(sessionName).appendNumber(seqNo);
164
165 Interest interest(interestName);
166 interest.setMustBeFresh(true);
167
168 m_face.expressInterest(interest,
169 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800170 bind(onTimeout, _1), // Nack
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700171 onTimeout);
172
173 _LOG_DEBUG("<< Socket::fetchData");
174}
175
176void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800177Socket::onInterest(const Name& prefix, const Interest& interest)
178{
179 shared_ptr<const Data>data = m_ims.find(interest);
180 if (static_cast<bool>(data)) {
181 m_face.put(*data);
182 }
183}
184
185void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800186Socket::onData(const Interest& interest, const Data& data,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700187 const ndn::OnDataValidated& onValidated,
188 const ndn::OnDataValidationFailed& onFailed)
189{
190 _LOG_DEBUG("Socket::onData");
Yingdi Yucd339022014-11-05 17:51:19 -0800191
192 if (static_cast<bool>(m_validator))
193 m_validator->validate(data, onValidated, onFailed);
194 else
195 onValidated(data.shared_from_this());
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700196}
197
198void
199Socket::onDataTimeout(const Interest& interest, int nRetries,
200 const ndn::OnDataValidated& onValidated,
201 const ndn::OnDataValidationFailed& onFailed)
202{
203 _LOG_DEBUG("Socket::onDataTimeout");
204 if (nRetries <= 0)
205 return;
206
207 m_face.expressInterest(interest,
208 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
209 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800210 onValidated, onFailed), // Nack
211 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700212 onValidated, onFailed));
213}
214
215void
216Socket::onDataValidationFailed(const shared_ptr<const Data>& data,
217 const std::string& failureInfo)
218{
219}
220
221ndn::ConstBufferPtr
222Socket::getRootDigest() const
223{
224 return m_logic.getRootDigest();
225}
226
227} // namespace chronosync