Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [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: Yingdi Yu <yingdi@cs.ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef SYNC_SOCKET_H |
| 22 | #define SYNC_SOCKET_H |
| 23 | |
| 24 | #include "sync-logic.h" |
| 25 | #include <boost/function.hpp> |
| 26 | #include <boost/unordered_map.hpp> |
| 27 | #include "sync-seq-no.h" |
| 28 | #include <ndn.cxx/wrapper/wrapper.h> |
| 29 | #include <utility> |
| 30 | #include <map> |
| 31 | #include <vector> |
| 32 | #include <sstream> |
| 33 | |
| 34 | namespace Sync { |
| 35 | |
| 36 | /** |
| 37 | * \ingroup sync |
| 38 | * @brief A simple interface to interact with client code |
| 39 | */ |
| 40 | class SyncSocket |
| 41 | { |
| 42 | public: |
| 43 | typedef boost::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback; |
| 44 | typedef boost::function< void ( const std::string &/*prefix*/ ) > RemoveCallback; |
| 45 | /** |
| 46 | * @brief the constructor for SyncAppSocket; the parameter syncPrefix |
| 47 | * should be passed to the constructor of m_syncAppWrapper; the other |
| 48 | * parameter should be passed to the constructor of m_fetcher; furthermore, |
| 49 | * the fetch function of m_fetcher should be a second paramter passed to |
| 50 | * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell |
| 51 | * m_fetcher to fetch the actual app data after it learns the names |
| 52 | * |
| 53 | * @param syncPrefix the name prefix for Sync Interest |
| 54 | * @param dataCallback the callback to process data |
| 55 | */ |
| 56 | SyncSocket (const std::string &syncPrefix, |
| 57 | ndn::Ptr<SyncPolicyManager> syncPolicyManager, |
| 58 | NewDataCallback dataCallback, |
| 59 | RemoveCallback rmCallback); |
| 60 | |
| 61 | ~SyncSocket (); |
| 62 | |
| 63 | bool |
| 64 | publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness); |
| 65 | |
| 66 | void |
| 67 | remove (const std::string &prefix) |
| 68 | { m_syncLogic.remove(prefix); } |
| 69 | |
| 70 | void |
| 71 | fetchData(const std::string &prefix, const SeqNo &seq, const ndn::DataCallback& callback, int retry = 0); |
| 72 | |
| 73 | std::string |
| 74 | getRootDigest() |
| 75 | { return m_syncLogic.getRootDigest(); } |
| 76 | |
| 77 | uint32_t |
| 78 | getNextSeq (const std::string &prefix, uint32_t session); |
| 79 | |
| 80 | SyncLogic & |
| 81 | getLogic () |
| 82 | { return m_syncLogic; } |
| 83 | |
| 84 | // make this a static function so we don't have to create socket instance without |
| 85 | // knowing the local prefix. it's a wrong place for this function anyway |
| 86 | static std::string |
| 87 | GetLocalPrefix (); |
| 88 | |
| 89 | private: |
| 90 | void |
| 91 | passCallback(const std::vector<MissingDataInfo> &v) |
| 92 | { m_newDataCallback(v, this); } |
| 93 | |
| 94 | void |
| 95 | onChatDataTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest, int retry); |
| 96 | |
| 97 | void |
| 98 | onChatDataUnverified(ndn::Ptr<ndn::Data> data); |
| 99 | |
| 100 | private: |
| 101 | typedef boost::unordered_map<std::string, SeqNo> SequenceLog; |
| 102 | NewDataCallback m_newDataCallback; |
| 103 | SequenceLog m_sequenceLog; |
| 104 | ndn::Ptr<SyncPolicyManager> m_syncPolicyManager; |
| 105 | ndn::Ptr<ndn::Wrapper> m_handler; |
| 106 | SyncLogic m_syncLogic; |
| 107 | }; |
| 108 | |
| 109 | } // Sync |
| 110 | |
| 111 | #endif // SYNC_SOCKET_H |