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> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 26 | #include "sync-seq-no.h" |
Yingdi Yu | e815471 | 2014-01-21 10:20:14 -0800 | [diff] [blame] | 27 | #include <ndn-cpp-dev/face.hpp> |
| 28 | #include <ndn-cpp-dev/security/verifier.hpp> |
| 29 | #include <ndn-cpp-dev/security/key-chain.hpp> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 30 | #include <utility> |
| 31 | #include <map> |
| 32 | #include <vector> |
| 33 | #include <sstream> |
| 34 | |
| 35 | namespace Sync { |
| 36 | |
| 37 | /** |
| 38 | * \ingroup sync |
| 39 | * @brief A simple interface to interact with client code |
| 40 | */ |
| 41 | class SyncSocket |
| 42 | { |
| 43 | public: |
| 44 | typedef boost::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback; |
| 45 | typedef boost::function< void ( const std::string &/*prefix*/ ) > RemoveCallback; |
| 46 | /** |
| 47 | * @brief the constructor for SyncAppSocket; the parameter syncPrefix |
| 48 | * should be passed to the constructor of m_syncAppWrapper; the other |
| 49 | * parameter should be passed to the constructor of m_fetcher; furthermore, |
| 50 | * the fetch function of m_fetcher should be a second paramter passed to |
| 51 | * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell |
| 52 | * m_fetcher to fetch the actual app data after it learns the names |
| 53 | * |
| 54 | * @param syncPrefix the name prefix for Sync Interest |
| 55 | * @param dataCallback the callback to process data |
| 56 | */ |
| 57 | SyncSocket (const std::string &syncPrefix, |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame] | 58 | ndn::ptr_lib::shared_ptr<SecPolicySync> policy, |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 59 | ndn::ptr_lib::shared_ptr<ndn::Face> face, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 60 | NewDataCallback dataCallback, |
| 61 | RemoveCallback rmCallback); |
| 62 | |
| 63 | ~SyncSocket (); |
| 64 | |
| 65 | bool |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 66 | publishData(const ndn::Name &prefix, uint32_t session, const char *buf, size_t len, int freshness); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 67 | |
| 68 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 69 | remove (const ndn::Name &prefix) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 70 | { m_syncLogic.remove(prefix); } |
| 71 | |
| 72 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 73 | fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 74 | |
| 75 | std::string |
| 76 | getRootDigest() |
| 77 | { return m_syncLogic.getRootDigest(); } |
| 78 | |
| 79 | uint32_t |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 80 | getNextSeq (const ndn::Name &prefix, uint32_t session); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 81 | |
| 82 | SyncLogic & |
| 83 | getLogic () |
| 84 | { return m_syncLogic; } |
| 85 | |
| 86 | // make this a static function so we don't have to create socket instance without |
| 87 | // knowing the local prefix. it's a wrong place for this function anyway |
| 88 | static std::string |
| 89 | GetLocalPrefix (); |
| 90 | |
| 91 | private: |
| 92 | void |
| 93 | passCallback(const std::vector<MissingDataInfo> &v) |
| 94 | { m_newDataCallback(v, this); } |
| 95 | |
| 96 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 97 | onData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 98 | const ndn::ptr_lib::shared_ptr<ndn::Data>& data, |
| 99 | const ndn::OnVerified& onVerified, |
| 100 | const ndn::OnVerifyFailed& onVerifyFailed); |
| 101 | |
| 102 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 103 | onDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 104 | int retry, |
| 105 | const ndn::OnVerified& onVerified, |
| 106 | const ndn::OnVerifyFailed& onVerifyFailed); |
| 107 | |
| 108 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 109 | onDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 110 | |
| 111 | private: |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame^] | 112 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 113 | NewDataCallback m_newDataCallback; |
| 114 | SequenceLog m_sequenceLog; |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame] | 115 | ndn::ptr_lib::shared_ptr<SecPolicySync> m_policy; |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 116 | ndn::ptr_lib::shared_ptr<ndn::Verifier> m_verifier; |
| 117 | ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain; |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 118 | ndn::ptr_lib::shared_ptr<ndn::Face> m_face; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 119 | SyncLogic m_syncLogic; |
| 120 | }; |
| 121 | |
| 122 | } // Sync |
| 123 | |
| 124 | #endif // SYNC_SOCKET_H |