blob: 164296a042bcf7d6c0327d0001f9a80146b770bf [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 Zhu74dd53c2013-01-10 23:39:57 -0800120 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800121 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800122
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800123 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800124
125 // reply sync Interest with oldHash as last component
126 Name syncName = constructSyncName(oldHash);
127 Bytes syncData;
128 msgToBytes(msg, syncData);
129 m_handle->publishData(syncName, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800130 cout << m_userName << " publishes: " << *oldHash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800131 printMsg(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800132
133 // 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;
134 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
135 ostringstream ss;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800136 ss << *m_rootHash;
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800137 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.1));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800138 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800139 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800140}
141
142void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800143SyncCore::handleInterest(const Name &name)
144{
145 int size = name.size();
146 int prefixSize = m_syncPrefix.size();
147 if (size == prefixSize + 1)
148 {
149 // this is normal sync interest
150 handleSyncInterest(name);
151 }
152 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
153 {
154 // this is recovery interest
155 handleRecoverInterest(name);
156 }
157}
158
159void
160SyncCore::handleRecoverInterest(const Name &name)
161{
162 Bytes hashBytes = name.getComp(name.size() - 1);
163 // this is the hash unkonwn to the sender of the interest
164 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800165 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800166 {
167 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800168 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800169 // DEBUG
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800170 /*
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800171 assert(msg->state_size() > 0);
172 int size = msg->state_size();
173 int index = 0;
174 cout << "Reply recover interest with: " << endl;
175 while (index < size)
176 {
177 SyncState state = msg->state(index);
178 string strName = state.name();
179 string strLoc = state.locator();
180 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;
181 if (state.type() == SyncState::UPDATE)
182 {
183 cout << "Action: update" << endl;
184 }
185 else
186 {
187 cout << "Action: delete" << endl;
188 }
189 index++;
190 }
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800191 */
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800192 // END DEBUG
Zhenkai Zhue851b952013-01-13 22:29:57 -0800193 Bytes syncData;
194 msgToBytes(msg, syncData);
195 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800196 cout << m_userName << " publishes " << hash << endl;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800197 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800198 }
199 else
200 {
201 // we don't recognize this hash, can not help
202 }
203}
204
205void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800206SyncCore::handleSyncInterest(const Name &name)
207{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800208 Bytes hashBytes = name.getComp(name.size() - 1);
209 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
210 if (*hash == *m_rootHash)
211 {
212 // we have the same hash; nothing needs to be done
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800213 cout << "same as root hash: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800214 return;
215 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800216 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800217 {
218 // we know something more
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800219 cout << "found hash in sync log" << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800220 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800221
222 Bytes syncData;
223 msgToBytes(msg, syncData);
224 m_handle->publishData(name, syncData, FRESHNESS);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800225 cout << m_userName << " publishes: " << *hash << endl;
226 printMsg(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800227 }
228 else
229 {
230 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
231 ostringstream ss;
232 ss << *hash;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800233 double wait = m_recoverWaitGenerator->nextInterval();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800234 cout << m_userName << ", rootHash: " << *m_rootHash << ", hash: " << *hash << endl;
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800235 cout << "recover task scheduled after wait: " << wait << endl;
236 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::recover, this, hash), ss.str(), m_scheduler, wait));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800237 m_scheduler->addTask(task);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800238
Zhenkai Zhue851b952013-01-13 22:29:57 -0800239 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800240}
241
242Closure::TimeoutCallbackReturnValue
243SyncCore::handleSyncInterestTimeout(const Name &name)
244{
245 // sendInterestInterest with the current root hash;
246 sendSyncInterest();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800247 return Closure::RESULT_OK;
248}
249
250Closure::TimeoutCallbackReturnValue
251SyncCore::handleRecoverInterestTimeout(const Name &name)
252{
253 // We do not re-express recovery interest for now
254 // if difference is not resolved, the sync interest will trigger
255 // recovery anyway; so it's not so important to have recovery interest
256 // re-expressed
257 return Closure::RESULT_OK;
258}
259
260void
261SyncCore::handleRecoverData(const Name &name, const Bytes &content)
262{
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800263 //cout << "handle recover data" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800264 handleStateData(content);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800265 sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800266}
267
268void
269SyncCore::handleSyncData(const Name &name, const Bytes &content)
270{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800271 // suppress recover in interest - data out of order case
272 handleStateData(content);
273
274 // resume outstanding sync interest
275 sendSyncInterest();
276}
277
278void
279SyncCore::handleStateData(const Bytes &content)
280{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800281 SyncStateMsgPtr msg(new SyncStateMsg);
282 bool success = msg->ParseFromArray(head(content), content.size());
283 if(!success)
284 {
285 // ignore misformed SyncData
Zhenkai Zhue851b952013-01-13 22:29:57 -0800286 cerr << "Misformed SyncData"<< endl;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800287 return;
288 }
289
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800290 cout << m_userName << " receives Msg " << endl;
291 printMsg (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800292 int size = msg->state_size();
293 int index = 0;
294 while (index < size)
295 {
296 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800297 string devStr = state.name();
298 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800299 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800300 if (state.type() == SyncState::UPDATE)
301 {
302 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800303 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800304 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800305 if (state.has_locator())
306 {
307 string locStr = state.locator();
308 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800309 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800310 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800311 WriteLock lock(m_ypMutex);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800312 m_yp[deviceName] = locatorName;
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -0800313 cout << "self: " << m_userName << ", device: " << deviceName << " < == > " << locatorName << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800314 }
315 }
316 else
317 {
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800318 cout << "nani" << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319 deregister(deviceName);
320 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800321 index++;
322 }
323
Zhenkai Zhue851b952013-01-13 22:29:57 -0800324 // find the actuall difference and invoke callback on the actual difference
325 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800326 m_rootHash = m_log->RememberStateInStateLog();
327 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800328
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800329 if (diff->state_size() > 0)
330 {
331 m_stateMsgCallback(diff);
332 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800333}
334
335void
336SyncCore::sendSyncInterest()
337{
338 Name syncInterest = constructSyncName(m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800339 m_handle->sendInterest(syncInterest, m_syncClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800340 cout << m_userName << " send SYNC interest: " << *m_rootHash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800341}
342
343void
344SyncCore::recover(const HashPtr &hash)
345{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800346 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800347 {
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800348 cout << m_userName << ", Recover for: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800349 // unfortunately we still don't recognize this hash
350 Bytes bytes;
351 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
352 Name recoverInterest = m_syncPrefix;
353 recoverInterest.appendComp(RECOVER);
354 // append the unknown hash
355 recoverInterest.appendComp(bytes);
356 m_handle->sendInterest(recoverInterest, m_recoverClosure);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800357 cout << m_userName << " send RECOVER Interest: " << *hash << endl;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800358 }
359 else
360 {
361 // we already learned the hash; cheers!
362 }
363}
364
365void
366SyncCore::deregister(const Name &name)
367{
368 // Do nothing for now
369 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800370}
371
372Name
373SyncCore::constructSyncName(const HashPtr &hash)
374{
375 Bytes bytes;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800376 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes());
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800377 Name syncName = m_syncPrefix;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800378 syncName.appendComp(bytes);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800379 return syncName;
380}
381
382void
383SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
384{
Zhenkai Zhue851b952013-01-13 22:29:57 -0800385 int size = msg->ByteSize();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800386 bytes.resize(size);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800387 msg->SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800388}
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800389
390sqlite3_int64
391SyncCore::seq(const Name &name)
392{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800393 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800394}