Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 28 | INIT_LOGGER("Socket"); |
| 29 | |
| 30 | |
| 31 | namespace chronosync { |
| 32 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 33 | const ndn::Name Socket::DEFAULT_NAME; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 34 | const ndn::Name Socket::DEFAULT_PREFIX; |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 35 | const ndn::shared_ptr<ndn::Validator> Socket::DEFAULT_VALIDATOR; |
| 36 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 37 | Socket::Socket(const Name& syncPrefix, |
| 38 | const Name& userPrefix, |
| 39 | ndn::Face& face, |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 40 | const UpdateCallback& updateCallback, |
| 41 | const Name& signingId, |
| 42 | ndn::shared_ptr<ndn::Validator> validator) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 43 | : m_userPrefix(userPrefix) |
| 44 | , m_face(face) |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 45 | , m_logic(face, syncPrefix, userPrefix, updateCallback) |
| 46 | , m_signingId(signingId) |
| 47 | , m_validator(validator) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 48 | { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 49 | 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 Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 56 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 57 | Socket::addSyncNode(const Name& prefix, const Name& signingId) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 58 | { |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 59 | 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 Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 68 | m_logic.addUserNode(prefix, signingId); |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 69 | m_registeredPrefixList[prefix] = |
| 70 | m_face.setInterestFilter(prefix, |
| 71 | bind(&Socket::onInterest, this, _1, _2), |
| 72 | [] (const Name& prefix, const std::string& msg) {}); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 76 | Socket::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 | |
| 82 | void |
| 83 | Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 84 | const Name& prefix) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 85 | { |
| 86 | shared_ptr<Data> data = make_shared<Data>(); |
| 87 | data->setContent(content); |
| 88 | data->setFreshnessPeriod(freshness); |
| 89 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 90 | SeqNo newSeq = m_logic.getSeqNo(prefix) + 1; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 91 | Name dataName; |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 92 | dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 93 | data->setName(dataName); |
| 94 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 95 | if (m_signingId.empty()) |
| 96 | m_keyChain.sign(*data); |
| 97 | else |
| 98 | m_keyChain.signByIdentity(*data, m_signingId); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 99 | |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 100 | m_ims.insert(*data); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 101 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 102 | m_logic.updateSeqNo(newSeq, prefix); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | Socket::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 | |
| 125 | void |
| 126 | Socket::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 | |
| 146 | void |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 147 | Socket::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 | |
| 155 | void |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 156 | Socket::onData(const Interest& interest, Data& data, |
| 157 | const ndn::OnDataValidated& onValidated, |
| 158 | const ndn::OnDataValidationFailed& onFailed) |
| 159 | { |
| 160 | _LOG_DEBUG("Socket::onData"); |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 161 | |
| 162 | if (static_cast<bool>(m_validator)) |
| 163 | m_validator->validate(data, onValidated, onFailed); |
| 164 | else |
| 165 | onValidated(data.shared_from_this()); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
| 169 | Socket::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 | |
| 183 | void |
| 184 | Socket::onDataValidationFailed(const shared_ptr<const Data>& data, |
| 185 | const std::string& failureInfo) |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | ndn::ConstBufferPtr |
| 190 | Socket::getRootDigest() const |
| 191 | { |
| 192 | return m_logic.getRootDigest(); |
| 193 | } |
| 194 | |
| 195 | } // namespace chronosync |