blob: 8319c21d5437cf826cf1cd4a218f2d11070cb4c3 [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 *
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080018 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080020 */
21
22#include "sync-core.h"
23
Zhenkai Zhue851b952013-01-13 22:29:57 -080024const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080025const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080026const double SyncCore::RANDOM_PERCENT = 0.5;
27
Zhenkai Zhub330aed2013-01-17 13:29:37 -080028SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, const CcnxWrapperPtr &handle, const SchedulerPtr &scheduler)
29 : m_log(syncLog)
30 , m_scheduler(scheduler)
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -080031 , m_stateMsgCallback(callback)
32 , m_userName(userName)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080033 , m_localPrefix(localPrefix)
34 , m_syncPrefix(syncPrefix)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080035 , m_handle(handle)
Zhenkai Zhue573ae82013-01-15 13:15:52 -080036 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080037{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080038 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhue851b952013-01-13 22:29:57 -080039 m_syncClosure = new Closure(0, boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1));
40 m_recoverClosure = new Closure(0, boost::bind(&SyncCore::handleRecoverData, this, _1, _2), boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1));
41 m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Zhenkai Zhub330aed2013-01-17 13:29:37 -080042 m_log->initYP(m_yp);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080043 m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080044 sendSyncInterest();
45}
46
47SyncCore::~SyncCore()
48{
Zhenkai Zhue851b952013-01-13 22:29:57 -080049 if (m_syncClosure != 0)
50 {
51 delete m_syncClosure;
52 m_syncClosure = 0;
53 }
54
55 if (m_recoverClosure != 0)
56 {
57 delete m_recoverClosure;
58 m_recoverClosure = 0;
59 }
60}
61
62Name
63SyncCore::yp(const Name &deviceName)
64{
65 Name locator;
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080066 ReadLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -080067 if (m_yp.find(deviceName) != m_yp.end())
68 {
69 locator = m_yp[deviceName];
70 }
71 return locator;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080072}
73
74void
75SyncCore::updateLocalPrefix(const Name &localPrefix)
76{
77 m_localPrefix = localPrefix;
78 // optionally, we can have a sync action to announce the new prefix
79 // we are not doing this for now
80}
81
82void
Zhenkai Zhue851b952013-01-13 22:29:57 -080083SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080084{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080085 m_log->UpdateDeviceSeqNo(m_userName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -080086 // choose to update locator everytime
Zhenkai Zhub330aed2013-01-17 13:29:37 -080087 m_log->UpdateLocator(m_userName, m_localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080088 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080089 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090
Zhenkai Zhub330aed2013-01-17 13:29:37 -080091 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080092
93 // reply sync Interest with oldHash as last component
94 Name syncName = constructSyncName(oldHash);
95 Bytes syncData;
96 msgToBytes(msg, syncData);
97 m_handle->publishData(syncName, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080098 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080099
100 // 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;
101 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
102 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800103 ss << *m_rootHash;
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800104 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.1));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800105 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800106 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800107}
108
109void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800110SyncCore::handleInterest(const Name &name)
111{
112 int size = name.size();
113 int prefixSize = m_syncPrefix.size();
114 if (size == prefixSize + 1)
115 {
116 // this is normal sync interest
117 handleSyncInterest(name);
118 }
119 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
120 {
121 // this is recovery interest
122 handleRecoverInterest(name);
123 }
124}
125
126void
127SyncCore::handleRecoverInterest(const Name &name)
128{
129 Bytes hashBytes = name.getComp(name.size() - 1);
130 // this is the hash unkonwn to the sender of the interest
131 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800132 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800133 {
134 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800135 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800136 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800137 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800138 assert(msg->state_size() > 0);
139 int size = msg->state_size();
140 int index = 0;
141 cout << "Reply recover interest with: " << endl;
142 while (index < size)
143 {
144 SyncState state = msg->state(index);
145 string strName = state.name();
146 string strLoc = state.locator();
147 cout << "Name: " << Name((const unsigned char *)strName.c_str(), strName.size()) << ", Loc: " << Name((const unsigned char *)strLoc.c_str(), strLoc.size()) << ", seq: " << state.seq() << endl;
148 if (state.type() == SyncState::UPDATE)
149 {
150 cout << "Action: update" << endl;
151 }
152 else
153 {
154 cout << "Action: delete" << endl;
155 }
156 index++;
157 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800158 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800159 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800160 Bytes syncData;
161 msgToBytes(msg, syncData);
162 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800163 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800164 }
165 else
166 {
167 // we don't recognize this hash, can not help
168 }
169}
170
171void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800172SyncCore::handleSyncInterest(const Name &name)
173{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800174 Bytes hashBytes = name.getComp(name.size() - 1);
175 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
176 if (*hash == *m_rootHash)
177 {
178 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800179 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800180 return;
181 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800182 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800183 {
184 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800185 cout << "found hash in sync log" << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800186 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800187
188 Bytes syncData;
189 msgToBytes(msg, syncData);
190 m_handle->publishData(name, syncData, FRESHNESS);
191 }
192 else
193 {
194 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
195 ostringstream ss;
196 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800197 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800198 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800199 cout << "recover task scheduled after wait: " << wait << endl;
200 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800201 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800202
Zhenkai Zhue851b952013-01-13 22:29:57 -0800203 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800204}
205
206Closure::TimeoutCallbackReturnValue
207SyncCore::handleSyncInterestTimeout(const Name &name)
208{
209 // sendInterestInterest with the current root hash;
210 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800211 return Closure::RESULT_OK;
212}
213
214Closure::TimeoutCallbackReturnValue
215SyncCore::handleRecoverInterestTimeout(const Name &name)
216{
217 // We do not re-express recovery interest for now
218 // if difference is not resolved, the sync interest will trigger
219 // recovery anyway; so it's not so important to have recovery interest
220 // re-expressed
221 return Closure::RESULT_OK;
222}
223
224void
225SyncCore::handleRecoverData(const Name &name, const Bytes &content)
226{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800227 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800228 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800229 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800230}
231
232void
233SyncCore::handleSyncData(const Name &name, const Bytes &content)
234{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800235 // suppress recover in interest - data out of order case
236 handleStateData(content);
237
238 // resume outstanding sync interest
239 sendSyncInterest();
240}
241
242void
243SyncCore::handleStateData(const Bytes &content)
244{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800245 SyncStateMsgPtr msg(new SyncStateMsg);
246 bool success = msg->ParseFromArray(head(content), content.size());
247 if(!success)
248 {
249 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800250 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800251 return;
252 }
253
254 int size = msg->state_size();
255 int index = 0;
256 while (index < size)
257 {
258 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800259 string devStr = state.name();
260 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800261 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800262 if (state.type() == SyncState::UPDATE)
263 {
264 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800265 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800266 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800267 if (state.has_locator())
268 {
269 string locStr = state.locator();
270 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800271 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800272 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800273 WriteLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800274 m_yp[deviceName] = locatorName;
275 }
276 }
277 else
278 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800279 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800280 deregister(deviceName);
281 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800282 index++;
283 }
284
Zhenkai Zhue851b952013-01-13 22:29:57 -0800285 // find the actuall difference and invoke callback on the actual difference
286 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800287 m_rootHash = m_log->RememberStateInStateLog();
288 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800289
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800290 if (diff->state_size() > 0)
291 {
292 m_stateMsgCallback(diff);
293 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800294}
295
296void
297SyncCore::sendSyncInterest()
298{
299 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800300 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800301 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800302}
303
304void
305SyncCore::recover(const HashPtr &hash)
306{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800307 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800308 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800309 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800310 // unfortunately we still don't recognize this hash
311 Bytes bytes;
312 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
313 Name recoverInterest = m_syncPrefix;
314 recoverInterest.appendComp(RECOVER);
315 // append the unknown hash
316 recoverInterest.appendComp(bytes);
317 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800318 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319 }
320 else
321 {
322 // we already learned the hash; cheers!
323 }
324}
325
326void
327SyncCore::deregister(const Name &name)
328{
329 // Do nothing for now
330 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800331}
332
333Name
334SyncCore::constructSyncName(const HashPtr &hash)
335{
336 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800337 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800338 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800339 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800340 return syncName;
341}
342
343void
344SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
345{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800346 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800347 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800348 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800349}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800350
351sqlite3_int64
352SyncCore::seq(const Name &name)
353{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800354 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800355}