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 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 21 | #ifndef _SYNC_SOCKET_H |
| 22 | #define _SYNC_SOCKET_H |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 23 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/face.hpp> |
| 25 | #include <ndn-cxx/security/validator.hpp> |
| 26 | #include <ndn-cxx/security/validator-null.hpp> |
| 27 | #include <ndn-cxx/security/key-chain.hpp> |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 29 | #include "sync-logic.h" |
| 30 | #include "sync-seq-no.h" |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 31 | #include "sync-validator.h" |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 32 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 33 | #include <utility> |
| 34 | #include <map> |
| 35 | #include <vector> |
| 36 | #include <sstream> |
| 37 | |
| 38 | namespace Sync { |
| 39 | |
| 40 | /** |
| 41 | * \ingroup sync |
| 42 | * @brief A simple interface to interact with client code |
| 43 | */ |
| 44 | class SyncSocket |
| 45 | { |
| 46 | public: |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 47 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 48 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 49 | typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback; |
| 50 | typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback; |
| 51 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 52 | static const ndn::Name EMPTY_NAME; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 53 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 54 | SyncSocket(const ndn::Name& syncPrefix, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 55 | const ndn::Name& dataPrefix, |
| 56 | uint64_t dataSession, |
| 57 | bool withRoutingPrefix, |
| 58 | const ndn::Name& routingPrefix, |
| 59 | ndn::shared_ptr<ndn::Face> face, |
| 60 | const ndn::IdentityCertificate& myCertificate, |
| 61 | ndn::shared_ptr<ndn::SecRuleRelative> dataRule, |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 62 | NewDataCallback dataCallback, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 63 | RemoveCallback rmCallback); |
| 64 | |
| 65 | ~SyncSocket(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 67 | void |
| 68 | publishData(const uint8_t* buf, size_t len, int freshness, bool isCert = false); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 70 | void |
| 71 | leave() |
| 72 | { |
| 73 | m_syncLogic->remove(m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix); |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | remove(const ndn::Name& prefix) |
| 78 | { |
| 79 | m_syncLogic->remove(prefix); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 80 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 82 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 83 | fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 84 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 85 | std::string |
| 86 | getRootDigest() |
| 87 | { |
| 88 | return m_syncLogic->getRootDigest(); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 89 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 91 | uint64_t |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 92 | getNextSeq() |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 93 | { |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 94 | // If DNS works, we should use pure m_dataprefix rather than the one with routing prefix. |
| 95 | SequenceLog::iterator i = m_sequenceLog.find (m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 96 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 97 | if (i != m_sequenceLog.end ()) |
| 98 | { |
| 99 | SeqNo s = i->second; |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 100 | if (s.getSession() == m_dataSession) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 101 | return s.getSeq(); |
| 102 | } |
| 103 | return 0; |
| 104 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 105 | |
| 106 | SyncLogic & |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 107 | getLogic() |
| 108 | { |
| 109 | return *m_syncLogic; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 110 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 111 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 112 | void |
| 113 | addParticipant(const ndn::IdentityCertificate& introducee) |
| 114 | { |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 115 | if(m_withSecurity) |
| 116 | { |
| 117 | ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introducee); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | addParticipant(const IntroCertificate& introCert) |
| 123 | { |
| 124 | if(m_withSecurity) |
| 125 | { |
| 126 | ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | getIntroCertNames(std::vector<ndn::Name>& list) |
| 132 | { |
| 133 | if(m_withSecurity) |
| 134 | { |
| 135 | ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertNames(list); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | const IntroCertificate& |
| 140 | getIntroCertificate(const ndn::Name& name) |
| 141 | { |
| 142 | if(m_withSecurity) |
| 143 | { |
| 144 | return ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertificate(name); |
| 145 | } |
| 146 | throw Error("You are running SyncSocket without security!"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // // make this a static function so we don't have to create socket instance without |
| 150 | // // knowing the local prefix. it's a wrong place for this function anyway |
| 151 | // static std::string |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 152 | // GetLocalPrefix (); |
| 153 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 154 | private: |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 155 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 156 | publishDataInternal(ndn::shared_ptr<ndn::Data> data, bool isCert); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 157 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 158 | void |
| 159 | passCallback(const std::vector<MissingDataInfo> &v) |
| 160 | { |
| 161 | m_newDataCallback(v, this); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 162 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 163 | |
| 164 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 165 | onData(const ndn::Interest& interest, ndn::Data& data, const ndn::OnDataValidated& dataCallback); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 166 | |
| 167 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 168 | onDataTimeout(const ndn::Interest& interest, int retry, const ndn::OnDataValidated& dataCallback); |
| 169 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 170 | |
| 171 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 172 | onDataValidated(const ndn::shared_ptr<const ndn::Data>& data, |
| 173 | size_t interestNameSize, |
| 174 | const ndn::OnDataValidated& onValidated); |
| 175 | |
| 176 | void |
| 177 | onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 178 | const std::string& failureInfo); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 179 | |
| 180 | private: |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 181 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 182 | |
| 183 | ndn::Name m_dataPrefix; |
| 184 | uint64_t m_dataSession; |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 185 | ndn::Name m_routableDataPrefix; |
| 186 | bool m_withRoutingPrefix; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 187 | NewDataCallback m_newDataCallback; |
| 188 | SequenceLog m_sequenceLog; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 189 | ndn::IdentityCertificate m_myCertificate; |
| 190 | ndn::KeyChain m_keyChain; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 191 | ndn::shared_ptr<ndn::Face> m_face; |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame^] | 192 | boost::asio::io_service& m_ioService; |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 193 | bool m_withSecurity; |
| 194 | ndn::shared_ptr<ndn::Validator> m_syncValidator; |
| 195 | ndn::shared_ptr<SyncLogic> m_syncLogic; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // Sync |
| 199 | |
| 200 | #endif // SYNC_SOCKET_H |