blob: e6a4b01369022037c18a3c96d547214e2564146a [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 Afanasyeva2fabcf2013-02-05 11:26:37 -080041const std::string LOCAL_STATE_CHANGE_DELAYED_TAG = "local-state-changed";
42
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080043using namespace boost;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080044using namespace Ccnx;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080045
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080046SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080047 const StateMsgCallback &callback, CcnxWrapperPtr ccnx, double syncInterestInterval/*= -1.0*/)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080048 : m_ccnx (ccnx)
49 , m_log(syncLog)
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080050 , m_scheduler(new Scheduler ())
51 , m_stateMsgCallback(callback)
52 , m_syncPrefix(syncPrefix)
53 , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP))
Zhenkai Zhu95160102013-01-25 21:54:57 -080054 , m_syncInterestInterval(syncInterestInterval)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080055{
Zhenkai Zhub330aed2013-01-17 13:29:37 -080056 m_rootHash = m_log->RememberStateInStateLog();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080057
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080058 m_ccnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1));
Alexander Afanasyevdac84922013-01-20 23:32:17 -080059 // m_log->initYP(m_yp);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080060 m_log->UpdateLocalLocator (localPrefix);
61
Zhenkai Zhue573ae82013-01-15 13:15:52 -080062 m_scheduler->start();
Alexander Afanasyev50526282013-01-26 13:43:57 -080063
Zhenkai Zhu95160102013-01-25 21:54:57 -080064 double interval = (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) ? m_syncInterestInterval : 4.0;
Alexander Afanasyev50526282013-01-26 13:43:57 -080065 m_sendSyncInterestTask = make_shared<PeriodicTask>(bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG, m_scheduler, make_shared<SimpleIntervalGenerator>(interval));
66 // sendSyncInterest();
67 Scheduler::scheduleOneTimeTask (m_scheduler, 0.1, bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080068}
69
70SyncCore::~SyncCore()
71{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080072 m_scheduler->shutdown ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080073 // need to "deregister" closures
Zhenkai Zhue851b952013-01-13 22:29:57 -080074}
75
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080076void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080077SyncCore::updateLocalPrefix (const Name &localPrefix)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080078{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080079 m_log->UpdateLocalLocator (localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080080}
81
82void
Zhenkai Zhue851b952013-01-13 22:29:57 -080083SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080084{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080085 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080086 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080087}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080088
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080089void
90SyncCore::localStateChanged()
91{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080092 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080093 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080094
Zhenkai Zhub330aed2013-01-17 13:29:37 -080095 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080096
97 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080098 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -080099 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800100
101 m_ccnx->publishData(syncName, *syncData, FRESHNESS);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800102 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged ");
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800103 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800104 // _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800105
Alexander Afanasyev50526282013-01-26 13:43:57 -0800106 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800107 // 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;
108 // 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 -0800109 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800110 bind(&SyncCore::sendSyncInterest, this),
Alexander Afanasyev50526282013-01-26 13:43:57 -0800111 SYNC_INTEREST_TAG2);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800112
Zhenkai Zhud276f1e2013-01-24 17:37:31 -0800113 //sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800114}
115
116void
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800117SyncCore::localStateChangedDelayed ()
118{
119 // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one localStateChanged calls
120 Scheduler::scheduleOneTimeTask (m_scheduler, 0.5,
121 bind (&SyncCore::localStateChanged, this),
122 LOCAL_STATE_CHANGE_DELAYED_TAG);
123}
124
125void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800126SyncCore::handleInterest(const Name &name)
127{
128 int size = name.size();
129 int prefixSize = m_syncPrefix.size();
130 if (size == prefixSize + 1)
131 {
132 // this is normal sync interest
133 handleSyncInterest(name);
134 }
135 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
136 {
137 // this is recovery interest
138 handleRecoverInterest(name);
139 }
140}
141
142void
143SyncCore::handleRecoverInterest(const Name &name)
144{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800145 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name);
146
Zhenkai Zhue851b952013-01-13 22:29:57 -0800147 Bytes hashBytes = name.getComp(name.size() - 1);
148 // this is the hash unkonwn to the sender of the interest
149 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800150 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800151 {
152 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800153 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800154
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800155 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800156 m_ccnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800157 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800158 // _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800159 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800160 else
161 {
162 // we don't recognize this hash, can not help
163 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800164}
165
166void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800167SyncCore::handleSyncInterest(const Name &name)
168{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800169 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name);
170
Zhenkai Zhue851b952013-01-13 22:29:57 -0800171 Bytes hashBytes = name.getComp(name.size() - 1);
172 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
173 if (*hash == *m_rootHash)
174 {
175 // we have the same hash; nothing needs to be done
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800176 _LOG_TRACE ("same as root hash: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800177 return;
178 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800179 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800180 {
181 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800182 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800183 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800184
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800185 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800186 m_ccnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800187 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800188 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800189 }
190 else
191 {
192 // 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 -0800193 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800194 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800195 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800196
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800197 Scheduler::scheduleOneTimeTask (m_scheduler,
198 wait, boost::bind(&SyncCore::recover, this, hash),
199 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800200 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800201}
202
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800203void
204SyncCore::handleSyncInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800205{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800206 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800207}
208
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800209void
210SyncCore::handleRecoverInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800211{
212 // We do not re-express recovery interest for now
213 // if difference is not resolved, the sync interest will trigger
214 // recovery anyway; so it's not so important to have recovery interest
215 // re-expressed
Zhenkai Zhue851b952013-01-13 22:29:57 -0800216}
217
218void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800219SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800220{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800221 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800222 //cout << "handle recover data" << end;
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800223 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
224 {
225 handleStateData(*content->contentPtr ());
226 }
227 else
228 {
229 _LOG_ERROR ("Got recovery DATA with empty content");
230 }
Alexander Afanasyev50526282013-01-26 13:43:57 -0800231
232 // sendSyncInterest();
233 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
234 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
235 bind(&SyncCore::sendSyncInterest, this),
236 SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800237}
238
239void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800240SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800241{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800242 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name);
243
Zhenkai Zhue851b952013-01-13 22:29:57 -0800244 // suppress recover in interest - data out of order case
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800245 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
246 {
247 handleStateData(*content->contentPtr ());
248 }
249 else
250 {
251 _LOG_ERROR ("Got sync DATA with empty content");
252 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800253
254 // resume outstanding sync interest
Alexander Afanasyev50526282013-01-26 13:43:57 -0800255 // sendSyncInterest();
256
257 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
258 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
259 bind(&SyncCore::sendSyncInterest, this),
260 SYNC_INTEREST_TAG2);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800261}
262
263void
264SyncCore::handleStateData(const Bytes &content)
265{
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800266 SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content);
267 if(!(msg))
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800268 {
269 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800270 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800271 return;
272 }
273
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800274 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
275 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800276 int size = msg->state_size();
277 int index = 0;
278 while (index < size)
279 {
280 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800281 string devStr = state.name();
282 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800283 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800284 if (state.type() == SyncState::UPDATE)
285 {
286 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800287 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800288 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800289 if (state.has_locator())
290 {
291 string locStr = state.locator();
292 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800293 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800294 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800295
296 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800297 }
298 }
299 else
300 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800301 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800302 deregister(deviceName);
303 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800304 index++;
305 }
306
Zhenkai Zhue851b952013-01-13 22:29:57 -0800307 // find the actuall difference and invoke callback on the actual difference
308 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800309 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800310 // get diff with both new SeqNo and old SeqNo
311 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800312
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800313 if (diff->state_size() > 0)
314 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800315 m_stateMsgCallback (diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800316 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800317}
318
319void
320SyncCore::sendSyncInterest()
321{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800322 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
323
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800324 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800325
Zhenkai Zhu95160102013-01-25 21:54:57 -0800326 Selectors selectors;
327 if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0)
328 {
329 selectors.interestLifetime(m_syncInterestInterval);
330 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800331 m_ccnx->sendInterest(syncInterest,
332 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800333 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1, _2, _3)),
Zhenkai Zhu95160102013-01-25 21:54:57 -0800334 selectors);
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800335
Zhenkai Zhu95160102013-01-25 21:54:57 -0800336 // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800337 // if no such task exists, it will be added
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800338 m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800339}
340
341void
Alexander Afanasyev50526282013-01-26 13:43:57 -0800342SyncCore::recover(HashPtr hash)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800343{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800344 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800345 {
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800346 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800347 // unfortunately we still don't recognize this hash
348 Bytes bytes;
349 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800350
Zhenkai Zhue851b952013-01-13 22:29:57 -0800351 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800352 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
353
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800354 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800355
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800356 m_ccnx->sendInterest(recoverInterest,
357 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800358 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1, _2, _3)));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800359
Zhenkai Zhue851b952013-01-13 22:29:57 -0800360 }
361 else
362 {
363 // we already learned the hash; cheers!
364 }
365}
366
367void
368SyncCore::deregister(const Name &name)
369{
370 // Do nothing for now
371 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800372}
373
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800374sqlite3_int64
375SyncCore::seq(const Name &name)
376{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800377 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800378}