blob: 42612e05c32c9035faa77e0c27ad23bf8f442345 [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -07001/* -*- Mode: C32++; 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
21#include "sync-socket.h"
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080022#include "sync-logging.h"
23
24#include <ndn-cpp/security/identity/basic-identity-storage.hpp>
25#include <ndn-cpp/security/identity/osx-private-key-storage.hpp>
Yingdi Yu43e71612013-10-30 22:19:31 -070026
27using namespace std;
28using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080029using namespace ndn::ptr_lib;
30
31INIT_LOGGER ("SyncSocket");
Yingdi Yu43e71612013-10-30 22:19:31 -070032
33namespace Sync {
34
35SyncSocket::SyncSocket (const string &syncPrefix,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080036 shared_ptr<SyncPolicyManager> syncPolicyManager,
Yingdi Yu43e71612013-10-30 22:19:31 -070037 NewDataCallback dataCallback,
38 RemoveCallback rmCallback )
39 : m_newDataCallback(dataCallback)
40 , m_syncPolicyManager(syncPolicyManager)
41 , m_syncLogic (syncPrefix,
42 syncPolicyManager,
43 bind(&SyncSocket::passCallback, this, _1),
44 rmCallback)
45{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080046 m_transport = make_shared<TcpTransport>();
47 m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
48
49 connectToDaemon();
50
51 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
52 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
53 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
Yingdi Yu43e71612013-10-30 22:19:31 -070054}
55
56SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080057{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080058 m_face->shutdown();
59}
60
61void
62SyncSocket::connectToDaemon()
63{
64 //Hack! transport does not connect to daemon unless an interest is expressed.
65 Name name("/ndn");
66 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name);
67 m_face->expressInterest(*interest,
68 bind(&SyncSocket::onConnectionData, this, _1, _2),
69 bind(&SyncSocket::onConnectionDataTimeout, this, _1));
70}
71
72void
73SyncSocket::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
74 const shared_ptr<Data>& data)
75{
76 _LOG_DEBUG("onConnectionData");
77}
78
79void
80SyncSocket::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
81{
82 _LOG_DEBUG("onConnectionDataTimeout");
Yingdi Yu479e1172013-11-06 16:36:19 -080083}
Yingdi Yu43e71612013-10-30 22:19:31 -070084
85bool
86SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness)
87{
88 uint32_t sequence = getNextSeq(prefix, session);
89 ostringstream contentNameWithSeqno;
90 contentNameWithSeqno << prefix << "/" << session << "/" << sequence;
91
92 Name dataName(contentNameWithSeqno.str ());
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080093 Blob blob((const uint8_t*)buf, len);
Yingdi Yu43e71612013-10-30 22:19:31 -070094 Name signingIdentity = m_syncPolicyManager->inferSigningIdentity(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080095
96 shared_ptr<Data> data = make_shared<Data>(dataName);
97 data->setContent(blob.buf(), blob.size());
98 data->getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
99
100 Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(signingIdentity);
101 m_identityManager->signByCertificate(*data, certificateName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700102
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800103 m_transport->send(*data->wireEncode());
Yingdi Yu43e71612013-10-30 22:19:31 -0700104
105 SeqNo s(session, sequence + 1);
106 m_sequenceLog[prefix] = s;
107 m_syncLogic.addLocalNames (prefix, session, sequence);
108 return true;
109}
110
111void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800112SyncSocket::fetchData(const string &prefix, const SeqNo &seq, const OnVerified& onVerified, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -0700113{
114 ostringstream interestName;
115 interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq();
116 //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800117
118 const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onChatDataVerifyFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -0700119
120
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800121 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName.str());
122 m_face->expressInterest(*interest,
123 bind(&SyncSocket::onChatData, this, _1, _2, onVerified, onVerifyFailed),
124 bind(&SyncSocket::onChatDataTimeout, this, _1, retry, onVerified, onVerifyFailed));
125
Yingdi Yu43e71612013-10-30 22:19:31 -0700126}
127
128void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800129SyncSocket::onChatCert(const shared_ptr<const ndn::Interest>& interest,
130 const shared_ptr<Data>& cert,
131 shared_ptr<ValidationRequest> previousStep)
132{
133 shared_ptr<ValidationRequest> nextStep = m_syncPolicyManager->checkVerificationPolicy(cert,
134 previousStep->stepCount_,
135 previousStep->onVerified_,
136 previousStep->onVerifyFailed_);
137
138 if (nextStep)
139 m_face->expressInterest
140 (*nextStep->interest_,
141 bind(&SyncSocket::onChatCert, this, _1, _2, nextStep),
142 bind(&SyncSocket::onChatCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
143}
144
145void
146SyncSocket::onChatCertTimeout(const shared_ptr<const ndn::Interest>& interest,
147 const OnVerifyFailed& onVerifyFailed,
148 const shared_ptr<Data>& data,
149 shared_ptr<ValidationRequest> nextStep)
150{
151 if(nextStep->retry_ > 0)
152 m_face->expressInterest(*interest,
153 bind(&SyncSocket::onChatCert,
154 this,
155 _1,
156 _2,
157 nextStep),
158 bind(&SyncSocket::onChatCertTimeout,
159 this,
160 _1,
161 onVerifyFailed,
162 data,
163 nextStep));
164 else
165 onVerifyFailed(data);
166}
167
168void
169SyncSocket::onChatData(const shared_ptr<const ndn::Interest>& interest,
170 const shared_ptr<Data>& data,
171 const OnVerified& onVerified,
172 const OnVerifyFailed& onVerifyFailed)
173{
174 shared_ptr<ValidationRequest> nextStep = m_syncPolicyManager->checkVerificationPolicy(data, 0, onVerified, onVerifyFailed);
175
176 if (nextStep)
177 m_face->expressInterest
178 (*nextStep->interest_,
179 bind(&SyncSocket::onChatCert, this, _1, _2, nextStep),
180 bind(&SyncSocket::onChatCertTimeout, this, _1, onVerifyFailed, data, nextStep));
181}
182
183void
184SyncSocket::onChatDataTimeout(const shared_ptr<const ndn::Interest>& interest,
185 int retry,
186 const OnVerified& onVerified,
187 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700188{
189 if(retry > 0)
190 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800191 m_face->expressInterest(*interest,
192 bind(&SyncSocket::onChatData,
193 this,
194 _1,
195 _2,
196 onVerified,
197 onVerifyFailed),
198 bind(&SyncSocket::onChatDataTimeout,
199 this,
200 _1,
201 retry - 1,
202 onVerified,
203 onVerifyFailed));
204
Yingdi Yu43e71612013-10-30 22:19:31 -0700205 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800206 else
207 _LOG_DEBUG("Chat interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700208}
209
210void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800211SyncSocket::onChatDataVerifyFailed(const shared_ptr<Data>& data)
212{
213 _LOG_DEBUG("Chat data cannot be verified!");
214}
Yingdi Yu43e71612013-10-30 22:19:31 -0700215
216
217uint32_t
218SyncSocket::getNextSeq (const string &prefix, uint32_t session)
219{
220 SequenceLog::iterator i = m_sequenceLog.find (prefix);
221
222 if (i != m_sequenceLog.end ())
223 {
224 SeqNo s = i->second;
225 if (s.getSession() == session)
226 return s.getSeq();
227 }
228 return 0;
229}
230
231}//Sync