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