blob: 43f6b540106dcff8068a209b919c01b39524d42d [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"
Zhenkai Zhu5548e032012-03-12 20:10:15 -070024#include <boost/throw_exception.hpp>
25typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
26typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080027
28using namespace std;
29using namespace boost;
30
31namespace Sync
32{
33
Zhenkai Zhu5548e032012-03-12 20:10:15 -070034
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080035string
36AppDataPublish::getRecentData (const string &prefix, uint32_t session)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080037{
Alexander Afanasyev750d1872012-03-12 15:33:56 -070038 if (m_recentData.find(make_pair(prefix, session)) != m_recentData.end())
39 return m_recentData[make_pair(prefix, session)];
40 else
41 return "";
Chaoyi Bian3e99d862012-03-07 17:28:32 -080042}
43
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080044uint32_t
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070045AppDataPublish::getNextSeq (const string &prefix, uint32_t session)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080046{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070047 SequenceLog::iterator i = m_sequenceLog.find (prefix);
Chaoyi Bian3e99d862012-03-07 17:28:32 -080048
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070049 if (i != m_sequenceLog.end ())
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080050 {
51 Seq s = i->second;
52 if (s.session == session)
53 return s.seq;
54 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070055 else
56 return 0;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080057}
58
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070059uint32_t
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080060AppDataPublish::publishData (const string &name, uint32_t session, const string &dataBuffer, int freshness)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080061{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070062 uint32_t seq = getNextSeq (name, session);
Chaoyi Bian4194b742012-03-08 17:21:35 -080063
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080064 ostringstream contentNameWithSeqno;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070065 contentNameWithSeqno << name << "/" << session << "/" << seq;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080066
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080067 m_ccnxHandle->publishData (contentNameWithSeqno.str (), dataBuffer, freshness);
Chaoyi Bian3e99d862012-03-07 17:28:32 -080068
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070069 Seq s;
70 s.session = session;
71 s.seq = seq + 1;
72 m_sequenceLog[name] = s;
73
Zhenkai Zhub951a622012-03-13 22:37:37 -070074 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 Zhubc7bfce2012-03-09 14:37:52 -080078
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070079 return seq;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080080}
81
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070082} // Sync