Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
| 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 | |
| 28 | #include <ndn-cxx/face.hpp> |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 29 | #include <ndn-cxx/util/in-memory-storage-persistent.hpp> |
| 30 | #include <unordered_map> |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 31 | |
| 32 | #include "logic.hpp" |
| 33 | |
| 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, |
| 67 | ndn::shared_ptr<ndn::Validator> validator = DEFAULT_VALIDATOR); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 69 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 70 | addSyncNode(const Name& prefix, const Name& signingId = DEFAULT_NAME); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * @brief Publish a data packet in the session and trigger synchronization updates |
| 74 | * |
| 75 | * This method will create a data packet with the supplied content. |
| 76 | * The packet name is the local session + seqNo. |
| 77 | * The seqNo is automatically maintained by internal Logic. |
| 78 | * |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 79 | * @throws It will throw error, if the prefix does not exist in m_logic |
| 80 | * |
| 81 | * @param buf Pointer to the bytes in content |
| 82 | * @param len size of the bytes in content |
| 83 | * @param freshness FreshnessPeriod of the data packet. |
| 84 | */ |
| 85 | void |
| 86 | publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness, |
| 87 | const Name& prefix = DEFAULT_PREFIX); |
| 88 | |
| 89 | /** |
| 90 | * @brief Publish a data packet in the session and trigger synchronization updates |
| 91 | * |
| 92 | * This method will create a data packet with the supplied content. |
| 93 | * The packet name is the local session + seqNo. |
| 94 | * The seqNo is automatically maintained by internal Logic. |
| 95 | * |
| 96 | * @throws It will throw error, if the prefix does not exist in m_logic |
| 97 | * |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 98 | * @param content Block that will be set as the content of the data packet. |
| 99 | * @param freshness FreshnessPeriod of the data packet. |
| 100 | */ |
| 101 | void |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 102 | publishData(const Block& content, const ndn::time::milliseconds& freshness, |
| 103 | const Name& prefix = DEFAULT_PREFIX); |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * @brief Retrive a data packet with a particular seqNo from a session |
| 107 | * |
| 108 | * @param sessionName The name of the target session. |
| 109 | * @param seq The seqNo of the data packet. |
| 110 | * @param onValidated The callback when the retrieved packet has been validated. |
| 111 | * @param nRetries The number of retries. |
| 112 | */ |
| 113 | void |
| 114 | fetchData(const Name& sessionName, const SeqNo& seq, |
| 115 | const ndn::OnDataValidated& onValidated, |
| 116 | int nRetries = 0); |
| 117 | |
| 118 | /** |
| 119 | * @brief Retrive a data packet with a particular seqNo from a session |
| 120 | * |
| 121 | * @param sessionName The name of the target session. |
| 122 | * @param seq The seqNo of the data packet. |
| 123 | * @param onValidated The callback when the retrieved packet has been validated. |
| 124 | * @param nRetries The number of retries. |
| 125 | */ |
| 126 | void |
| 127 | fetchData(const Name& sessionName, const SeqNo& seq, |
| 128 | const ndn::OnDataValidated& onValidated, |
| 129 | const ndn::OnDataValidationFailed& onValidationFailed, |
| 130 | const ndn::OnTimeout& onTimeout, |
| 131 | int nRetries = 0); |
| 132 | |
| 133 | /// @brief Get the root digest of current sync tree |
| 134 | ndn::ConstBufferPtr |
| 135 | getRootDigest() const; |
| 136 | |
| 137 | Logic& |
| 138 | getLogic() |
| 139 | { |
| 140 | return m_logic; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | void |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 145 | onInterest(const Name& prefix, const Interest& interest); |
| 146 | |
| 147 | void |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 148 | onData(const Interest& interest, Data& data, |
| 149 | const ndn::OnDataValidated& dataCallback, |
| 150 | const ndn::OnDataValidationFailed& failCallback); |
| 151 | |
| 152 | void |
| 153 | onDataTimeout(const Interest& interest, int nRetries, |
| 154 | const ndn::OnDataValidated& dataCallback, |
| 155 | const ndn::OnDataValidationFailed& failCallback); |
| 156 | |
| 157 | void |
| 158 | onDataValidationFailed(const shared_ptr<const Data>& data, |
| 159 | const std::string& failureInfo); |
| 160 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 161 | public: |
| 162 | static const ndn::Name DEFAULT_NAME; |
Qiuhan Ding | e246b62 | 2014-12-03 21:57:48 -0800 | [diff] [blame] | 163 | static const ndn::Name DEFAULT_PREFIX; |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 164 | static const ndn::shared_ptr<ndn::Validator> DEFAULT_VALIDATOR; |
| 165 | |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 166 | private: |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 167 | typedef std::unordered_map<ndn::Name, const ndn::RegisteredPrefixId*> RegisteredPrefixList; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 168 | |
| 169 | Name m_userPrefix; |
| 170 | ndn::Face& m_face; |
| 171 | |
| 172 | Logic m_logic; |
| 173 | |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 174 | ndn::Name m_signingId; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 175 | ndn::KeyChain m_keyChain; |
Yingdi Yu | cd33902 | 2014-11-05 17:51:19 -0800 | [diff] [blame] | 176 | ndn::shared_ptr<ndn::Validator> m_validator; |
Qiuhan Ding | fb8c9e0 | 2015-01-30 14:04:55 -0800 | [diff] [blame^] | 177 | |
| 178 | RegisteredPrefixList m_registeredPrefixList; |
| 179 | ndn::util::InMemoryStoragePersistent m_ims; |
Yingdi Yu | 31ad44c | 2014-08-28 14:55:42 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | } // namespace chronosync |
| 183 | |
| 184 | #endif // CHRONOSYNC_SOCKET_HPP |