blob: 02fb85d56ff503c246f6bd2f85af610144e41a4f [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
Yingdi Yu43e71612013-10-30 22:19:31 -070024using namespace std;
25using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080026using namespace ndn::ptr_lib;
27
28INIT_LOGGER ("SyncSocket");
Yingdi Yu43e71612013-10-30 22:19:31 -070029
30namespace Sync {
31
32SyncSocket::SyncSocket (const string &syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080033 shared_ptr<SecPolicySync> policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080034 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070035 NewDataCallback dataCallback,
36 RemoveCallback rmCallback )
37 : m_newDataCallback(dataCallback)
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080038 , m_policy(policy)
39 , m_verifier(new Verifier(policy))
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080040 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +080041 , m_face(face)
Yingdi Yu43e71612013-10-30 22:19:31 -070042 , m_syncLogic (syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080043 policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080044 face,
Yingdi Yu43e71612013-10-30 22:19:31 -070045 bind(&SyncSocket::passCallback, this, _1),
46 rmCallback)
47{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080048 m_verifier->setFace(face);
Yingdi Yu43e71612013-10-30 22:19:31 -070049}
50
51SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080052{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080053}
54
Yingdi Yu43e71612013-10-30 22:19:31 -070055bool
Yingdi Yu6d638f02014-01-24 11:01:21 -080056SyncSocket::publishData(const Name &prefix, uint32_t session, const char *buf, size_t len, int freshness)
Yingdi Yu43e71612013-10-30 22:19:31 -070057{
58 uint32_t sequence = getNextSeq(prefix, session);
Yingdi Yu6d638f02014-01-24 11:01:21 -080059 ostringstream sessionStream;
60 ostringstream seqStream;
61 sessionStream << session;
62 seqStream << sequence;
Yingdi Yu43e71612013-10-30 22:19:31 -070063
Yingdi Yu6d638f02014-01-24 11:01:21 -080064 Name dataName = prefix;
65 dataName.append(sessionStream.str()).append(seqStream.str());
66
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080067 Name signingIdentity = m_policy->inferSigningIdentity(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080068
Yingdi Yu6d638f02014-01-24 11:01:21 -080069 Data data(dataName);
70 data.setContent(reinterpret_cast<const uint8_t*>(buf), len);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080071
Yingdi Yu6d638f02014-01-24 11:01:21 -080072 m_keyChain->signByIdentity(data, signingIdentity);
Yingdi Yu43e71612013-10-30 22:19:31 -070073
Yingdi Yu6d638f02014-01-24 11:01:21 -080074 m_face->put(data);
Yingdi Yu43e71612013-10-30 22:19:31 -070075
76 SeqNo s(session, sequence + 1);
77 m_sequenceLog[prefix] = s;
78 m_syncLogic.addLocalNames (prefix, session, sequence);
79 return true;
80}
81
82void
Yingdi Yu6d638f02014-01-24 11:01:21 -080083SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnVerified& onVerified, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -070084{
Yingdi Yu6d638f02014-01-24 11:01:21 -080085 ostringstream sessionStream;
86 ostringstream seqStream;
87 sessionStream << seq.getSession();
88 seqStream << seq.getSeq();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080089
Yingdi Yu6d638f02014-01-24 11:01:21 -080090 Name interestName = prefix;
91 interestName.append(sessionStream.str()).append(seqStream.str());
92
93 const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onDataVerifyFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -070094
95
Yingdi Yu6d638f02014-01-24 11:01:21 -080096 ndn::Interest interest(interestName);
97 m_face->expressInterest(interest,
98 bind(&SyncSocket::onData, this, _1, _2, onVerified, onVerifyFailed),
99 bind(&SyncSocket::onDataTimeout, this, _1, retry, onVerified, onVerifyFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800100
Yingdi Yu43e71612013-10-30 22:19:31 -0700101}
102
103void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800104SyncSocket::onData(const shared_ptr<const ndn::Interest>& interest,
105 const shared_ptr<Data>& data,
106 const OnVerified& onVerified,
107 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800108{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800109 m_verifier->verifyData(data, onVerified, onVerifyFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800110}
111
112void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800113SyncSocket::onDataTimeout(const shared_ptr<const ndn::Interest>& interest,
114 int retry,
115 const OnVerified& onVerified,
116 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700117{
118 if(retry > 0)
119 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800120 m_face->expressInterest(*interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800121 bind(&SyncSocket::onData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800122 this,
123 _1,
124 _2,
125 onVerified,
126 onVerifyFailed),
Yingdi Yu6d638f02014-01-24 11:01:21 -0800127 bind(&SyncSocket::onDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800128 this,
129 _1,
130 retry - 1,
131 onVerified,
132 onVerifyFailed));
133
Yingdi Yu43e71612013-10-30 22:19:31 -0700134 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800135 else
Yingdi Yu6d638f02014-01-24 11:01:21 -0800136 _LOG_DEBUG("interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700137}
138
139void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800140SyncSocket::onDataVerifyFailed(const shared_ptr<Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800141{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800142 _LOG_DEBUG("data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800143}
Yingdi Yu43e71612013-10-30 22:19:31 -0700144
145
146uint32_t
Yingdi Yu6d638f02014-01-24 11:01:21 -0800147SyncSocket::getNextSeq (const Name &prefix, uint32_t session)
Yingdi Yu43e71612013-10-30 22:19:31 -0700148{
149 SequenceLog::iterator i = m_sequenceLog.find (prefix);
150
151 if (i != m_sequenceLog.end ())
152 {
153 SeqNo s = i->second;
154 if (s.getSession() == session)
155 return s.getSeq();
156 }
157 return 0;
158}
159
160}//Sync