blob: 11dc684024fb1300bddb1f3454340124ccd533b9 [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>
29#include <ndn-cxx/security/validation-request.hpp>
30
31#include "logic.hpp"
32
33namespace chronosync {
34
35/**
36 * @brief A simple interface to interact with client code
37 *
38 * Though it is called Socket, it is not a real socket. It just trying to provide
39 * a simplified interface for data publishing and fetching.
40 *
41 * This interface simplify data publishing. Client can simply dump raw data
42 * into this interface without handling the ChronoSync specific details, such
43 * as sequence number and session id.
44 *
45 * This interface also simplify data fetching. Client only needs to provide a
46 * data fetching strategy (through a updateCallback).
47 */
48class Socket : noncopyable
49{
50public:
51 class Error : public std::runtime_error
52 {
53 public:
54 explicit
55 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58 }
59 };
60
61 Socket(const Name& syncPrefix,
62 const Name& userPrefix,
63 ndn::Face& face,
64 const UpdateCallback& updateCallback);
65
66 /**
67 * @brief Publish a data packet in the session and trigger synchronization updates
68 *
69 * This method will create a data packet with the supplied content.
70 * The packet name is the local session + seqNo.
71 * The seqNo is automatically maintained by internal Logic.
72 *
73 * @param buf Pointer to the bytes in content
74 * @param len size of the bytes in content
75 * @param freshness FreshnessPeriod of the data packet.
76 */
77 void
78 publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness);
79
80 /**
81 * @brief Publish a data packet in the session and trigger synchronization updates
82 *
83 * This method will create a data packet with the supplied content.
84 * The packet name is the local session + seqNo.
85 * The seqNo is automatically maintained by internal Logic.
86 *
87 * @param content Block that will be set as the content of the data packet.
88 * @param freshness FreshnessPeriod of the data packet.
89 */
90 void
91 publishData(const Block& content, const ndn::time::milliseconds& freshness);
92
93 /**
94 * @brief Retrive a data packet with a particular seqNo from a session
95 *
96 * @param sessionName The name of the target session.
97 * @param seq The seqNo of the data packet.
98 * @param onValidated The callback when the retrieved packet has been validated.
99 * @param nRetries The number of retries.
100 */
101 void
102 fetchData(const Name& sessionName, const SeqNo& seq,
103 const ndn::OnDataValidated& onValidated,
104 int nRetries = 0);
105
106 /**
107 * @brief Retrive a data packet with a particular seqNo from a session
108 *
109 * @param sessionName The name of the target session.
110 * @param seq The seqNo of the data packet.
111 * @param onValidated The callback when the retrieved packet has been validated.
112 * @param nRetries The number of retries.
113 */
114 void
115 fetchData(const Name& sessionName, const SeqNo& seq,
116 const ndn::OnDataValidated& onValidated,
117 const ndn::OnDataValidationFailed& onValidationFailed,
118 const ndn::OnTimeout& onTimeout,
119 int nRetries = 0);
120
121 /// @brief Get the root digest of current sync tree
122 ndn::ConstBufferPtr
123 getRootDigest() const;
124
125 Logic&
126 getLogic()
127 {
128 return m_logic;
129 }
130
131private:
132 void
133 onData(const Interest& interest, Data& data,
134 const ndn::OnDataValidated& dataCallback,
135 const ndn::OnDataValidationFailed& failCallback);
136
137 void
138 onDataTimeout(const Interest& interest, int nRetries,
139 const ndn::OnDataValidated& dataCallback,
140 const ndn::OnDataValidationFailed& failCallback);
141
142 void
143 onDataValidationFailed(const shared_ptr<const Data>& data,
144 const std::string& failureInfo);
145
146private:
147
148 Name m_userPrefix;
149 ndn::Face& m_face;
150
151 Logic m_logic;
152
153 ndn::KeyChain m_keyChain;
154};
155
156} // namespace chronosync
157
158#endif // CHRONOSYNC_SOCKET_HPP