blob: 695f7589296bf1f7df72fb563d499a95c2d28ed2 [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
Qiuhan Dinge246b622014-12-03 21:57:48 -080033const ndn::Name Socket::DEFAULT_PREFIX;
Yingdi Yucd339022014-11-05 17:51:19 -080034const ndn::Name Socket::DEFAULT_NAME;
35const 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{
49}
50
Yingdi Yu31ad44c2014-08-28 14:55:42 -070051void
Qiuhan Dinge246b622014-12-03 21:57:48 -080052Socket::addSyncNode(const Name& prefix, const Name& signingId)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070053{
Qiuhan Dinge246b622014-12-03 21:57:48 -080054 m_logic.addUserNode(prefix, signingId);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070055}
56
57void
Qiuhan Dinge246b622014-12-03 21:57:48 -080058Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
59 const Name& prefix)
60{
61 publishData(ndn::dataBlock(ndn::tlv::Content, buf, len), freshness, prefix);
62}
63
64void
65Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
66 const Name& prefix)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070067{
68 shared_ptr<Data> data = make_shared<Data>();
69 data->setContent(content);
70 data->setFreshnessPeriod(freshness);
71
Qiuhan Dinge246b622014-12-03 21:57:48 -080072 SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
Yingdi Yu31ad44c2014-08-28 14:55:42 -070073 Name dataName;
Qiuhan Dinge246b622014-12-03 21:57:48 -080074 dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070075 data->setName(dataName);
76
Yingdi Yucd339022014-11-05 17:51:19 -080077 if (m_signingId.empty())
78 m_keyChain.sign(*data);
79 else
80 m_keyChain.signByIdentity(*data, m_signingId);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070081
82 m_face.put(*data);
83
Qiuhan Dinge246b622014-12-03 21:57:48 -080084 m_logic.updateSeqNo(newSeq, prefix);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070085}
86
87void
88Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
89 const ndn::OnDataValidated& dataCallback,
90 int nRetries)
91{
92 Name interestName;
93 interestName.append(sessionName).appendNumber(seqNo);
94
95 Interest interest(interestName);
96 interest.setMustBeFresh(true);
97
98 ndn::OnDataValidationFailed failureCallback =
99 bind(&Socket::onDataValidationFailed, this, _1, _2);
100
101 m_face.expressInterest(interest,
102 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
103 bind(&Socket::onDataTimeout, this, _1, nRetries,
104 dataCallback, failureCallback));
105}
106
107void
108Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
109 const ndn::OnDataValidated& dataCallback,
110 const ndn::OnDataValidationFailed& failureCallback,
111 const ndn::OnTimeout& onTimeout,
112 int nRetries)
113{
114 _LOG_DEBUG(">> Socket::fetchData");
115 Name interestName;
116 interestName.append(sessionName).appendNumber(seqNo);
117
118 Interest interest(interestName);
119 interest.setMustBeFresh(true);
120
121 m_face.expressInterest(interest,
122 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
123 onTimeout);
124
125 _LOG_DEBUG("<< Socket::fetchData");
126}
127
128void
129Socket::onData(const Interest& interest, Data& data,
130 const ndn::OnDataValidated& onValidated,
131 const ndn::OnDataValidationFailed& onFailed)
132{
133 _LOG_DEBUG("Socket::onData");
Yingdi Yucd339022014-11-05 17:51:19 -0800134
135 if (static_cast<bool>(m_validator))
136 m_validator->validate(data, onValidated, onFailed);
137 else
138 onValidated(data.shared_from_this());
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700139}
140
141void
142Socket::onDataTimeout(const Interest& interest, int nRetries,
143 const ndn::OnDataValidated& onValidated,
144 const ndn::OnDataValidationFailed& onFailed)
145{
146 _LOG_DEBUG("Socket::onDataTimeout");
147 if (nRetries <= 0)
148 return;
149
150 m_face.expressInterest(interest,
151 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
152 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
153 onValidated, onFailed));
154}
155
156void
157Socket::onDataValidationFailed(const shared_ptr<const Data>& data,
158 const std::string& failureInfo)
159{
160}
161
162ndn::ConstBufferPtr
163Socket::getRootDigest() const
164{
165 return m_logic.getRootDigest();
166}
167
168} // namespace chronosync