blob: 45c6890f4a026365109b3e8c8259bb90af8adc79 [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;
34const ndn::shared_ptr<ndn::Validator> Socket::DEFAULT_VALIDATOR;
35
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,
41 ndn::shared_ptr<ndn::Validator> validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070042 : m_userPrefix(userPrefix)
43 , m_face(face)
Yingdi Yucd339022014-11-05 17:51:19 -080044 , m_logic(face, syncPrefix, userPrefix, updateCallback)
45 , m_signingId(signingId)
46 , m_validator(validator)
Yingdi Yu31ad44c2014-08-28 14:55:42 -070047{
48}
49
50
51void
52Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness)
53{
54 publishData(ndn::dataBlock(ndn::tlv::Content, buf, len), freshness);
55}
56
57void
58Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness)
59{
60 shared_ptr<Data> data = make_shared<Data>();
61 data->setContent(content);
62 data->setFreshnessPeriod(freshness);
63
64 SeqNo newSeq = m_logic.getSeqNo() + 1;
65 Name dataName;
66 dataName.append(m_logic.getSessionName()).appendNumber(newSeq);
67 data->setName(dataName);
68
Yingdi Yucd339022014-11-05 17:51:19 -080069 if (m_signingId.empty())
70 m_keyChain.sign(*data);
71 else
72 m_keyChain.signByIdentity(*data, m_signingId);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070073
74 m_face.put(*data);
75
76 m_logic.updateSeqNo(newSeq);
77}
78
79void
80Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
81 const ndn::OnDataValidated& dataCallback,
82 int nRetries)
83{
84 Name interestName;
85 interestName.append(sessionName).appendNumber(seqNo);
86
87 Interest interest(interestName);
88 interest.setMustBeFresh(true);
89
90 ndn::OnDataValidationFailed failureCallback =
91 bind(&Socket::onDataValidationFailed, this, _1, _2);
92
93 m_face.expressInterest(interest,
94 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
95 bind(&Socket::onDataTimeout, this, _1, nRetries,
96 dataCallback, failureCallback));
97}
98
99void
100Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
101 const ndn::OnDataValidated& dataCallback,
102 const ndn::OnDataValidationFailed& failureCallback,
103 const ndn::OnTimeout& onTimeout,
104 int nRetries)
105{
106 _LOG_DEBUG(">> Socket::fetchData");
107 Name interestName;
108 interestName.append(sessionName).appendNumber(seqNo);
109
110 Interest interest(interestName);
111 interest.setMustBeFresh(true);
112
113 m_face.expressInterest(interest,
114 bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
115 onTimeout);
116
117 _LOG_DEBUG("<< Socket::fetchData");
118}
119
120void
121Socket::onData(const Interest& interest, Data& data,
122 const ndn::OnDataValidated& onValidated,
123 const ndn::OnDataValidationFailed& onFailed)
124{
125 _LOG_DEBUG("Socket::onData");
Yingdi Yucd339022014-11-05 17:51:19 -0800126
127 if (static_cast<bool>(m_validator))
128 m_validator->validate(data, onValidated, onFailed);
129 else
130 onValidated(data.shared_from_this());
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700131}
132
133void
134Socket::onDataTimeout(const Interest& interest, int nRetries,
135 const ndn::OnDataValidated& onValidated,
136 const ndn::OnDataValidationFailed& onFailed)
137{
138 _LOG_DEBUG("Socket::onDataTimeout");
139 if (nRetries <= 0)
140 return;
141
142 m_face.expressInterest(interest,
143 bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
144 bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
145 onValidated, onFailed));
146}
147
148void
149Socket::onDataValidationFailed(const shared_ptr<const Data>& data,
150 const std::string& failureInfo)
151{
152}
153
154ndn::ConstBufferPtr
155Socket::getRootDigest() const
156{
157 return m_logic.getRootDigest();
158}
159
160} // namespace chronosync