Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [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" |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 22 | #include "sync-logging.h" |
| 23 | |
| 24 | #include <ndn-cpp/security/identity/basic-identity-storage.hpp> |
| 25 | #include <ndn-cpp/security/identity/osx-private-key-storage.hpp> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
| 28 | using namespace ndn; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 29 | using namespace ndn::ptr_lib; |
| 30 | |
| 31 | INIT_LOGGER ("SyncSocket"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 32 | |
| 33 | namespace Sync { |
| 34 | |
| 35 | SyncSocket::SyncSocket (const string &syncPrefix, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 36 | shared_ptr<SyncPolicyManager> syncPolicyManager, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 37 | NewDataCallback dataCallback, |
| 38 | RemoveCallback rmCallback ) |
| 39 | : m_newDataCallback(dataCallback) |
| 40 | , m_syncPolicyManager(syncPolicyManager) |
| 41 | , m_syncLogic (syncPrefix, |
| 42 | syncPolicyManager, |
| 43 | bind(&SyncSocket::passCallback, this, _1), |
| 44 | rmCallback) |
| 45 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 46 | m_transport = make_shared<TcpTransport>(); |
| 47 | m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost")); |
| 48 | |
| 49 | connectToDaemon(); |
| 50 | |
| 51 | shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>(); |
| 52 | shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>(); |
| 53 | m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | SyncSocket::~SyncSocket() |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame] | 57 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 58 | m_face->shutdown(); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | SyncSocket::connectToDaemon() |
| 63 | { |
| 64 | //Hack! transport does not connect to daemon unless an interest is expressed. |
| 65 | Name name("/ndn"); |
| 66 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name); |
| 67 | m_face->expressInterest(*interest, |
| 68 | bind(&SyncSocket::onConnectionData, this, _1, _2), |
| 69 | bind(&SyncSocket::onConnectionDataTimeout, this, _1)); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | SyncSocket::onConnectionData(const shared_ptr<const ndn::Interest>& interest, |
| 74 | const shared_ptr<Data>& data) |
| 75 | { |
| 76 | _LOG_DEBUG("onConnectionData"); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | SyncSocket::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest) |
| 81 | { |
| 82 | _LOG_DEBUG("onConnectionDataTimeout"); |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame] | 83 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 84 | |
| 85 | bool |
| 86 | SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness) |
| 87 | { |
| 88 | uint32_t sequence = getNextSeq(prefix, session); |
| 89 | ostringstream contentNameWithSeqno; |
| 90 | contentNameWithSeqno << prefix << "/" << session << "/" << sequence; |
| 91 | |
| 92 | Name dataName(contentNameWithSeqno.str ()); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 93 | Blob blob((const uint8_t*)buf, len); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 94 | Name signingIdentity = m_syncPolicyManager->inferSigningIdentity(dataName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 95 | |
| 96 | shared_ptr<Data> data = make_shared<Data>(dataName); |
| 97 | data->setContent(blob.buf(), blob.size()); |
| 98 | data->getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
| 99 | |
| 100 | Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(signingIdentity); |
| 101 | m_identityManager->signByCertificate(*data, certificateName); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 102 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 103 | m_transport->send(*data->wireEncode()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 104 | |
| 105 | SeqNo s(session, sequence + 1); |
| 106 | m_sequenceLog[prefix] = s; |
| 107 | m_syncLogic.addLocalNames (prefix, session, sequence); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 112 | SyncSocket::fetchData(const string &prefix, const SeqNo &seq, const OnVerified& onVerified, int retry) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 113 | { |
| 114 | ostringstream interestName; |
| 115 | interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq(); |
| 116 | //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 117 | |
| 118 | const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onChatDataVerifyFailed, this, _1); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 119 | |
| 120 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 121 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName.str()); |
| 122 | m_face->expressInterest(*interest, |
| 123 | bind(&SyncSocket::onChatData, this, _1, _2, onVerified, onVerifyFailed), |
| 124 | bind(&SyncSocket::onChatDataTimeout, this, _1, retry, onVerified, onVerifyFailed)); |
| 125 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 129 | SyncSocket::onChatCert(const shared_ptr<const ndn::Interest>& interest, |
| 130 | const shared_ptr<Data>& cert, |
| 131 | shared_ptr<ValidationRequest> previousStep) |
| 132 | { |
| 133 | shared_ptr<ValidationRequest> nextStep = m_syncPolicyManager->checkVerificationPolicy(cert, |
| 134 | previousStep->stepCount_, |
| 135 | previousStep->onVerified_, |
| 136 | previousStep->onVerifyFailed_); |
| 137 | |
| 138 | if (nextStep) |
| 139 | m_face->expressInterest |
| 140 | (*nextStep->interest_, |
| 141 | bind(&SyncSocket::onChatCert, this, _1, _2, nextStep), |
| 142 | bind(&SyncSocket::onChatCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep)); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | SyncSocket::onChatCertTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 147 | const OnVerifyFailed& onVerifyFailed, |
| 148 | const shared_ptr<Data>& data, |
| 149 | shared_ptr<ValidationRequest> nextStep) |
| 150 | { |
| 151 | if(nextStep->retry_ > 0) |
| 152 | m_face->expressInterest(*interest, |
| 153 | bind(&SyncSocket::onChatCert, |
| 154 | this, |
| 155 | _1, |
| 156 | _2, |
| 157 | nextStep), |
| 158 | bind(&SyncSocket::onChatCertTimeout, |
| 159 | this, |
| 160 | _1, |
| 161 | onVerifyFailed, |
| 162 | data, |
| 163 | nextStep)); |
| 164 | else |
| 165 | onVerifyFailed(data); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | SyncSocket::onChatData(const shared_ptr<const ndn::Interest>& interest, |
| 170 | const shared_ptr<Data>& data, |
| 171 | const OnVerified& onVerified, |
| 172 | const OnVerifyFailed& onVerifyFailed) |
| 173 | { |
| 174 | shared_ptr<ValidationRequest> nextStep = m_syncPolicyManager->checkVerificationPolicy(data, 0, onVerified, onVerifyFailed); |
| 175 | |
| 176 | if (nextStep) |
| 177 | m_face->expressInterest |
| 178 | (*nextStep->interest_, |
| 179 | bind(&SyncSocket::onChatCert, this, _1, _2, nextStep), |
| 180 | bind(&SyncSocket::onChatCertTimeout, this, _1, onVerifyFailed, data, nextStep)); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | SyncSocket::onChatDataTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 185 | int retry, |
| 186 | const OnVerified& onVerified, |
| 187 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 188 | { |
| 189 | if(retry > 0) |
| 190 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 191 | m_face->expressInterest(*interest, |
| 192 | bind(&SyncSocket::onChatData, |
| 193 | this, |
| 194 | _1, |
| 195 | _2, |
| 196 | onVerified, |
| 197 | onVerifyFailed), |
| 198 | bind(&SyncSocket::onChatDataTimeout, |
| 199 | this, |
| 200 | _1, |
| 201 | retry - 1, |
| 202 | onVerified, |
| 203 | onVerifyFailed)); |
| 204 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 205 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 206 | else |
| 207 | _LOG_DEBUG("Chat interest eventually time out!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 211 | SyncSocket::onChatDataVerifyFailed(const shared_ptr<Data>& data) |
| 212 | { |
| 213 | _LOG_DEBUG("Chat data cannot be verified!"); |
| 214 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 215 | |
| 216 | |
| 217 | uint32_t |
| 218 | SyncSocket::getNextSeq (const string &prefix, uint32_t session) |
| 219 | { |
| 220 | SequenceLog::iterator i = m_sequenceLog.find (prefix); |
| 221 | |
| 222 | if (i != m_sequenceLog.end ()) |
| 223 | { |
| 224 | SeqNo s = i->second; |
| 225 | if (s.getSession() == session) |
| 226 | return s.getSeq(); |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | }//Sync |