Zhenkai Zhu | bed7895 | 2012-03-06 11:06:08 -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 | bed7895 | 2012-03-06 11:06:08 -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_SOCKET_H |
| 24 | #define SYNC_APP_SOCKET_H |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 25 | |
| 26 | #include "sync-logic.h" |
Zhenkai Zhu | bed7895 | 2012-03-06 11:06:08 -0800 | [diff] [blame] | 27 | #include <boost/function.hpp> |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 28 | #include <boost/unordered_map.hpp> |
| 29 | #include "sync-seq-no.h" |
| 30 | #include "sync-ccnx-wrapper.h" |
| 31 | #include <utility> |
| 32 | #include <map> |
| 33 | #include <vector> |
| 34 | #include <sstream> |
Zhenkai Zhu | bed7895 | 2012-03-06 11:06:08 -0800 | [diff] [blame] | 35 | |
| 36 | namespace Sync { |
| 37 | |
| 38 | /** |
| 39 | * \ingroup sync |
| 40 | * @brief A simple interface to interact with client code |
| 41 | */ |
| 42 | class SyncAppSocket |
| 43 | { |
| 44 | public: |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 45 | typedef boost::function< void (const std::vector<MissingDataInfo> &, SyncAppSocket * ) > NewDataCallback; |
| 46 | typedef boost::function< void ( const std::string &/*prefix*/ ) > RemoveCallback; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 47 | /** |
| 48 | * @brief the constructor for SyncAppSocket; the parameter syncPrefix |
| 49 | * should be passed to the constructor of m_syncAppWrapper; the other |
| 50 | * parameter should be passed to the constructor of m_fetcher; furthermore, |
| 51 | * the fetch function of m_fetcher should be a second paramter passed to |
| 52 | * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell |
| 53 | * m_fetcher to fetch the actual app data after it learns the names |
| 54 | * |
| 55 | * @param syncPrefix the name prefix for Sync Interest |
| 56 | * @param dataCallback the callback to process data |
| 57 | */ |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 58 | SyncAppSocket (const std::string &syncPrefix, NewDataCallback dataCallback, RemoveCallback rmCallback); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 59 | ~SyncAppSocket (); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 61 | /** |
| 62 | * @brief publish data from local client and tell SyncLogic to update |
| 63 | * the sync tree by adding the local names |
| 64 | * |
| 65 | * @param prefix the name prefix for the data |
| 66 | * @param session session to which data is published |
| 67 | * @param dataBuffer the data itself |
| 68 | * @param freshness the freshness time for the data (in seconds) |
| 69 | */ |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 70 | bool publishString (const std::string &prefix, uint32_t session, const std::string &dataBuffer, int freshness); |
| 71 | |
| 72 | bool publishRaw(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 73 | |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 74 | /** |
| 75 | * @brief delete a participant's subtree from the sync tree; SyncLogic will do the work |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 76 | * this is just a wrapper |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 77 | * |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 78 | * @param prefix the prefix for the participant |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 79 | */ |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 80 | void remove (const std::string &prefix) {m_syncLogic.remove(prefix);} |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 81 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 82 | void fetchString(const std::string &prefix, const SeqNo &seq, CcnxWrapper::StringDataCallback callback, int retry = 0); |
| 83 | void fetchRaw(const std::string &prefix, const SeqNo &seq, CcnxWrapper::RawDataCallback callback, int retry = 0); |
| 84 | |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 85 | // for sync-demo |
| 86 | std::string getRootDigest() {return m_syncLogic.getRootDigest();} |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 87 | uint32_t |
| 88 | getNextSeq (const std::string &prefix, uint32_t session); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | 5548c04 | 2012-10-04 19:10:09 -0700 | [diff] [blame] | 90 | SyncLogic & |
| 91 | getLogic () { return m_syncLogic; } |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 92 | |
Zhenkai Zhu | e761767 | 2012-10-15 13:51:42 -0700 | [diff] [blame] | 93 | // make this a static function so we don't have to create socket instance without |
| 94 | // knowing the local prefix. it's a wrong place for this function anyway |
| 95 | static std::string |
| 96 | GetLocalPrefix (); |
Alexander Afanasyev | 5548c04 | 2012-10-04 19:10:09 -0700 | [diff] [blame] | 97 | |
Zhenkai Zhu | 3e6d479 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 98 | private: |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 99 | void |
| 100 | passCallback(const std::vector<MissingDataInfo> &v) {m_newDataCallback(v, this);} |
| 101 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 102 | private: |
| 103 | typedef boost::unordered_map<std::string, SeqNo> SequenceLog; |
| 104 | NewDataCallback m_newDataCallback; |
| 105 | SequenceLog m_sequenceLog; |
| 106 | CcnxWrapperPtr m_ccnxHandle; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 107 | SyncLogic m_syncLogic; |
Zhenkai Zhu | bed7895 | 2012-03-06 11:06:08 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // Sync |
| 111 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 112 | #endif // SYNC_APP_SOCKET_H |