blob: cb41b3060c472d4fe23b54b32828b86ff1f79c51 [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 Yu280bb962014-01-30 09:52:43 -080033SyncSocket::SyncSocket (const Name &syncPrefix,
34 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080035 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070036 NewDataCallback dataCallback,
37 RemoveCallback rmCallback )
38 : m_newDataCallback(dataCallback)
Yingdi Yu280bb962014-01-30 09:52:43 -080039 , m_validator(validator)
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 Yu280bb962014-01-30 09:52:43 -080043 validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080044 face,
Yingdi Yu43e71612013-10-30 22:19:31 -070045 bind(&SyncSocket::passCallback, this, _1),
46 rmCallback)
Yingdi Yu280bb962014-01-30 09:52:43 -080047{}
Yingdi Yu43e71612013-10-30 22:19:31 -070048
49SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080050{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080051}
52
Yingdi Yu43e71612013-10-30 22:19:31 -070053bool
Yingdi Yu280bb962014-01-30 09:52:43 -080054SyncSocket::publishData(const Name &prefix, uint64_t session, const char *buf, size_t len, int freshness)
Yingdi Yu43e71612013-10-30 22:19:31 -070055{
Yingdi Yu280bb962014-01-30 09:52:43 -080056 uint64_t sequence = getNextSeq(prefix, session);
Yingdi Yu43e71612013-10-30 22:19:31 -070057
Yingdi Yu6d638f02014-01-24 11:01:21 -080058 Name dataName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080059 dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080060
Yingdi Yu6d638f02014-01-24 11:01:21 -080061 Data data(dataName);
62 data.setContent(reinterpret_cast<const uint8_t*>(buf), len);
Alexander Afanasyev531803b2014-02-05 15:57:35 -080063 data.setFreshnessPeriod(1000*freshness);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080064
Yingdi Yu280bb962014-01-30 09:52:43 -080065 m_keyChain->sign(data);
Yingdi Yu43e71612013-10-30 22:19:31 -070066
Yingdi Yu6d638f02014-01-24 11:01:21 -080067 m_face->put(data);
Yingdi Yu43e71612013-10-30 22:19:31 -070068
69 SeqNo s(session, sequence + 1);
70 m_sequenceLog[prefix] = s;
71 m_syncLogic.addLocalNames (prefix, session, sequence);
72 return true;
73}
74
75void
Yingdi Yu280bb962014-01-30 09:52:43 -080076SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& onValidated, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -070077{
Yingdi Yu6d638f02014-01-24 11:01:21 -080078 Name interestName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080079 interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
Yingdi Yu6d638f02014-01-24 11:01:21 -080080
Yingdi Yu280bb962014-01-30 09:52:43 -080081 const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -070082
Yingdi Yu6d638f02014-01-24 11:01:21 -080083 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -080084 interest.setMustBeFresh(true);
Yingdi Yu6d638f02014-01-24 11:01:21 -080085 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -080086 bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed),
87 bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080088
Yingdi Yu43e71612013-10-30 22:19:31 -070089}
90
91void
Yingdi Yu6d638f02014-01-24 11:01:21 -080092SyncSocket::onData(const shared_ptr<const ndn::Interest>& interest,
93 const shared_ptr<Data>& data,
Yingdi Yu280bb962014-01-30 09:52:43 -080094 const OnDataValidated& onValidated,
95 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080096{
Yingdi Yu280bb962014-01-30 09:52:43 -080097 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080098}
99
100void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800101SyncSocket::onDataTimeout(const shared_ptr<const ndn::Interest>& interest,
102 int retry,
Yingdi Yu280bb962014-01-30 09:52:43 -0800103 const OnDataValidated& onValidated,
104 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700105{
106 if(retry > 0)
107 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800108 m_face->expressInterest(*interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800109 bind(&SyncSocket::onData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800110 this,
111 _1,
112 _2,
Yingdi Yu280bb962014-01-30 09:52:43 -0800113 onValidated,
114 onValidationFailed),
Yingdi Yu6d638f02014-01-24 11:01:21 -0800115 bind(&SyncSocket::onDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800116 this,
117 _1,
118 retry - 1,
Yingdi Yu280bb962014-01-30 09:52:43 -0800119 onValidated,
120 onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800121
Yingdi Yu43e71612013-10-30 22:19:31 -0700122 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800123 else
Yingdi Yu6d638f02014-01-24 11:01:21 -0800124 _LOG_DEBUG("interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700125}
126
127void
Yingdi Yu280bb962014-01-30 09:52:43 -0800128SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800129{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800130 _LOG_DEBUG("data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800131}
Yingdi Yu43e71612013-10-30 22:19:31 -0700132
133
Yingdi Yu280bb962014-01-30 09:52:43 -0800134uint64_t
135SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
Yingdi Yu43e71612013-10-30 22:19:31 -0700136{
137 SequenceLog::iterator i = m_sequenceLog.find (prefix);
138
139 if (i != m_sequenceLog.end ())
140 {
141 SeqNo s = i->second;
142 if (s.getSession() == session)
143 return s.getSeq();
144 }
145 return 0;
146}
147
148}//Sync