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