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 | |
| 33 | Socket::Socket(const Name& syncPrefix, |
| 34 | const Name& userPrefix, |
| 35 | ndn::Face& face, |
| 36 | const UpdateCallback& updateCallback) |
| 37 | : m_userPrefix(userPrefix) |
| 38 | , m_face(face) |
| 39 | , m_logic(face, |
| 40 | syncPrefix, |
| 41 | userPrefix, |
| 42 | updateCallback) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void |
| 48 | Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness) |
| 49 | { |
| 50 | publishData(ndn::dataBlock(ndn::tlv::Content, buf, len), freshness); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness) |
| 55 | { |
| 56 | shared_ptr<Data> data = make_shared<Data>(); |
| 57 | data->setContent(content); |
| 58 | data->setFreshnessPeriod(freshness); |
| 59 | |
| 60 | SeqNo newSeq = m_logic.getSeqNo() + 1; |
| 61 | Name dataName; |
| 62 | dataName.append(m_logic.getSessionName()).appendNumber(newSeq); |
| 63 | data->setName(dataName); |
| 64 | |
| 65 | m_keyChain.sign(*data); |
| 66 | |
| 67 | m_face.put(*data); |
| 68 | |
| 69 | m_logic.updateSeqNo(newSeq); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | Socket::fetchData(const Name& sessionName, const SeqNo& seqNo, |
| 74 | const ndn::OnDataValidated& dataCallback, |
| 75 | int nRetries) |
| 76 | { |
| 77 | Name interestName; |
| 78 | interestName.append(sessionName).appendNumber(seqNo); |
| 79 | |
| 80 | Interest interest(interestName); |
| 81 | interest.setMustBeFresh(true); |
| 82 | |
| 83 | ndn::OnDataValidationFailed failureCallback = |
| 84 | bind(&Socket::onDataValidationFailed, this, _1, _2); |
| 85 | |
| 86 | m_face.expressInterest(interest, |
| 87 | bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback), |
| 88 | bind(&Socket::onDataTimeout, this, _1, nRetries, |
| 89 | dataCallback, failureCallback)); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | Socket::fetchData(const Name& sessionName, const SeqNo& seqNo, |
| 94 | const ndn::OnDataValidated& dataCallback, |
| 95 | const ndn::OnDataValidationFailed& failureCallback, |
| 96 | const ndn::OnTimeout& onTimeout, |
| 97 | int nRetries) |
| 98 | { |
| 99 | _LOG_DEBUG(">> Socket::fetchData"); |
| 100 | Name interestName; |
| 101 | interestName.append(sessionName).appendNumber(seqNo); |
| 102 | |
| 103 | Interest interest(interestName); |
| 104 | interest.setMustBeFresh(true); |
| 105 | |
| 106 | m_face.expressInterest(interest, |
| 107 | bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback), |
| 108 | onTimeout); |
| 109 | |
| 110 | _LOG_DEBUG("<< Socket::fetchData"); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | Socket::onData(const Interest& interest, Data& data, |
| 115 | const ndn::OnDataValidated& onValidated, |
| 116 | const ndn::OnDataValidationFailed& onFailed) |
| 117 | { |
| 118 | _LOG_DEBUG("Socket::onData"); |
| 119 | // Placeholder for validator |
| 120 | onValidated(data.shared_from_this()); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | Socket::onDataTimeout(const Interest& interest, int nRetries, |
| 125 | const ndn::OnDataValidated& onValidated, |
| 126 | const ndn::OnDataValidationFailed& onFailed) |
| 127 | { |
| 128 | _LOG_DEBUG("Socket::onDataTimeout"); |
| 129 | if (nRetries <= 0) |
| 130 | return; |
| 131 | |
| 132 | m_face.expressInterest(interest, |
| 133 | bind(&Socket::onData, this, _1, _2, onValidated, onFailed), |
| 134 | bind(&Socket::onDataTimeout, this, _1, nRetries - 1, |
| 135 | onValidated, onFailed)); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | Socket::onDataValidationFailed(const shared_ptr<const Data>& data, |
| 140 | const std::string& failureInfo) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | ndn::ConstBufferPtr |
| 145 | Socket::getRootDigest() const |
| 146 | { |
| 147 | return m_logic.getRootDigest(); |
| 148 | } |
| 149 | |
| 150 | } // namespace chronosync |