blob: e7bee973e3dd876d663b448ffa1a3f55cc26914b [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080020 * 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 }
Chaoyi Biana5048172012-04-03 15:21:50 -070055 return 0;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080056}
57
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070058uint32_t
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080059AppDataPublish::publishData (const string &name, uint32_t session, const string &dataBuffer, int freshness)
Chaoyi Bian3e99d862012-03-07 17:28:32 -080060{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070061 uint32_t seq = getNextSeq (name, session);
Chaoyi Bian4194b742012-03-08 17:21:35 -080062
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080063 ostringstream contentNameWithSeqno;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070064 contentNameWithSeqno << name << "/" << session << "/" << seq;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080065
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080066 m_ccnxHandle->publishData (contentNameWithSeqno.str (), dataBuffer, freshness);
Chaoyi Bian3e99d862012-03-07 17:28:32 -080067
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070068 Seq s;
69 s.session = session;
70 s.seq = seq + 1;
71 m_sequenceLog[name] = s;
72
Zhenkai Zhub951a622012-03-13 22:37:37 -070073 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 Zhubc7bfce2012-03-09 14:37:52 -080077
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070078 return seq;
Chaoyi Bian3e99d862012-03-07 17:28:32 -080079}
80
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070081} // Sync