blob: 8e781f15ddd0098550b155307fe6d2475a712eb3 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeva9369b42017-01-11 11:58:00 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "sync-core.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080022#include "sync-state-helper.hpp"
Lijing Wange84adea2015-05-31 16:25:16 -070023#include "core/logging.hpp"
24
25#include <ndn-cxx/util/string-helper.hpp>
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080026
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080027#include <boost/lexical_cast.hpp>
Lijing Wange84adea2015-05-31 16:25:16 -070028
29namespace ndn {
30namespace chronoshare {
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080031
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080032_LOG_INIT(Sync.Core);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080033
Lijing Wange84adea2015-05-31 16:25:16 -070034const int SyncCore::FRESHNESS = 2;
35const std::string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080036const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080037const double SyncCore::RANDOM_PERCENT = 0.5;
38
Lijing Wange84adea2015-05-31 16:25:16 -070039SyncCore::SyncCore(Face& face, SyncLogPtr syncLog, const Name& userName, const Name& localPrefix,
40 const Name& syncPrefix, const StateMsgCallback& callback,
Lijing Wang8e56d082016-12-25 14:45:23 -080041 time::seconds syncInterestInterval /*= -1.0*/)
Lijing Wange84adea2015-05-31 16:25:16 -070042 : m_face(face)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080043 , m_log(syncLog)
Lijing Wange84adea2015-05-31 16:25:16 -070044 , m_scheduler(m_face.getIoService())
45 , m_syncInterestEvent(m_scheduler)
46 , m_periodicInterestEvent(m_scheduler)
47 , m_localStateDelayedEvent(m_scheduler)
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080048 , m_stateMsgCallback(callback)
49 , m_syncPrefix(syncPrefix)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080050 , m_recoverWaitGenerator(
Lijing Wange84adea2015-05-31 16:25:16 -070051 new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::Direction::UP))
Zhenkai Zhu95160102013-01-25 21:54:57 -080052 , m_syncInterestInterval(syncInterestInterval)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080053{
Lijing Wange84adea2015-05-31 16:25:16 -070054 m_rootDigest = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080055
Lijing Wange84adea2015-05-31 16:25:16 -070056 m_registeredPrefixId =
57 m_face.setInterestFilter(m_syncPrefix, bind(&SyncCore::handleInterest, this, _1, _2),
58 RegisterPrefixSuccessCallback(),
59 bind(&SyncCore::onRegisterFailed, this, _1, _2));
60
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 m_log->UpdateLocalLocator(localPrefix);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080062
Lijing Wang8e56d082016-12-25 14:45:23 -080063 time::seconds interval =
64 (m_syncInterestInterval > time::seconds(0) && m_syncInterestInterval < time::seconds(30)) ?
65 m_syncInterestInterval : time::seconds(4);
Alexander Afanasyev50526282013-01-26 13:43:57 -080066
Lijing Wange84adea2015-05-31 16:25:16 -070067 m_periodicInterestEvent =
68 m_scheduler.scheduleEvent(interval, bind(&SyncCore::sendPeriodicSyncInterest, this, interval));
69
70 m_syncInterestEvent =
71 m_scheduler.scheduleEvent(time::milliseconds(100), bind(&SyncCore::sendSyncInterest, this));
72}
73
74void
75SyncCore::sendPeriodicSyncInterest(const time::seconds& interval)
76{
77 sendSyncInterest();
78 m_periodicInterestEvent =
79 m_scheduler.scheduleEvent(interval, bind(&SyncCore::sendPeriodicSyncInterest, this, interval));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080080}
81
82SyncCore::~SyncCore()
83{
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080084 // need to "deregister" closures
Lijing Wange84adea2015-05-31 16:25:16 -070085 m_face.unsetInterestFilter(m_registeredPrefixId);
Zhenkai Zhue851b952013-01-13 22:29:57 -080086}
87
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080088void
Zhenkai Zhue851b952013-01-13 22:29:57 -080089SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 m_log->UpdateLocalSeqNo(seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080092 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080093}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080094
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080095void
96SyncCore::localStateChanged()
97{
Lijing Wange84adea2015-05-31 16:25:16 -070098 ConstBufferPtr oldDigest = m_rootDigest;
99 m_rootDigest = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800100
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 _LOG_DEBUG("[" << m_log->GetLocalName() << "] localStateChanged ");
Lijing Wange84adea2015-05-31 16:25:16 -0700102 _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes: oldDigest--" << toHex(*oldDigest)
103 << " newDigest--"
104 << toHex(*m_rootDigest));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800105
Lijing Wange84adea2015-05-31 16:25:16 -0700106 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldDigest, *m_rootDigest);
107
108 // reply sync Interest with oldDigest as last component
109
110 Name syncName(m_syncPrefix);
Alexander Afanasyevc91fc332017-02-10 17:44:41 -0800111 syncName.append(name::Component(oldDigest));
Lijing Wange84adea2015-05-31 16:25:16 -0700112
113 BufferPtr syncData = serializeGZipMsg(*msg);
114
115 // Create Data packet
116 shared_ptr<Data> data = make_shared<Data>();
117 data->setName(syncName);
118 data->setFreshnessPeriod(time::seconds(FRESHNESS));
119 data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size());
120 m_keyChain.sign(*data);
121 m_face.put(*data);
122
123 _LOG_TRACE(msg);
124
125 // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no
126 // problem, we know the new root digest already;
127 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets
128 // reversed at receivers
129 m_syncInterestEvent =
130 m_scheduler.scheduleEvent(time::milliseconds(50), bind(&SyncCore::sendSyncInterest, this));
131
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800132
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800133 // sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800134}
135
136void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800137SyncCore::localStateChangedDelayed()
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800138{
Lijing Wange84adea2015-05-31 16:25:16 -0700139 // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one
140 // localStateChanged calls
141 m_localStateDelayedEvent =
142 m_scheduler.scheduleEvent(time::milliseconds(500), bind(&SyncCore::localStateChanged, this));
143}
144
145// ------------------------------------------------------------------------------------ send &
146// handle interest
147
148void
149SyncCore::sendSyncInterest()
150{
151 Name syncInterest(m_syncPrefix);
152 // syncInterest.append(name::Component(*m_rootDigest));
Alexander Afanasyevc91fc332017-02-10 17:44:41 -0800153 syncInterest.append(name::Component(m_rootDigest));
Lijing Wange84adea2015-05-31 16:25:16 -0700154
155 _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> send SYNC Interest for " << toHex(*m_rootDigest)
156 << ": "
157 << syncInterest);
158
159 Interest interest(syncInterest);
Lijing Wang8e56d082016-12-25 14:45:23 -0800160 if (m_syncInterestInterval > time::seconds(0) && m_syncInterestInterval < time::seconds(30)) {
161 interest.setInterestLifetime(m_syncInterestInterval);
Lijing Wange84adea2015-05-31 16:25:16 -0700162 }
163
164 m_face.expressInterest(interest, bind(&SyncCore::handleSyncData, this, _1, _2),
165 bind(&SyncCore::handleSyncInterestTimeout, this, _1));
166
167 // // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds
168 // // from now
169 // // if no such task exists, it will be added
170 // m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800171}
172
173void
Lijing Wange84adea2015-05-31 16:25:16 -0700174SyncCore::recover(ConstBufferPtr digest)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800175{
Lijing Wange84adea2015-05-31 16:25:16 -0700176 if (!(*digest == *m_rootDigest) && m_log->LookupSyncLog(*digest) <= 0) {
177 _LOG_TRACE(m_log->GetLocalName() << ", Recover for received_Digest " << toHex(*digest));
178 // unfortunately we still don't recognize this digest
179 // append the unknown digest
180 Name recoverInterest(m_syncPrefix);
Alexander Afanasyevc91fc332017-02-10 17:44:41 -0800181 recoverInterest.append(RECOVER).append(name::Component(digest));
Lijing Wange84adea2015-05-31 16:25:16 -0700182
183 _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> send RECOVER Interests for " << toHex(*digest));
184
185 m_face.expressInterest(recoverInterest,
186 bind(&SyncCore::handleRecoverData, this, _1, _2),
187 bind(&SyncCore::handleRecoverInterestTimeout, this, _1));
188 }
189 else {
190 // we already learned the digest; cheers!
191 }
192}
193
194void
195SyncCore::handleInterest(const InterestFilter& filter, const Interest& interest)
196{
197 Name name = interest.getName();
198 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<< handleInterest with Name: " << name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800199 int size = name.size();
200 int prefixSize = m_syncPrefix.size();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800201 if (size == prefixSize + 1) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800202 // this is normal sync interest
203 handleSyncInterest(name);
204 }
Lijing Wange84adea2015-05-31 16:25:16 -0700205 else if (size == prefixSize + 2 && name.get(m_syncPrefix.size()).toUri() == RECOVER) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800206 // this is recovery interest
207 handleRecoverInterest(name);
208 }
209}
210
211void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800212SyncCore::handleSyncInterest(const Name& name)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800213{
Lijing Wange84adea2015-05-31 16:25:16 -0700214 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< handle SYNC Interest with Name: " << name);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800215
Lijing Wange84adea2015-05-31 16:25:16 -0700216 ConstBufferPtr digest = make_shared<Buffer>(name.get(-1).value(), name.get(-1).value_size());
217 if (*digest == *m_rootDigest) {
218 // we have the same digest; nothing needs to be done
219 _LOG_TRACE("same as root digest: " << toHex(*digest));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800220 return;
221 }
Lijing Wange84adea2015-05-31 16:25:16 -0700222 else if (m_log->LookupSyncLog(*digest) > 0) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800223 // we know something more
Lijing Wange84adea2015-05-31 16:25:16 -0700224 _LOG_TRACE("found digest in sync log");
225 SyncStateMsgPtr msg = m_log->FindStateDifferences(*digest, *m_rootDigest);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800226
Lijing Wange84adea2015-05-31 16:25:16 -0700227 BufferPtr syncData = serializeGZipMsg(*msg);
228 shared_ptr<Data> data = make_shared<Data>();
229 data->setName(name);
230 data->setFreshnessPeriod(time::seconds(FRESHNESS));
231 data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size());
232 m_keyChain.sign(*data);
233 m_face.put(*data);
234
235 _LOG_TRACE(m_log->GetLocalName() << " publishes: " << toHex(*digest) << " my_rootDigest:"
236 << toHex(*m_rootDigest));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800237 _LOG_TRACE(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800238 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800239 else {
Lijing Wange84adea2015-05-31 16:25:16 -0700240 // we don't recognize the digest, send recover Interest if still don't know the digest after a
241 // randomized wait period
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800242 double wait = m_recoverWaitGenerator->nextInterval();
Lijing Wange84adea2015-05-31 16:25:16 -0700243 _LOG_TRACE(m_log->GetLocalName() << ", my_rootDigest: " << toHex(*m_rootDigest)
244 << ", received_Digest: "
245 << toHex(*digest));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800246 _LOG_TRACE("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800247
Lijing Wange84adea2015-05-31 16:25:16 -0700248 // @todo Figure out how to cancel scheduled events when class is destroyed
249 m_scheduler.scheduleEvent(time::milliseconds(static_cast<int>(wait * 1000)),
250 bind(&SyncCore::recover, this, digest));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800251 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800252}
253
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800254void
Lijing Wange84adea2015-05-31 16:25:16 -0700255SyncCore::handleRecoverInterest(const Name& name)
256{
257 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< handle RECOVER Interest with name " << name);
258
259 ConstBufferPtr digest = make_shared<Buffer>(name.get(-1).value(), name.get(-1).value_size());
260 // this is the digest unkonwn to the sender of the interest
261 _LOG_DEBUG("rootDigest: " << toHex(*digest));
262 if (m_log->LookupSyncLog(*digest) > 0) {
263 _LOG_DEBUG("Find in our sync_log! " << toHex(*digest));
264 // we know the digest, should reply everything and the newest thing, but not the digest!!! This
265 // is important
266 unsigned char _origin = 0;
267 BufferPtr origin = make_shared<Buffer>(_origin);
268 // std::cout << "size of origin " << origin->size() << std::endl;
269 SyncStateMsgPtr msg = m_log->FindStateDifferences(*origin, *m_rootDigest);
270
271 BufferPtr syncData = serializeGZipMsg(*msg);
272 shared_ptr<Data> data = make_shared<Data>();
273 data->setName(name);
274 data->setFreshnessPeriod(time::seconds(FRESHNESS));
275 data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size());
276 m_keyChain.sign(*data);
277 m_face.put(*data);
278
279 _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes " << toHex(*digest)
280 << " FindStateDifferences(0, m_rootDigest/"
281 << toHex(*m_rootDigest)
282 << ")");
283 _LOG_TRACE(msg);
284 }
285 else {
286 // we don't recognize this digest, can not help
287 _LOG_DEBUG("we don't recognize this digest, can not help");
288 }
289}
290
291void
292SyncCore::handleSyncInterestTimeout(const Interest& interest)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800293{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800294 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800295}
296
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800297void
Lijing Wange84adea2015-05-31 16:25:16 -0700298SyncCore::handleRecoverInterestTimeout(const Interest& interest)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800299{
300 // We do not re-express recovery interest for now
301 // if difference is not resolved, the sync interest will trigger
302 // recovery anyway; so it's not so important to have recovery interest
303 // re-expressed
Zhenkai Zhue851b952013-01-13 22:29:57 -0800304}
305
306void
Lijing Wange84adea2015-05-31 16:25:16 -0700307SyncCore::handleSyncData(const Interest& interest, Data& data)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800308{
Lijing Wange84adea2015-05-31 16:25:16 -0700309 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< receive SYNC DATA with interest: "
310 << interest.toUri());
Alexander Afanasyev50526282013-01-26 13:43:57 -0800311
Lijing Wange84adea2015-05-31 16:25:16 -0700312 const Block& content = data.getContent();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800313 // suppress recover in interest - data out of order case
Lijing Wange84adea2015-05-31 16:25:16 -0700314 if (data.getContent().value() && content.size() > 0) {
315 handleStateData(Buffer(content.value(), content.value_size()));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800316 }
317 else {
318 _LOG_ERROR("Got sync DATA with empty content");
319 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800320
321 // resume outstanding sync interest
Lijing Wange84adea2015-05-31 16:25:16 -0700322 m_syncInterestEvent =
323 m_scheduler.scheduleEvent(time::milliseconds(0), bind(&SyncCore::sendSyncInterest, this));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800324}
325
326void
Lijing Wange84adea2015-05-31 16:25:16 -0700327SyncCore::handleRecoverData(const Interest& interest, Data& data)
328{
329 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< receive RECOVER DATA with interest: "
330 << interest.toUri());
331 // cout << "handle recover data" << end;
332 const Block& content = data.getContent();
333 if (content.value() && content.size() > 0) {
334 handleStateData(Buffer(content.value(), content.value_size()));
335 }
336 else {
337 _LOG_ERROR("Got recovery DATA with empty content");
338 }
339
340 m_syncInterestEvent =
341 m_scheduler.scheduleEvent(time::milliseconds(0), bind(&SyncCore::sendSyncInterest, this));
342}
343
344void
345SyncCore::handleStateData(const Buffer& content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800346{
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800347 SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800348 if (!(msg)) {
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800349 // ignore misformed SyncData
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800350 _LOG_ERROR("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800351 return;
352 }
353
Lijing Wange84adea2015-05-31 16:25:16 -0700354 _LOG_TRACE("[" << m_log->GetLocalName() << "]"
355 << " receives Msg ");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800356 _LOG_TRACE(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800357 int size = msg->state_size();
358 int index = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800359 while (index < size) {
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800360 SyncState state = msg->state(index);
Lijing Wange84adea2015-05-31 16:25:16 -0700361 std::string devStr = state.name();
362 Name deviceName(Block((const unsigned char*)devStr.c_str(), devStr.size()));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800363 if (state.type() == SyncState::UPDATE) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800364 sqlite3_int64 seqno = state.seq();
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800365 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800366 if (state.has_locator()) {
Lijing Wange84adea2015-05-31 16:25:16 -0700367 std::string locStr = state.locator();
368 Name locatorName(Block((const unsigned char*)locStr.c_str(), locStr.size()));
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800369 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800370
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800371 _LOG_TRACE("self: " << m_log->GetLocalName() << ", device: " << deviceName << " < == > "
372 << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800373 }
374 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800375 else {
376 _LOG_ERROR("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800377 deregister(deviceName);
378 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800379 index++;
380 }
381
Zhenkai Zhue851b952013-01-13 22:29:57 -0800382 // find the actuall difference and invoke callback on the actual difference
Lijing Wange84adea2015-05-31 16:25:16 -0700383 ConstBufferPtr oldDigest = m_rootDigest;
384 m_rootDigest = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800385 // get diff with both new SeqNo and old SeqNo
Lijing Wange84adea2015-05-31 16:25:16 -0700386 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldDigest, *m_rootDigest, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800387
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800388 if (diff->state_size() > 0) {
389 m_stateMsgCallback(diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800390 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800391}
392
393void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800394SyncCore::deregister(const Name& name)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800395{
396 // Do nothing for now
397 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800398}
399
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800400sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800401SyncCore::seq(const Name& name)
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800402{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800403 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800404}
Lijing Wange84adea2015-05-31 16:25:16 -0700405
406} // namespace chronoshare
407} // namespace ndn