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 | */ |
| 22 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 23 | #ifndef SYNC_APP_DATA_PUBLISH_H |
| 24 | #define SYNC_APP_DATA_PUBLISH_H |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 25 | |
Zhenkai Zhu | b951a62 | 2012-03-13 22:37:37 -0700 | [diff] [blame] | 26 | #include <boost/unordered_map.hpp> |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 27 | #include "sync-seq-no.h" |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 28 | #include "sync-ccnx-wrapper.h" |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 29 | #include <utility> |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 30 | #include <map> |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 31 | |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 32 | namespace Sync { |
| 33 | |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 34 | struct Seq |
| 35 | { |
| 36 | uint32_t session; |
| 37 | uint32_t seq; |
| 38 | }; |
| 39 | |
Zhenkai Zhu | 5548e03 | 2012-03-12 20:10:15 -0700 | [diff] [blame] | 40 | struct GetSeqException : virtual boost::exception, virtual std::exception { }; |
| 41 | |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 42 | /** |
| 43 | * \ingroup sync |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 44 | * @brief publishes application data using incrementing sequence number (for |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 45 | * each sequence namber and keeps track of most recently published data for |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 46 | * each name prefix |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 47 | */ |
| 48 | class AppDataPublish |
| 49 | { |
| 50 | public: |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 51 | AppDataPublish (CcnxWrapperPtr ccnxHandle) |
| 52 | : m_ccnxHandle (ccnxHandle) |
| 53 | { } |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 55 | /** |
| 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 Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 66 | /** |
| 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 Zhu | 1ee93c9 | 2012-03-12 20:32:57 -0700 | [diff] [blame] | 72 | getNextSeq (const std::string &prefix, uint32_t session); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 74 | /** |
| 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 Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 82 | * @return published sequence number, will throw an exception if something wrong happened |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 83 | */ |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 84 | uint32_t publishData (const std::string &name, uint32_t session, const std::string &dataBuffer, int freshness); |
Chaoyi Bian | 3e99d86 | 2012-03-07 17:28:32 -0800 | [diff] [blame] | 85 | |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 86 | private: |
Zhenkai Zhu | b951a62 | 2012-03-13 22:37:37 -0700 | [diff] [blame] | 87 | typedef boost::unordered_map<std::string, Seq> SequenceLog; |
| 88 | typedef boost::unordered_map<std::pair<std::string, uint32_t>, std::string> RecentData; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 90 | CcnxWrapperPtr m_ccnxHandle; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 91 | SequenceLog m_sequenceLog; |
| 92 | RecentData m_recentData; |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // Sync |
| 96 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 97 | #endif // SYNC_APP_DATA_PUBLISH_H |