blob: 32a1243e99623c8ff4b5aa9ca43e914d751846c4 [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 Zhu74dd53c2013-01-10 23:39:57 -080041 m_scheduler->start();
42 sendSyncInterest();
43}
44
45SyncCore::~SyncCore()
46{
Zhenkai Zhue851b952013-01-13 22:29:57 -080047 m_scheduler->shutdown();
48 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
156 SyncStateMsgPtr msg = m_log.FindStateDifferences(*hash, *m_rootHash);
157
158 Bytes syncData;
159 msgToBytes(msg, syncData);
160 m_handle->publishData(name, syncData, FRESHNESS);
161 }
162 else
163 {
164 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
165 ostringstream ss;
166 ss << *hash;
167 IntervalGeneratorPtr generator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP));
168 TaskPtr task(new PeriodicTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, generator, 1));
169 m_scheduler->addTask(task);
170 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800171}
172
173Closure::TimeoutCallbackReturnValue
174SyncCore::handleSyncInterestTimeout(const Name &name)
175{
176 // sendInterestInterest with the current root hash;
177 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800178 return Closure::RESULT_OK;
179}
180
181Closure::TimeoutCallbackReturnValue
182SyncCore::handleRecoverInterestTimeout(const Name &name)
183{
184 // We do not re-express recovery interest for now
185 // if difference is not resolved, the sync interest will trigger
186 // recovery anyway; so it's not so important to have recovery interest
187 // re-expressed
188 return Closure::RESULT_OK;
189}
190
191void
192SyncCore::handleRecoverData(const Name &name, const Bytes &content)
193{
194 handleStateData(content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800195}
196
197void
198SyncCore::handleSyncData(const Name &name, const Bytes &content)
199{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800200 // suppress recover in interest - data out of order case
201 handleStateData(content);
202
203 // resume outstanding sync interest
204 sendSyncInterest();
205}
206
207void
208SyncCore::handleStateData(const Bytes &content)
209{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800210 SyncStateMsgPtr msg(new SyncStateMsg);
211 bool success = msg->ParseFromArray(head(content), content.size());
212 if(!success)
213 {
214 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800215 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800216 return;
217 }
218
219 int size = msg->state_size();
220 int index = 0;
221 while (index < size)
222 {
223 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800224 string devStr = state.name();
225 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
226 if (state.type() == SyncState::UPDATE)
227 {
228 sqlite3_int64 seqno = state.seq();
229 m_log.UpdateDeviceSeqNo(deviceName, seqno);
230 if (state.has_locator())
231 {
232 string locStr = state.locator();
233 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
234 m_log.UpdateLocator(deviceName, locatorName);
235 WriteLock(m_ypMutex);
236 m_yp[deviceName] = locatorName;
237 }
238 }
239 else
240 {
241 deregister(deviceName);
242 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800243 index++;
244 }
245
Zhenkai Zhue851b952013-01-13 22:29:57 -0800246 // find the actuall difference and invoke callback on the actual difference
247 HashPtr oldHash = m_rootHash;
248 m_rootHash = m_log.RememberStateInStateLog();
249 SyncStateMsgPtr diff = m_log.FindStateDifferences(*oldHash, *m_rootHash);
250 m_stateMsgCallback(diff);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800251}
252
253void
254SyncCore::sendSyncInterest()
255{
256 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800257 m_handle->sendInterest(syncInterest, m_syncClosure);
258}
259
260void
261SyncCore::recover(const HashPtr &hash)
262{
263 if (!(*hash == *m_rootHash) && m_log.LookupSyncLog(*hash) <= 0)
264 {
265 // unfortunately we still don't recognize this hash
266 Bytes bytes;
267 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
268 Name recoverInterest = m_syncPrefix;
269 recoverInterest.appendComp(RECOVER);
270 // append the unknown hash
271 recoverInterest.appendComp(bytes);
272 m_handle->sendInterest(recoverInterest, m_recoverClosure);
273 }
274 else
275 {
276 // we already learned the hash; cheers!
277 }
278}
279
280void
281SyncCore::deregister(const Name &name)
282{
283 // Do nothing for now
284 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800285}
286
287Name
288SyncCore::constructSyncName(const HashPtr &hash)
289{
290 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800291 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800292 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800293 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800294 return syncName;
295}
296
297void
298SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
299{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800300 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800301 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800302 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800303}