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 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 33 | const ndn::Name Socket::DEFAULT_PREFIX; |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 34 | const ndn::Name Socket::DEFAULT_NAME; |
| 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 | { |
| 49 | } |
| 50 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 51 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 52 | Socket::addSyncNode(const Name& prefix, const Name& signingId) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 53 | { |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 54 | m_logic.addUserNode(prefix, signingId); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 58 | Socket::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 | |
| 64 | void |
| 65 | Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 66 | const Name& prefix) |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 67 | { |
| 68 | shared_ptr<Data> data = make_shared<Data>(); |
| 69 | data->setContent(content); |
| 70 | data->setFreshnessPeriod(freshness); |
| 71 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 72 | SeqNo newSeq = m_logic.getSeqNo(prefix) + 1; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 73 | Name dataName; |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 74 | dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 75 | data->setName(dataName); |
| 76 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 77 | if (m_signingId.empty()) |
| 78 | m_keyChain.sign(*data); |
| 79 | else |
| 80 | m_keyChain.signByIdentity(*data, m_signingId); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 81 | |
| 82 | m_face.put(*data); |
| 83 | |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame^] | 84 | m_logic.updateSeqNo(newSeq, prefix); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | Socket::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 | |
| 107 | void |
| 108 | Socket::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 | |
| 128 | void |
| 129 | Socket::onData(const Interest& interest, Data& data, |
| 130 | const ndn::OnDataValidated& onValidated, |
| 131 | const ndn::OnDataValidationFailed& onFailed) |
| 132 | { |
| 133 | _LOG_DEBUG("Socket::onData"); |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 134 | |
| 135 | if (static_cast<bool>(m_validator)) |
| 136 | m_validator->validate(data, onValidated, onFailed); |
| 137 | else |
| 138 | onValidated(data.shared_from_this()); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | Socket::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 | |
| 156 | void |
| 157 | Socket::onDataValidationFailed(const shared_ptr<const Data>& data, |
| 158 | const std::string& failureInfo) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | ndn::ConstBufferPtr |
| 163 | Socket::getRootDigest() const |
| 164 | { |
| 165 | return m_logic.getRootDigest(); |
| 166 | } |
| 167 | |
| 168 | } // namespace chronosync |