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