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 | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 36 | shared_ptr<SyncPolicyManager> syncPolicyManager, |
| 37 | shared_ptr<Face> face, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 38 | NewDataCallback dataCallback, |
| 39 | RemoveCallback rmCallback ) |
| 40 | : m_newDataCallback(dataCallback) |
| 41 | , m_syncPolicyManager(syncPolicyManager) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame^] | 42 | , m_verifier(new Verifier(syncPolicyManager)) |
| 43 | , m_keyChain(new KeyChain()) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 44 | , m_face(face) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 45 | , m_syncLogic (syncPrefix, |
| 46 | syncPolicyManager, |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 47 | face, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 48 | bind(&SyncSocket::passCallback, this, _1), |
| 49 | rmCallback) |
| 50 | { |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame^] | 51 | m_verifier->setFace(face); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | SyncSocket::~SyncSocket() |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame] | 55 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 58 | bool |
| 59 | SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness) |
| 60 | { |
| 61 | uint32_t sequence = getNextSeq(prefix, session); |
| 62 | ostringstream contentNameWithSeqno; |
| 63 | contentNameWithSeqno << prefix << "/" << session << "/" << sequence; |
| 64 | |
| 65 | Name dataName(contentNameWithSeqno.str ()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 66 | Name signingIdentity = m_syncPolicyManager->inferSigningIdentity(dataName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 67 | |
| 68 | shared_ptr<Data> data = make_shared<Data>(dataName); |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame^] | 69 | data->setContent(reinterpret_cast<const uint8_t*>(buf), len); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 70 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame^] | 71 | Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(signingIdentity); |
| 72 | m_keyChain->sign(*data, certificateName); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 73 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame^] | 74 | m_face->put(*data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 75 | |
| 76 | SeqNo s(session, sequence + 1); |
| 77 | m_sequenceLog[prefix] = s; |
| 78 | m_syncLogic.addLocalNames (prefix, session, sequence); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 83 | 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] | 84 | { |
| 85 | ostringstream interestName; |
| 86 | interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq(); |
| 87 | //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 88 | |
| 89 | const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onChatDataVerifyFailed, this, _1); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 90 | |
| 91 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 92 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName.str()); |
| 93 | m_face->expressInterest(*interest, |
| 94 | bind(&SyncSocket::onChatData, this, _1, _2, onVerified, onVerifyFailed), |
| 95 | bind(&SyncSocket::onChatDataTimeout, this, _1, retry, onVerified, onVerifyFailed)); |
| 96 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 100 | SyncSocket::onChatData(const shared_ptr<const ndn::Interest>& interest, |
| 101 | const shared_ptr<Data>& data, |
| 102 | const OnVerified& onVerified, |
| 103 | const OnVerifyFailed& onVerifyFailed) |
| 104 | { |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame^] | 105 | m_verifier->verifyData(data, onVerified, onVerifyFailed); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | SyncSocket::onChatDataTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 110 | int retry, |
| 111 | const OnVerified& onVerified, |
| 112 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 113 | { |
| 114 | if(retry > 0) |
| 115 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 116 | m_face->expressInterest(*interest, |
| 117 | bind(&SyncSocket::onChatData, |
| 118 | this, |
| 119 | _1, |
| 120 | _2, |
| 121 | onVerified, |
| 122 | onVerifyFailed), |
| 123 | bind(&SyncSocket::onChatDataTimeout, |
| 124 | this, |
| 125 | _1, |
| 126 | retry - 1, |
| 127 | onVerified, |
| 128 | onVerifyFailed)); |
| 129 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 130 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 131 | else |
| 132 | _LOG_DEBUG("Chat interest eventually time out!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 136 | SyncSocket::onChatDataVerifyFailed(const shared_ptr<Data>& data) |
| 137 | { |
| 138 | _LOG_DEBUG("Chat data cannot be verified!"); |
| 139 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 140 | |
| 141 | |
| 142 | uint32_t |
| 143 | SyncSocket::getNextSeq (const string &prefix, uint32_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 |