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" |
Zhenkai Zhu | 5548e03 | 2012-03-12 20:10:15 -0700 | [diff] [blame] | 24 | #include <boost/throw_exception.hpp> |
| 25 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 26 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace std; |
| 29 | using namespace boost; |
| 30 | |
| 31 | namespace Sync |
| 32 | { |
| 33 | |
Zhenkai Zhu | 5548e03 | 2012-03-12 20:10:15 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 35 | string |
| 36 | AppDataPublish::getRecentData (const string &prefix, uint32_t session) |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 37 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 38 | if (m_recentData.find(make_pair(prefix, session)) != m_recentData.end()) |
| 39 | return m_recentData[make_pair(prefix, session)]; |
| 40 | else |
| 41 | return ""; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 44 | uint32_t |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 45 | AppDataPublish::getNextSeq (const string &prefix, uint32_t session) |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 46 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 47 | SequenceLog::iterator i = m_sequenceLog.find (prefix); |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 49 | if (i != m_sequenceLog.end ()) |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 50 | { |
| 51 | Seq s = i->second; |
| 52 | if (s.session == session) |
| 53 | return s.seq; |
| 54 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 55 | else |
| 56 | return 0; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 59 | uint32_t |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 60 | 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] | 61 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 62 | uint32_t seq = getNextSeq (name, session); |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 64 | ostringstream contentNameWithSeqno; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 65 | contentNameWithSeqno << name << "/" << session << "/" << seq; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 67 | m_ccnxHandle->publishData (contentNameWithSeqno.str (), dataBuffer, freshness); |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 68 | |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 69 | Seq s; |
| 70 | s.session = session; |
| 71 | s.seq = seq + 1; |
| 72 | m_sequenceLog[name] = s; |
| 73 | |
Zhenkai Zhu | b951a62 | 2012-03-13 22:37:37 -0700 | [diff] [blame] | 74 | unordered_map<pair<string, uint32_t>, string>::iterator it = m_recentData.find(make_pair(name, session)); |
| 75 | if (it != m_recentData.end()) |
| 76 | m_recentData.erase(it); |
| 77 | m_recentData.insert(make_pair(make_pair(name, session), dataBuffer)); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 79 | return seq; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 82 | } // Sync |