blob: c4f9a268d67fde10a6211b3fddcd37f9315208fc [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 Afanasyeva9369b42017-01-11 11:58:00 -080032INIT_LOGGER("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,
41 long syncInterestInterval /*= -1.0*/)
42 : 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 Wange84adea2015-05-31 16:25:16 -070063 time::seconds interval = time::seconds(
64 (m_syncInterestInterval > 0 && m_syncInterestInterval < 30) ? m_syncInterestInterval : 4);
Alexander Afanasyev50526282013-01-26 13:43:57 -080065
Lijing Wange84adea2015-05-31 16:25:16 -070066 m_periodicInterestEvent =
67 m_scheduler.scheduleEvent(interval, bind(&SyncCore::sendPeriodicSyncInterest, this, interval));
68
69 m_syncInterestEvent =
70 m_scheduler.scheduleEvent(time::milliseconds(100), bind(&SyncCore::sendSyncInterest, this));
71}
72
73void
74SyncCore::sendPeriodicSyncInterest(const time::seconds& interval)
75{
76 sendSyncInterest();
77 m_periodicInterestEvent =
78 m_scheduler.scheduleEvent(interval, bind(&SyncCore::sendPeriodicSyncInterest, this, interval));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080079}
80
81SyncCore::~SyncCore()
82{
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080083 // need to "deregister" closures
Lijing Wange84adea2015-05-31 16:25:16 -070084 m_face.unsetInterestFilter(m_registeredPrefixId);
Zhenkai Zhue851b952013-01-13 22:29:57 -080085}
86
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080087void
Zhenkai Zhue851b952013-01-13 22:29:57 -080088SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080089{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090 m_log->UpdateLocalSeqNo(seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080091 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080092}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080093
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080094void
95SyncCore::localStateChanged()
96{
Lijing Wange84adea2015-05-31 16:25:16 -070097 ConstBufferPtr oldDigest = m_rootDigest;
98 m_rootDigest = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080099
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 _LOG_DEBUG("[" << m_log->GetLocalName() << "] localStateChanged ");
Lijing Wange84adea2015-05-31 16:25:16 -0700101 _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes: oldDigest--" << toHex(*oldDigest)
102 << " newDigest--"
103 << toHex(*m_rootDigest));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800104
Lijing Wange84adea2015-05-31 16:25:16 -0700105 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldDigest, *m_rootDigest);
106
107 // reply sync Interest with oldDigest as last component
108
109 Name syncName(m_syncPrefix);
110 syncName.appendImplicitSha256Digest(oldDigest);
111
112 BufferPtr syncData = serializeGZipMsg(*msg);
113
114 // Create Data packet
115 shared_ptr<Data> data = make_shared<Data>();
116 data->setName(syncName);
117 data->setFreshnessPeriod(time::seconds(FRESHNESS));
118 data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size());
119 m_keyChain.sign(*data);
120 m_face.put(*data);
121
122 _LOG_TRACE(msg);
123
124 // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no
125 // problem, we know the new root digest already;
126 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets
127 // reversed at receivers
128 m_syncInterestEvent =
129 m_scheduler.scheduleEvent(time::milliseconds(50), bind(&SyncCore::sendSyncInterest, this));
130
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800131
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800132 // sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800133}
134
135void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800136SyncCore::localStateChangedDelayed()
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800137{
Lijing Wange84adea2015-05-31 16:25:16 -0700138 // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one
139 // localStateChanged calls
140 m_localStateDelayedEvent =
141 m_scheduler.scheduleEvent(time::milliseconds(500), bind(&SyncCore::localStateChanged, this));
142}
143
144// ------------------------------------------------------------------------------------ send &
145// handle interest
146
147void
148SyncCore::sendSyncInterest()
149{
150 Name syncInterest(m_syncPrefix);
151 // syncInterest.append(name::Component(*m_rootDigest));
152 syncInterest.appendImplicitSha256Digest(m_rootDigest);
153
154 _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> send SYNC Interest for " << toHex(*m_rootDigest)
155 << ": "
156 << syncInterest);
157
158 Interest interest(syncInterest);
159 if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30) {
160 interest.setInterestLifetime(time::seconds(m_syncInterestInterval));
161 }
162
163 m_face.expressInterest(interest, bind(&SyncCore::handleSyncData, this, _1, _2),
164 bind(&SyncCore::handleSyncInterestTimeout, this, _1));
165
166 // // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds
167 // // from now
168 // // if no such task exists, it will be added
169 // m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800170}
171
172void
Lijing Wange84adea2015-05-31 16:25:16 -0700173SyncCore::recover(ConstBufferPtr digest)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800174{
Lijing Wange84adea2015-05-31 16:25:16 -0700175 if (!(*digest == *m_rootDigest) && m_log->LookupSyncLog(*digest) <= 0) {
176 _LOG_TRACE(m_log->GetLocalName() << ", Recover for received_Digest " << toHex(*digest));
177 // unfortunately we still don't recognize this digest
178 // append the unknown digest
179 Name recoverInterest(m_syncPrefix);
180 recoverInterest.append(RECOVER).appendImplicitSha256Digest(digest);
181
182 _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> send RECOVER Interests for " << toHex(*digest));
183
184 m_face.expressInterest(recoverInterest,
185 bind(&SyncCore::handleRecoverData, this, _1, _2),
186 bind(&SyncCore::handleRecoverInterestTimeout, this, _1));
187 }
188 else {
189 // we already learned the digest; cheers!
190 }
191}
192
193void
194SyncCore::handleInterest(const InterestFilter& filter, const Interest& interest)
195{
196 Name name = interest.getName();
197 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<< handleInterest with Name: " << name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800198 int size = name.size();
199 int prefixSize = m_syncPrefix.size();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800200 if (size == prefixSize + 1) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800201 // this is normal sync interest
202 handleSyncInterest(name);
203 }
Lijing Wange84adea2015-05-31 16:25:16 -0700204 else if (size == prefixSize + 2 && name.get(m_syncPrefix.size()).toUri() == RECOVER) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800205 // this is recovery interest
206 handleRecoverInterest(name);
207 }
208}
209
210void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800211SyncCore::handleSyncInterest(const Name& name)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800212{
Lijing Wange84adea2015-05-31 16:25:16 -0700213 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< handle SYNC Interest with Name: " << name);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800214
Lijing Wange84adea2015-05-31 16:25:16 -0700215 ConstBufferPtr digest = make_shared<Buffer>(name.get(-1).value(), name.get(-1).value_size());
216 if (*digest == *m_rootDigest) {
217 // we have the same digest; nothing needs to be done
218 _LOG_TRACE("same as root digest: " << toHex(*digest));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800219 return;
220 }
Lijing Wange84adea2015-05-31 16:25:16 -0700221 else if (m_log->LookupSyncLog(*digest) > 0) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800222 // we know something more
Lijing Wange84adea2015-05-31 16:25:16 -0700223 _LOG_TRACE("found digest in sync log");
224 SyncStateMsgPtr msg = m_log->FindStateDifferences(*digest, *m_rootDigest);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800225
Lijing Wange84adea2015-05-31 16:25:16 -0700226 BufferPtr syncData = serializeGZipMsg(*msg);
227 shared_ptr<Data> data = make_shared<Data>();
228 data->setName(name);
229 data->setFreshnessPeriod(time::seconds(FRESHNESS));
230 data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size());
231 m_keyChain.sign(*data);
232 m_face.put(*data);
233
234 _LOG_TRACE(m_log->GetLocalName() << " publishes: " << toHex(*digest) << " my_rootDigest:"
235 << toHex(*m_rootDigest));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800236 _LOG_TRACE(msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800237 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800238 else {
Lijing Wange84adea2015-05-31 16:25:16 -0700239 // we don't recognize the digest, send recover Interest if still don't know the digest after a
240 // randomized wait period
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800241 double wait = m_recoverWaitGenerator->nextInterval();
Lijing Wange84adea2015-05-31 16:25:16 -0700242 _LOG_TRACE(m_log->GetLocalName() << ", my_rootDigest: " << toHex(*m_rootDigest)
243 << ", received_Digest: "
244 << toHex(*digest));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800245 _LOG_TRACE("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800246
Lijing Wange84adea2015-05-31 16:25:16 -0700247 // @todo Figure out how to cancel scheduled events when class is destroyed
248 m_scheduler.scheduleEvent(time::milliseconds(static_cast<int>(wait * 1000)),
249 bind(&SyncCore::recover, this, digest));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800250 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800251}
252
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800253void
Lijing Wange84adea2015-05-31 16:25:16 -0700254SyncCore::handleRecoverInterest(const Name& name)
255{
256 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< handle RECOVER Interest with name " << name);
257
258 ConstBufferPtr digest = make_shared<Buffer>(name.get(-1).value(), name.get(-1).value_size());
259 // this is the digest unkonwn to the sender of the interest
260 _LOG_DEBUG("rootDigest: " << toHex(*digest));
261 if (m_log->LookupSyncLog(*digest) > 0) {
262 _LOG_DEBUG("Find in our sync_log! " << toHex(*digest));
263 // we know the digest, should reply everything and the newest thing, but not the digest!!! This
264 // is important
265 unsigned char _origin = 0;
266 BufferPtr origin = make_shared<Buffer>(_origin);
267 // std::cout << "size of origin " << origin->size() << std::endl;
268 SyncStateMsgPtr msg = m_log->FindStateDifferences(*origin, *m_rootDigest);
269
270 BufferPtr syncData = serializeGZipMsg(*msg);
271 shared_ptr<Data> data = make_shared<Data>();
272 data->setName(name);
273 data->setFreshnessPeriod(time::seconds(FRESHNESS));
274 data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size());
275 m_keyChain.sign(*data);
276 m_face.put(*data);
277
278 _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes " << toHex(*digest)
279 << " FindStateDifferences(0, m_rootDigest/"
280 << toHex(*m_rootDigest)
281 << ")");
282 _LOG_TRACE(msg);
283 }
284 else {
285 // we don't recognize this digest, can not help
286 _LOG_DEBUG("we don't recognize this digest, can not help");
287 }
288}
289
290void
291SyncCore::handleSyncInterestTimeout(const Interest& interest)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800292{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800293 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800294}
295
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800296void
Lijing Wange84adea2015-05-31 16:25:16 -0700297SyncCore::handleRecoverInterestTimeout(const Interest& interest)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800298{
299 // We do not re-express recovery interest for now
300 // if difference is not resolved, the sync interest will trigger
301 // recovery anyway; so it's not so important to have recovery interest
302 // re-expressed
Zhenkai Zhue851b952013-01-13 22:29:57 -0800303}
304
305void
Lijing Wange84adea2015-05-31 16:25:16 -0700306SyncCore::handleSyncData(const Interest& interest, Data& data)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800307{
Lijing Wange84adea2015-05-31 16:25:16 -0700308 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< receive SYNC DATA with interest: "
309 << interest.toUri());
Alexander Afanasyev50526282013-01-26 13:43:57 -0800310
Lijing Wange84adea2015-05-31 16:25:16 -0700311 const Block& content = data.getContent();
Zhenkai Zhue851b952013-01-13 22:29:57 -0800312 // suppress recover in interest - data out of order case
Lijing Wange84adea2015-05-31 16:25:16 -0700313 if (data.getContent().value() && content.size() > 0) {
314 handleStateData(Buffer(content.value(), content.value_size()));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800315 }
316 else {
317 _LOG_ERROR("Got sync DATA with empty content");
318 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800319
320 // resume outstanding sync interest
Lijing Wange84adea2015-05-31 16:25:16 -0700321 m_syncInterestEvent =
322 m_scheduler.scheduleEvent(time::milliseconds(0), bind(&SyncCore::sendSyncInterest, this));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800323}
324
325void
Lijing Wange84adea2015-05-31 16:25:16 -0700326SyncCore::handleRecoverData(const Interest& interest, Data& data)
327{
328 _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< receive RECOVER DATA with interest: "
329 << interest.toUri());
330 // cout << "handle recover data" << end;
331 const Block& content = data.getContent();
332 if (content.value() && content.size() > 0) {
333 handleStateData(Buffer(content.value(), content.value_size()));
334 }
335 else {
336 _LOG_ERROR("Got recovery DATA with empty content");
337 }
338
339 m_syncInterestEvent =
340 m_scheduler.scheduleEvent(time::milliseconds(0), bind(&SyncCore::sendSyncInterest, this));
341}
342
343void
344SyncCore::handleStateData(const Buffer& content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800345{
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800346 SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800347 if (!(msg)) {
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800348 // ignore misformed SyncData
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800349 _LOG_ERROR("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800350 return;
351 }
352
Lijing Wange84adea2015-05-31 16:25:16 -0700353 _LOG_TRACE("[" << m_log->GetLocalName() << "]"
354 << " receives Msg ");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800355 _LOG_TRACE(msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800356 int size = msg->state_size();
357 int index = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800358 while (index < size) {
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800359 SyncState state = msg->state(index);
Lijing Wange84adea2015-05-31 16:25:16 -0700360 std::string devStr = state.name();
361 Name deviceName(Block((const unsigned char*)devStr.c_str(), devStr.size()));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800362 if (state.type() == SyncState::UPDATE) {
Zhenkai Zhue851b952013-01-13 22:29:57 -0800363 sqlite3_int64 seqno = state.seq();
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800364 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800365 if (state.has_locator()) {
Lijing Wange84adea2015-05-31 16:25:16 -0700366 std::string locStr = state.locator();
367 Name locatorName(Block((const unsigned char*)locStr.c_str(), locStr.size()));
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800368 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800369
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800370 _LOG_TRACE("self: " << m_log->GetLocalName() << ", device: " << deviceName << " < == > "
371 << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800372 }
373 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800374 else {
375 _LOG_ERROR("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800376 deregister(deviceName);
377 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800378 index++;
379 }
380
Zhenkai Zhue851b952013-01-13 22:29:57 -0800381 // find the actuall difference and invoke callback on the actual difference
Lijing Wange84adea2015-05-31 16:25:16 -0700382 ConstBufferPtr oldDigest = m_rootDigest;
383 m_rootDigest = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800384 // get diff with both new SeqNo and old SeqNo
Lijing Wange84adea2015-05-31 16:25:16 -0700385 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldDigest, *m_rootDigest, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800386
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800387 if (diff->state_size() > 0) {
388 m_stateMsgCallback(diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800389 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800390}
391
392void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800393SyncCore::deregister(const Name& name)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800394{
395 // Do nothing for now
396 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800397}
398
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800399sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800400SyncCore::seq(const Name& name)
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800401{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800402 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800403}
Lijing Wange84adea2015-05-31 16:25:16 -0700404
405} // namespace chronoshare
406} // namespace ndn