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