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" |
| 22 | |
| 23 | using namespace std; |
| 24 | using namespace ndn; |
| 25 | |
| 26 | namespace Sync { |
| 27 | |
| 28 | SyncSocket::SyncSocket (const string &syncPrefix, |
| 29 | Ptr<SyncPolicyManager> syncPolicyManager, |
| 30 | NewDataCallback dataCallback, |
| 31 | RemoveCallback rmCallback ) |
| 32 | : m_newDataCallback(dataCallback) |
| 33 | , m_syncPolicyManager(syncPolicyManager) |
Yingdi Yu | 8600a09 | 2013-11-01 16:12:31 -0700 | [diff] [blame] | 34 | , m_handler(Ptr<Wrapper>(new Wrapper(Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), |
| 35 | m_syncPolicyManager, |
| 36 | NULL))))) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 37 | , m_syncLogic (syncPrefix, |
| 38 | syncPolicyManager, |
Yingdi Yu | 8600a09 | 2013-11-01 16:12:31 -0700 | [diff] [blame] | 39 | m_handler, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 40 | bind(&SyncSocket::passCallback, this, _1), |
| 41 | rmCallback) |
| 42 | { |
Yingdi Yu | 8600a09 | 2013-11-01 16:12:31 -0700 | [diff] [blame] | 43 | // Ptr<security::Keychain> keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), m_syncPolicyManager, NULL)); |
| 44 | // m_handler = Ptr<Wrapper>(new Wrapper(keychain)); |
| 45 | m_syncPolicyManager->setWrapper(m_handler); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | SyncSocket::~SyncSocket() |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame^] | 49 | { |
| 50 | m_handler->shutdown(); |
| 51 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 52 | |
| 53 | bool |
| 54 | SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness) |
| 55 | { |
| 56 | uint32_t sequence = getNextSeq(prefix, session); |
| 57 | ostringstream contentNameWithSeqno; |
| 58 | contentNameWithSeqno << prefix << "/" << session << "/" << sequence; |
| 59 | |
| 60 | Name dataName(contentNameWithSeqno.str ()); |
| 61 | Blob blob(buf, len); |
| 62 | Name signingIdentity = m_syncPolicyManager->inferSigningIdentity(dataName); |
| 63 | |
| 64 | m_handler->publishDataByIdentity (dataName, |
| 65 | blob, |
| 66 | signingIdentity, |
| 67 | freshness); |
| 68 | |
| 69 | SeqNo s(session, sequence + 1); |
| 70 | m_sequenceLog[prefix] = s; |
| 71 | m_syncLogic.addLocalNames (prefix, session, sequence); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | SyncSocket::fetchData(const string &prefix, const SeqNo &seq, const DataCallback& callback, int retry) |
| 77 | { |
| 78 | ostringstream interestName; |
| 79 | interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq(); |
| 80 | //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl; |
| 81 | |
| 82 | |
| 83 | Ptr<ndn::Interest> interestPtr = Ptr<ndn::Interest>(new ndn::Interest(interestName.str())); |
| 84 | Ptr<Closure> closure = Ptr<Closure> (new Closure(callback, |
| 85 | boost::bind(&SyncSocket::onChatDataTimeout, |
| 86 | this, |
| 87 | _1, |
| 88 | _2, |
| 89 | retry), |
| 90 | boost::bind(&SyncSocket::onChatDataUnverified, |
| 91 | this, |
| 92 | _1))); |
| 93 | m_handler->sendInterest(interestPtr, closure); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | SyncSocket::onChatDataTimeout(Ptr<Closure> closure, Ptr<ndn::Interest> interest, int retry) |
| 98 | { |
| 99 | if(retry > 0) |
| 100 | { |
| 101 | Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback, |
| 102 | boost::bind(&SyncSocket::onChatDataTimeout, |
| 103 | this, |
| 104 | _1, |
| 105 | _2, |
| 106 | retry - 1), |
| 107 | closure->m_unverifiedCallback, |
| 108 | closure->m_stepCount) |
| 109 | ); |
| 110 | m_handler->sendInterest(interest, newClosure); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | SyncSocket::onChatDataUnverified(Ptr<Data> data) |
| 116 | {} |
| 117 | |
| 118 | |
| 119 | uint32_t |
| 120 | SyncSocket::getNextSeq (const string &prefix, uint32_t session) |
| 121 | { |
| 122 | SequenceLog::iterator i = m_sequenceLog.find (prefix); |
| 123 | |
| 124 | if (i != m_sequenceLog.end ()) |
| 125 | { |
| 126 | SeqNo s = i->second; |
| 127 | if (s.getSession() == session) |
| 128 | return s.getSeq(); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | }//Sync |