blob: e37a36754c0e14ea6da4cd55e5459462bdd41129 [file] [log] [blame]
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 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 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "sync-core.h"
23
Zhenkai Zhue851b952013-01-13 22:29:57 -080024const string SyncCore::RECOVER = "RECOVER";
25const double SyncCore::WAIT = 0.2;
26const double SyncCore::RANDOM_PERCENT = 0.5;
27
28SyncCore::SyncCore(const string &path, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, CcnxWrapperPtr handle, SchedulerPtr scheduler)
29 : m_log(path, userName.toString())
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080030 , m_localPrefix(localPrefix)
31 , m_syncPrefix(syncPrefix)
32 , m_stateMsgCallback(callback)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080033 , m_handle(handle)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080034 , m_scheduler(scheduler)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080035{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080036 m_rootHash = m_log.RememberStateInStateLog();
Zhenkai Zhue851b952013-01-13 22:29:57 -080037 m_syncClosure = new Closure(0, boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1));
38 m_recoverClosure = new Closure(0, boost::bind(&SyncCore::handleRecoverData, this, _1, _2), boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1));
39 m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
40 m_log.initYP(m_yp);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080041 //m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080042 sendSyncInterest();
43}
44
45SyncCore::~SyncCore()
46{
Zhenkai Zhue29616f2013-01-14 15:40:57 -080047 //m_scheduler->shutdown();
Zhenkai Zhue851b952013-01-13 22:29:57 -080048 if (m_syncClosure != 0)
49 {
50 delete m_syncClosure;
51 m_syncClosure = 0;
52 }
53
54 if (m_recoverClosure != 0)
55 {
56 delete m_recoverClosure;
57 m_recoverClosure = 0;
58 }
59}
60
61Name
62SyncCore::yp(const Name &deviceName)
63{
64 Name locator;
65 ReadLock(m_ypMutex);
66 if (m_yp.find(deviceName) != m_yp.end())
67 {
68 locator = m_yp[deviceName];
69 }
70 return locator;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080071}
72
73void
74SyncCore::updateLocalPrefix(const Name &localPrefix)
75{
76 m_localPrefix = localPrefix;
77 // optionally, we can have a sync action to announce the new prefix
78 // we are not doing this for now
79}
80
81void
Zhenkai Zhue851b952013-01-13 22:29:57 -080082SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080083{
Zhenkai Zhue851b952013-01-13 22:29:57 -080084 m_log.UpdateDeviceSeqNo(m_userName, seqno);
85 // choose to update locator everytime
86 m_log.UpdateLocator(m_userName, m_localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080087 HashPtr oldHash = m_rootHash;
88 m_rootHash = m_log.RememberStateInStateLog();
89
Zhenkai Zhue851b952013-01-13 22:29:57 -080090 SyncStateMsgPtr msg = m_log.FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080091
92 // reply sync Interest with oldHash as last component
93 Name syncName = constructSyncName(oldHash);
94 Bytes syncData;
95 msgToBytes(msg, syncData);
96 m_handle->publishData(syncName, syncData, FRESHNESS);
97
98 // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no problem, we know the new root hash already;
99 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
100 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800101 ss << *m_rootHash;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800102 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.05));
103 m_scheduler->addTask(task);
104}
105
106void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800107SyncCore::handleInterest(const Name &name)
108{
109 int size = name.size();
110 int prefixSize = m_syncPrefix.size();
111 if (size == prefixSize + 1)
112 {
113 // this is normal sync interest
114 handleSyncInterest(name);
115 }
116 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
117 {
118 // this is recovery interest
119 handleRecoverInterest(name);
120 }
121}
122
123void
124SyncCore::handleRecoverInterest(const Name &name)
125{
126 Bytes hashBytes = name.getComp(name.size() - 1);
127 // this is the hash unkonwn to the sender of the interest
128 Hash hash(head(hashBytes), hashBytes.size());
129 if (m_log.LookupSyncLog(hash) > 0)
130 {
131 // we know the hash, should reply everything
132 SyncStateMsgPtr msg = m_log.FindStateDifferences(*(Hash::Origin), *m_rootHash);
133 Bytes syncData;
134 msgToBytes(msg, syncData);
135 m_handle->publishData(name, syncData, FRESHNESS);
136 }
137 else
138 {
139 // we don't recognize this hash, can not help
140 }
141}
142
143void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800144SyncCore::handleSyncInterest(const Name &name)
145{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800146 Bytes hashBytes = name.getComp(name.size() - 1);
147 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
148 if (*hash == *m_rootHash)
149 {
150 // we have the same hash; nothing needs to be done
151 return;
152 }
153 else if (m_log.LookupSyncLog(*hash) > 0)
154 {
155 // we know something more
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800156 cout << "SyncInterest: " << name << endl;
157 cout << "hash: " << *hash << endl;
158 cout << "root hash: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800159 SyncStateMsgPtr msg = m_log.FindStateDifferences(*hash, *m_rootHash);
160
161 Bytes syncData;
162 msgToBytes(msg, syncData);
163 m_handle->publishData(name, syncData, FRESHNESS);
164 }
165 else
166 {
167 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
168 ostringstream ss;
169 ss << *hash;
170 IntervalGeneratorPtr generator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP));
171 TaskPtr task(new PeriodicTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, generator, 1));
172 m_scheduler->addTask(task);
173 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800174}
175
176Closure::TimeoutCallbackReturnValue
177SyncCore::handleSyncInterestTimeout(const Name &name)
178{
179 // sendInterestInterest with the current root hash;
180 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800181 return Closure::RESULT_OK;
182}
183
184Closure::TimeoutCallbackReturnValue
185SyncCore::handleRecoverInterestTimeout(const Name &name)
186{
187 // We do not re-express recovery interest for now
188 // if difference is not resolved, the sync interest will trigger
189 // recovery anyway; so it's not so important to have recovery interest
190 // re-expressed
191 return Closure::RESULT_OK;
192}
193
194void
195SyncCore::handleRecoverData(const Name &name, const Bytes &content)
196{
197 handleStateData(content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800198}
199
200void
201SyncCore::handleSyncData(const Name &name, const Bytes &content)
202{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800203 // suppress recover in interest - data out of order case
204 handleStateData(content);
205
206 // resume outstanding sync interest
207 sendSyncInterest();
208}
209
210void
211SyncCore::handleStateData(const Bytes &content)
212{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800213 SyncStateMsgPtr msg(new SyncStateMsg);
214 bool success = msg->ParseFromArray(head(content), content.size());
215 if(!success)
216 {
217 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800218 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800219 return;
220 }
221
222 int size = msg->state_size();
223 int index = 0;
224 while (index < size)
225 {
226 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800227 string devStr = state.name();
228 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
229 if (state.type() == SyncState::UPDATE)
230 {
231 sqlite3_int64 seqno = state.seq();
232 m_log.UpdateDeviceSeqNo(deviceName, seqno);
233 if (state.has_locator())
234 {
235 string locStr = state.locator();
236 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
237 m_log.UpdateLocator(deviceName, locatorName);
238 WriteLock(m_ypMutex);
239 m_yp[deviceName] = locatorName;
240 }
241 }
242 else
243 {
244 deregister(deviceName);
245 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800246 index++;
247 }
248
Zhenkai Zhue851b952013-01-13 22:29:57 -0800249 // find the actuall difference and invoke callback on the actual difference
250 HashPtr oldHash = m_rootHash;
251 m_rootHash = m_log.RememberStateInStateLog();
252 SyncStateMsgPtr diff = m_log.FindStateDifferences(*oldHash, *m_rootHash);
253 m_stateMsgCallback(diff);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800254}
255
256void
257SyncCore::sendSyncInterest()
258{
259 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800260 m_handle->sendInterest(syncInterest, m_syncClosure);
261}
262
263void
264SyncCore::recover(const HashPtr &hash)
265{
266 if (!(*hash == *m_rootHash) && m_log.LookupSyncLog(*hash) <= 0)
267 {
268 // unfortunately we still don't recognize this hash
269 Bytes bytes;
270 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
271 Name recoverInterest = m_syncPrefix;
272 recoverInterest.appendComp(RECOVER);
273 // append the unknown hash
274 recoverInterest.appendComp(bytes);
275 m_handle->sendInterest(recoverInterest, m_recoverClosure);
276 }
277 else
278 {
279 // we already learned the hash; cheers!
280 }
281}
282
283void
284SyncCore::deregister(const Name &name)
285{
286 // Do nothing for now
287 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800288}
289
290Name
291SyncCore::constructSyncName(const HashPtr &hash)
292{
293 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800294 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800295 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800296 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800297 return syncName;
298}
299
300void
301SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
302{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800303 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800304 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800305 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800306}