Zhenkai Zhu | bed7895 | 2012-03-06 11:06:08 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; 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: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | bed7895 | 2012-03-06 11:06:08 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-app-socket.h" |
| 24 | |
| 25 | using namespace std; |
| 26 | using namespace boost; |
| 27 | |
| 28 | namespace Sync |
| 29 | { |
| 30 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 31 | SyncAppSocket::SyncAppSocket (const string &syncPrefix, NewDataCallback dataCallback, RemoveCallback rmCallback ) |
Alexander Afanasyev | 3aaea67 | 2012-10-05 15:25:11 -0700 | [diff] [blame] | 32 | : m_ccnxHandle (CcnxWrapper::Create ()) |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 33 | , m_newDataCallback(dataCallback) |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 34 | , m_syncLogic (syncPrefix, |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 35 | bind(&SyncAppSocket::passCallback, this, _1), |
| 36 | rmCallback) |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 37 | { |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | SyncAppSocket::~SyncAppSocket() |
| 41 | { |
Alexander Afanasyev | 60126e0 | 2012-10-05 16:54:42 -0700 | [diff] [blame] | 42 | CcnxWrapper::Destroy (); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 45 | bool |
| 46 | SyncAppSocket::publishString (const string &prefix, uint32_t session, const string &dataBuffer, int freshness) |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 47 | { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 48 | uint32_t sequence = getNextSeq(prefix, session); |
| 49 | ostringstream contentNameWithSeqno; |
| 50 | contentNameWithSeqno << prefix << "/" << session << "/" << sequence; |
| 51 | m_ccnxHandle->publishStringData (contentNameWithSeqno.str (), dataBuffer, freshness); |
| 52 | |
| 53 | SeqNo s(session, sequence + 1); |
| 54 | m_sequenceLog[prefix] = s; |
| 55 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 56 | m_syncLogic.addLocalNames (prefix, session, sequence); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 59 | bool |
| 60 | SyncAppSocket::publishRaw(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness) |
| 61 | { |
| 62 | uint32_t sequence = getNextSeq(prefix, session); |
| 63 | ostringstream contentNameWithSeqno; |
| 64 | contentNameWithSeqno << prefix << "/" << session << "/" << sequence; |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 65 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 66 | m_ccnxHandle->publishRawData (contentNameWithSeqno.str (), buf, len, freshness); |
| 67 | |
| 68 | SeqNo s(session, sequence + 1); |
| 69 | m_sequenceLog[prefix] = s; |
| 70 | m_syncLogic.addLocalNames (prefix, session, sequence); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void |
| 75 | SyncAppSocket::fetchString(const std::string &prefix, const SeqNo &seq, CcnxWrapper::StringDataCallback callback, int retry) |
| 76 | { |
| 77 | ostringstream interestName; |
| 78 | interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq(); |
| 79 | m_ccnxHandle->sendInterestForString(interestName.str(), callback, retry); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | SyncAppSocket::fetchRaw(const std::string &prefix, const SeqNo &seq, CcnxWrapper::RawDataCallback callback, int retry) |
| 84 | { |
| 85 | ostringstream interestName; |
| 86 | interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq(); |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 87 | //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl; |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 88 | m_ccnxHandle->sendInterest(interestName.str(), callback, retry); |
| 89 | } |
| 90 | |
| 91 | uint32_t |
| 92 | SyncAppSocket::getNextSeq (const string &prefix, uint32_t session) |
| 93 | { |
| 94 | SequenceLog::iterator i = m_sequenceLog.find (prefix); |
| 95 | |
| 96 | if (i != m_sequenceLog.end ()) |
| 97 | { |
| 98 | SeqNo s = i->second; |
| 99 | if (s.getSession() == session) |
| 100 | return s.getSeq(); |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 105 | } |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 106 | |