blob: 17d52ee485af3c27fd10f1af134f3cc6638b0122 [file] [log] [blame]
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -08001/* -*- 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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian3e99d862012-03-07 17:28:32 -080022
23#include "sync-app-data-publish.h"
24
25using namespace std;
26using namespace boost;
27
28namespace Sync
29{
30
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080031string
32AppDataPublish::getRecentData (const string &prefix, uint32_t session)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080033{
Alexander Afanasyev750d1872012-03-12 15:33:56 -070034 if (m_recentData.find(make_pair(prefix, session)) != m_recentData.end())
35 return m_recentData[make_pair(prefix, session)];
36 else
37 return "";
Chaoyi Bian3e99d862012-03-07 17:28:32 -080038}
39
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080040uint32_t
41AppDataPublish::getHighestSeq (const string &prefix, uint32_t session)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080042{
Chaoyi Bian4194b742012-03-08 17:21:35 -080043 unordered_map<string, Seq>::iterator i = m_sequenceLog.find(prefix);
Chaoyi Bian3e99d862012-03-07 17:28:32 -080044
45 if (i != m_sequenceLog.end())
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080046 {
47 Seq s = i->second;
48 if (s.session == session)
49 return s.seq;
50 }
Chaoyi Bian3e99d862012-03-07 17:28:32 -080051
Chaoyi Bian4194b742012-03-08 17:21:35 -080052 return 0;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080053}
54
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080055bool
56AppDataPublish::publishData (const string &name, uint32_t session, const string &dataBuffer, int freshness)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080057{
Chaoyi Bian4194b742012-03-08 17:21:35 -080058 uint32_t seq = getHighestSeq(name, session);
59 if (seq == 0)
60 m_sequenceLog.erase(name);
61
62 seq++;
63 if (seq == 0)
64 seq = 1;
65 Seq s;
66 s.session = session;
67 s.seq = seq;
68 m_sequenceLog[name] = s;
69
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080070 ostringstream contentNameWithSeqno;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070071 contentNameWithSeqno << name << "/" << session << "/" << seq;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080072
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080073 m_ccnxHandle->publishData (contentNameWithSeqno.str (), dataBuffer, freshness);
Chaoyi Bian3e99d862012-03-07 17:28:32 -080074
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080075 unordered_map<pair<string, uint32_t>, string>::iterator it = m_recentData.find(make_pair(name, session));
Alexander Afanasyev750d1872012-03-12 15:33:56 -070076 if (it != m_recentData.end())
77 m_recentData.erase(it);
78 m_recentData.insert(make_pair(make_pair(name, session), dataBuffer));
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080079
Chaoyi Bian3e99d862012-03-07 17:28:32 -080080 return true;
81}
82
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080083}