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" |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 28 | #include <ndn-cpp/face.hpp> |
| 29 | #include <ndn-cpp/security/identity/identity-manager.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 | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 58 | ndn::ptr_lib::shared_ptr<SyncPolicyManager> syncPolicyManager, |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame^] | 59 | ndn::ptr_lib::shared_ptr<ndn::Face> face, |
| 60 | ndn::ptr_lib::shared_ptr<ndn::Transport> transport, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 61 | NewDataCallback dataCallback, |
| 62 | RemoveCallback rmCallback); |
| 63 | |
| 64 | ~SyncSocket (); |
| 65 | |
| 66 | bool |
| 67 | publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness); |
| 68 | |
| 69 | void |
| 70 | remove (const std::string &prefix) |
| 71 | { m_syncLogic.remove(prefix); } |
| 72 | |
| 73 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 74 | fetchData(const std::string &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 75 | |
| 76 | std::string |
| 77 | getRootDigest() |
| 78 | { return m_syncLogic.getRootDigest(); } |
| 79 | |
| 80 | uint32_t |
| 81 | getNextSeq (const std::string &prefix, uint32_t session); |
| 82 | |
| 83 | SyncLogic & |
| 84 | getLogic () |
| 85 | { return m_syncLogic; } |
| 86 | |
| 87 | // make this a static function so we don't have to create socket instance without |
| 88 | // knowing the local prefix. it's a wrong place for this function anyway |
| 89 | static std::string |
| 90 | GetLocalPrefix (); |
| 91 | |
| 92 | private: |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame^] | 93 | // void |
| 94 | // connectToDaemon(); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 95 | |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame^] | 96 | // void |
| 97 | // onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 98 | // const ndn::ptr_lib::shared_ptr<ndn::Data>& data); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 99 | |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame^] | 100 | // void |
| 101 | // onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 102 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 103 | void |
| 104 | passCallback(const std::vector<MissingDataInfo> &v) |
| 105 | { m_newDataCallback(v, this); } |
| 106 | |
| 107 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 108 | onChatCert(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 109 | const ndn::ptr_lib::shared_ptr<ndn::Data>& cert, |
| 110 | ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 111 | |
| 112 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 113 | onChatCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 114 | const ndn::OnVerifyFailed& onVerifyFailed, |
| 115 | const ndn::ptr_lib::shared_ptr<ndn::Data>& data, |
| 116 | ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep); |
| 117 | |
| 118 | void |
| 119 | onChatData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 120 | const ndn::ptr_lib::shared_ptr<ndn::Data>& data, |
| 121 | const ndn::OnVerified& onVerified, |
| 122 | const ndn::OnVerifyFailed& onVerifyFailed); |
| 123 | |
| 124 | void |
| 125 | onChatDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 126 | int retry, |
| 127 | const ndn::OnVerified& onVerified, |
| 128 | const ndn::OnVerifyFailed& onVerifyFailed); |
| 129 | |
| 130 | void |
| 131 | onChatDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | typedef boost::unordered_map<std::string, SeqNo> SequenceLog; |
| 135 | NewDataCallback m_newDataCallback; |
| 136 | SequenceLog m_sequenceLog; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 137 | ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager; |
| 138 | ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 139 | ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport; |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame^] | 140 | ndn::ptr_lib::shared_ptr<ndn::Face> m_face; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 141 | SyncLogic m_syncLogic; |
| 142 | }; |
| 143 | |
| 144 | } // Sync |
| 145 | |
| 146 | #endif // SYNC_SOCKET_H |