blob: c01fb050dc391ff07cde4040d45a6034b8ffc0a0 [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
67 /**
68 * @brief Publish a data packet in the session and trigger synchronization updates
69 *
70 * This method will create a data packet with the supplied content.
71 * The packet name is the local session + seqNo.
72 * The seqNo is automatically maintained by internal Logic.
73 *
74 * @param buf Pointer to the bytes in content
75 * @param len size of the bytes in content
76 * @param freshness FreshnessPeriod of the data packet.
77 */
78 void
79 publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness);
80
81 /**
82 * @brief Publish a data packet in the session and trigger synchronization updates
83 *
84 * This method will create a data packet with the supplied content.
85 * The packet name is the local session + seqNo.
86 * The seqNo is automatically maintained by internal Logic.
87 *
88 * @param content Block that will be set as the content of the data packet.
89 * @param freshness FreshnessPeriod of the data packet.
90 */
91 void
92 publishData(const Block& content, const ndn::time::milliseconds& freshness);
93
94 /**
95 * @brief Retrive a data packet with a particular seqNo from a session
96 *
97 * @param sessionName The name of the target session.
98 * @param seq The seqNo of the data packet.
99 * @param onValidated The callback when the retrieved packet has been validated.
100 * @param nRetries The number of retries.
101 */
102 void
103 fetchData(const Name& sessionName, const SeqNo& seq,
104 const ndn::OnDataValidated& onValidated,
105 int nRetries = 0);
106
107 /**
108 * @brief Retrive a data packet with a particular seqNo from a session
109 *
110 * @param sessionName The name of the target session.
111 * @param seq The seqNo of the data packet.
112 * @param onValidated The callback when the retrieved packet has been validated.
113 * @param nRetries The number of retries.
114 */
115 void
116 fetchData(const Name& sessionName, const SeqNo& seq,
117 const ndn::OnDataValidated& onValidated,
118 const ndn::OnDataValidationFailed& onValidationFailed,
119 const ndn::OnTimeout& onTimeout,
120 int nRetries = 0);
121
122 /// @brief Get the root digest of current sync tree
123 ndn::ConstBufferPtr
124 getRootDigest() const;
125
126 Logic&
127 getLogic()
128 {
129 return m_logic;
130 }
131
132private:
133 void
134 onData(const Interest& interest, Data& data,
135 const ndn::OnDataValidated& dataCallback,
136 const ndn::OnDataValidationFailed& failCallback);
137
138 void
139 onDataTimeout(const Interest& interest, int nRetries,
140 const ndn::OnDataValidated& dataCallback,
141 const ndn::OnDataValidationFailed& failCallback);
142
143 void
144 onDataValidationFailed(const shared_ptr<const Data>& data,
145 const std::string& failureInfo);
146
Yingdi Yucd339022014-11-05 17:51:19 -0800147public:
148 static const ndn::Name DEFAULT_NAME;
149 static const ndn::shared_ptr<ndn::Validator> DEFAULT_VALIDATOR;
150
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700151private:
152
153 Name m_userPrefix;
154 ndn::Face& m_face;
155
156 Logic m_logic;
157
Yingdi Yucd339022014-11-05 17:51:19 -0800158 ndn::Name m_signingId;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700159 ndn::KeyChain m_keyChain;
Yingdi Yucd339022014-11-05 17:51:19 -0800160 ndn::shared_ptr<ndn::Validator> m_validator;
Yingdi Yu31ad44c2014-08-28 14:55:42 -0700161};
162
163} // namespace chronosync
164
165#endif // CHRONOSYNC_SOCKET_HPP