blob: f3f0edc59f4ce64ebfc4131f3da8b0a32358c3f9 [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
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "sync-core.hpp"
23#include "sync-state-helper.hpp"
24#include "logging.hpp"
25#include "random-interval-generator.hpp"
26#include "simple-interval-generator.hpp"
27#include "periodic-task.hpp"
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 Afanasyev1dd37ed2013-08-14 18:08:09 -070044using namespace Ndnx;
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,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070047 const StateMsgCallback &callback, NdnxWrapperPtr ndnx, double syncInterestInterval/*= -1.0*/)
48 : m_ndnx (ndnx)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080049 , 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 Afanasyev1dd37ed2013-08-14 18:08:09 -070058 m_ndnx->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
Zhenkai Zhue851b952013-01-13 22:29:57 -080077SyncCore::updateLocalState(sqlite3_int64 seqno)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080078{
Alexander Afanasyev7326a252013-01-20 23:43:25 -080079 m_log->UpdateLocalSeqNo (seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080080 localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080081}
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080082
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080083void
84SyncCore::localStateChanged()
85{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080086 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -080087 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080088
Zhenkai Zhub330aed2013-01-17 13:29:37 -080089 SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090
91 // reply sync Interest with oldHash as last component
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080092 Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes());
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -080093 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080094
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070095 m_ndnx->publishData(syncName, *syncData, FRESHNESS);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080096 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged ");
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080097 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -080098 // _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080099
Alexander Afanasyev50526282013-01-26 13:43:57 -0800100 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800101 // 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;
102 // 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 -0800103 Scheduler::scheduleOneTimeTask (m_scheduler, 0.05,
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800104 bind(&SyncCore::sendSyncInterest, this),
Alexander Afanasyev50526282013-01-26 13:43:57 -0800105 SYNC_INTEREST_TAG2);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800106
Zhenkai Zhud276f1e2013-01-24 17:37:31 -0800107 //sendSyncInterest();
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800108}
109
110void
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800111SyncCore::localStateChangedDelayed ()
112{
113 // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one localStateChanged calls
114 Scheduler::scheduleOneTimeTask (m_scheduler, 0.5,
115 bind (&SyncCore::localStateChanged, this),
116 LOCAL_STATE_CHANGE_DELAYED_TAG);
117}
118
119void
Zhenkai Zhue851b952013-01-13 22:29:57 -0800120SyncCore::handleInterest(const Name &name)
121{
122 int size = name.size();
123 int prefixSize = m_syncPrefix.size();
124 if (size == prefixSize + 1)
125 {
126 // this is normal sync interest
127 handleSyncInterest(name);
128 }
129 else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER)
130 {
131 // this is recovery interest
132 handleRecoverInterest(name);
133 }
134}
135
136void
137SyncCore::handleRecoverInterest(const Name &name)
138{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800139 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name);
140
Zhenkai Zhue851b952013-01-13 22:29:57 -0800141 Bytes hashBytes = name.getComp(name.size() - 1);
142 // this is the hash unkonwn to the sender of the interest
143 Hash hash(head(hashBytes), hashBytes.size());
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800144 if (m_log->LookupSyncLog(hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800145 {
146 // we know the hash, should reply everything
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800147 SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800148
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800149 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700150 m_ndnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800151 _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800152 // _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800153 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800154 else
155 {
156 // we don't recognize this hash, can not help
157 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800158}
159
160void
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800161SyncCore::handleSyncInterest(const Name &name)
162{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800163 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name);
164
Zhenkai Zhue851b952013-01-13 22:29:57 -0800165 Bytes hashBytes = name.getComp(name.size() - 1);
166 HashPtr hash(new Hash(head(hashBytes), hashBytes.size()));
167 if (*hash == *m_rootHash)
168 {
169 // we have the same hash; nothing needs to be done
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800170 _LOG_TRACE ("same as root hash: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800171 return;
172 }
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800173 else if (m_log->LookupSyncLog(*hash) > 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800174 {
175 // we know something more
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800176 _LOG_TRACE ("found hash in sync log");
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800177 SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800178
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800179 BytesPtr syncData = serializeGZipMsg (*msg);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700180 m_ndnx->publishData(name, *syncData, FRESHNESS);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800181 _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800182 _LOG_TRACE (msg);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800183 }
184 else
185 {
186 // 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 -0800187 double wait = m_recoverWaitGenerator->nextInterval();
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800188 _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800189 _LOG_TRACE ("recover task scheduled after wait: " << wait);
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800190
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800191 Scheduler::scheduleOneTimeTask (m_scheduler,
192 wait, boost::bind(&SyncCore::recover, this, hash),
193 "r-"+lexical_cast<string> (*hash));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800194 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800195}
196
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800197void
198SyncCore::handleSyncInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800199{
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800200 // sync interest will be resent by scheduler
Zhenkai Zhue851b952013-01-13 22:29:57 -0800201}
202
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800203void
204SyncCore::handleRecoverInterestTimeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800205{
206 // We do not re-express recovery interest for now
207 // if difference is not resolved, the sync interest will trigger
208 // recovery anyway; so it's not so important to have recovery interest
209 // re-expressed
Zhenkai Zhue851b952013-01-13 22:29:57 -0800210}
211
212void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800213SyncCore::handleRecoverData(const Name &name, PcoPtr content)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800214{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800215 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name);
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800216 //cout << "handle recover data" << end;
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800217 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
218 {
219 handleStateData(*content->contentPtr ());
220 }
221 else
222 {
223 _LOG_ERROR ("Got recovery DATA with empty content");
224 }
Alexander Afanasyev50526282013-01-26 13:43:57 -0800225
226 // sendSyncInterest();
227 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
228 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
229 bind(&SyncCore::sendSyncInterest, this),
230 SYNC_INTEREST_TAG2);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800231}
232
233void
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800234SyncCore::handleSyncData(const Name &name, PcoPtr content)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800235{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800236 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name);
237
Zhenkai Zhue851b952013-01-13 22:29:57 -0800238 // suppress recover in interest - data out of order case
Alexander Afanasyev30ef9222013-01-26 10:41:38 -0800239 if (content && content->contentPtr () && content->contentPtr ()->size () > 0)
240 {
241 handleStateData(*content->contentPtr ());
242 }
243 else
244 {
245 _LOG_ERROR ("Got sync DATA with empty content");
246 }
Zhenkai Zhue851b952013-01-13 22:29:57 -0800247
248 // resume outstanding sync interest
Alexander Afanasyev50526282013-01-26 13:43:57 -0800249 // sendSyncInterest();
250
251 m_scheduler->deleteTask (SYNC_INTEREST_TAG2);
252 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
253 bind(&SyncCore::sendSyncInterest, this),
254 SYNC_INTEREST_TAG2);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800255}
256
257void
258SyncCore::handleStateData(const Bytes &content)
259{
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800260 SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content);
261 if(!(msg))
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800262 {
263 // ignore misformed SyncData
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800264 _LOG_ERROR ("Misformed SyncData");
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800265 return;
266 }
267
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800268 _LOG_TRACE (m_log->GetLocalName () << " receives Msg ");
269 _LOG_TRACE (msg);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800270 int size = msg->state_size();
271 int index = 0;
272 while (index < size)
273 {
274 SyncState state = msg->state(index);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800275 string devStr = state.name();
276 Name deviceName((const unsigned char *)devStr.c_str(), devStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800277 // cout << "Got Name: " << deviceName;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800278 if (state.type() == SyncState::UPDATE)
279 {
280 sqlite3_int64 seqno = state.seq();
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800281 // cout << ", Got seq: " << seqno << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800282 m_log->UpdateDeviceSeqNo(deviceName, seqno);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800283 if (state.has_locator())
284 {
285 string locStr = state.locator();
286 Name locatorName((const unsigned char *)locStr.c_str(), locStr.size());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800287 // cout << ", Got loc: " << locatorName << endl;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800288 m_log->UpdateLocator(deviceName, locatorName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800289
290 _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800291 }
292 }
293 else
294 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800295 _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet");
Zhenkai Zhue851b952013-01-13 22:29:57 -0800296 deregister(deviceName);
297 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800298 index++;
299 }
300
Zhenkai Zhue851b952013-01-13 22:29:57 -0800301 // find the actuall difference and invoke callback on the actual difference
302 HashPtr oldHash = m_rootHash;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800303 m_rootHash = m_log->RememberStateInStateLog();
Zhenkai Zhu085aae72013-01-17 21:09:01 -0800304 // get diff with both new SeqNo and old SeqNo
305 SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800306
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800307 if (diff->state_size() > 0)
308 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800309 m_stateMsgCallback (diff);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800310 }
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800311}
312
313void
314SyncCore::sendSyncInterest()
315{
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800316 Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes());
317
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800318 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800319
Zhenkai Zhu95160102013-01-25 21:54:57 -0800320 Selectors selectors;
321 if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0)
322 {
323 selectors.interestLifetime(m_syncInterestInterval);
324 }
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700325 m_ndnx->sendInterest(syncInterest,
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800326 Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800327 boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1, _2, _3)),
Zhenkai Zhu95160102013-01-25 21:54:57 -0800328 selectors);
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800329
Zhenkai Zhu95160102013-01-25 21:54:57 -0800330 // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800331 // if no such task exists, it will be added
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800332 m_scheduler->rescheduleTask(m_sendSyncInterestTask);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800333}
334
335void
Alexander Afanasyev50526282013-01-26 13:43:57 -0800336SyncCore::recover(HashPtr hash)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800337{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800338 if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0)
Zhenkai Zhue851b952013-01-13 22:29:57 -0800339 {
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800340 _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ());
Zhenkai Zhue851b952013-01-13 22:29:57 -0800341 // unfortunately we still don't recognize this hash
342 Bytes bytes;
343 readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800344
Zhenkai Zhue851b952013-01-13 22:29:57 -0800345 // append the unknown hash
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800346 Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes);
347
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800348 _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ());
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800349
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700350 m_ndnx->sendInterest(recoverInterest,
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800351 Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800352 boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1, _2, _3)));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800353
Zhenkai Zhue851b952013-01-13 22:29:57 -0800354 }
355 else
356 {
357 // we already learned the hash; cheers!
358 }
359}
360
361void
362SyncCore::deregister(const Name &name)
363{
364 // Do nothing for now
365 // TODO: handle deregistering
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800366}
367
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800368sqlite3_int64
369SyncCore::seq(const Name &name)
370{
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800371 return m_log->SeqNo(name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800372}