blob: 9a2c6ffecb9ed90da87b1edfe8f7ae48d7581ec5 [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 */
22
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080023#ifndef SYNC_APP_DATA_PUBLISH_H
24#define SYNC_APP_DATA_PUBLISH_H
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080025
Zhenkai Zhub951a622012-03-13 22:37:37 -070026#include <boost/unordered_map.hpp>
Chaoyi Bian4194b742012-03-08 17:21:35 -080027#include "sync-seq-no.h"
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080028#include "sync-ccnx-wrapper.h"
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080029#include <utility>
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070030#include <map>
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080031
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080032namespace Sync {
33
Chaoyi Bian4194b742012-03-08 17:21:35 -080034struct Seq
35{
36 uint32_t session;
37 uint32_t seq;
38};
39
Zhenkai Zhu5548e032012-03-12 20:10:15 -070040struct GetSeqException : virtual boost::exception, virtual std::exception { };
41
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080042/**
43 * \ingroup sync
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080044 * @brief publishes application data using incrementing sequence number (for
Chaoyi Bian3e99d862012-03-07 17:28:32 -080045 * each sequence namber and keeps track of most recently published data for
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080046 * each name prefix
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080047 */
48class AppDataPublish
49{
50public:
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080051 AppDataPublish (CcnxWrapperPtr ccnxHandle)
52 : m_ccnxHandle (ccnxHandle)
53 { }
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080054
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080055 /**
56 * @brief get the name (including sequence number) and the content
57 * (unencoded, just XML stanza) of the most recent published data
58 *
59 * @param prefix the name prefix to look for
60 * @param session session
61 * @return the pair of name and content
62 */
63 std::string
64 getRecentData (const std::string &prefix, uint32_t session);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080065
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080066 /**
67 * @brief get the most recent sequence number for a name prefix
68 * @param prefix the name prefix to look for
69 * @param session session
70 */
71 uint32_t
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070072 getNextSeq (const std::string &prefix, uint32_t session);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080073
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080074 /**
75 * @brief publish data for a name prefix, updates the corresponding
76 * sequence number and recent data
77 *
78 * @param name data prefix
79 * @param session session to which data is published
80 * @param dataBuffer the data itself
81 * @param freshness the freshness for the data object
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070082 * @return published sequence number, will throw an exception if something wrong happened
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080083 */
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070084 uint32_t publishData (const std::string &name, uint32_t session, const std::string &dataBuffer, int freshness);
Chaoyi Bian3e99d862012-03-07 17:28:32 -080085
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080086private:
Zhenkai Zhub951a622012-03-13 22:37:37 -070087 typedef boost::unordered_map<std::string, Seq> SequenceLog;
88 typedef boost::unordered_map<std::pair<std::string, uint32_t>, std::string> RecentData;
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070089
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080090 CcnxWrapperPtr m_ccnxHandle;
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070091 SequenceLog m_sequenceLog;
92 RecentData m_recentData;
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080093};
94
95} // Sync
96
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080097#endif // SYNC_APP_DATA_PUBLISH_H