blob: 118844128e037f939b1ddb5459357c683f7c8ef6 [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 +080026
27INIT_LOGGER ("SyncSocket");
Yingdi Yu43e71612013-10-30 22:19:31 -070028
29namespace Sync {
30
Alexander Afanasyev531803b2014-02-05 15:57:35 -080031using ndn::shared_ptr;
32
Yingdi Yu0eee6002014-02-11 15:54:17 -080033SyncSocket::SyncSocket (const Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080034 const ndn::Name& dataPrefix,
35 uint64_t dataSession,
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080036 shared_ptr<Face> face,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080037 const IdentityCertificate& myCertificate,
38 shared_ptr<SecRuleRelative> dataRule,
Yingdi Yu43e71612013-10-30 22:19:31 -070039 NewDataCallback dataCallback,
40 RemoveCallback rmCallback )
Yingdi Yu3da10fe2014-02-27 16:37:34 -080041 : m_dataPrefix(dataPrefix)
42 , m_dataSession(dataSession)
43 , m_newDataCallback(dataCallback)
44 , m_myCertificate(myCertificate)
Yingdi Yu6e235db2013-12-27 08:40:53 +080045 , m_face(face)
Yingdi Yu51c80252014-02-10 19:32:05 -080046 , m_ioService(face->ioService())
Yingdi Yuaed0f3e2014-02-28 14:54:16 -080047{
48 if(static_cast<bool>(dataRule))
49 {
50 m_withSecurity = true;
51 m_syncValidator = shared_ptr<Validator>(new SyncValidator(syncPrefix,
52 m_myCertificate,
53 m_face,
54 bind(&SyncSocket::publishData, this, _1, _2, _3, true),
55 dataRule));
56 }
57 else
58 {
59 m_withSecurity = false;
60 m_syncValidator = shared_ptr<Validator>(new ValidatorNull());
61 }
62
63
64 m_syncLogic = shared_ptr<SyncLogic>(new SyncLogic(syncPrefix,
65 myCertificate,
66 m_syncValidator,
67 m_face,
68 bind(&SyncSocket::passCallback, this, _1),
69 rmCallback));
70}
Yingdi Yu43e71612013-10-30 22:19:31 -070071
72SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080073{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080074}
75
Yingdi Yu3da10fe2014-02-27 16:37:34 -080076void
77SyncSocket::publishData(const uint8_t* buf, size_t len, int freshness, bool isCert)
Yingdi Yu43e71612013-10-30 22:19:31 -070078{
Yingdi Yu51c80252014-02-10 19:32:05 -080079 shared_ptr<Data> data = make_shared<Data>();
80 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
81 data->setFreshnessPeriod(1000*freshness);
82
Yingdi Yu3da10fe2014-02-27 16:37:34 -080083 m_ioService->post(bind(&SyncSocket::publishDataInternal, this,
84 data, m_dataPrefix, m_dataSession, isCert));
Yingdi Yu51c80252014-02-10 19:32:05 -080085}
86
87void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080088SyncSocket::publishDataInternal(shared_ptr<Data> data, const Name &prefix, uint64_t session, bool isCert)
Yingdi Yu51c80252014-02-10 19:32:05 -080089{
Yingdi Yu280bb962014-01-30 09:52:43 -080090 uint64_t sequence = getNextSeq(prefix, session);
Yingdi Yu6d638f02014-01-24 11:01:21 -080091 Name dataName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080092 dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
Yingdi Yu3da10fe2014-02-27 16:37:34 -080093 if(isCert)
94 dataName.append("INTRO-CERT");
Yingdi Yu51c80252014-02-10 19:32:05 -080095 data->setName(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080096
Yingdi Yu3da10fe2014-02-27 16:37:34 -080097 m_keyChain.sign(*data, m_myCertificate.getName());
Yingdi Yu51c80252014-02-10 19:32:05 -080098 m_face->put(*data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080099
Yingdi Yu43e71612013-10-30 22:19:31 -0700100 SeqNo s(session, sequence + 1);
Yingdi Yu51c80252014-02-10 19:32:05 -0800101
Yingdi Yu43e71612013-10-30 22:19:31 -0700102 m_sequenceLog[prefix] = s;
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800103 m_syncLogic->addLocalNames (prefix, session, sequence);
Yingdi Yu43e71612013-10-30 22:19:31 -0700104}
105
106void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800107SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& dataCallback, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -0700108{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800109 Name interestName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -0800110 interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
Yingdi Yu6d638f02014-01-24 11:01:21 -0800111
Yingdi Yu6d638f02014-01-24 11:01:21 -0800112 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800113 interest.setMustBeFresh(true);
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800114
115 const OnDataValidated& onValidated = bind(&SyncSocket::onDataValidated, this, _1, interestName.size(), dataCallback);
116 const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1, _2);
117
Yingdi Yu6d638f02014-01-24 11:01:21 -0800118 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800119 bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed),
120 bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800121
Yingdi Yu43e71612013-10-30 22:19:31 -0700122}
123
124void
Yingdi Yu51c80252014-02-10 19:32:05 -0800125SyncSocket::onData(const ndn::Interest& interest, Data& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800126 const OnDataValidated& onValidated,
127 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800128{
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800129 m_syncValidator->validate(data, onValidated, onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800130}
131
132void
Yingdi Yu51c80252014-02-10 19:32:05 -0800133SyncSocket::onDataTimeout(const ndn::Interest& interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800134 int retry,
Yingdi Yu280bb962014-01-30 09:52:43 -0800135 const OnDataValidated& onValidated,
136 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700137{
138 if(retry > 0)
139 {
Yingdi Yu51c80252014-02-10 19:32:05 -0800140 m_face->expressInterest(interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800141 bind(&SyncSocket::onData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800142 this,
143 _1,
144 _2,
Yingdi Yu280bb962014-01-30 09:52:43 -0800145 onValidated,
146 onValidationFailed),
Yingdi Yu6d638f02014-01-24 11:01:21 -0800147 bind(&SyncSocket::onDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800148 this,
149 _1,
150 retry - 1,
Yingdi Yu280bb962014-01-30 09:52:43 -0800151 onValidated,
152 onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800153
Yingdi Yu43e71612013-10-30 22:19:31 -0700154 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800155 else
Yingdi Yu6d638f02014-01-24 11:01:21 -0800156 _LOG_DEBUG("interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700157}
158
159void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800160SyncSocket::onDataValidated(const shared_ptr<const Data>& data,
161 size_t interestNameSize,
162 const OnDataValidated& onValidated)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800163{
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800164 _LOG_DEBUG("--------------------" << data->getName());
165 if(data->getName().size() > interestNameSize
166 && data->getName().get(interestNameSize).toEscapedString() == "INTRO-CERT")
167 {
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800168 if(!m_withSecurity)
169 return;
170
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800171 Data rawData;
172 rawData.wireDecode(data->getContent().blockFromValue());
173 IntroCertificate introCert(rawData);
Yingdi Yuaed0f3e2014-02-28 14:54:16 -0800174 dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert);
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800175 }
176 else
177 {
178 onValidated(data);
179 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800180}
Yingdi Yu43e71612013-10-30 22:19:31 -0700181
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800182void
183SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data,
184 const std::string& failureInfo)
Yingdi Yu43e71612013-10-30 22:19:31 -0700185{
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800186 _LOG_DEBUG("data cannot be verified!: " << failureInfo);
Yingdi Yu43e71612013-10-30 22:19:31 -0700187}
188
189}//Sync