Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Ashlesh Gawande | aae853c | 2018-02-06 17:28:50 -0600 | [diff] [blame] | 3 | * Copyright (c) 2012-2018 University of California, Los Angeles |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 15 | * |
| 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 Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 23 | */ |
| 24 | |
| 25 | #ifndef CHRONOSYNC_SOCKET_HPP |
| 26 | #define CHRONOSYNC_SOCKET_HPP |
| 27 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 28 | #include "logic.hpp" |
| 29 | |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 30 | #include <unordered_map> |
| 31 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 32 | #include <ndn-cxx/ims/in-memory-storage-persistent.hpp> |
| 33 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 34 | namespace chronosync { |
| 35 | |
| 36 | /** |
| 37 | * @brief A simple interface to interact with client code |
| 38 | * |
| 39 | * Though it is called Socket, it is not a real socket. It just trying to provide |
| 40 | * a simplified interface for data publishing and fetching. |
| 41 | * |
| 42 | * This interface simplify data publishing. Client can simply dump raw data |
| 43 | * into this interface without handling the ChronoSync specific details, such |
| 44 | * as sequence number and session id. |
| 45 | * |
| 46 | * This interface also simplify data fetching. Client only needs to provide a |
| 47 | * data fetching strategy (through a updateCallback). |
| 48 | */ |
| 49 | class Socket : noncopyable |
| 50 | { |
| 51 | public: |
| 52 | class Error : public std::runtime_error |
| 53 | { |
| 54 | public: |
| 55 | explicit |
| 56 | Error(const std::string& what) |
| 57 | : std::runtime_error(what) |
| 58 | { |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | Socket(const Name& syncPrefix, |
| 63 | const Name& userPrefix, |
| 64 | ndn::Face& face, |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 65 | const UpdateCallback& updateCallback, |
| 66 | const Name& signingId = DEFAULT_NAME, |
Ashlesh Gawande | aae853c | 2018-02-06 17:28:50 -0600 | [diff] [blame] | 67 | std::shared_ptr<Validator> validator = DEFAULT_VALIDATOR, |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 68 | const time::milliseconds& syncInterestLifetime = Logic::DEFAULT_SYNC_INTEREST_LIFETIME, |
Ashlesh Gawande | db862e0 | 2018-03-30 17:15:08 -0500 | [diff] [blame] | 69 | const name::Component& session = name::Component()); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 70 | |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 71 | ~Socket(); |
| 72 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 73 | using DataValidatedCallback = function<void(const Data&)>; |
| 74 | |
| 75 | using DataValidationErrorCallback = function<void(const Data&, const ValidationError& error)> ; |
| 76 | |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 77 | /** |
| 78 | * @brief Add a sync node under same logic |
| 79 | * |
| 80 | * This method will add a new sync node in logic and then register this prefix. |
| 81 | * If pass an empty prefix, it will return directly without doing anything |
| 82 | * If the prefix is already registered, return directly |
| 83 | * |
| 84 | * @param prefix Prefix of the new node |
| 85 | * @param signingId Signing ID for the packet sent out by the new node |
Alexander Afanasyev | bf5bc6c | 2018-02-19 11:26:09 -0500 | [diff] [blame] | 86 | * @param session Manually defined session number |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 87 | */ |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 88 | void |
Ashlesh Gawande | db862e0 | 2018-03-30 17:15:08 -0500 | [diff] [blame] | 89 | addSyncNode(const Name& prefix, const Name& signingId = DEFAULT_NAME, |
| 90 | const name::Component& session = name::Component()); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Qiuhan Ding | 03b9de3 | 2015-01-30 14:03:12 -0800 | [diff] [blame] | 93 | * @brief Remove a sync node under same logic |
| 94 | * |
| 95 | * This method will remove a sync node in logic, unregister this prefix |
| 96 | * and then clear the in memory storage about this prefix. |
| 97 | * logic will be reset after removal of this node |
| 98 | * |
| 99 | * @param prefix Prefix of the node to remove |
| 100 | */ |
| 101 | void |
| 102 | removeSyncNode(const Name& prefix); |
| 103 | |
| 104 | /** |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 105 | * @brief Publish a data packet in the session and trigger synchronization updates |
| 106 | * |
| 107 | * This method will create a data packet with the supplied content. |
| 108 | * The packet name is the local session + seqNo. |
| 109 | * The seqNo is automatically maintained by internal Logic. |
| 110 | * |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 111 | * @throws It will throw error, if the prefix does not exist in m_logic |
| 112 | * |
| 113 | * @param buf Pointer to the bytes in content |
| 114 | * @param len size of the bytes in content |
| 115 | * @param freshness FreshnessPeriod of the data packet. |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 116 | * @param prefix The user prefix that will be used to publish the data. |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 117 | */ |
| 118 | void |
| 119 | publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness, |
| 120 | const Name& prefix = DEFAULT_PREFIX); |
| 121 | |
| 122 | /** |
| 123 | * @brief Publish a data packet in the session and trigger synchronization updates |
| 124 | * |
| 125 | * This method will create a data packet with the supplied content. |
| 126 | * The packet name is the local session + seqNo. |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 127 | * The seqNo is set by the application. |
| 128 | * |
| 129 | * @throws It will throw error, if the prefix does not exist in m_logic |
| 130 | * |
| 131 | * @param buf Pointer to the bytes in content |
| 132 | * @param len size of the bytes in content |
| 133 | * @param freshness FreshnessPeriod of the data packet. |
| 134 | * @param seqNo Sequence number of the data |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 135 | * @param prefix The user prefix that will be used to publish the data. |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 136 | */ |
| 137 | void |
| 138 | publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness, |
| 139 | const uint64_t& seqNo, const Name& prefix = DEFAULT_PREFIX); |
| 140 | |
| 141 | /** |
| 142 | * @brief Publish a data packet in the session and trigger synchronization updates |
| 143 | * |
| 144 | * This method will create a data packet with the supplied content. |
| 145 | * The packet name is the local session + seqNo. |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 146 | * The seqNo is automatically maintained by internal Logic. |
| 147 | * |
| 148 | * @throws It will throw error, if the prefix does not exist in m_logic |
| 149 | * |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 150 | * @param content Block that will be set as the content of the data packet. |
| 151 | * @param freshness FreshnessPeriod of the data packet. |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 152 | * @param prefix The user prefix that will be used to publish the data. |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 153 | */ |
| 154 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 155 | publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 156 | const Name& prefix = DEFAULT_PREFIX); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 159 | * @brief Publish a data packet in the session and trigger synchronization updates |
| 160 | * |
| 161 | * This method will create a data packet with the supplied content. |
| 162 | * The packet name is the local session + seqNo. |
| 163 | * The seqNo is set by the application. |
| 164 | * |
| 165 | * @throws It will throw error, if the prefix does not exist in m_logic |
| 166 | * |
| 167 | * @param content Block that will be set as the content of the data packet. |
| 168 | * @param freshness FreshnessPeriod of the data packet. |
| 169 | * @param seqNo Sequence number of the data |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 170 | * @param prefix The user prefix that will be used to publish the data. |
Ashlesh Gawande | 8d1347a | 2017-04-03 19:10:28 -0500 | [diff] [blame] | 171 | */ |
| 172 | void |
| 173 | publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 174 | const uint64_t& seqNo, const Name& prefix = DEFAULT_PREFIX); |
| 175 | |
| 176 | /** |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 177 | * @brief Retrive a data packet with a particular seqNo from a session |
| 178 | * |
| 179 | * @param sessionName The name of the target session. |
| 180 | * @param seq The seqNo of the data packet. |
| 181 | * @param onValidated The callback when the retrieved packet has been validated. |
| 182 | * @param nRetries The number of retries. |
| 183 | */ |
| 184 | void |
| 185 | fetchData(const Name& sessionName, const SeqNo& seq, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 186 | const DataValidatedCallback& onValidated, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 187 | int nRetries = 0); |
| 188 | |
| 189 | /** |
| 190 | * @brief Retrive a data packet with a particular seqNo from a session |
| 191 | * |
| 192 | * @param sessionName The name of the target session. |
| 193 | * @param seq The seqNo of the data packet. |
| 194 | * @param onValidated The callback when the retrieved packet has been validated. |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 195 | * @param onValidationFailed The callback when the retrieved packet failed validation. |
| 196 | * @param onTimeout The callback when data is not retrieved. |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 197 | * @param nRetries The number of retries. |
| 198 | */ |
| 199 | void |
| 200 | fetchData(const Name& sessionName, const SeqNo& seq, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 201 | const DataValidatedCallback& onValidated, |
| 202 | const DataValidationErrorCallback& onValidationFailed, |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 203 | const ndn::TimeoutCallback& onTimeout, |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 204 | int nRetries = 0); |
| 205 | |
| 206 | /// @brief Get the root digest of current sync tree |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 207 | ConstBufferPtr |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 208 | getRootDigest() const; |
| 209 | |
| 210 | Logic& |
| 211 | getLogic() |
| 212 | { |
| 213 | return m_logic; |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | void |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 218 | onInterest(const Name& prefix, const Interest& interest); |
| 219 | |
| 220 | void |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 221 | onData(const Interest& interest, const Data& data, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 222 | const DataValidatedCallback& dataCallback, |
| 223 | const DataValidationErrorCallback& failCallback); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 224 | |
| 225 | void |
| 226 | onDataTimeout(const Interest& interest, int nRetries, |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 227 | const DataValidatedCallback& dataCallback, |
| 228 | const DataValidationErrorCallback& failCallback); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 229 | |
| 230 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 231 | onDataValidationFailed(const Data& data, |
| 232 | const ValidationError& error); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 233 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 234 | public: |
| 235 | static const ndn::Name DEFAULT_NAME; |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 236 | static const ndn::Name DEFAULT_PREFIX; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 237 | static const std::shared_ptr<Validator> DEFAULT_VALIDATOR; |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 238 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 239 | private: |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 240 | using RegisteredPrefixList = std::unordered_map<ndn::Name, const ndn::RegisteredPrefixId*>; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 241 | |
| 242 | Name m_userPrefix; |
| 243 | ndn::Face& m_face; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 244 | Logic m_logic; |
| 245 | |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 246 | Name m_signingId; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 247 | ndn::KeyChain m_keyChain; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 248 | std::shared_ptr<Validator> m_validator; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame] | 249 | |
| 250 | RegisteredPrefixList m_registeredPrefixList; |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 251 | ndn::InMemoryStoragePersistent m_ims; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | } // namespace chronosync |
| 255 | |
| 256 | #endif // CHRONOSYNC_SOCKET_HPP |