Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/web/index.html> |
| 20 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 21 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 25 | #ifndef _SYNC_SOCKET_H |
| 26 | #define _SYNC_SOCKET_H |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 27 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/face.hpp> |
| 29 | #include <ndn-cxx/security/validator.hpp> |
| 30 | #include <ndn-cxx/security/validator-null.hpp> |
| 31 | #include <ndn-cxx/security/key-chain.hpp> |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 33 | #include "sync-logic.h" |
| 34 | #include "sync-seq-no.h" |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 35 | #include "sync-validator.h" |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 36 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 37 | #include <utility> |
| 38 | #include <map> |
| 39 | #include <vector> |
| 40 | #include <sstream> |
| 41 | |
| 42 | namespace Sync { |
| 43 | |
| 44 | /** |
| 45 | * \ingroup sync |
| 46 | * @brief A simple interface to interact with client code |
| 47 | */ |
| 48 | class SyncSocket |
| 49 | { |
| 50 | public: |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 51 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 52 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 53 | typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback; |
| 54 | typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback; |
| 55 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 56 | static const ndn::Name EMPTY_NAME; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 58 | SyncSocket(const ndn::Name& syncPrefix, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 59 | const ndn::Name& dataPrefix, |
| 60 | uint64_t dataSession, |
| 61 | bool withRoutingPrefix, |
| 62 | const ndn::Name& routingPrefix, |
| 63 | ndn::shared_ptr<ndn::Face> face, |
| 64 | const ndn::IdentityCertificate& myCertificate, |
| 65 | ndn::shared_ptr<ndn::SecRuleRelative> dataRule, |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 66 | NewDataCallback dataCallback, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 67 | RemoveCallback rmCallback); |
| 68 | |
| 69 | ~SyncSocket(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 71 | void |
| 72 | 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] | 73 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 74 | void |
| 75 | leave() |
| 76 | { |
| 77 | m_syncLogic->remove(m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix); |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | remove(const ndn::Name& prefix) |
| 82 | { |
| 83 | m_syncLogic->remove(prefix); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 84 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 85 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 86 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 87 | 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] | 88 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 89 | std::string |
| 90 | getRootDigest() |
| 91 | { |
| 92 | return m_syncLogic->getRootDigest(); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 93 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 94 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 95 | uint64_t |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 96 | getNextSeq() |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 97 | { |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 98 | // If DNS works, we should use pure m_dataprefix rather than the one with routing prefix. |
| 99 | SequenceLog::iterator i = m_sequenceLog.find (m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 100 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 101 | if (i != m_sequenceLog.end ()) |
| 102 | { |
| 103 | SeqNo s = i->second; |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 104 | if (s.getSession() == m_dataSession) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 105 | return s.getSeq(); |
| 106 | } |
| 107 | return 0; |
| 108 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 109 | |
| 110 | SyncLogic & |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 111 | getLogic() |
| 112 | { |
| 113 | return *m_syncLogic; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 114 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 115 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 116 | void |
| 117 | addParticipant(const ndn::IdentityCertificate& introducee) |
| 118 | { |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 119 | if(m_withSecurity) |
| 120 | { |
| 121 | ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introducee); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | addParticipant(const IntroCertificate& introCert) |
| 127 | { |
| 128 | if(m_withSecurity) |
| 129 | { |
| 130 | ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | getIntroCertNames(std::vector<ndn::Name>& list) |
| 136 | { |
| 137 | if(m_withSecurity) |
| 138 | { |
| 139 | ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertNames(list); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | const IntroCertificate& |
| 144 | getIntroCertificate(const ndn::Name& name) |
| 145 | { |
| 146 | if(m_withSecurity) |
| 147 | { |
| 148 | return ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertificate(name); |
| 149 | } |
| 150 | throw Error("You are running SyncSocket without security!"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // // make this a static function so we don't have to create socket instance without |
| 154 | // // knowing the local prefix. it's a wrong place for this function anyway |
| 155 | // static std::string |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 156 | // GetLocalPrefix (); |
| 157 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 158 | private: |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 159 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 160 | publishDataInternal(ndn::shared_ptr<ndn::Data> data, bool isCert); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 161 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 162 | void |
| 163 | passCallback(const std::vector<MissingDataInfo> &v) |
| 164 | { |
| 165 | m_newDataCallback(v, this); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 166 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 167 | |
| 168 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 169 | onData(const ndn::Interest& interest, ndn::Data& data, const ndn::OnDataValidated& dataCallback); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 170 | |
| 171 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 172 | onDataTimeout(const ndn::Interest& interest, int retry, const ndn::OnDataValidated& dataCallback); |
| 173 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 174 | |
| 175 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 176 | onDataValidated(const ndn::shared_ptr<const ndn::Data>& data, |
| 177 | size_t interestNameSize, |
| 178 | const ndn::OnDataValidated& onValidated); |
| 179 | |
| 180 | void |
| 181 | onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 182 | const std::string& failureInfo); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 183 | |
| 184 | private: |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 185 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 186 | |
| 187 | ndn::Name m_dataPrefix; |
| 188 | uint64_t m_dataSession; |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 189 | ndn::Name m_routableDataPrefix; |
| 190 | bool m_withRoutingPrefix; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 191 | NewDataCallback m_newDataCallback; |
| 192 | SequenceLog m_sequenceLog; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 193 | ndn::IdentityCertificate m_myCertificate; |
| 194 | ndn::KeyChain m_keyChain; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 195 | ndn::shared_ptr<ndn::Face> m_face; |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame] | 196 | boost::asio::io_service& m_ioService; |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 197 | bool m_withSecurity; |
| 198 | ndn::shared_ptr<ndn::Validator> m_syncValidator; |
| 199 | ndn::shared_ptr<SyncLogic> m_syncLogic; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // Sync |
| 203 | |
| 204 | #endif // SYNC_SOCKET_H |