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 | db49f4e | 2014-08-14 23:01:02 -0700 | [diff] [blame] | 23 | #include <boost/lexical_cast.hpp> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 24 | |
| 25 | using namespace std; |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 26 | |
| 27 | using ndn::Data; |
| 28 | using ndn::Face; |
| 29 | using ndn::KeyChain; |
| 30 | using ndn::Name; |
| 31 | using ndn::OnDataValidated; |
| 32 | using ndn::OnDataValidationFailed; |
| 33 | using ndn::Validator; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 34 | |
| 35 | INIT_LOGGER ("SyncSocket"); |
| 36 | |
| 37 | namespace Sync { |
| 38 | |
| 39 | using ndn::shared_ptr; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 40 | using ndn::make_shared; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 41 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 42 | SyncSocket::SyncSocket (const Name &syncPrefix, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 43 | shared_ptr<Validator> validator, |
| 44 | shared_ptr<Face> face, |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 45 | NewDataCallback dataCallback, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 46 | RemoveCallback rmCallback ) |
| 47 | : m_newDataCallback(dataCallback) |
| 48 | , m_validator(validator) |
| 49 | , m_keyChain(new KeyChain()) |
| 50 | , m_face(face) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 51 | , m_syncLogic (syncPrefix, |
| 52 | validator, |
| 53 | face, |
| 54 | bind(&SyncSocket::passCallback, this, _1), |
| 55 | rmCallback) |
Alexander Afanasyev | db49f4e | 2014-08-14 23:01:02 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 58 | |
| 59 | SyncSocket::~SyncSocket() |
| 60 | { |
| 61 | } |
| 62 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 63 | bool |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 64 | SyncSocket::publishData(const Name &prefix, uint64_t session, |
| 65 | const char *buf, size_t len, |
| 66 | int freshness,uint64_t seq) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 67 | { |
| 68 | shared_ptr<Data> data = make_shared<Data>(); |
| 69 | data->setContent(reinterpret_cast<const uint8_t*>(buf), len); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 70 | data->setFreshnessPeriod(ndn::time::seconds(freshness)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 71 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 72 | m_face->getIoService().post(bind(&SyncSocket::publishDataInternal, this, |
| 73 | data, prefix, session,seq)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 74 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 75 | return true; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | SyncSocket::publishDataInternal(shared_ptr<Data> data, |
| 80 | const Name &prefix, uint64_t session, uint64_t seq) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 81 | { |
| 82 | uint64_t sequence = seq; |
| 83 | Name dataName = prefix; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 84 | dataName.append(boost::lexical_cast<string>(session)) |
| 85 | .append(boost::lexical_cast<string>(sequence)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 86 | data->setName(dataName); |
| 87 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 88 | m_keyChain->sign(*data); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 89 | m_face->put(*data); |
| 90 | |
| 91 | SeqNo s(session, sequence + 1); |
| 92 | |
| 93 | m_sequenceLog[prefix] = s; |
| 94 | m_syncLogic.addLocalNames (prefix, session, sequence); |
| 95 | } |
| 96 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 97 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 98 | SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, |
| 99 | const OnDataValidated& onValidated, int retry) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 100 | { |
| 101 | Name interestName = prefix; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 102 | interestName.append(boost::lexical_cast<string>(seq.getSession())) |
| 103 | .append(boost::lexical_cast<string>(seq.getSeq())); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 104 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 105 | const OnDataValidationFailed& onValidationFailed = |
| 106 | bind(&SyncSocket::onDataValidationFailed, this, _1); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 107 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 108 | ndn::Interest interest(interestName); |
| 109 | interest.setMustBeFresh(true); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 110 | m_face->expressInterest(interest, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 111 | bind(&SyncSocket::onData, this, _1, _2, |
| 112 | onValidated, onValidationFailed), |
| 113 | bind(&SyncSocket::onDataTimeout, this, _1, retry, |
| 114 | onValidated, onValidationFailed)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 115 | |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | SyncSocket::onData(const ndn::Interest& interest, Data& data, |
| 120 | const OnDataValidated& onValidated, |
| 121 | const OnDataValidationFailed& onValidationFailed) |
| 122 | { |
| 123 | m_validator->validate(data, onValidated, onValidationFailed); |
| 124 | } |
| 125 | |
| 126 | void |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 127 | SyncSocket::onDataTimeout(const ndn::Interest& interest, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 128 | int retry, |
| 129 | const OnDataValidated& onValidated, |
| 130 | const OnDataValidationFailed& onValidationFailed) |
| 131 | { |
| 132 | if(retry > 0) |
| 133 | { |
| 134 | m_face->expressInterest(interest, |
| 135 | bind(&SyncSocket::onData, |
| 136 | this, |
| 137 | _1, |
| 138 | _2, |
| 139 | onValidated, |
| 140 | onValidationFailed), |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 141 | bind(&SyncSocket::onDataTimeout, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 142 | this, |
| 143 | _1, |
| 144 | retry - 1, |
| 145 | onValidated, |
| 146 | onValidationFailed)); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 147 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | _LOG_DEBUG("interest eventually time out!"); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data) |
| 155 | { |
| 156 | _LOG_DEBUG("data cannot be verified!"); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | uint64_t |
| 161 | SyncSocket::getNextSeq (const Name &prefix, uint64_t session) |
| 162 | { |
| 163 | SequenceLog::iterator i = m_sequenceLog.find (prefix); |
| 164 | |
| 165 | if (i != m_sequenceLog.end ()) |
| 166 | { |
| 167 | SeqNo s = i->second; |
| 168 | if (s.getSession() == session) |
| 169 | return s.getSeq(); |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | }//Sync |