blob: 259e7304216b54059d537cf22b4de5ea62429d10 [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
Yingdi Yu280bb962014-01-30 09:52:43 -080031SyncSocket::SyncSocket (const Name &syncPrefix,
32 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080033 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070034 NewDataCallback dataCallback,
35 RemoveCallback rmCallback )
36 : m_newDataCallback(dataCallback)
Yingdi Yu280bb962014-01-30 09:52:43 -080037 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080038 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +080039 , m_face(face)
Yingdi Yu43e71612013-10-30 22:19:31 -070040 , m_syncLogic (syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080041 validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080042 face,
Yingdi Yu43e71612013-10-30 22:19:31 -070043 bind(&SyncSocket::passCallback, this, _1),
44 rmCallback)
Yingdi Yu280bb962014-01-30 09:52:43 -080045{}
Yingdi Yu43e71612013-10-30 22:19:31 -070046
47SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080048{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080049}
50
Yingdi Yu43e71612013-10-30 22:19:31 -070051bool
Yingdi Yu280bb962014-01-30 09:52:43 -080052SyncSocket::publishData(const Name &prefix, uint64_t session, const char *buf, size_t len, int freshness)
Yingdi Yu43e71612013-10-30 22:19:31 -070053{
Yingdi Yu280bb962014-01-30 09:52:43 -080054 uint64_t sequence = getNextSeq(prefix, session);
Yingdi Yu43e71612013-10-30 22:19:31 -070055
Yingdi Yu6d638f02014-01-24 11:01:21 -080056 Name dataName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080057 dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080058
Yingdi Yu6d638f02014-01-24 11:01:21 -080059 Data data(dataName);
60 data.setContent(reinterpret_cast<const uint8_t*>(buf), len);
Yingdi Yu280bb962014-01-30 09:52:43 -080061 data.setFreshnessPeriod(freshness);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080062
Yingdi Yu280bb962014-01-30 09:52:43 -080063 m_keyChain->sign(data);
Yingdi Yu43e71612013-10-30 22:19:31 -070064
Yingdi Yu6d638f02014-01-24 11:01:21 -080065 m_face->put(data);
Yingdi Yu43e71612013-10-30 22:19:31 -070066
67 SeqNo s(session, sequence + 1);
68 m_sequenceLog[prefix] = s;
69 m_syncLogic.addLocalNames (prefix, session, sequence);
70 return true;
71}
72
73void
Yingdi Yu280bb962014-01-30 09:52:43 -080074SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& onValidated, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -070075{
Yingdi Yu6d638f02014-01-24 11:01:21 -080076 Name interestName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080077 interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
Yingdi Yu6d638f02014-01-24 11:01:21 -080078
Yingdi Yu280bb962014-01-30 09:52:43 -080079 const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -070080
Yingdi Yu6d638f02014-01-24 11:01:21 -080081 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -080082 interest.setMustBeFresh(true);
Yingdi Yu6d638f02014-01-24 11:01:21 -080083 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -080084 bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed),
85 bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080086
Yingdi Yu43e71612013-10-30 22:19:31 -070087}
88
89void
Yingdi Yu6d638f02014-01-24 11:01:21 -080090SyncSocket::onData(const shared_ptr<const ndn::Interest>& interest,
91 const shared_ptr<Data>& data,
Yingdi Yu280bb962014-01-30 09:52:43 -080092 const OnDataValidated& onValidated,
93 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080094{
Yingdi Yu280bb962014-01-30 09:52:43 -080095 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080096}
97
98void
Yingdi Yu6d638f02014-01-24 11:01:21 -080099SyncSocket::onDataTimeout(const shared_ptr<const ndn::Interest>& interest,
100 int retry,
Yingdi Yu280bb962014-01-30 09:52:43 -0800101 const OnDataValidated& onValidated,
102 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700103{
104 if(retry > 0)
105 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800106 m_face->expressInterest(*interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800107 bind(&SyncSocket::onData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800108 this,
109 _1,
110 _2,
Yingdi Yu280bb962014-01-30 09:52:43 -0800111 onValidated,
112 onValidationFailed),
Yingdi Yu6d638f02014-01-24 11:01:21 -0800113 bind(&SyncSocket::onDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800114 this,
115 _1,
116 retry - 1,
Yingdi Yu280bb962014-01-30 09:52:43 -0800117 onValidated,
118 onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800119
Yingdi Yu43e71612013-10-30 22:19:31 -0700120 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800121 else
Yingdi Yu6d638f02014-01-24 11:01:21 -0800122 _LOG_DEBUG("interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700123}
124
125void
Yingdi Yu280bb962014-01-30 09:52:43 -0800126SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800127{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800128 _LOG_DEBUG("data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800129}
Yingdi Yu43e71612013-10-30 22:19:31 -0700130
131
Yingdi Yu280bb962014-01-30 09:52:43 -0800132uint64_t
133SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
Yingdi Yu43e71612013-10-30 22:19:31 -0700134{
135 SequenceLog::iterator i = m_sequenceLog.find (prefix);
136
137 if (i != m_sequenceLog.end ())
138 {
139 SeqNo s = i->second;
140 if (s.getSession() == session)
141 return s.getSeq();
142 }
143 return 0;
144}
145
146}//Sync