blob: e3fbcf1fd2dd6b6f62082cb09c3752434e44e28d [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"
23
24using namespace std;
25using namespace ndn;
26
27INIT_LOGGER ("SyncSocket");
28
29namespace Sync {
30
31using ndn::shared_ptr;
Yingdi Yu40cd1c32014-04-17 15:02:17 -070032using ndn::make_shared;
akmhoque66e66182014-02-21 17:56:03 -060033
Yingdi Yu40cd1c32014-04-17 15:02:17 -070034SyncSocket::SyncSocket (const Name &syncPrefix,
akmhoque66e66182014-02-21 17:56:03 -060035 shared_ptr<Validator> validator,
36 shared_ptr<Face> face,
Yingdi Yu40cd1c32014-04-17 15:02:17 -070037 NewDataCallback dataCallback,
akmhoque66e66182014-02-21 17:56:03 -060038 RemoveCallback rmCallback )
39 : m_newDataCallback(dataCallback)
40 , m_validator(validator)
41 , m_keyChain(new KeyChain())
42 , m_face(face)
akmhoque66e66182014-02-21 17:56:03 -060043 , m_syncLogic (syncPrefix,
44 validator,
45 face,
46 bind(&SyncSocket::passCallback, this, _1),
47 rmCallback)
48{}
49
50SyncSocket::~SyncSocket()
51{
52}
53
Yingdi Yu40cd1c32014-04-17 15:02:17 -070054bool
akmhoque157b0a42014-05-13 00:26:37 -050055SyncSocket::publishData(const Name &prefix, uint64_t session,
56 const char *buf, size_t len,
57 int freshness,uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060058{
59 shared_ptr<Data> data = make_shared<Data>();
60 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
akmhoque157b0a42014-05-13 00:26:37 -050061 data->setFreshnessPeriod(ndn::time::seconds(freshness));
akmhoque66e66182014-02-21 17:56:03 -060062
akmhoque157b0a42014-05-13 00:26:37 -050063 m_face->getIoService().post(bind(&SyncSocket::publishDataInternal, this,
64 data, prefix, session,seq));
akmhoque66e66182014-02-21 17:56:03 -060065
Yingdi Yu40cd1c32014-04-17 15:02:17 -070066 return true;
akmhoque66e66182014-02-21 17:56:03 -060067}
68
69void
akmhoque157b0a42014-05-13 00:26:37 -050070SyncSocket::publishDataInternal(shared_ptr<Data> data,
71 const Name &prefix, uint64_t session, uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060072{
73 uint64_t sequence = seq;
74 Name dataName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -050075 dataName.append(boost::lexical_cast<string>(session))
76 .append(boost::lexical_cast<string>(sequence));
akmhoque66e66182014-02-21 17:56:03 -060077 data->setName(dataName);
78
Yingdi Yu40cd1c32014-04-17 15:02:17 -070079 m_keyChain->sign(*data);
akmhoque66e66182014-02-21 17:56:03 -060080 m_face->put(*data);
81
82 SeqNo s(session, sequence + 1);
83
84 m_sequenceLog[prefix] = s;
85 m_syncLogic.addLocalNames (prefix, session, sequence);
86}
87
Yingdi Yu40cd1c32014-04-17 15:02:17 -070088void
akmhoque157b0a42014-05-13 00:26:37 -050089SyncSocket::fetchData(const Name &prefix, const SeqNo &seq,
90 const OnDataValidated& onValidated, int retry)
akmhoque66e66182014-02-21 17:56:03 -060091{
92 Name interestName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -050093 interestName.append(boost::lexical_cast<string>(seq.getSession()))
94 .append(boost::lexical_cast<string>(seq.getSeq()));
akmhoque66e66182014-02-21 17:56:03 -060095
akmhoque157b0a42014-05-13 00:26:37 -050096 const OnDataValidationFailed& onValidationFailed =
97 bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu40cd1c32014-04-17 15:02:17 -070098
akmhoque66e66182014-02-21 17:56:03 -060099 ndn::Interest interest(interestName);
100 interest.setMustBeFresh(true);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700101 m_face->expressInterest(interest,
akmhoque157b0a42014-05-13 00:26:37 -0500102 bind(&SyncSocket::onData, this, _1, _2,
103 onValidated, onValidationFailed),
104 bind(&SyncSocket::onDataTimeout, this, _1, retry,
105 onValidated, onValidationFailed));
akmhoque66e66182014-02-21 17:56:03 -0600106
107}
108
109void
110SyncSocket::onData(const ndn::Interest& interest, Data& data,
111 const OnDataValidated& onValidated,
112 const OnDataValidationFailed& onValidationFailed)
113{
114 m_validator->validate(data, onValidated, onValidationFailed);
115}
116
117void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700118SyncSocket::onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600119 int retry,
120 const OnDataValidated& onValidated,
121 const OnDataValidationFailed& onValidationFailed)
122{
123 if(retry > 0)
124 {
125 m_face->expressInterest(interest,
126 bind(&SyncSocket::onData,
127 this,
128 _1,
129 _2,
130 onValidated,
131 onValidationFailed),
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700132 bind(&SyncSocket::onDataTimeout,
akmhoque66e66182014-02-21 17:56:03 -0600133 this,
134 _1,
135 retry - 1,
136 onValidated,
137 onValidationFailed));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700138
akmhoque66e66182014-02-21 17:56:03 -0600139 }
140 else
141 _LOG_DEBUG("interest eventually time out!");
142}
143
144void
145SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
146{
147 _LOG_DEBUG("data cannot be verified!");
148}
149
150
151uint64_t
152SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
153{
154 SequenceLog::iterator i = m_sequenceLog.find (prefix);
155
156 if (i != m_sequenceLog.end ())
157 {
158 SeqNo s = i->second;
159 if (s.getSession() == session)
160 return s.getSeq();
161 }
162 return 0;
163}
164
165}//Sync