akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- Mode: C32++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "sync-socket.h" |
| 22 | #include "sync-logging.h" |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | db49f4e | 2014-08-14 23:01:02 -0700 | [diff] [blame] | 24 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 25 | #include <boost/thread.hpp> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 28 | |
| 29 | using ndn::Data; |
| 30 | using ndn::Face; |
| 31 | using ndn::KeyChain; |
| 32 | using ndn::Name; |
| 33 | using ndn::OnDataValidated; |
| 34 | using ndn::OnDataValidationFailed; |
| 35 | using ndn::Validator; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 36 | |
| 37 | INIT_LOGGER ("SyncSocket"); |
| 38 | |
| 39 | namespace Sync { |
| 40 | |
| 41 | using ndn::shared_ptr; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 42 | using ndn::make_shared; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 43 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 44 | SyncSocket::SyncSocket (const Name &syncPrefix, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 45 | shared_ptr<Validator> validator, |
| 46 | shared_ptr<Face> face, |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 47 | NewDataCallback dataCallback, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 48 | RemoveCallback rmCallback ) |
| 49 | : m_newDataCallback(dataCallback) |
| 50 | , m_validator(validator) |
| 51 | , m_keyChain(new KeyChain()) |
| 52 | , m_face(face) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 53 | , m_syncLogic (syncPrefix, |
| 54 | validator, |
| 55 | face, |
| 56 | bind(&SyncSocket::passCallback, this, _1), |
| 57 | rmCallback) |
Alexander Afanasyev | db49f4e | 2014-08-14 23:01:02 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 60 | |
| 61 | SyncSocket::~SyncSocket() |
| 62 | { |
| 63 | } |
| 64 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 65 | bool |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | SyncSocket::publishData(const Name &prefix, uint64_t session, |
| 67 | const char *buf, size_t len, |
| 68 | int freshness,uint64_t seq) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 69 | { |
| 70 | shared_ptr<Data> data = make_shared<Data>(); |
| 71 | data->setContent(reinterpret_cast<const uint8_t*>(buf), len); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 72 | data->setFreshnessPeriod(ndn::time::seconds(freshness)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 73 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 74 | m_face->getIoService().post(bind(&SyncSocket::publishDataInternal, this, |
| 75 | data, prefix, session,seq)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 76 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 77 | return true; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | SyncSocket::publishDataInternal(shared_ptr<Data> data, |
| 82 | const Name &prefix, uint64_t session, uint64_t seq) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 83 | { |
| 84 | uint64_t sequence = seq; |
| 85 | Name dataName = prefix; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 86 | dataName.append(boost::lexical_cast<string>(session)) |
| 87 | .append(boost::lexical_cast<string>(sequence)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 88 | data->setName(dataName); |
| 89 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 90 | m_keyChain->sign(*data); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 91 | m_face->put(*data); |
| 92 | |
| 93 | SeqNo s(session, sequence + 1); |
| 94 | |
| 95 | m_sequenceLog[prefix] = s; |
| 96 | m_syncLogic.addLocalNames (prefix, session, sequence); |
| 97 | } |
| 98 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 99 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, |
| 101 | const OnDataValidated& onValidated, int retry) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 102 | { |
| 103 | Name interestName = prefix; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 104 | interestName.append(boost::lexical_cast<string>(seq.getSession())) |
| 105 | .append(boost::lexical_cast<string>(seq.getSeq())); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 106 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 107 | const OnDataValidationFailed& onValidationFailed = |
| 108 | bind(&SyncSocket::onDataValidationFailed, this, _1); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 109 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 110 | ndn::Interest interest(interestName); |
| 111 | interest.setMustBeFresh(true); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 112 | m_face->expressInterest(interest, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 113 | bind(&SyncSocket::onData, this, _1, _2, |
| 114 | onValidated, onValidationFailed), |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 115 | bind(&SyncSocket::onDataTimeout, this, _1, retry, // Nack |
| 116 | onValidated, onValidationFailed), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 117 | bind(&SyncSocket::onDataTimeout, this, _1, retry, |
| 118 | onValidated, onValidationFailed)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 119 | |
| 120 | } |
| 121 | |
| 122 | void |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 123 | SyncSocket::onData(const ndn::Interest& interest, const Data& data, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 124 | const OnDataValidated& onValidated, |
| 125 | const OnDataValidationFailed& onValidationFailed) |
| 126 | { |
| 127 | m_validator->validate(data, onValidated, onValidationFailed); |
| 128 | } |
| 129 | |
| 130 | void |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 131 | SyncSocket::onDataTimeout(const ndn::Interest& interest, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 132 | int retry, |
| 133 | const OnDataValidated& onValidated, |
| 134 | const OnDataValidationFailed& onValidationFailed) |
| 135 | { |
| 136 | if(retry > 0) |
| 137 | { |
| 138 | m_face->expressInterest(interest, |
| 139 | bind(&SyncSocket::onData, |
| 140 | this, |
| 141 | _1, |
| 142 | _2, |
| 143 | onValidated, |
| 144 | onValidationFailed), |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 145 | bind(&SyncSocket::onDataTimeout, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 146 | this, |
| 147 | _1, |
| 148 | retry - 1, |
| 149 | onValidated, |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 150 | onValidationFailed), |
| 151 | bind(&SyncSocket::onDataTimeout, |
| 152 | this, |
| 153 | _1, |
| 154 | retry - 1, |
| 155 | onValidated, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 156 | onValidationFailed)); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 157 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 158 | } |
| 159 | else |
| 160 | _LOG_DEBUG("interest eventually time out!"); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data) |
| 165 | { |
| 166 | _LOG_DEBUG("data cannot be verified!"); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | uint64_t |
| 171 | SyncSocket::getNextSeq (const Name &prefix, uint64_t session) |
| 172 | { |
| 173 | SequenceLog::iterator i = m_sequenceLog.find (prefix); |
| 174 | |
| 175 | if (i != m_sequenceLog.end ()) |
| 176 | { |
| 177 | SeqNo s = i->second; |
| 178 | if (s.getSession() == session) |
| 179 | return s.getSeq(); |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | }//Sync |