blob: 3e09c79f24b6c81eceb03f14de115c702f280530 [file] [log] [blame]
Yingdi Yu31ad44c2014-08-28 14:55:42 -07001/* -*- 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>
Yingdi Yu31ad44c2014-08-28 14:55:42 -070029
30#include "logic.hpp"
31
32namespace chronosync {
33
34/**
35 * @brief A simple interface to interact with client code
36 *
37 * Though it is called Socket, it is not a real socket. It just trying to provide
38 * a simplified interface for data publishing and fetching.
39 *
40 * This interface simplify data publishing. Client can simply dump raw data
41 * into this interface without handling the ChronoSync specific details, such
42 * as sequence number and session id.
43 *
44 * This interface also simplify data fetching. Client only needs to provide a
45 * data fetching strategy (through a updateCallback).
46 */
47class Socket : noncopyable
48{
49public:
50 class Error : public std::runtime_error
51 {
52 public:
53 explicit
54 Error(const std::string& what)
55 : std::runtime_error(what)
56 {
57 }
58 };
59
60 Socket(const Name& syncPrefix,
61 const Name& userPrefix,
62 ndn::Face& face,
Yingdi Yucd339022014-11-05 17:51:19 -080063 const UpdateCallback& updateCallback,
64 const Name& signingId = DEFAULT_NAME,
65 ndn::shared_ptr<ndn::Validator> validator = DEFAULT_VALIDATOR);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070066
Yingdi Yu31ad44c2014-08-28 14:55:42 -070067 void
Qiuhan Dinge246b622014-12-03 21:57:48 -080068 addSyncNode(const Name& prefix, const Name& signingId = DEFAULT_NAME);
Yingdi Yu31ad44c2014-08-28 14:55:42 -070069
70 /**
71 * @brief Publish a data packet in the session and trigger synchronization updates
72 *
73 * This method will create a data packet with the supplied content.
74 * The packet name is the local session + seqNo.
75 * The seqNo is automatically maintained by internal Logic.
76 *
Qiuhan Dinge246b622014-12-03 21:57:48 -080077 * @throws It will throw error, if the prefix does not exist in m_logic
78 *
79 * @param buf Pointer to the bytes in content
80 * @param len size of the bytes in content
81 * @param freshness FreshnessPeriod of the data packet.
82 */
83 void
84 publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
85 const Name& prefix = DEFAULT_PREFIX);
86
87 /**
88 * @brief Publish a data packet in the session and trigger synchronization updates
89 *
90 * This method will create a data packet with the supplied content.
91 * The packet name is the local session + seqNo.
92 * The seqNo is automatically maintained by internal Logic.
93 *
94 * @throws It will throw error, if the prefix does not exist in m_logic
95 *
Yingdi Yu31ad44c2014-08-28 14:55:42 -070096 * @param content Block that will be set as the content of the data packet.
97 * @param freshness FreshnessPeriod of the data packet.
98 */
99 void
Qiuhan Dinge246b622014-12-03 21:57:48 -0800100 publishData(const Block& content, const ndn::time::milliseconds& freshness,
101 const Name& prefix = DEFAULT_PREFIX);
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700102
103 /**
104 * @brief Retrive a data packet with a particular seqNo from a session
105 *
106 * @param sessionName The name of the target session.
107 * @param seq The seqNo of the data packet.
108 * @param onValidated The callback when the retrieved packet has been validated.
109 * @param nRetries The number of retries.
110 */
111 void
112 fetchData(const Name& sessionName, const SeqNo& seq,
113 const ndn::OnDataValidated& onValidated,
114 int nRetries = 0);
115
116 /**
117 * @brief Retrive a data packet with a particular seqNo from a session
118 *
119 * @param sessionName The name of the target session.
120 * @param seq The seqNo of the data packet.
121 * @param onValidated The callback when the retrieved packet has been validated.
122 * @param nRetries The number of retries.
123 */
124 void
125 fetchData(const Name& sessionName, const SeqNo& seq,
126 const ndn::OnDataValidated& onValidated,
127 const ndn::OnDataValidationFailed& onValidationFailed,
128 const ndn::OnTimeout& onTimeout,
129 int nRetries = 0);
130
131 /// @brief Get the root digest of current sync tree
132 ndn::ConstBufferPtr
133 getRootDigest() const;
134
135 Logic&
136 getLogic()
137 {
138 return m_logic;
139 }
140
141private:
142 void
143 onData(const Interest& interest, Data& data,
144 const ndn::OnDataValidated& dataCallback,
145 const ndn::OnDataValidationFailed& failCallback);
146
147 void
148 onDataTimeout(const Interest& interest, int nRetries,
149 const ndn::OnDataValidated& dataCallback,
150 const ndn::OnDataValidationFailed& failCallback);
151
152 void
153 onDataValidationFailed(const shared_ptr<const Data>& data,
154 const std::string& failureInfo);
155
Yingdi Yucd339022014-11-05 17:51:19 -0800156public:
157 static const ndn::Name DEFAULT_NAME;
Qiuhan Dinge246b622014-12-03 21:57:48 -0800158 static const ndn::Name DEFAULT_PREFIX;
Yingdi Yucd339022014-11-05 17:51:19 -0800159 static const ndn::shared_ptr<ndn::Validator> DEFAULT_VALIDATOR;
160
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700161private:
162
163 Name m_userPrefix;
164 ndn::Face& m_face;
165
166 Logic m_logic;
167
Yingdi Yucd339022014-11-05 17:51:19 -0800168 ndn::Name m_signingId;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700169 ndn::KeyChain m_keyChain;
Yingdi Yucd339022014-11-05 17:51:19 -0800170 ndn::shared_ptr<ndn::Validator> m_validator;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700171};
172
173} // namespace chronosync
174
175#endif // CHRONOSYNC_SOCKET_HPP