blob: 19fac0de041bd005e73afd837ef3a41d610533d1 [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
19 */
20
Alexander Afanasyev531803b2014-02-05 15:57:35 -080021#ifndef _SYNC_SOCKET_H
22#define _SYNC_SOCKET_H
Yingdi Yu280bb962014-01-30 09:52:43 -080023
Yingdi Yue8154712014-01-21 10:20:14 -080024#include <ndn-cpp-dev/face.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080025#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080026#include <ndn-cpp-dev/security/validator-null.hpp>
Yingdi Yue8154712014-01-21 10:20:14 -080027#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080028
Alexander Afanasyev531803b2014-02-05 15:57:35 -080029#include "sync-logic.h"
30#include "sync-seq-no.h"
Yingdi Yu3da10fe2014-02-27 16:37:34 -080031#include "sync-validator.h"
Alexander Afanasyev531803b2014-02-05 15:57:35 -080032
Yingdi Yu43e71612013-10-30 22:19:31 -070033#include <utility>
34#include <map>
35#include <vector>
36#include <sstream>
37
38namespace Sync {
39
40/**
41 * \ingroup sync
42 * @brief A simple interface to interact with client code
43 */
44class SyncSocket
45{
46public:
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080047 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
48
Yingdi Yu280bb962014-01-30 09:52:43 -080049 typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback;
50 typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback;
51
Yingdi Yu0eee6002014-02-11 15:54:17 -080052 SyncSocket (const ndn::Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080053 const ndn::Name& dataPrefix,
54 uint64_t dataSession,
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080055 ndn::shared_ptr<ndn::Face> face,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080056 const ndn::IdentityCertificate& myCertificate,
57 ndn::shared_ptr<ndn::SecRuleRelative> dataRule,
Yingdi Yu43e71612013-10-30 22:19:31 -070058 NewDataCallback dataCallback,
59 RemoveCallback rmCallback);
60
61 ~SyncSocket ();
62
Yingdi Yu3da10fe2014-02-27 16:37:34 -080063 void
64 publishData(const uint8_t* buf, size_t len, int freshness, bool isCert = false);
Yingdi Yu43e71612013-10-30 22:19:31 -070065
66 void
Yingdi Yu6d638f02014-01-24 11:01:21 -080067 remove (const ndn::Name &prefix)
Yingdi Yu3da10fe2014-02-27 16:37:34 -080068 {
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080069 m_syncLogic->remove(prefix);
Yingdi Yu3da10fe2014-02-27 16:37:34 -080070 }
Yingdi Yu43e71612013-10-30 22:19:31 -070071
72 void
Yingdi Yu280bb962014-01-30 09:52:43 -080073 fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0);
Yingdi Yu43e71612013-10-30 22:19:31 -070074
75 std::string
76 getRootDigest()
Yingdi Yu3da10fe2014-02-27 16:37:34 -080077 {
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080078 return m_syncLogic->getRootDigest();
Yingdi Yu3da10fe2014-02-27 16:37:34 -080079 }
Yingdi Yu43e71612013-10-30 22:19:31 -070080
Yingdi Yu280bb962014-01-30 09:52:43 -080081 uint64_t
Yingdi Yu3da10fe2014-02-27 16:37:34 -080082 getNextSeq (const ndn::Name &prefix, uint64_t session)
83 {
84 SequenceLog::iterator i = m_sequenceLog.find (prefix);
85
86 if (i != m_sequenceLog.end ())
87 {
88 SeqNo s = i->second;
89 if (s.getSession() == session)
90 return s.getSeq();
91 }
92 return 0;
93 }
Yingdi Yu43e71612013-10-30 22:19:31 -070094
95 SyncLogic &
96 getLogic ()
Yingdi Yu3da10fe2014-02-27 16:37:34 -080097 {
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080098 return *m_syncLogic;
Yingdi Yu3da10fe2014-02-27 16:37:34 -080099 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700100
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800101 void
102 addParticipant(const ndn::IdentityCertificate& introducee)
103 {
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800104 if(m_withSecurity)
105 {
106 ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introducee);
107 }
108 }
109
110 void
111 addParticipant(const IntroCertificate& introCert)
112 {
113 if(m_withSecurity)
114 {
115 ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert);
116 }
117 }
118
119 void
120 getIntroCertNames(std::vector<ndn::Name>& list)
121 {
122 if(m_withSecurity)
123 {
124 ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertNames(list);
125 }
126 }
127
128 const IntroCertificate&
129 getIntroCertificate(const ndn::Name& name)
130 {
131 if(m_withSecurity)
132 {
133 return ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertificate(name);
134 }
135 throw Error("You are running SyncSocket without security!");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800136 }
137
138 // // make this a static function so we don't have to create socket instance without
139 // // knowing the local prefix. it's a wrong place for this function anyway
140 // static std::string
141 // GetLocalPrefix ();
Yingdi Yu43e71612013-10-30 22:19:31 -0700142
143private:
Yingdi Yu51c80252014-02-10 19:32:05 -0800144 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800145 publishDataInternal(ndn::shared_ptr<ndn::Data> data,
146 const ndn::Name &prefix,
147 uint64_t session,
148 bool isCert);
Yingdi Yu51c80252014-02-10 19:32:05 -0800149
Yingdi Yu43e71612013-10-30 22:19:31 -0700150 void
151 passCallback(const std::vector<MissingDataInfo> &v)
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800152 {
153 m_newDataCallback(v, this);
154 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700155
156 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800157 onData(const ndn::Interest& interest, ndn::Data& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800158 const ndn::OnDataValidated& onValidated,
159 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800160
161 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800162 onDataTimeout(const ndn::Interest& interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800163 int retry,
164 const ndn::OnDataValidated& onValidated,
165 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800166
167 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800168 onDataValidated(const ndn::shared_ptr<const ndn::Data>& data,
169 size_t interestNameSize,
170 const ndn::OnDataValidated& onValidated);
171
172 void
173 onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
174 const std::string& failureInfo);
Yingdi Yu43e71612013-10-30 22:19:31 -0700175
176private:
Yingdi Yu6d638f02014-01-24 11:01:21 -0800177 typedef std::map<ndn::Name, SeqNo> SequenceLog;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800178
179 ndn::Name m_dataPrefix;
180 uint64_t m_dataSession;
Yingdi Yu43e71612013-10-30 22:19:31 -0700181 NewDataCallback m_newDataCallback;
182 SequenceLog m_sequenceLog;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800183 ndn::IdentityCertificate m_myCertificate;
184 ndn::KeyChain m_keyChain;
Yingdi Yu280bb962014-01-30 09:52:43 -0800185 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu51c80252014-02-10 19:32:05 -0800186 ndn::shared_ptr<boost::asio::io_service> m_ioService;
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800187 bool m_withSecurity;
188 ndn::shared_ptr<ndn::Validator> m_syncValidator;
189 ndn::shared_ptr<SyncLogic> m_syncLogic;
Yingdi Yu43e71612013-10-30 22:19:31 -0700190};
191
192} // Sync
193
194#endif // SYNC_SOCKET_H