blob: eccb3fa8330e22ecb84c40f6f4eae96b848eb101 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080022
Alexander Afanasyevb6cf56e2012-05-29 19:33:49 -070023#ifdef NS3_MODULE
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070024#include <ns3/ccnx-pit.h>
25#include <ns3/ccnx.h>
Alexander Afanasyevb6cf56e2012-05-29 19:33:49 -070026#endif
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070027
Chaoyi Bian11f294f2012-03-08 14:28:06 -080028#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080029#include "sync-diff-leaf.h"
30#include "sync-full-leaf.h"
Alexander Afanasyevce001692013-07-14 11:34:41 -070031#include "sync-logging.h"
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070032#include "sync-state.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070033
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080034#include <ndn-cpp/security/identity/basic-identity-storage.hpp>
35#include <ndn-cpp/security/identity/osx-private-key-storage.hpp>
36
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080037#include <boost/make_shared.hpp>
38#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070039#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080040#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041
42using namespace std;
43using namespace boost;
Yingdi Yu43e71612013-10-30 22:19:31 -070044using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080045using namespace ndn::ptr_lib;
46using namespace ndn::func_lib;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080047
Chaoyi Bianf007f922012-04-09 20:05:37 -070048INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070049
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070050#ifdef NS3_MODULE
51#define GET_RANDOM(var) var.GetValue ()
52#else
53#define GET_RANDOM(var) var ()
54#endif
55
56#define TIME_SECONDS_WITH_JITTER(sec) \
57 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
58
Alexander Afanasyev42612262012-05-05 10:58:37 -070059#define TIME_MILLISECONDS_WITH_JITTER(ms) \
60 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070061
62
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080063namespace Sync
64{
65
Yingdi Yu43e71612013-10-30 22:19:31 -070066 SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080067 shared_ptr<SyncPolicyManager> syncPolicyManager,
Yingdi Yu43e71612013-10-30 22:19:31 -070068 LogicUpdateCallback onUpdate,
69 LogicRemoveCallback onRemove)
70 : m_state (new FullState)
71 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
72 , m_syncPrefix (syncPrefix)
73 , m_onUpdate (onUpdate)
74 , m_onRemove (onRemove)
75 , m_perBranch (false)
76 , m_policyManager(syncPolicyManager)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070077#ifndef NS3_MODULE
Yingdi Yu43e71612013-10-30 22:19:31 -070078 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
79 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
80 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070081#else
Yingdi Yu43e71612013-10-30 22:19:31 -070082 , m_rangeUniformRandom (200,1000)
83 , m_reexpressionJitter (10,500)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070084#endif
Yingdi Yu43e71612013-10-30 22:19:31 -070085 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070086{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070087#ifndef NS3_MODULE
88 // In NS3 module these functions are moved to StartApplication method
Yingdi Yu43e71612013-10-30 22:19:31 -070089
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080090 m_transport = make_shared<TcpTransport>();
91 m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
92
93 connectToDaemon();
94
95 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
96 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
97 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
98
99 m_syncRegisteredPrefixId = m_face->registerPrefix(m_syncPrefix,
100 bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4),
101 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
102
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700103
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700104 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700105 bind (&SyncLogic::sendSyncInterest, this),
106 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700107#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800108}
109
Yingdi Yu43e71612013-10-30 22:19:31 -0700110SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800111 shared_ptr<SyncPolicyManager> syncPolicyManager,
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700112 LogicPerBranchCallback onUpdateBranch)
113 : m_state (new FullState)
114 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
115 , m_syncPrefix (syncPrefix)
116 , m_onUpdateBranch (onUpdateBranch)
117 , m_perBranch(true)
Yingdi Yu43e71612013-10-30 22:19:31 -0700118 , m_policyManager(syncPolicyManager)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700119#ifndef NS3_MODULE
120 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
121 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
122 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
123#else
124 , m_rangeUniformRandom (200,1000)
125 , m_reexpressionJitter (10,500)
126#endif
Alexander Afanasyevce001692013-07-14 11:34:41 -0700127 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700128{
129#ifndef NS3_MODULE
130 // In NS3 module these functions are moved to StartApplication method
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800131
132 m_transport = make_shared<TcpTransport>();
133 m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
134
135 connectToDaemon();
136
137 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
138 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
139 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700140
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800141 m_syncRegisteredPrefixId = m_face->registerPrefix(m_syncPrefix,
142 bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4),
143 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700144
145 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
146 bind (&SyncLogic::sendSyncInterest, this),
147 REEXPRESSING_INTEREST);
148#endif
149}
150
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800151SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800152{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800153 m_face->removeRegisteredPrefix(m_syncRegisteredPrefixId);
154 m_face->shutdown();
155}
156
157void
158SyncLogic::connectToDaemon()
159{
160 //Hack! transport does not connect to daemon unless an interest is expressed.
161 Name name("/ndn");
162 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name);
163 m_face->expressInterest(*interest,
164 bind(&SyncLogic::onConnectionData, this, _1, _2),
165 bind(&SyncLogic::onConnectionDataTimeout, this, _1));
166}
167
168void
169SyncLogic::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
170 const shared_ptr<Data>& data)
171{
172 _LOG_DEBUG("onConnectionData");
173}
174
175void
176SyncLogic::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
177{
178 _LOG_DEBUG("onConnectionDataTimeout");
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700179}
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700180
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700181#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700182void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700183SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700184{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700185 m_ccnxHandle->SetNode (GetNode ());
186 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700187
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700188 m_ccnxHandle->setInterestFilter (m_syncPrefix,
189 bind (&SyncLogic::respondSyncInterest, this, _1));
190
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700191 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700192 bind (&SyncLogic::sendSyncInterest, this),
193 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700194}
195
196void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700197SyncLogic::StopApplication ()
198{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700199 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700200 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700201 m_scheduler.cancel (REEXPRESSING_INTEREST);
202 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700203}
204#endif
205
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800206void
207SyncLogic::stop()
208{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800209 m_face->removeRegisteredPrefix(m_syncRegisteredPrefixId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800210 m_scheduler.cancel (REEXPRESSING_INTEREST);
211 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
212}
213
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700214/**
215 * Two types of intersts
216 *
217 * Normal name: .../<hash>
218 * Recovery name: .../recovery/<hash>
219 */
220boost::tuple<DigestConstPtr, std::string>
221SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700222{
Yingdi Yu43e71612013-10-30 22:19:31 -0700223 BOOST_ASSERT (name.find (m_syncPrefix.toUri()) == 0);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700224
Yingdi Yu43e71612013-10-30 22:19:31 -0700225 string hash = name.substr (m_syncPrefix.toUri().size (), name.size ()-m_syncPrefix.toUri().size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700226 if (hash[0] == '/')
227 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700228 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700229
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700230 size_t pos = hash.find ('/');
231 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700232 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700233 interestType = hash.substr (0, pos);
234 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700235 }
236
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700237 _LOG_TRACE (hash << ", " << interestType);
238
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800239 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700240 istringstream is (hash);
241 is >> *digest;
242
243 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700244}
245
246void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800247SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix,
248 const shared_ptr<const ndn::Interest>& interest,
249 Transport& transport,
250 uint64_t registeredPrefixId)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700251{
Yingdi Yu43e71612013-10-30 22:19:31 -0700252 _LOG_DEBUG("respondSyncInterest: " << interest->getName().toUri());
253 string name = interest->getName().toUri();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700254 try
255 {
256 _LOG_TRACE ("<< I " << name);
257
258 DigestConstPtr digest;
259 string type;
260 tie (digest, type) = convertNameToDigestAndType (name);
261
262 if (type == "normal") // kind of ineffective...
263 {
264 processSyncInterest (name, digest);
265 }
266 else if (type == "recovery")
267 {
268 processSyncRecoveryInterest (name, digest);
269 }
270 }
271 catch (Error::DigestCalculationError &e)
272 {
273 _LOG_TRACE ("Something fishy happened...");
274 // log error. ignoring it for now, later we should log it
275 return ;
276 }
277}
278
279void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800280SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix)
281{
282 _LOG_DEBUG("Sync prefix registration failed!");
283}
284
285void
286SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest,
287 const shared_ptr<Data>& data,
288 int stepCount,
289 const OnVerified& onVerified,
290 const OnVerifyFailed& onVerifyFailed)
291{
292 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
293
294 if (nextStep)
295 m_face->expressInterest
296 (*nextStep->interest_,
297 bind(&SyncLogic::onSyncCert, this, _1, _2, nextStep),
298 bind(&SyncLogic::onSyncCertTimeout, this, _1, onVerifyFailed, data, nextStep));
299}
300
301void
302SyncLogic::onSyncDataTimeout(const shared_ptr<const ndn::Interest>& interest,
303 int retry,
304 int stepCount,
305 const OnVerified& onVerified,
306 const OnVerifyFailed& onVerifyFailed)
307{
308 if(retry > 0)
309 {
310 m_face->expressInterest(*interest,
311 bind(&SyncLogic::onSyncData,
312 this,
313 _1,
314 _2,
315 stepCount,
316 onVerified,
317 onVerifyFailed),
318 bind(&SyncLogic::onSyncDataTimeout,
319 this,
320 _1,
321 retry - 1,
322 stepCount,
323 onVerified,
324 onVerifyFailed));
325 }
326 else
327 _LOG_DEBUG("Sync interest eventually times out!");
328}
329
330void
331SyncLogic::onSyncDataVerifyFailed(const shared_ptr<Data>& data)
332{
333 _LOG_DEBUG("Sync data cannot be verified!");
334}
335
336void
337SyncLogic::onSyncDataVerified(const shared_ptr<Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700338{
Yingdi Yu43e71612013-10-30 22:19:31 -0700339 string name = data->getName().toUri();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800340 const char* wireData = (const char*)data->getContent().buf();
341 size_t len = data->getContent().size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700342
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700343 try
344 {
345 _LOG_TRACE ("<< D " << name);
346
347 DigestConstPtr digest;
348 string type;
349 tie (digest, type) = convertNameToDigestAndType (name);
350
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700351 if (type == "normal")
352 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700353 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700354 }
355 else
356 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700357 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700358 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700359 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700360 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700361 }
362 catch (Error::DigestCalculationError &e)
363 {
364 _LOG_TRACE ("Something fishy happened...");
365 // log error. ignoring it for now, later we should log it
366 return;
367 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700368}
369
Yingdi Yu43e71612013-10-30 22:19:31 -0700370void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800371SyncLogic::onSyncCert(const shared_ptr<const ndn::Interest>& interest,
372 const shared_ptr<Data>& cert,
373 shared_ptr<ValidationRequest> previousStep)
Yingdi Yu43e71612013-10-30 22:19:31 -0700374{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800375 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert,
376 previousStep->stepCount_,
377 previousStep->onVerified_,
378 previousStep->onVerifyFailed_);
379
380 if (nextStep)
381 m_face->expressInterest
382 (*nextStep->interest_,
383 bind(&SyncLogic::onSyncCert, this, _1, _2, nextStep),
384 bind(&SyncLogic::onSyncCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
Yingdi Yu43e71612013-10-30 22:19:31 -0700385}
386
387void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800388SyncLogic::onSyncCertTimeout(const ptr_lib::shared_ptr<const ndn::Interest>& interest,
389 const OnVerifyFailed& onVerifyFailed,
390 const shared_ptr<Data>& data,
391 shared_ptr<ValidationRequest> nextStep)
392{
393 if(nextStep->retry_ > 0)
394 m_face->expressInterest(*interest,
395 bind(&SyncLogic::onSyncCert,
396 this,
397 _1,
398 _2,
399 nextStep),
400 bind(&SyncLogic::onSyncCertTimeout,
401 this,
402 _1,
403 onVerifyFailed,
404 data,
405 nextStep));
406 else
407 onVerifyFailed(data);
408}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700409
410void
411SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700412{
Yingdi Yu43e71612013-10-30 22:19:31 -0700413 _LOG_DEBUG("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700414 DigestConstPtr rootDigest;
415 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800416 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700417 rootDigest = m_state->getDigest();
418 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700419
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700420 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700421 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700422 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700423
424 SyncStateMsg ssm;
425 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800426 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700427 ssm << (*m_state);
428 }
429 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700430 return;
431 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700432
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700433 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700434 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700435 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700436 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700437 return;
438 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700439
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700440 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
441
442 if (stateInDiffLog != m_log.end ())
443 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700444 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700445
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700446 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700447 return;
448 }
449
450 if (!timedProcessing)
451 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700452 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700453 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700454 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700455 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
456 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700457 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700458
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700459 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
460 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700461
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700462 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
463 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700464 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700465 }
466 else
467 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700468 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700469
470 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700471 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700472 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800473}
474
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800475void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700476SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800477{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800478 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700479 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700480
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700481 try
482 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700483
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700484 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700485
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700486 ownInterestSatisfied = (name == m_outstandingInterestName);
487
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700488 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700489 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700490 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700491 {
492 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700493 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700494 }
495 msg >> diff;
496
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700497 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700498 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700499 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700500 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
501 BOOST_ASSERT (diffLeaf != 0);
502
503 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700504 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700505 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700506 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700507
508 bool inserted = false;
509 bool updated = false;
510 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700511 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800512 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700513 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
514 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800515
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700516 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700517 {
518 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700519 if (!oldSeq.isValid())
520 {
521 oldSeq = SeqNo(seq.getSession(), 0);
522 }
523 else
524 {
525 ++oldSeq;
526 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700527 // there is no need for application to process update on forwarder node
528 if (info->toString() != forwarderPrefix)
529 {
530 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700531 if (m_perBranch)
532 {
533 ostringstream interestName;
534 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
535 m_onUpdateBranch(interestName.str());
536 }
537 else
538 {
539 v.push_back(mdi);
540 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700541 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700542 }
543 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700544 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700545 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800546 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700547 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700548 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700549 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700550 if (!m_perBranch)
551 {
552 m_onRemove (info->toString ());
553 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700554 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700555 }
556 else
557 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700558 }
559 }
560
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700561 if (!v.empty())
562 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700563 if (!m_perBranch)
564 {
565 m_onUpdate(v);
566 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700567 }
568
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700569 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700570 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700571 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700572 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700573 _LOG_TRACE ("Something really fishy happened during state decoding " <<
574 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700575 diffLog.reset ();
576 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700577 }
578
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700579 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
580 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700581 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700582 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700583
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700584 // this is kind of wrong
585 // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT, there is a point to satisfy them using new state
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700586
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700587 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700588 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700589 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700590 bind (&SyncLogic::sendSyncInterest, this),
591 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700592 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800593}
594
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800595void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700596SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800597{
Yingdi Yu43e71612013-10-30 22:19:31 -0700598 _LOG_DEBUG("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700599 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
600
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700601 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700602 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700603 _LOG_TRACE ("Could not find " << *digest << " in digest log");
604 return;
605 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700606
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700607 SyncStateMsg ssm;
608 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800609 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700610 ssm << (*m_state);
611 }
612 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700613}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700614
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700615void
616SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
617{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800618 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700619 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800620 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700621 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
622 {
623 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
624 /// @todo Impose limit on how many state info should be send out
625 }
626 }
627
628 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800629 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700630 uint32_t counter = 0;
631 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700632 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800633 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700634
635 if (!interest.m_unknown)
636 {
637 _LOG_TRACE (">> D " << interest.m_name);
638 sendSyncData (interest.m_name, interest.m_digest, diffLog);
639 }
640 else
641 {
642 _LOG_TRACE (">> D (unknown)" << interest.m_name);
643 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
644 }
645 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700646 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700647 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700648 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700649 catch (Error::InterestTableIsEmpty &e)
650 {
651 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800652 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800653}
654
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800655void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700656SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700657{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700658 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700659 if (m_log.size () > 0)
660 {
661 m_log.get<sequenced> ().front ()->setNext (diffLog);
662 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700663 m_log.erase (m_state->getDigest()); // remove diff state with the same digest. next pointers are still valid
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700664 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700665 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700666}
667
668void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700669SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
670{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700671 DiffStatePtr diff;
672 {
673 //cout << "Add local names" <<endl;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800674 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700675 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700676
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700677 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700678
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700679 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700680 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700681
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700682 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700683
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800684 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700685 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700686 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700687 }
688
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700689 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
690 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700691}
692
693void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700694SyncLogic::remove(const string &prefix)
695{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700696 DiffStatePtr diff;
697 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800698 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700699 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700700 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700701
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700702 // increment the sequence number for the forwarder node
703 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700704
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700705 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
706 SeqNo seqNo (0);
707 if (item != m_state->getLeaves ().end ())
708 {
709 seqNo = (*item)->getSeq ();
710 ++seqNo;
711 }
712 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700713
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800714 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700715 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700716 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700717
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700718 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700719 }
720
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700721 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700722}
723
724void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800725SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800726{
Yingdi Yu43e71612013-10-30 22:19:31 -0700727 _LOG_DEBUG("sendSyncInterest");
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800728 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800729
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700730 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800731 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700732
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700733 os << m_syncPrefix << "/" << *m_state->getDigest();
734 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700735 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700736 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700737
Yingdi Yu43e71612013-10-30 22:19:31 -0700738 _LOG_DEBUG("sendSyncInterest: " << os.str());
739
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700740 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700741 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700742 bind (&SyncLogic::sendSyncInterest, this),
743 REEXPRESSING_INTEREST);
Yingdi Yu43e71612013-10-30 22:19:31 -0700744
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800745 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
746
747 OnVerified onVerified = bind(&SyncLogic::onSyncDataVerified, this, _1);
748 OnVerifyFailed onVerifyFailed = bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
749
750 m_face->expressInterest(*interest,
751 bind(&SyncLogic::onSyncData, this, _1, _2, 0, onVerified, onVerifyFailed),
752 bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, 0, onVerified, onVerifyFailed));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800753}
754
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700755void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700756SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
757{
758 ostringstream os;
759 os << m_syncPrefix << "/recovery/" << *digest;
760 _LOG_TRACE (">> I " << os.str ());
761
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700762 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
763 m_recoveryRetransmissionInterval <<= 1;
764
765 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
766 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
767 {
768 m_scheduler.schedule (nextRetransmission,
769 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
770 REEXPRESSING_RECOVERY_INTEREST);
771 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700772
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800773 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
774
775 OnVerified onVerified = bind(&SyncLogic::onSyncDataVerified, this, _1);
776 OnVerifyFailed onVerifyFailed = bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
777
778 m_face->expressInterest(*interest,
779 bind(&SyncLogic::onSyncData, this, _1, _2, 0, onVerified, onVerifyFailed),
780 bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, 0, onVerified, onVerifyFailed));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700781}
782
783
784void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700785SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
786{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700787 SyncStateMsg msg;
788 msg << (*state);
789 sendSyncData(name, digest, msg);
790}
791
792// pass in state msg instead of state, so that there is no need to lock the state until
793// this function returns
794void
795SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm)
796{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700797 _LOG_TRACE (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700798 int size = ssm.ByteSize();
799 char *wireData = new char[size];
800 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800801 Blob blob((const uint8_t*)wireData, size);
Yingdi Yu43e71612013-10-30 22:19:31 -0700802 Name dataName(name);
803 Name signingIdentity = m_policyManager->inferSigningIdentity(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800804
805 shared_ptr<Data> syncData = make_shared<Data>(dataName);
806 syncData->setContent(blob.buf(), blob.size());
807 syncData->getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
808
809 Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(signingIdentity);
810 m_identityManager->signByCertificate(*syncData, certificateName);
811
812 m_transport->send(*syncData->wireEncode());
813
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700814 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700815
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700816 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700817 bool satisfiedOwnInterest = false;
818 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800819 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700820 satisfiedOwnInterest = (m_outstandingInterestName == name);
821 }
822
823 if (satisfiedOwnInterest)
824 {
825 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
826
827 m_scheduler.cancel (REEXPRESSING_INTEREST);
828 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
829 bind (&SyncLogic::sendSyncInterest, this),
830 REEXPRESSING_INTEREST);
831 }
832}
833
Zhenkai Zhue5660932012-06-04 15:25:20 -0700834string
835SyncLogic::getRootDigest()
836{
837 ostringstream os;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800838 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhue5660932012-06-04 15:25:20 -0700839 os << *m_state->getDigest();
840 return os.str();
841}
842
Alexander Afanasyev80237382012-10-04 10:20:47 -0700843size_t
844SyncLogic::getNumberOfBranches () const
845{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800846 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700847 return m_state->getLeaves ().size ();
848}
849
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700850void
851SyncLogic::printState () const
852{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800853 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700854
855 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
856 {
857 std::cout << *leaf << std::endl;
858 }
859}
860
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700861std::map<std::string, bool>
862SyncLogic::getBranchPrefixes() const
863{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800864 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700865
866 std::map<std::string, bool> m;
867
868 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
869 {
870 std::string prefix = leaf->getInfo()->toString();
871 // do not return forwarder prefix
872 if (prefix != forwarderPrefix)
873 {
874 m.insert(pair<std::string, bool>(prefix, false));
875 }
876 }
877
878 return m;
879}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700880
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800881}