blob: 9c5ea9fa5aacab9feb9d742c6cc8861949f6813d [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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"
22#include "sync-logging.h"
Alexander Afanasyevdb49f4e2014-08-14 23:01:02 -070023#include <boost/lexical_cast.hpp>
akmhoque66e66182014-02-21 17:56:03 -060024
25using namespace std;
Vince Lehman0a7da612014-10-29 14:39:29 -050026
27using ndn::Data;
28using ndn::Face;
29using ndn::KeyChain;
30using ndn::Name;
31using ndn::OnDataValidated;
32using ndn::OnDataValidationFailed;
33using ndn::Validator;
akmhoque66e66182014-02-21 17:56:03 -060034
35INIT_LOGGER ("SyncSocket");
36
37namespace Sync {
38
39using ndn::shared_ptr;
Yingdi Yu40cd1c32014-04-17 15:02:17 -070040using ndn::make_shared;
akmhoque66e66182014-02-21 17:56:03 -060041
Yingdi Yu40cd1c32014-04-17 15:02:17 -070042SyncSocket::SyncSocket (const Name &syncPrefix,
akmhoque66e66182014-02-21 17:56:03 -060043 shared_ptr<Validator> validator,
44 shared_ptr<Face> face,
Yingdi Yu40cd1c32014-04-17 15:02:17 -070045 NewDataCallback dataCallback,
akmhoque66e66182014-02-21 17:56:03 -060046 RemoveCallback rmCallback )
47 : m_newDataCallback(dataCallback)
48 , m_validator(validator)
49 , m_keyChain(new KeyChain())
50 , m_face(face)
akmhoque66e66182014-02-21 17:56:03 -060051 , m_syncLogic (syncPrefix,
52 validator,
53 face,
54 bind(&SyncSocket::passCallback, this, _1),
55 rmCallback)
Alexander Afanasyevdb49f4e2014-08-14 23:01:02 -070056{
57}
akmhoque66e66182014-02-21 17:56:03 -060058
59SyncSocket::~SyncSocket()
60{
61}
62
Yingdi Yu40cd1c32014-04-17 15:02:17 -070063bool
akmhoque157b0a42014-05-13 00:26:37 -050064SyncSocket::publishData(const Name &prefix, uint64_t session,
65 const char *buf, size_t len,
66 int freshness,uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060067{
68 shared_ptr<Data> data = make_shared<Data>();
69 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
akmhoque157b0a42014-05-13 00:26:37 -050070 data->setFreshnessPeriod(ndn::time::seconds(freshness));
akmhoque66e66182014-02-21 17:56:03 -060071
akmhoque157b0a42014-05-13 00:26:37 -050072 m_face->getIoService().post(bind(&SyncSocket::publishDataInternal, this,
73 data, prefix, session,seq));
akmhoque66e66182014-02-21 17:56:03 -060074
Yingdi Yu40cd1c32014-04-17 15:02:17 -070075 return true;
akmhoque66e66182014-02-21 17:56:03 -060076}
77
78void
akmhoque157b0a42014-05-13 00:26:37 -050079SyncSocket::publishDataInternal(shared_ptr<Data> data,
80 const Name &prefix, uint64_t session, uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060081{
82 uint64_t sequence = seq;
83 Name dataName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -050084 dataName.append(boost::lexical_cast<string>(session))
85 .append(boost::lexical_cast<string>(sequence));
akmhoque66e66182014-02-21 17:56:03 -060086 data->setName(dataName);
87
Yingdi Yu40cd1c32014-04-17 15:02:17 -070088 m_keyChain->sign(*data);
akmhoque66e66182014-02-21 17:56:03 -060089 m_face->put(*data);
90
91 SeqNo s(session, sequence + 1);
92
93 m_sequenceLog[prefix] = s;
94 m_syncLogic.addLocalNames (prefix, session, sequence);
95}
96
Yingdi Yu40cd1c32014-04-17 15:02:17 -070097void
akmhoque157b0a42014-05-13 00:26:37 -050098SyncSocket::fetchData(const Name &prefix, const SeqNo &seq,
99 const OnDataValidated& onValidated, int retry)
akmhoque66e66182014-02-21 17:56:03 -0600100{
101 Name interestName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -0500102 interestName.append(boost::lexical_cast<string>(seq.getSession()))
103 .append(boost::lexical_cast<string>(seq.getSeq()));
akmhoque66e66182014-02-21 17:56:03 -0600104
akmhoque157b0a42014-05-13 00:26:37 -0500105 const OnDataValidationFailed& onValidationFailed =
106 bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700107
akmhoque66e66182014-02-21 17:56:03 -0600108 ndn::Interest interest(interestName);
109 interest.setMustBeFresh(true);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700110 m_face->expressInterest(interest,
akmhoque157b0a42014-05-13 00:26:37 -0500111 bind(&SyncSocket::onData, this, _1, _2,
112 onValidated, onValidationFailed),
113 bind(&SyncSocket::onDataTimeout, this, _1, retry,
114 onValidated, onValidationFailed));
akmhoque66e66182014-02-21 17:56:03 -0600115
116}
117
118void
119SyncSocket::onData(const ndn::Interest& interest, Data& data,
120 const OnDataValidated& onValidated,
121 const OnDataValidationFailed& onValidationFailed)
122{
123 m_validator->validate(data, onValidated, onValidationFailed);
124}
125
126void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700127SyncSocket::onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600128 int retry,
129 const OnDataValidated& onValidated,
130 const OnDataValidationFailed& onValidationFailed)
131{
132 if(retry > 0)
133 {
134 m_face->expressInterest(interest,
135 bind(&SyncSocket::onData,
136 this,
137 _1,
138 _2,
139 onValidated,
140 onValidationFailed),
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700141 bind(&SyncSocket::onDataTimeout,
akmhoque66e66182014-02-21 17:56:03 -0600142 this,
143 _1,
144 retry - 1,
145 onValidated,
146 onValidationFailed));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700147
akmhoque66e66182014-02-21 17:56:03 -0600148 }
149 else
150 _LOG_DEBUG("interest eventually time out!");
151}
152
153void
154SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
155{
156 _LOG_DEBUG("data cannot be verified!");
157}
158
159
160uint64_t
161SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
162{
163 SequenceLog::iterator i = m_sequenceLog.find (prefix);
164
165 if (i != m_sequenceLog.end ())
166 {
167 SeqNo s = i->second;
168 if (s.getSession() == session)
169 return s.getSeq();
170 }
171 return 0;
172}
173
174}//Sync