blob: 4aaf1a5066563abad495a013c3a37d3d2b2a69be [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"
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080023#include "sync-state-helper.h"
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080024#include "logging.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080025#include "random-interval-generator.h"
Zhenkai Zhu3566f2f2013-01-25 23:47:59 -080026#include "simple-interval-generator.h"
27#include "periodic-task.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080028
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080029#include <boost/lexical_cast.hpp>
Zhenkai Zhu3b406592013-01-25 21:07:49 -080030#include <boost/make_shared.hpp>
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080031
32INIT_LOGGER ("Sync.Core");
33
Zhenkai Zhue851b952013-01-13 22:29:57 -080034const string SyncCore::RECOVER = "RECOVER";
Zhenkai Zhue573ae82013-01-15 13:15:52 -080035const double SyncCore::WAIT = 0.05;
Zhenkai Zhue851b952013-01-13 22:29:57 -080036const double SyncCore::RANDOM_PERCENT = 0.5;
37
Alexander Afanasyev50526282013-01-26 13:43:57 -080038const std::string SYNC_INTEREST_TAG = "send-sync-interest";
39const std::string SYNC_INTEREST_TAG2 = "send-sync-interest2";
40
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080041using namespace boost;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080042using namespace Ccnx;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080043
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080044SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080045 const StateMsgCallback &callback, CcnxWrapperPtr ccnx, double syncInterestInterval/*= -1.0*/)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080046 : m_ccnx (ccnx)
47 , m_log(syncLog)
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080048 , m_scheduler(new Scheduler ())
49 , m_stateMsgCallback(callback)
50 , m_syncPrefix(syncPrefix)
51 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu95160102013-01-25 21:54:57 -080052 , m_syncInterestInterval(syncInterestInterval)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080053{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080054 m_rootHash = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080055
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080056 m_ccnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Alexander Afanasyevdac84922013-01-20 23:32:17 -080057 // m_log->initYP(m_yp);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080058 m_log->UpdateLocalLocator (localPrefix);
59
Zhenkai Zhue573ae82013-01-15 13:15:52 -080060 m_scheduler->start();
Alexander Afanasyev50526282013-01-26 13:43:57 -080061
Zhenkai Zhu95160102013-01-25 21:54:57 -080062 double interval = (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) ? m_syncInterestInterval : 4.0;
Alexander Afanasyev50526282013-01-26 13:43:57 -080063 m_sendSyncInterestTask = make_shared<PeriodicTask>(bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG, m_scheduler, make_shared<SimpleIntervalGenerator>(interval));
64 // sendSyncInterest();
65 Scheduler::scheduleOneTimeTask (m_scheduler, 0.1, bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080066}
67
68SyncCore::~SyncCore()
69{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080070 // m_scheduler->shutdown ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080071 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080072}
73
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080074void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080075SyncCore::updateLocalPrefix (const Name &localPrefix)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080076{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080077 m_log->UpdateLocalLocator (localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080078}
79
80void
Zhenkai Zhue851b952013-01-13 22:29:57 -080081SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080082{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080083 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080084 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080085}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080086
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080087void
88SyncCore::localStateChanged()
89{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080091 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080092
Zhenkai Zhub330aed2013-01-17 13:29:37 -080093 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080094
95 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080096 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -080097 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080098
99 m_ccnx->publishData(syncName, *syncData, FRESHNESS);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800100 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged ");
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800101 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800102 // _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800103
Alexander Afanasyev50526282013-01-26 13:43:57 -0800104 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800105 // 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;
106 // 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 -0800107 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800108 bind(&SyncCore::sendSyncInterest, this),
Alexander Afanasyev50526282013-01-26 13:43:57 -0800109 SYNC_INTEREST_TAG2);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800110
Zhenkai Zhud276f1e2013-01-24 17:37:31 -0800111 //sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800112}
113
114void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800115SyncCore::handleInterest(const Name &name)
116{
117 int size = name.size();
118 int prefixSize = m_syncPrefix.size();
119 if (size == prefixSize + 1)
120 {
121 // this is normal sync interest
122 handleSyncInterest(name);
123 }
124 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
125 {
126 // this is recovery interest
127 handleRecoverInterest(name);
128 }
129}
130
131void
132SyncCore::handleRecoverInterest(const Name &name)
133{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800134 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name);
135
Zhenkai Zhue851b952013-01-13 22:29:57 -0800136 Bytes hashBytes = name.getComp(name.size() - 1);
137 // this is the hash unkonwn to the sender of the interest
138 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800139 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800140 {
141 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800142 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800143
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800144 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800145 m_ccnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800146 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800147 // _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800148 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800149 else
150 {
151 // we don't recognize this hash, can not help
152 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800153}
154
155void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800156SyncCore::handleSyncInterest(const Name &name)
157{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800158 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name);
159
Zhenkai Zhue851b952013-01-13 22:29:57 -0800160 Bytes hashBytes = name.getComp(name.size() - 1);
161 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
162 if (*hash == *m_rootHash)
163 {
164 // we have the same hash; nothing needs to be done
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800165 _LOG_TRACE ("same as root hash: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800166 return;
167 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800168 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800169 {
170 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800171 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800172 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800173
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800174 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800175 m_ccnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800176 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800177 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800178 }
179 else
180 {
181 // 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 -0800182 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800183 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800184 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800185
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800186 Scheduler::scheduleOneTimeTask (m_scheduler,
187 wait, boost::bind(&SyncCore::recover, this, hash),
188 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800189 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800190}
191
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800192void
193SyncCore::handleSyncInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800194{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800195 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800196}
197
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800198void
199SyncCore::handleRecoverInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800200{
201 // We do not re-express recovery interest for now
202 // if difference is not resolved, the sync interest will trigger
203 // recovery anyway; so it's not so important to have recovery interest
204 // re-expressed
Zhenkai Zhue851b952013-01-13 22:29:57 -0800205}
206
207void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800208SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800209{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800210 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800211 //cout << "handle recover data" << end;
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800212 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
213 {
214 handleStateData(*content->contentPtr ());
215 }
216 else
217 {
218 _LOG_ERROR ("Got recovery DATA with empty content");
219 }
Alexander Afanasyev50526282013-01-26 13:43:57 -0800220
221 // sendSyncInterest();
222 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
223 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
224 bind(&SyncCore::sendSyncInterest, this),
225 SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800226}
227
228void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800229SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800230{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800231 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name);
232
Zhenkai Zhue851b952013-01-13 22:29:57 -0800233 // suppress recover in interest - data out of order case
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800234 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
235 {
236 handleStateData(*content->contentPtr ());
237 }
238 else
239 {
240 _LOG_ERROR ("Got sync DATA with empty content");
241 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800242
243 // resume outstanding sync interest
Alexander Afanasyev50526282013-01-26 13:43:57 -0800244 // sendSyncInterest();
245
246 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
247 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
248 bind(&SyncCore::sendSyncInterest, this),
249 SYNC_INTEREST_TAG2);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800250}
251
252void
253SyncCore::handleStateData(const Bytes &content)
254{
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800255 SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content);
256 if(!(msg))
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800257 {
258 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800259 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800260 return;
261 }
262
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800263 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
264 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800265 int size = msg->state_size();
266 int index = 0;
267 while (index < size)
268 {
269 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800270 string devStr = state.name();
271 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800272 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800273 if (state.type() == SyncState::UPDATE)
274 {
275 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800276 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800277 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800278 if (state.has_locator())
279 {
280 string locStr = state.locator();
281 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800282 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800283 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800284
285 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800286 }
287 }
288 else
289 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800290 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800291 deregister(deviceName);
292 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800293 index++;
294 }
295
Zhenkai Zhue851b952013-01-13 22:29:57 -0800296 // find the actuall difference and invoke callback on the actual difference
297 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800298 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800299 // get diff with both new SeqNo and old SeqNo
300 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800301
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800302 if (diff->state_size() > 0)
303 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800304 m_stateMsgCallback (diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800305 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800306}
307
308void
309SyncCore::sendSyncInterest()
310{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800311 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
312
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800313 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800314
Zhenkai Zhu95160102013-01-25 21:54:57 -0800315 Selectors selectors;
316 if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0)
317 {
318 selectors.interestLifetime(m_syncInterestInterval);
319 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800320 m_ccnx->sendInterest(syncInterest,
321 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800322 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1, _2, _3)),
Zhenkai Zhu95160102013-01-25 21:54:57 -0800323 selectors);
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800324
Zhenkai Zhu95160102013-01-25 21:54:57 -0800325 // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800326 // if no such task exists, it will be added
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800327 m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800328}
329
330void
Alexander Afanasyev50526282013-01-26 13:43:57 -0800331SyncCore::recover(HashPtr hash)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800332{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800333 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800334 {
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800335 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800336 // unfortunately we still don't recognize this hash
337 Bytes bytes;
338 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800339
Zhenkai Zhue851b952013-01-13 22:29:57 -0800340 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800341 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
342
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800343 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800344
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800345 m_ccnx->sendInterest(recoverInterest,
346 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800347 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1, _2, _3)));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800348
Zhenkai Zhue851b952013-01-13 22:29:57 -0800349 }
350 else
351 {
352 // we already learned the hash; cheers!
353 }
354}
355
356void
357SyncCore::deregister(const Name &name)
358{
359 // Do nothing for now
360 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800361}
362
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800363sqlite3_int64
364SyncCore::seq(const Name &name)
365{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800366 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800367}