Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -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> |
| 19 | * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-app-data-publish.h" |
| 24 | |
| 25 | using namespace std; |
| 26 | using namespace boost; |
| 27 | |
| 28 | namespace Sync |
| 29 | { |
| 30 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 31 | string |
| 32 | AppDataPublish::getRecentData (const string &prefix, uint32_t session) |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 33 | { |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 34 | if (m_recentData.find(make_pair(prefix, session)) != m_recentData.end()) |
| 35 | return m_recentData[make_pair(prefix, session)]; |
| 36 | else |
| 37 | return ""; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 40 | uint32_t |
| 41 | AppDataPublish::getHighestSeq (const string &prefix, uint32_t session) |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 42 | { |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 43 | unordered_map<string, Seq>::iterator i = m_sequenceLog.find(prefix); |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 44 | |
| 45 | if (i != m_sequenceLog.end()) |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 46 | { |
| 47 | Seq s = i->second; |
| 48 | if (s.session == session) |
| 49 | return s.seq; |
| 50 | } |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 51 | |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 52 | return 0; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 55 | bool |
| 56 | AppDataPublish::publishData (const string &name, uint32_t session, const string &dataBuffer, int freshness) |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 57 | { |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 58 | 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 Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 70 | ostringstream contentNameWithSeqno; |
| 71 | contentNameWithSeqno << name << "/" << seq; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 73 | m_ccnxHandle->publishData (contentNameWithSeqno.str (), dataBuffer, freshness); |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 74 | |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 75 | unordered_map<pair<string, uint32_t>, string>::iterator it = m_recentData.find(make_pair(name, session)); |
| 76 | if (it != m_recentData.end()) |
| 77 | m_recentData.erase(it); |
| 78 | m_recentData.insert(make_pair(make_pair(name, session), dataBuffer)); |
| 79 | |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 83 | } |