blob: 7356f734ec667a08aeee2a1e21a5149c449fb098 [file] [log] [blame]
Yingdi Yu31ad44c2014-08-28 14:55:42 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2014 University of California, Los Angeles
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
28INIT_LOGGER("Socket");
29
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;
Yingdi Yucd339022014-11-05 17:51:19 -080035const ndn::shared_ptr<ndn::Validator> Socket::DEFAULT_VALIDATOR;
36
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,
42 ndn::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
Yingdi Yu31ad44c2014-08-28 14:55:42 -070056void
Qiuhan Dinge246b622014-12-03 21:57:48 -080057Socket::addSyncNode(const Name& prefix, const Name& signingId)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070058{
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080059 if (prefix == DEFAULT_NAME)
60 return;
61
62 auto itr = m_registeredPrefixList.find(prefix);
63 if (itr != m_registeredPrefixList.end())
64 return;
65
66 if (m_userPrefix == DEFAULT_NAME)
67 m_userPrefix = prefix;
Qiuhan Dinge246b622014-12-03 21:57:48 -080068 m_logic.addUserNode(prefix, signingId);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080069 m_registeredPrefixList[prefix] =
70 m_face.setInterestFilter(prefix,
71 bind(&Socket::onInterest, this, _1, _2),
72 [] (const Name& prefix, const std::string& msg) {});
Yingdi Yu31ad44c2014-08-28 14:55:42 -070073}
74
75void
Qiuhan Dinge246b622014-12-03 21:57:48 -080076Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
77 const Name& prefix)
78{
79 publishData(ndn::dataBlock(ndn::tlv::Content, buf, len), freshness, prefix);
80}
81
82void
83Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
84 const Name& prefix)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070085{
86 shared_ptr<Data> data = make_shared<Data>();
87 data->setContent(content);
88 data->setFreshnessPeriod(freshness);
89
Qiuhan Dinge246b622014-12-03 21:57:48 -080090 SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
Yingdi Yu31ad44c2014-08-28 14:55:42 -070091 Name dataName;
Qiuhan Dinge246b622014-12-03 21:57:48 -080092 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070093 data->setName(dataName);
94
Yingdi Yucd339022014-11-05 17:51:19 -080095 if (m_signingId.empty())
96 m_keyChain.sign(*data);
97 else
98 m_keyChain.signByIdentity(*data, m_signingId);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070099
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800100 m_ims.insert(*data);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700101
Qiuhan Dinge246b622014-12-03 21:57:48 -0800102 m_logic.updateSeqNo(newSeq, prefix);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700103}
104
105void
106Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
107 const ndn::OnDataValidated& dataCallback,
108 int nRetries)
109{
110 Name interestName;
111 interestName.append(sessionName).appendNumber(seqNo);
112
113 Interest interest(interestName);
114 interest.setMustBeFresh(true);
115
116 ndn::OnDataValidationFailed failureCallback =
117 bind(&Socket::onDataValidationFailed, this, _1, _2);
118
119 m_face.expressInterest(interest,
120 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
121 bind(&Socket::onDataTimeout, this, _1, nRetries,
122 dataCallback, failureCallback));
123}
124
125void
126Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
127 const ndn::OnDataValidated& dataCallback,
128 const ndn::OnDataValidationFailed& failureCallback,
129 const ndn::OnTimeout& onTimeout,
130 int nRetries)
131{
132 _LOG_DEBUG(">> Socket::fetchData");
133 Name interestName;
134 interestName.append(sessionName).appendNumber(seqNo);
135
136 Interest interest(interestName);
137 interest.setMustBeFresh(true);
138
139 m_face.expressInterest(interest,
140 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
141 onTimeout);
142
143 _LOG_DEBUG("<< Socket::fetchData");
144}
145
146void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800147Socket::onInterest(const Name& prefix, const Interest& interest)
148{
149 shared_ptr<const Data>data = m_ims.find(interest);
150 if (static_cast<bool>(data)) {
151 m_face.put(*data);
152 }
153}
154
155void
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700156Socket::onData(const Interest& interest, Data& data,
157 const ndn::OnDataValidated& onValidated,
158 const ndn::OnDataValidationFailed& onFailed)
159{
160 _LOG_DEBUG("Socket::onData");
Yingdi Yucd339022014-11-05 17:51:19 -0800161
162 if (static_cast<bool>(m_validator))
163 m_validator->validate(data, onValidated, onFailed);
164 else
165 onValidated(data.shared_from_this());
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700166}
167
168void
169Socket::onDataTimeout(const Interest& interest, int nRetries,
170 const ndn::OnDataValidated& onValidated,
171 const ndn::OnDataValidationFailed& onFailed)
172{
173 _LOG_DEBUG("Socket::onDataTimeout");
174 if (nRetries <= 0)
175 return;
176
177 m_face.expressInterest(interest,
178 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
179 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
180 onValidated, onFailed));
181}
182
183void
184Socket::onDataValidationFailed(const shared_ptr<const Data>& data,
185 const std::string& failureInfo)
186{
187}
188
189ndn::ConstBufferPtr
190Socket::getRootDigest() const
191{
192 return m_logic.getRootDigest();
193}
194
195} // namespace chronosync