blob: b37d65756137f3249e456cb14a14cfdf558946ba [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,
34 const Name& identity,
Yingdi Yu280bb962014-01-30 09:52:43 -080035 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080036 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070037 NewDataCallback dataCallback,
38 RemoveCallback rmCallback )
39 : m_newDataCallback(dataCallback)
Yingdi Yu0eee6002014-02-11 15:54:17 -080040 , m_identity(identity)
Yingdi Yu280bb962014-01-30 09:52:43 -080041 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080042 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +080043 , m_face(face)
Yingdi Yu51c80252014-02-10 19:32:05 -080044 , m_ioService(face->ioService())
Yingdi Yu43e71612013-10-30 22:19:31 -070045 , m_syncLogic (syncPrefix,
Yingdi Yu0eee6002014-02-11 15:54:17 -080046 identity,
Yingdi Yu280bb962014-01-30 09:52:43 -080047 validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080048 face,
Yingdi Yu43e71612013-10-30 22:19:31 -070049 bind(&SyncSocket::passCallback, this, _1),
50 rmCallback)
Yingdi Yu280bb962014-01-30 09:52:43 -080051{}
Yingdi Yu43e71612013-10-30 22:19:31 -070052
53SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080054{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080055}
56
Yingdi Yu43e71612013-10-30 22:19:31 -070057bool
Yingdi Yu280bb962014-01-30 09:52:43 -080058SyncSocket::publishData(const Name &prefix, uint64_t session, const char *buf, size_t len, int freshness)
Yingdi Yu43e71612013-10-30 22:19:31 -070059{
Yingdi Yu51c80252014-02-10 19:32:05 -080060 shared_ptr<Data> data = make_shared<Data>();
61 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
62 data->setFreshnessPeriod(1000*freshness);
63
64 m_ioService->post(bind(&SyncSocket::publishDataInternal, this, data, prefix, session));
65
66 return true;
67}
68
69void
70SyncSocket::publishDataInternal(shared_ptr<Data> data, const Name &prefix, uint64_t session)
71{
Yingdi Yu280bb962014-01-30 09:52:43 -080072 uint64_t sequence = getNextSeq(prefix, session);
Yingdi Yu6d638f02014-01-24 11:01:21 -080073 Name dataName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080074 dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
Yingdi Yu51c80252014-02-10 19:32:05 -080075 data->setName(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080076
Yingdi Yu0eee6002014-02-11 15:54:17 -080077 m_keyChain->signByIdentity(*data, m_identity);
Yingdi Yu51c80252014-02-10 19:32:05 -080078 m_face->put(*data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080079
Yingdi Yu43e71612013-10-30 22:19:31 -070080 SeqNo s(session, sequence + 1);
Yingdi Yu51c80252014-02-10 19:32:05 -080081
Yingdi Yu43e71612013-10-30 22:19:31 -070082 m_sequenceLog[prefix] = s;
83 m_syncLogic.addLocalNames (prefix, session, sequence);
Yingdi Yu43e71612013-10-30 22:19:31 -070084}
85
86void
Yingdi Yu280bb962014-01-30 09:52:43 -080087SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& onValidated, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -070088{
Yingdi Yu6d638f02014-01-24 11:01:21 -080089 Name interestName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080090 interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
Yingdi Yu6d638f02014-01-24 11:01:21 -080091
Yingdi Yu280bb962014-01-30 09:52:43 -080092 const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -070093
Yingdi Yu6d638f02014-01-24 11:01:21 -080094 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -080095 interest.setMustBeFresh(true);
Yingdi Yu6d638f02014-01-24 11:01:21 -080096 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -080097 bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed),
98 bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080099
Yingdi Yu43e71612013-10-30 22:19:31 -0700100}
101
102void
Yingdi Yu51c80252014-02-10 19:32:05 -0800103SyncSocket::onData(const ndn::Interest& interest, Data& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800104 const OnDataValidated& onValidated,
105 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800106{
Yingdi Yu280bb962014-01-30 09:52:43 -0800107 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800108}
109
110void
Yingdi Yu51c80252014-02-10 19:32:05 -0800111SyncSocket::onDataTimeout(const ndn::Interest& interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800112 int retry,
Yingdi Yu280bb962014-01-30 09:52:43 -0800113 const OnDataValidated& onValidated,
114 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700115{
116 if(retry > 0)
117 {
Yingdi Yu51c80252014-02-10 19:32:05 -0800118 m_face->expressInterest(interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800119 bind(&SyncSocket::onData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800120 this,
121 _1,
122 _2,
Yingdi Yu280bb962014-01-30 09:52:43 -0800123 onValidated,
124 onValidationFailed),
Yingdi Yu6d638f02014-01-24 11:01:21 -0800125 bind(&SyncSocket::onDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800126 this,
127 _1,
128 retry - 1,
Yingdi Yu280bb962014-01-30 09:52:43 -0800129 onValidated,
130 onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800131
Yingdi Yu43e71612013-10-30 22:19:31 -0700132 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800133 else
Yingdi Yu6d638f02014-01-24 11:01:21 -0800134 _LOG_DEBUG("interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700135}
136
137void
Yingdi Yu280bb962014-01-30 09:52:43 -0800138SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800139{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800140 _LOG_DEBUG("data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800141}
Yingdi Yu43e71612013-10-30 22:19:31 -0700142
143
Yingdi Yu280bb962014-01-30 09:52:43 -0800144uint64_t
145SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
Yingdi Yu43e71612013-10-30 22:19:31 -0700146{
147 SequenceLog::iterator i = m_sequenceLog.find (prefix);
148
149 if (i != m_sequenceLog.end ())
150 {
151 SeqNo s = i->second;
152 if (s.getSession() == session)
153 return s.getSeq();
154 }
155 return 0;
156}
157
158}//Sync