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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 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 | } |
Chaoyi Bian | a504817 | 2012-04-03 15:21:50 -0700 | [diff] [blame] | 55 | return 0; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 58 | uint32_t |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 59 | 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] | 60 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 61 | uint32_t seq = getNextSeq (name, session); |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 63 | ostringstream contentNameWithSeqno; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 64 | contentNameWithSeqno << name << "/" << session << "/" << seq; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 66 | m_ccnxHandle->publishData (contentNameWithSeqno.str (), dataBuffer, freshness); |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 67 | |
Zhenkai Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 68 | Seq s; |
| 69 | s.session = session; |
| 70 | s.seq = seq + 1; |
| 71 | m_sequenceLog[name] = s; |
| 72 | |
Zhenkai Zhu | b951a62 | 2012-03-13 22:37:37 -0700 | [diff] [blame] | 73 | unordered_map<pair<string, uint32_t>, string>::iterator it = m_recentData.find(make_pair(name, session)); |
| 74 | if (it != m_recentData.end()) |
| 75 | m_recentData.erase(it); |
| 76 | m_recentData.insert(make_pair(make_pair(name, session), dataBuffer)); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 78 | return seq; |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 81 | } // Sync |