blob: a0d7573570232dcccb7b57651a2f9c5112d90254 [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 Zhu4e1c1d92013-01-17 14:12:46 -080028// for debugging
29static void
30printMsg(SyncStateMsgPtr &msg)
31{
32 cout << " ===== start Msg ======" << endl;
33 int size = msg->state_size();
34 if (size > 0)
35 {
36 int index = 0;
37 while (index < size)
38 {
39 SyncState state = msg->state(index);
40 string strName = state.name();
41 string strLocator = state.locator();
42 sqlite3_int64 seq = state.seq();
43 cout << "Name: " << Name((const unsigned char *)strName.c_str(), strName.size());
44 cout << ", Locator: " << Name((const unsigned char *)strLocator.c_str(), strLocator.size());
45 cout << ", seq: " << seq << endl;
46 index ++;
47 }
48 }
49 else
50 {
51 cout << "Msg size 0" << endl;
52 }
53 cout << " ++++++++ end Msg ++++++++ \n\n" << endl;
54}
55
Zhenkai Zhub330aed2013-01-17 13:29:37 -080056SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, const CcnxWrapperPtr &handle, const SchedulerPtr &scheduler)
57 : m_log(syncLog)
58 , m_scheduler(scheduler)
Alexander Afanasyeva44a7a22013-01-14 17:37:06 -080059 , m_stateMsgCallback(callback)
60 , m_userName(userName)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080061 , m_localPrefix(localPrefix)
62 , m_syncPrefix(syncPrefix)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080063 , m_handle(handle)
Zhenkai Zhue573ae82013-01-15 13:15:52 -080064 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080065{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080066 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhue851b952013-01-13 22:29:57 -080067 m_syncClosure = new Closure(0, boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1));
68 m_recoverClosure = new Closure(0, boost::bind(&SyncCore::handleRecoverData, this, _1, _2), boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1));
69 m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Zhenkai Zhub330aed2013-01-17 13:29:37 -080070 m_log->initYP(m_yp);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080071 m_scheduler->start();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080072 sendSyncInterest();
73}
74
75SyncCore::~SyncCore()
76{
Zhenkai Zhue851b952013-01-13 22:29:57 -080077 if (m_syncClosure != 0)
78 {
79 delete m_syncClosure;
80 m_syncClosure = 0;
81 }
82
83 if (m_recoverClosure != 0)
84 {
85 delete m_recoverClosure;
86 m_recoverClosure = 0;
87 }
88}
89
90Name
91SyncCore::yp(const Name &deviceName)
92{
93 Name locator;
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080094 ReadLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -080095 if (m_yp.find(deviceName) != m_yp.end())
96 {
97 locator = m_yp[deviceName];
98 }
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080099 else
100 {
101 cout << "self: " << m_userName << ", deviceName: " << deviceName << " not found in yp " << endl;
102 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800103 return locator;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800104}
105
106void
107SyncCore::updateLocalPrefix(const Name &localPrefix)
108{
109 m_localPrefix = localPrefix;
110 // optionally, we can have a sync action to announce the new prefix
111 // we are not doing this for now
112}
113
114void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800115SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800116{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800117 m_log->UpdateDeviceSeqNo(m_userName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800118 // choose to update locator everytime
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800119 m_log->UpdateLocator(m_userName, m_localPrefix);
Zhenkai Zhu9f6c41e2013-01-17 14:18:14 -0800120 {
121 WriteLock lock(m_ypMutex);
122 m_yp[m_userName] = m_localPrefix;
123 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800124 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800125 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800126
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800127 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800128
129 // reply sync Interest with oldHash as last component
130 Name syncName = constructSyncName(oldHash);
131 Bytes syncData;
132 msgToBytes(msg, syncData);
133 m_handle->publishData(syncName, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800134 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800135 printMsg(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800136
137 // 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;
138 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
139 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800140 ss << *m_rootHash;
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800141 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.1));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800142 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800143 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800144}
145
146void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800147SyncCore::handleInterest(const Name &name)
148{
149 int size = name.size();
150 int prefixSize = m_syncPrefix.size();
151 if (size == prefixSize + 1)
152 {
153 // this is normal sync interest
154 handleSyncInterest(name);
155 }
156 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
157 {
158 // this is recovery interest
159 handleRecoverInterest(name);
160 }
161}
162
163void
164SyncCore::handleRecoverInterest(const Name &name)
165{
166 Bytes hashBytes = name.getComp(name.size() - 1);
167 // this is the hash unkonwn to the sender of the interest
168 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800169 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800170 {
171 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800172 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800173 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800174 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800175 assert(msg->state_size() > 0);
176 int size = msg->state_size();
177 int index = 0;
178 cout << "Reply recover interest with: " << endl;
179 while (index < size)
180 {
181 SyncState state = msg->state(index);
182 string strName = state.name();
183 string strLoc = state.locator();
184 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;
185 if (state.type() == SyncState::UPDATE)
186 {
187 cout << "Action: update" << endl;
188 }
189 else
190 {
191 cout << "Action: delete" << endl;
192 }
193 index++;
194 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800195 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800196 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800197 Bytes syncData;
198 msgToBytes(msg, syncData);
199 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800200 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800201 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800202 }
203 else
204 {
205 // we don't recognize this hash, can not help
206 }
207}
208
209void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800210SyncCore::handleSyncInterest(const Name &name)
211{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800212 Bytes hashBytes = name.getComp(name.size() - 1);
213 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
214 if (*hash == *m_rootHash)
215 {
216 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800217 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800218 return;
219 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800220 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800221 {
222 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800223 cout << "found hash in sync log" << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800224 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800225
226 Bytes syncData;
227 msgToBytes(msg, syncData);
228 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800229 cout << m_userName << " publishes: " << *hash << endl;
230 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800231 }
232 else
233 {
234 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
235 ostringstream ss;
236 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800237 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800238 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800239 cout << "recover task scheduled after wait: " << wait << endl;
240 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800241 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800242
Zhenkai Zhue851b952013-01-13 22:29:57 -0800243 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800244}
245
246Closure::TimeoutCallbackReturnValue
247SyncCore::handleSyncInterestTimeout(const Name &name)
248{
249 // sendInterestInterest with the current root hash;
250 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800251 return Closure::RESULT_OK;
252}
253
254Closure::TimeoutCallbackReturnValue
255SyncCore::handleRecoverInterestTimeout(const Name &name)
256{
257 // We do not re-express recovery interest for now
258 // if difference is not resolved, the sync interest will trigger
259 // recovery anyway; so it's not so important to have recovery interest
260 // re-expressed
261 return Closure::RESULT_OK;
262}
263
264void
265SyncCore::handleRecoverData(const Name &name, const Bytes &content)
266{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800267 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800268 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800269 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800270}
271
272void
273SyncCore::handleSyncData(const Name &name, const Bytes &content)
274{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800275 // suppress recover in interest - data out of order case
276 handleStateData(content);
277
278 // resume outstanding sync interest
279 sendSyncInterest();
280}
281
282void
283SyncCore::handleStateData(const Bytes &content)
284{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800285 SyncStateMsgPtr msg(new SyncStateMsg);
286 bool success = msg->ParseFromArray(head(content), content.size());
287 if(!success)
288 {
289 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800290 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800291 return;
292 }
293
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800294 cout << m_userName << " receives Msg " << endl;
295 printMsg (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800296 int size = msg->state_size();
297 int index = 0;
298 while (index < size)
299 {
300 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800301 string devStr = state.name();
302 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800303 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800304 if (state.type() == SyncState::UPDATE)
305 {
306 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800307 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800308 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800309 if (state.has_locator())
310 {
311 string locStr = state.locator();
312 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800313 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800314 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800315 WriteLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800316 m_yp[deviceName] = locatorName;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800317 cout << "self: " << m_userName << ", device: " << deviceName << " < == > " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800318 }
319 }
320 else
321 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800322 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800323 deregister(deviceName);
324 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800325 index++;
326 }
327
Zhenkai Zhue851b952013-01-13 22:29:57 -0800328 // find the actuall difference and invoke callback on the actual difference
329 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800330 m_rootHash = m_log->RememberStateInStateLog();
331 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800332
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800333 if (diff->state_size() > 0)
334 {
335 m_stateMsgCallback(diff);
336 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800337}
338
339void
340SyncCore::sendSyncInterest()
341{
342 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800343 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800344 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800345}
346
347void
348SyncCore::recover(const HashPtr &hash)
349{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800350 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800351 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800352 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800353 // unfortunately we still don't recognize this hash
354 Bytes bytes;
355 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
356 Name recoverInterest = m_syncPrefix;
357 recoverInterest.appendComp(RECOVER);
358 // append the unknown hash
359 recoverInterest.appendComp(bytes);
360 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800361 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800362 }
363 else
364 {
365 // we already learned the hash; cheers!
366 }
367}
368
369void
370SyncCore::deregister(const Name &name)
371{
372 // Do nothing for now
373 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800374}
375
376Name
377SyncCore::constructSyncName(const HashPtr &hash)
378{
379 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800380 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800381 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800382 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800383 return syncName;
384}
385
386void
387SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
388{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800389 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800390 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800391 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800392}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800393
394sqlite3_int64
395SyncCore::seq(const Name &name)
396{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800397 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800398}