blob: 3dcef9e558bae327c0a707bec7103daca4840bf3 [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 Yu7c64e5c2014-04-30 14:06:37 -070024#include <ndn-cxx/face.hpp>
25#include <ndn-cxx/security/validator.hpp>
26#include <ndn-cxx/security/validator-null.hpp>
27#include <ndn-cxx/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 Yud4ef5d32014-03-01 02:02:01 -080052 static const ndn::Name EMPTY_NAME;
Yingdi Yu43e71612013-10-30 22:19:31 -070053
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070054 SyncSocket(const ndn::Name& syncPrefix,
Yingdi Yud4ef5d32014-03-01 02:02:01 -080055 const ndn::Name& dataPrefix,
56 uint64_t dataSession,
57 bool withRoutingPrefix,
58 const ndn::Name& routingPrefix,
59 ndn::shared_ptr<ndn::Face> face,
60 const ndn::IdentityCertificate& myCertificate,
61 ndn::shared_ptr<ndn::SecRuleRelative> dataRule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070062 NewDataCallback dataCallback,
Yingdi Yud4ef5d32014-03-01 02:02:01 -080063 RemoveCallback rmCallback);
64
65 ~SyncSocket();
Yingdi Yu43e71612013-10-30 22:19:31 -070066
Yingdi Yu3da10fe2014-02-27 16:37:34 -080067 void
68 publishData(const uint8_t* buf, size_t len, int freshness, bool isCert = false);
Yingdi Yu43e71612013-10-30 22:19:31 -070069
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070070 void
71 leave()
72 {
73 m_syncLogic->remove(m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix);
Yingdi Yud4ef5d32014-03-01 02:02:01 -080074 }
75
76 void
77 remove(const ndn::Name& prefix)
78 {
79 m_syncLogic->remove(prefix);
Yingdi Yu3da10fe2014-02-27 16:37:34 -080080 }
Yingdi Yu43e71612013-10-30 22:19:31 -070081
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070082 void
Yingdi Yu280bb962014-01-30 09:52:43 -080083 fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0);
Yingdi Yu43e71612013-10-30 22:19:31 -070084
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070085 std::string
86 getRootDigest()
87 {
88 return m_syncLogic->getRootDigest();
Yingdi Yu3da10fe2014-02-27 16:37:34 -080089 }
Yingdi Yu43e71612013-10-30 22:19:31 -070090
Yingdi Yu280bb962014-01-30 09:52:43 -080091 uint64_t
Yingdi Yud4ef5d32014-03-01 02:02:01 -080092 getNextSeq()
Yingdi Yu3da10fe2014-02-27 16:37:34 -080093 {
Yingdi Yud4ef5d32014-03-01 02:02:01 -080094 // If DNS works, we should use pure m_dataprefix rather than the one with routing prefix.
95 SequenceLog::iterator i = m_sequenceLog.find (m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070096
Yingdi Yu3da10fe2014-02-27 16:37:34 -080097 if (i != m_sequenceLog.end ())
98 {
99 SeqNo s = i->second;
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800100 if (s.getSession() == m_dataSession)
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800101 return s.getSeq();
102 }
103 return 0;
104 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700105
106 SyncLogic &
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700107 getLogic()
108 {
109 return *m_syncLogic;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800110 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700111
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800112 void
113 addParticipant(const ndn::IdentityCertificate& introducee)
114 {
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800115 if(m_withSecurity)
116 {
117 ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introducee);
118 }
119 }
120
121 void
122 addParticipant(const IntroCertificate& introCert)
123 {
124 if(m_withSecurity)
125 {
126 ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert);
127 }
128 }
129
130 void
131 getIntroCertNames(std::vector<ndn::Name>& list)
132 {
133 if(m_withSecurity)
134 {
135 ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertNames(list);
136 }
137 }
138
139 const IntroCertificate&
140 getIntroCertificate(const ndn::Name& name)
141 {
142 if(m_withSecurity)
143 {
144 return ndn::dynamic_pointer_cast<SyncValidator>(m_syncValidator)->getIntroCertificate(name);
145 }
146 throw Error("You are running SyncSocket without security!");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800147 }
148
149 // // make this a static function so we don't have to create socket instance without
150 // // knowing the local prefix. it's a wrong place for this function anyway
151 // static std::string
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700152 // GetLocalPrefix ();
153
Yingdi Yu43e71612013-10-30 22:19:31 -0700154private:
Yingdi Yu51c80252014-02-10 19:32:05 -0800155 void
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800156 publishDataInternal(ndn::shared_ptr<ndn::Data> data, bool isCert);
Yingdi Yu51c80252014-02-10 19:32:05 -0800157
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700158 void
159 passCallback(const std::vector<MissingDataInfo> &v)
160 {
161 m_newDataCallback(v, this);
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800162 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700163
164 void
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800165 onData(const ndn::Interest& interest, ndn::Data& data, const ndn::OnDataValidated& dataCallback);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800166
167 void
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800168 onDataTimeout(const ndn::Interest& interest, int retry, const ndn::OnDataValidated& dataCallback);
169
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800170
171 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800172 onDataValidated(const ndn::shared_ptr<const ndn::Data>& data,
173 size_t interestNameSize,
174 const ndn::OnDataValidated& onValidated);
175
176 void
177 onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
178 const std::string& failureInfo);
Yingdi Yu43e71612013-10-30 22:19:31 -0700179
180private:
Yingdi Yu6d638f02014-01-24 11:01:21 -0800181 typedef std::map<ndn::Name, SeqNo> SequenceLog;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800182
183 ndn::Name m_dataPrefix;
184 uint64_t m_dataSession;
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800185 ndn::Name m_routableDataPrefix;
186 bool m_withRoutingPrefix;
Yingdi Yu43e71612013-10-30 22:19:31 -0700187 NewDataCallback m_newDataCallback;
188 SequenceLog m_sequenceLog;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800189 ndn::IdentityCertificate m_myCertificate;
190 ndn::KeyChain m_keyChain;
Yingdi Yu280bb962014-01-30 09:52:43 -0800191 ndn::shared_ptr<ndn::Face> m_face;
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700192 boost::asio::io_service& m_ioService;
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800193 bool m_withSecurity;
194 ndn::shared_ptr<ndn::Validator> m_syncValidator;
195 ndn::shared_ptr<SyncLogic> m_syncLogic;
Yingdi Yu43e71612013-10-30 22:19:31 -0700196};
197
198} // Sync
199
200#endif // SYNC_SOCKET_H