blob: 834dbee706c72e5792a17c74467d9d38c39aa1f9 [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;
26using namespace ndn;
27
28INIT_LOGGER ("SyncSocket");
29
30namespace Sync {
31
32using ndn::shared_ptr;
Yingdi Yu40cd1c32014-04-17 15:02:17 -070033using ndn::make_shared;
akmhoque66e66182014-02-21 17:56:03 -060034
Yingdi Yu40cd1c32014-04-17 15:02:17 -070035SyncSocket::SyncSocket (const Name &syncPrefix,
akmhoque66e66182014-02-21 17:56:03 -060036 shared_ptr<Validator> validator,
37 shared_ptr<Face> face,
Yingdi Yu40cd1c32014-04-17 15:02:17 -070038 NewDataCallback dataCallback,
akmhoque66e66182014-02-21 17:56:03 -060039 RemoveCallback rmCallback )
40 : m_newDataCallback(dataCallback)
41 , m_validator(validator)
42 , m_keyChain(new KeyChain())
43 , m_face(face)
akmhoque66e66182014-02-21 17:56:03 -060044 , m_syncLogic (syncPrefix,
45 validator,
46 face,
47 bind(&SyncSocket::passCallback, this, _1),
48 rmCallback)
Alexander Afanasyevdb49f4e2014-08-14 23:01:02 -070049{
50}
akmhoque66e66182014-02-21 17:56:03 -060051
52SyncSocket::~SyncSocket()
53{
54}
55
Yingdi Yu40cd1c32014-04-17 15:02:17 -070056bool
akmhoque157b0a42014-05-13 00:26:37 -050057SyncSocket::publishData(const Name &prefix, uint64_t session,
58 const char *buf, size_t len,
59 int freshness,uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060060{
61 shared_ptr<Data> data = make_shared<Data>();
62 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
akmhoque157b0a42014-05-13 00:26:37 -050063 data->setFreshnessPeriod(ndn::time::seconds(freshness));
akmhoque66e66182014-02-21 17:56:03 -060064
akmhoque157b0a42014-05-13 00:26:37 -050065 m_face->getIoService().post(bind(&SyncSocket::publishDataInternal, this,
66 data, prefix, session,seq));
akmhoque66e66182014-02-21 17:56:03 -060067
Yingdi Yu40cd1c32014-04-17 15:02:17 -070068 return true;
akmhoque66e66182014-02-21 17:56:03 -060069}
70
71void
akmhoque157b0a42014-05-13 00:26:37 -050072SyncSocket::publishDataInternal(shared_ptr<Data> data,
73 const Name &prefix, uint64_t session, uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060074{
75 uint64_t sequence = seq;
76 Name dataName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -050077 dataName.append(boost::lexical_cast<string>(session))
78 .append(boost::lexical_cast<string>(sequence));
akmhoque66e66182014-02-21 17:56:03 -060079 data->setName(dataName);
80
Yingdi Yu40cd1c32014-04-17 15:02:17 -070081 m_keyChain->sign(*data);
akmhoque66e66182014-02-21 17:56:03 -060082 m_face->put(*data);
83
84 SeqNo s(session, sequence + 1);
85
86 m_sequenceLog[prefix] = s;
87 m_syncLogic.addLocalNames (prefix, session, sequence);
88}
89
Yingdi Yu40cd1c32014-04-17 15:02:17 -070090void
akmhoque157b0a42014-05-13 00:26:37 -050091SyncSocket::fetchData(const Name &prefix, const SeqNo &seq,
92 const OnDataValidated& onValidated, int retry)
akmhoque66e66182014-02-21 17:56:03 -060093{
94 Name interestName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -050095 interestName.append(boost::lexical_cast<string>(seq.getSession()))
96 .append(boost::lexical_cast<string>(seq.getSeq()));
akmhoque66e66182014-02-21 17:56:03 -060097
akmhoque157b0a42014-05-13 00:26:37 -050098 const OnDataValidationFailed& onValidationFailed =
99 bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700100
akmhoque66e66182014-02-21 17:56:03 -0600101 ndn::Interest interest(interestName);
102 interest.setMustBeFresh(true);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700103 m_face->expressInterest(interest,
akmhoque157b0a42014-05-13 00:26:37 -0500104 bind(&SyncSocket::onData, this, _1, _2,
105 onValidated, onValidationFailed),
106 bind(&SyncSocket::onDataTimeout, this, _1, retry,
107 onValidated, onValidationFailed));
akmhoque66e66182014-02-21 17:56:03 -0600108
109}
110
111void
112SyncSocket::onData(const ndn::Interest& interest, Data& data,
113 const OnDataValidated& onValidated,
114 const OnDataValidationFailed& onValidationFailed)
115{
116 m_validator->validate(data, onValidated, onValidationFailed);
117}
118
119void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700120SyncSocket::onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600121 int retry,
122 const OnDataValidated& onValidated,
123 const OnDataValidationFailed& onValidationFailed)
124{
125 if(retry > 0)
126 {
127 m_face->expressInterest(interest,
128 bind(&SyncSocket::onData,
129 this,
130 _1,
131 _2,
132 onValidated,
133 onValidationFailed),
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700134 bind(&SyncSocket::onDataTimeout,
akmhoque66e66182014-02-21 17:56:03 -0600135 this,
136 _1,
137 retry - 1,
138 onValidated,
139 onValidationFailed));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700140
akmhoque66e66182014-02-21 17:56:03 -0600141 }
142 else
143 _LOG_DEBUG("interest eventually time out!");
144}
145
146void
147SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
148{
149 _LOG_DEBUG("data cannot be verified!");
150}
151
152
153uint64_t
154SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
155{
156 SequenceLog::iterator i = m_sequenceLog.find (prefix);
157
158 if (i != m_sequenceLog.end ())
159 {
160 SeqNo s = i->second;
161 if (s.getSession() == session)
162 return s.getSeq();
163 }
164 return 0;
165}
166
167}//Sync