blob: 1442bd3f5035ad5b0e889e6b653ee9d82480a6c1 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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"
22#include "sync-state-helper.hpp"
23#include "logging.hpp"
24#include "random-interval-generator.hpp"
25#include "simple-interval-generator.hpp"
26#include "periodic-task.hpp"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080027
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080028#include <boost/lexical_cast.hpp>
Zhenkai Zhu3b406592013-01-25 21:07:49 -080029#include <boost/make_shared.hpp>
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080030
31INIT_LOGGER ("Sync.Core");
32
Zhenkai Zhue851b952013-01-13 22:29:57 -080033const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080034const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080035const double SyncCore::RANDOM_PERCENT = 0.5;
36
Alexander Afanasyev50526282013-01-26 13:43:57 -080037const std::string SYNC_INTEREST_TAG = "send-sync-interest";
38const std::string SYNC_INTEREST_TAG2 = "send-sync-interest2";
39
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -080040const std::string LOCAL_STATE_CHANGE_DELAYED_TAG = "local-state-changed";
41
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080042using namespace boost;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070043using namespace Ndnx;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080044
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080045SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070046 const StateMsgCallback &callback, NdnxWrapperPtr ndnx, double syncInterestInterval/*= -1.0*/)
47 : m_ndnx (ndnx)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080048 , m_log(syncLog)
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080049 , m_scheduler(new Scheduler ())
50 , m_stateMsgCallback(callback)
51 , m_syncPrefix(syncPrefix)
52 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu95160102013-01-25 21:54:57 -080053 , m_syncInterestInterval(syncInterestInterval)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080054{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080055 m_rootHash = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080056
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070057 m_ndnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Alexander Afanasyevdac84922013-01-20 23:32:17 -080058 // m_log->initYP(m_yp);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080059 m_log->UpdateLocalLocator (localPrefix);
60
Zhenkai Zhue573ae82013-01-15 13:15:52 -080061 m_scheduler->start();
Alexander Afanasyev50526282013-01-26 13:43:57 -080062
Zhenkai Zhu95160102013-01-25 21:54:57 -080063 double interval = (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) ? m_syncInterestInterval : 4.0;
Alexander Afanasyev50526282013-01-26 13:43:57 -080064 m_sendSyncInterestTask = make_shared<PeriodicTask>(bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG, m_scheduler, make_shared<SimpleIntervalGenerator>(interval));
65 // sendSyncInterest();
66 Scheduler::scheduleOneTimeTask (m_scheduler, 0.1, bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080067}
68
69SyncCore::~SyncCore()
70{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080071 m_scheduler->shutdown ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080072 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080073}
74
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080075void
Zhenkai Zhue851b952013-01-13 22:29:57 -080076SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080077{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080078 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080079 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080080}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080081
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080082void
83SyncCore::localStateChanged()
84{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080085 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080086 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080087
Zhenkai Zhub330aed2013-01-17 13:29:37 -080088 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080089
90 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080091 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -080092 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080093
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070094 m_ndnx->publishData(syncName, *syncData, FRESHNESS);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080095 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged ");
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080096 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -080097 // _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080098
Alexander Afanasyev50526282013-01-26 13:43:57 -080099 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800100 // 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
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800102 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800103 bind(&SyncCore::sendSyncInterest, this),
Alexander Afanasyev50526282013-01-26 13:43:57 -0800104 SYNC_INTEREST_TAG2);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800105
Zhenkai Zhud276f1e2013-01-24 17:37:31 -0800106 //sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800107}
108
109void
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800110SyncCore::localStateChangedDelayed ()
111{
112 // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one localStateChanged calls
113 Scheduler::scheduleOneTimeTask (m_scheduler, 0.5,
114 bind (&SyncCore::localStateChanged, this),
115 LOCAL_STATE_CHANGE_DELAYED_TAG);
116}
117
118void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800119SyncCore::handleInterest(const Name &name)
120{
121 int size = name.size();
122 int prefixSize = m_syncPrefix.size();
123 if (size == prefixSize + 1)
124 {
125 // this is normal sync interest
126 handleSyncInterest(name);
127 }
128 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
129 {
130 // this is recovery interest
131 handleRecoverInterest(name);
132 }
133}
134
135void
136SyncCore::handleRecoverInterest(const Name &name)
137{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800138 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name);
139
Zhenkai Zhue851b952013-01-13 22:29:57 -0800140 Bytes hashBytes = name.getComp(name.size() - 1);
141 // this is the hash unkonwn to the sender of the interest
142 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800143 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800144 {
145 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800146 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800147
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800148 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700149 m_ndnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800150 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800151 // _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800152 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800153 else
154 {
155 // we don't recognize this hash, can not help
156 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800157}
158
159void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800160SyncCore::handleSyncInterest(const Name &name)
161{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800162 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name);
163
Zhenkai Zhue851b952013-01-13 22:29:57 -0800164 Bytes hashBytes = name.getComp(name.size() - 1);
165 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
166 if (*hash == *m_rootHash)
167 {
168 // we have the same hash; nothing needs to be done
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800169 _LOG_TRACE ("same as root hash: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800170 return;
171 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800172 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800173 {
174 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800175 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800176 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800177
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800178 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700179 m_ndnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800180 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800181 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800182 }
183 else
184 {
185 // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800186 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800187 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800188 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800189
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800190 Scheduler::scheduleOneTimeTask (m_scheduler,
191 wait, boost::bind(&SyncCore::recover, this, hash),
192 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800193 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800194}
195
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800196void
197SyncCore::handleSyncInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800198{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800199 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800200}
201
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800202void
203SyncCore::handleRecoverInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800204{
205 // We do not re-express recovery interest for now
206 // if difference is not resolved, the sync interest will trigger
207 // recovery anyway; so it's not so important to have recovery interest
208 // re-expressed
Zhenkai Zhue851b952013-01-13 22:29:57 -0800209}
210
211void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800212SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800213{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800214 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800215 //cout << "handle recover data" << end;
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800216 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
217 {
218 handleStateData(*content->contentPtr ());
219 }
220 else
221 {
222 _LOG_ERROR ("Got recovery DATA with empty content");
223 }
Alexander Afanasyev50526282013-01-26 13:43:57 -0800224
225 // sendSyncInterest();
226 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
227 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
228 bind(&SyncCore::sendSyncInterest, this),
229 SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800230}
231
232void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800233SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800234{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800235 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name);
236
Zhenkai Zhue851b952013-01-13 22:29:57 -0800237 // suppress recover in interest - data out of order case
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800238 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
239 {
240 handleStateData(*content->contentPtr ());
241 }
242 else
243 {
244 _LOG_ERROR ("Got sync DATA with empty content");
245 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800246
247 // resume outstanding sync interest
Alexander Afanasyev50526282013-01-26 13:43:57 -0800248 // sendSyncInterest();
249
250 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
251 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
252 bind(&SyncCore::sendSyncInterest, this),
253 SYNC_INTEREST_TAG2);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800254}
255
256void
257SyncCore::handleStateData(const Bytes &content)
258{
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800259 SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content);
260 if(!(msg))
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800261 {
262 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800263 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800264 return;
265 }
266
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800267 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
268 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800269 int size = msg->state_size();
270 int index = 0;
271 while (index < size)
272 {
273 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800274 string devStr = state.name();
275 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800276 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800277 if (state.type() == SyncState::UPDATE)
278 {
279 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800280 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800281 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800282 if (state.has_locator())
283 {
284 string locStr = state.locator();
285 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800286 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800287 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800288
289 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800290 }
291 }
292 else
293 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800294 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800295 deregister(deviceName);
296 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800297 index++;
298 }
299
Zhenkai Zhue851b952013-01-13 22:29:57 -0800300 // find the actuall difference and invoke callback on the actual difference
301 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800302 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800303 // get diff with both new SeqNo and old SeqNo
304 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800305
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800306 if (diff->state_size() > 0)
307 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800308 m_stateMsgCallback (diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800309 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800310}
311
312void
313SyncCore::sendSyncInterest()
314{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800315 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
316
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800317 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800318
Zhenkai Zhu95160102013-01-25 21:54:57 -0800319 Selectors selectors;
320 if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0)
321 {
322 selectors.interestLifetime(m_syncInterestInterval);
323 }
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700324 m_ndnx->sendInterest(syncInterest,
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800325 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800326 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1, _2, _3)),
Zhenkai Zhu95160102013-01-25 21:54:57 -0800327 selectors);
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800328
Zhenkai Zhu95160102013-01-25 21:54:57 -0800329 // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800330 // if no such task exists, it will be added
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800331 m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800332}
333
334void
Alexander Afanasyev50526282013-01-26 13:43:57 -0800335SyncCore::recover(HashPtr hash)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800336{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800337 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800338 {
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800339 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800340 // unfortunately we still don't recognize this hash
341 Bytes bytes;
342 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800343
Zhenkai Zhue851b952013-01-13 22:29:57 -0800344 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800345 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
346
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800347 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800348
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700349 m_ndnx->sendInterest(recoverInterest,
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800350 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800351 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1, _2, _3)));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800352
Zhenkai Zhue851b952013-01-13 22:29:57 -0800353 }
354 else
355 {
356 // we already learned the hash; cheers!
357 }
358}
359
360void
361SyncCore::deregister(const Name &name)
362{
363 // Do nothing for now
364 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800365}
366
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800367sqlite3_int64
368SyncCore::seq(const Name &name)
369{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800370 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800371}