blob: 0e4e8293f1076c6ce600b10ac1a417203aee36be [file] [log] [blame]
Alexander Afanasyevce001692013-07-14 11:34:41 -07001/* -*- Mode: C32++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Zhenkai Zhubed78952012-03-06 11:06:08 -08002/*
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 Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhubed78952012-03-06 11:06:08 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080022
23#include "sync-app-socket.h"
24
25using namespace std;
26using namespace boost;
27
28namespace Sync
29{
30
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070031SyncAppSocket::SyncAppSocket (const string &syncPrefix, NewDataCallback dataCallback, RemoveCallback rmCallback )
Alexander Afanasyevce001692013-07-14 11:34:41 -070032 : m_newDataCallback(dataCallback)
33 , m_ccnxHandle (new CcnxWrapper())
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080034 , m_syncLogic (syncPrefix,
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070035 bind(&SyncAppSocket::passCallback, this, _1),
36 rmCallback)
Chaoyi Bian11f294f2012-03-08 14:28:06 -080037{
Chaoyi Bian11f294f2012-03-08 14:28:06 -080038}
39
40SyncAppSocket::~SyncAppSocket()
41{
Chaoyi Bian11f294f2012-03-08 14:28:06 -080042}
43
Zhenkai Zhue7617672012-10-15 13:51:42 -070044std::string
45SyncAppSocket::GetLocalPrefix()
46{
47 // this handle is supposed to be short lived
Zhenkai Zhu9587cd62012-10-17 11:58:37 -070048 CcnxWrapperPtr handle( new CcnxWrapper());
Zhenkai Zhue7617672012-10-15 13:51:42 -070049 return handle->getLocalPrefix();
50}
51
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070052bool
53SyncAppSocket::publishString (const string &prefix, uint32_t session, const string &dataBuffer, int freshness)
Chaoyi Bian11f294f2012-03-08 14:28:06 -080054{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070055 uint32_t sequence = getNextSeq(prefix, session);
56 ostringstream contentNameWithSeqno;
57 contentNameWithSeqno << prefix << "/" << session << "/" << sequence;
58 m_ccnxHandle->publishStringData (contentNameWithSeqno.str (), dataBuffer, freshness);
59
60 SeqNo s(session, sequence + 1);
61 m_sequenceLog[prefix] = s;
62
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070063 m_syncLogic.addLocalNames (prefix, session, sequence);
Alexander Afanasyevce001692013-07-14 11:34:41 -070064 return true;
Chaoyi Bian11f294f2012-03-08 14:28:06 -080065}
66
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070067bool
68SyncAppSocket::publishRaw(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness)
69{
70 uint32_t sequence = getNextSeq(prefix, session);
71 ostringstream contentNameWithSeqno;
72 contentNameWithSeqno << prefix << "/" << session << "/" << sequence;
Zhenkai Zhudc70a292012-06-01 14:00:59 -070073
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070074 m_ccnxHandle->publishRawData (contentNameWithSeqno.str (), buf, len, freshness);
75
76 SeqNo s(session, sequence + 1);
77 m_sequenceLog[prefix] = s;
78 m_syncLogic.addLocalNames (prefix, session, sequence);
Alexander Afanasyevce001692013-07-14 11:34:41 -070079 return true;
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070080}
81
82
83void
84SyncAppSocket::fetchString(const std::string &prefix, const SeqNo &seq, CcnxWrapper::StringDataCallback callback, int retry)
85{
86 ostringstream interestName;
87 interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq();
88 m_ccnxHandle->sendInterestForString(interestName.str(), callback, retry);
89}
90
91void
92SyncAppSocket::fetchRaw(const std::string &prefix, const SeqNo &seq, CcnxWrapper::RawDataCallback callback, int retry)
93{
94 ostringstream interestName;
95 interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq();
Zhenkai Zhudc70a292012-06-01 14:00:59 -070096 //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl;
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070097 m_ccnxHandle->sendInterest(interestName.str(), callback, retry);
98}
99
100uint32_t
101SyncAppSocket::getNextSeq (const string &prefix, uint32_t session)
102{
103 SequenceLog::iterator i = m_sequenceLog.find (prefix);
104
105 if (i != m_sequenceLog.end ())
106 {
107 SeqNo s = i->second;
108 if (s.getSession() == session)
109 return s.getSeq();
110 }
111 return 0;
112}
113
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800114}
Zhenkai Zhue5660932012-06-04 15:25:20 -0700115