blob: 191b92b4ba90b9de6302891bcca14b0c7524b7b9 [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());
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -080097 BytesPtr syncData = serializeMsg (*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
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800144 BytesPtr syncData = serializeMsg (*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
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800174 BytesPtr syncData = serializeMsg (*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
192Closure::TimeoutCallbackReturnValue
193SyncCore::handleSyncInterestTimeout(const Name &name)
194{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800195 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800196 return Closure::RESULT_OK;
197}
198
199Closure::TimeoutCallbackReturnValue
200SyncCore::handleRecoverInterestTimeout(const Name &name)
201{
202 // We do not re-express recovery interest for now
203 // if difference is not resolved, the sync interest will trigger
204 // recovery anyway; so it's not so important to have recovery interest
205 // re-expressed
206 return Closure::RESULT_OK;
207}
208
209void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800210SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800211{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800212 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800213 //cout << "handle recover data" << end;
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800214 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
215 {
216 handleStateData(*content->contentPtr ());
217 }
218 else
219 {
220 _LOG_ERROR ("Got recovery DATA with empty content");
221 }
Alexander Afanasyev50526282013-01-26 13:43:57 -0800222
223 // sendSyncInterest();
224 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
225 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
226 bind(&SyncCore::sendSyncInterest, this),
227 SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800228}
229
230void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800231SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800232{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800233 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name);
234
Zhenkai Zhue851b952013-01-13 22:29:57 -0800235 // suppress recover in interest - data out of order case
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800236 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
237 {
238 handleStateData(*content->contentPtr ());
239 }
240 else
241 {
242 _LOG_ERROR ("Got sync DATA with empty content");
243 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800244
245 // resume outstanding sync interest
Alexander Afanasyev50526282013-01-26 13:43:57 -0800246 // sendSyncInterest();
247
248 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
249 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
250 bind(&SyncCore::sendSyncInterest, this),
251 SYNC_INTEREST_TAG2);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800252}
253
254void
255SyncCore::handleStateData(const Bytes &content)
256{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800257 SyncStateMsgPtr msg(new SyncStateMsg);
258 bool success = msg->ParseFromArray(head(content), content.size());
259 if(!success)
260 {
261 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800262 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800263 return;
264 }
265
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800266 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
267 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800268 int size = msg->state_size();
269 int index = 0;
270 while (index < size)
271 {
272 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800273 string devStr = state.name();
274 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800275 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800276 if (state.type() == SyncState::UPDATE)
277 {
278 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800279 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800280 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800281 if (state.has_locator())
282 {
283 string locStr = state.locator();
284 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800285 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800286 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800287
288 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800289 }
290 }
291 else
292 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800293 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800294 deregister(deviceName);
295 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800296 index++;
297 }
298
Zhenkai Zhue851b952013-01-13 22:29:57 -0800299 // find the actuall difference and invoke callback on the actual difference
300 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800301 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800302 // get diff with both new SeqNo and old SeqNo
303 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800304
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800305 if (diff->state_size() > 0)
306 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800307 m_stateMsgCallback (diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800308 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800309}
310
311void
312SyncCore::sendSyncInterest()
313{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800314 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
315
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800316 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800317
Zhenkai Zhu95160102013-01-25 21:54:57 -0800318 Selectors selectors;
319 if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0)
320 {
321 selectors.interestLifetime(m_syncInterestInterval);
322 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800323 m_ccnx->sendInterest(syncInterest,
324 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
Zhenkai Zhu95160102013-01-25 21:54:57 -0800325 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)),
326 selectors);
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800327
Zhenkai Zhu95160102013-01-25 21:54:57 -0800328 // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800329 // if no such task exists, it will be added
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800330 m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800331}
332
333void
Alexander Afanasyev50526282013-01-26 13:43:57 -0800334SyncCore::recover(HashPtr hash)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800335{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800336 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800337 {
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800338 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800339 // unfortunately we still don't recognize this hash
340 Bytes bytes;
341 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800342
Zhenkai Zhue851b952013-01-13 22:29:57 -0800343 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800344 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
345
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800346 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800347
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800348 m_ccnx->sendInterest(recoverInterest,
349 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
350 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1)));
351
Zhenkai Zhue851b952013-01-13 22:29:57 -0800352 }
353 else
354 {
355 // we already learned the hash; cheers!
356 }
357}
358
359void
360SyncCore::deregister(const Name &name)
361{
362 // Do nothing for now
363 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800364}
365
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800366sqlite3_int64
367SyncCore::seq(const Name &name)
368{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800369 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800370}