blob: f5903380f0ee9b95e92ba505769cd280cf3cb89f [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 Yu51c80252014-02-10 19:32:05 -080042 , m_ioService(face->ioService())
Yingdi Yu43e71612013-10-30 22:19:31 -070043 , m_syncLogic (syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080044 validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080045 face,
Yingdi Yu43e71612013-10-30 22:19:31 -070046 bind(&SyncSocket::passCallback, this, _1),
47 rmCallback)
Yingdi Yu280bb962014-01-30 09:52:43 -080048{}
Yingdi Yu43e71612013-10-30 22:19:31 -070049
50SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080051{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080052}
53
Yingdi Yu43e71612013-10-30 22:19:31 -070054bool
Yingdi Yu280bb962014-01-30 09:52:43 -080055SyncSocket::publishData(const Name &prefix, uint64_t session, const char *buf, size_t len, int freshness)
Yingdi Yu43e71612013-10-30 22:19:31 -070056{
Yingdi Yu51c80252014-02-10 19:32:05 -080057 shared_ptr<Data> data = make_shared<Data>();
58 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
59 data->setFreshnessPeriod(1000*freshness);
60
61 m_ioService->post(bind(&SyncSocket::publishDataInternal, this, data, prefix, session));
62
63 return true;
64}
65
66void
67SyncSocket::publishDataInternal(shared_ptr<Data> data, const Name &prefix, uint64_t session)
68{
Yingdi Yu280bb962014-01-30 09:52:43 -080069 uint64_t sequence = getNextSeq(prefix, session);
Yingdi Yu6d638f02014-01-24 11:01:21 -080070 Name dataName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080071 dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
Yingdi Yu51c80252014-02-10 19:32:05 -080072 data->setName(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080073
Yingdi Yu51c80252014-02-10 19:32:05 -080074 m_keyChain->sign(*data);
75 m_face->put(*data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080076
Yingdi Yu43e71612013-10-30 22:19:31 -070077 SeqNo s(session, sequence + 1);
Yingdi Yu51c80252014-02-10 19:32:05 -080078
Yingdi Yu43e71612013-10-30 22:19:31 -070079 m_sequenceLog[prefix] = s;
80 m_syncLogic.addLocalNames (prefix, session, sequence);
Yingdi Yu43e71612013-10-30 22:19:31 -070081}
82
83void
Yingdi Yu280bb962014-01-30 09:52:43 -080084SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& onValidated, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -070085{
Yingdi Yu6d638f02014-01-24 11:01:21 -080086 Name interestName = prefix;
Yingdi Yu280bb962014-01-30 09:52:43 -080087 interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
Yingdi Yu6d638f02014-01-24 11:01:21 -080088
Yingdi Yu280bb962014-01-30 09:52:43 -080089 const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -070090
Yingdi Yu6d638f02014-01-24 11:01:21 -080091 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -080092 interest.setMustBeFresh(true);
Yingdi Yu6d638f02014-01-24 11:01:21 -080093 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -080094 bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed),
95 bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080096
Yingdi Yu43e71612013-10-30 22:19:31 -070097}
98
99void
Yingdi Yu51c80252014-02-10 19:32:05 -0800100SyncSocket::onData(const ndn::Interest& interest, Data& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800101 const OnDataValidated& onValidated,
102 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800103{
Yingdi Yu280bb962014-01-30 09:52:43 -0800104 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800105}
106
107void
Yingdi Yu51c80252014-02-10 19:32:05 -0800108SyncSocket::onDataTimeout(const ndn::Interest& interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800109 int retry,
Yingdi Yu280bb962014-01-30 09:52:43 -0800110 const OnDataValidated& onValidated,
111 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700112{
113 if(retry > 0)
114 {
Yingdi Yu51c80252014-02-10 19:32:05 -0800115 m_face->expressInterest(interest,
Yingdi Yu6d638f02014-01-24 11:01:21 -0800116 bind(&SyncSocket::onData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800117 this,
118 _1,
119 _2,
Yingdi Yu280bb962014-01-30 09:52:43 -0800120 onValidated,
121 onValidationFailed),
Yingdi Yu6d638f02014-01-24 11:01:21 -0800122 bind(&SyncSocket::onDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800123 this,
124 _1,
125 retry - 1,
Yingdi Yu280bb962014-01-30 09:52:43 -0800126 onValidated,
127 onValidationFailed));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800128
Yingdi Yu43e71612013-10-30 22:19:31 -0700129 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800130 else
Yingdi Yu6d638f02014-01-24 11:01:21 -0800131 _LOG_DEBUG("interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700132}
133
134void
Yingdi Yu280bb962014-01-30 09:52:43 -0800135SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800136{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800137 _LOG_DEBUG("data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800138}
Yingdi Yu43e71612013-10-30 22:19:31 -0700139
140
Yingdi Yu280bb962014-01-30 09:52:43 -0800141uint64_t
142SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
Yingdi Yu43e71612013-10-30 22:19:31 -0700143{
144 SequenceLog::iterator i = m_sequenceLog.find (prefix);
145
146 if (i != m_sequenceLog.end ())
147 {
148 SeqNo s = i->second;
149 if (s.getSession() == session)
150 return s.getSeq();
151 }
152 return 0;
153}
154
155}//Sync