blob: 38b624aaa0541db0578763eaa059f1c6c6f03341 [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 Yu6e235db2013-12-27 08:40:53 +080068 shared_ptr<Face> face,
69 shared_ptr<Transport> transport,
Yingdi Yu43e71612013-10-30 22:19:31 -070070 LogicUpdateCallback onUpdate,
71 LogicRemoveCallback onRemove)
72 : m_state (new FullState)
73 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
74 , m_syncPrefix (syncPrefix)
75 , m_onUpdate (onUpdate)
76 , m_onRemove (onRemove)
77 , m_perBranch (false)
78 , m_policyManager(syncPolicyManager)
Yingdi Yu6e235db2013-12-27 08:40:53 +080079 , m_face(face)
80 , m_transport(transport)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070081#ifndef NS3_MODULE
Yingdi Yu43e71612013-10-30 22:19:31 -070082 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
83 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
84 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070085#else
Yingdi Yu43e71612013-10-30 22:19:31 -070086 , m_rangeUniformRandom (200,1000)
87 , m_reexpressionJitter (10,500)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070088#endif
Yingdi Yu43e71612013-10-30 22:19:31 -070089 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070090{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070091#ifndef NS3_MODULE
92 // In NS3 module these functions are moved to StartApplication method
Yingdi Yu43e71612013-10-30 22:19:31 -070093
Yingdi Yu6e235db2013-12-27 08:40:53 +080094 // m_transport = make_shared<TcpTransport>();
95 // m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080096
Yingdi Yu6e235db2013-12-27 08:40:53 +080097 // connectToDaemon();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080098
99 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
100 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
101 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
102
103 m_syncRegisteredPrefixId = m_face->registerPrefix(m_syncPrefix,
104 bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4),
105 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
106
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700107
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700108 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700109 bind (&SyncLogic::sendSyncInterest, this),
110 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700111#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800112}
113
Yingdi Yu43e71612013-10-30 22:19:31 -0700114SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800115 shared_ptr<SyncPolicyManager> syncPolicyManager,
Yingdi Yu6e235db2013-12-27 08:40:53 +0800116 shared_ptr<Face> face,
117 shared_ptr<Transport> transport,
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700118 LogicPerBranchCallback onUpdateBranch)
119 : m_state (new FullState)
120 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
121 , m_syncPrefix (syncPrefix)
122 , m_onUpdateBranch (onUpdateBranch)
123 , m_perBranch(true)
Yingdi Yu43e71612013-10-30 22:19:31 -0700124 , m_policyManager(syncPolicyManager)
Yingdi Yu6e235db2013-12-27 08:40:53 +0800125 , m_face(face)
126 , m_transport(transport)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700127#ifndef NS3_MODULE
128 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
129 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
130 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
131#else
132 , m_rangeUniformRandom (200,1000)
133 , m_reexpressionJitter (10,500)
134#endif
Alexander Afanasyevce001692013-07-14 11:34:41 -0700135 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700136{
137#ifndef NS3_MODULE
138 // In NS3 module these functions are moved to StartApplication method
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800139
Yingdi Yu6e235db2013-12-27 08:40:53 +0800140 // m_transport = make_shared<TcpTransport>();
141 // m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800142
Yingdi Yu6e235db2013-12-27 08:40:53 +0800143 // connectToDaemon();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800144
145 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
146 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
147 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700148
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800149 m_syncRegisteredPrefixId = m_face->registerPrefix(m_syncPrefix,
150 bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4),
151 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700152
153 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
154 bind (&SyncLogic::sendSyncInterest, this),
155 REEXPRESSING_INTEREST);
156#endif
157}
158
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800159SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800160{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800161 m_face->removeRegisteredPrefix(m_syncRegisteredPrefixId);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800162}
163
Yingdi Yu6e235db2013-12-27 08:40:53 +0800164// void
165// SyncLogic::connectToDaemon()
166// {
167// //Hack! transport does not connect to daemon unless an interest is expressed.
168// Name name("/ndn");
169// shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name);
170// m_face->expressInterest(*interest,
171// bind(&SyncLogic::onConnectionData, this, _1, _2),
172// bind(&SyncLogic::onConnectionDataTimeout, this, _1));
173// }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800174
Yingdi Yu6e235db2013-12-27 08:40:53 +0800175// void
176// SyncLogic::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
177// const shared_ptr<Data>& data)
178// {
179// _LOG_DEBUG("onConnectionData");
180// }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800181
Yingdi Yu6e235db2013-12-27 08:40:53 +0800182// void
183// SyncLogic::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
184// {
185// _LOG_DEBUG("onConnectionDataTimeout");
186// }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700187
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700188#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700189void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700190SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700191{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700192 m_ccnxHandle->SetNode (GetNode ());
193 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700194
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700195 m_ccnxHandle->setInterestFilter (m_syncPrefix,
196 bind (&SyncLogic::respondSyncInterest, this, _1));
197
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700198 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700199 bind (&SyncLogic::sendSyncInterest, this),
200 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700201}
202
203void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700204SyncLogic::StopApplication ()
205{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700206 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700207 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700208 m_scheduler.cancel (REEXPRESSING_INTEREST);
209 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700210}
211#endif
212
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800213void
214SyncLogic::stop()
215{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800216 m_face->removeRegisteredPrefix(m_syncRegisteredPrefixId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800217 m_scheduler.cancel (REEXPRESSING_INTEREST);
218 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
219}
220
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700221/**
222 * Two types of intersts
223 *
224 * Normal name: .../<hash>
225 * Recovery name: .../recovery/<hash>
226 */
227boost::tuple<DigestConstPtr, std::string>
228SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700229{
Yingdi Yu43e71612013-10-30 22:19:31 -0700230 BOOST_ASSERT (name.find (m_syncPrefix.toUri()) == 0);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700231
Yingdi Yu43e71612013-10-30 22:19:31 -0700232 string hash = name.substr (m_syncPrefix.toUri().size (), name.size ()-m_syncPrefix.toUri().size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700233 if (hash[0] == '/')
234 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700235 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700236
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700237 size_t pos = hash.find ('/');
238 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700239 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700240 interestType = hash.substr (0, pos);
241 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700242 }
243
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700244 _LOG_TRACE (hash << ", " << interestType);
245
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800246 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700247 istringstream is (hash);
248 is >> *digest;
249
250 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700251}
252
253void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800254SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix,
255 const shared_ptr<const ndn::Interest>& interest,
256 Transport& transport,
257 uint64_t registeredPrefixId)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700258{
Yingdi Yu43e71612013-10-30 22:19:31 -0700259 _LOG_DEBUG("respondSyncInterest: " << interest->getName().toUri());
260 string name = interest->getName().toUri();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700261 try
262 {
263 _LOG_TRACE ("<< I " << name);
264
265 DigestConstPtr digest;
266 string type;
267 tie (digest, type) = convertNameToDigestAndType (name);
268
269 if (type == "normal") // kind of ineffective...
270 {
271 processSyncInterest (name, digest);
272 }
273 else if (type == "recovery")
274 {
275 processSyncRecoveryInterest (name, digest);
276 }
277 }
278 catch (Error::DigestCalculationError &e)
279 {
280 _LOG_TRACE ("Something fishy happened...");
281 // log error. ignoring it for now, later we should log it
282 return ;
283 }
284}
285
286void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800287SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix)
288{
289 _LOG_DEBUG("Sync prefix registration failed!");
290}
291
292void
293SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest,
294 const shared_ptr<Data>& data,
295 int stepCount,
296 const OnVerified& onVerified,
297 const OnVerifyFailed& onVerifyFailed)
298{
299 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
300
301 if (nextStep)
302 m_face->expressInterest
303 (*nextStep->interest_,
304 bind(&SyncLogic::onSyncCert, this, _1, _2, nextStep),
305 bind(&SyncLogic::onSyncCertTimeout, this, _1, onVerifyFailed, data, nextStep));
306}
307
308void
309SyncLogic::onSyncDataTimeout(const shared_ptr<const ndn::Interest>& interest,
310 int retry,
311 int stepCount,
312 const OnVerified& onVerified,
313 const OnVerifyFailed& onVerifyFailed)
314{
315 if(retry > 0)
316 {
317 m_face->expressInterest(*interest,
318 bind(&SyncLogic::onSyncData,
319 this,
320 _1,
321 _2,
322 stepCount,
323 onVerified,
324 onVerifyFailed),
325 bind(&SyncLogic::onSyncDataTimeout,
326 this,
327 _1,
328 retry - 1,
329 stepCount,
330 onVerified,
331 onVerifyFailed));
332 }
333 else
334 _LOG_DEBUG("Sync interest eventually times out!");
335}
336
337void
338SyncLogic::onSyncDataVerifyFailed(const shared_ptr<Data>& data)
339{
340 _LOG_DEBUG("Sync data cannot be verified!");
341}
342
343void
344SyncLogic::onSyncDataVerified(const shared_ptr<Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700345{
Yingdi Yu43e71612013-10-30 22:19:31 -0700346 string name = data->getName().toUri();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800347 const char* wireData = (const char*)data->getContent().buf();
348 size_t len = data->getContent().size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700349
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700350 try
351 {
352 _LOG_TRACE ("<< D " << name);
353
354 DigestConstPtr digest;
355 string type;
356 tie (digest, type) = convertNameToDigestAndType (name);
357
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700358 if (type == "normal")
359 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700360 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700361 }
362 else
363 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700364 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700365 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700366 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700367 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700368 }
369 catch (Error::DigestCalculationError &e)
370 {
371 _LOG_TRACE ("Something fishy happened...");
372 // log error. ignoring it for now, later we should log it
373 return;
374 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700375}
376
Yingdi Yu43e71612013-10-30 22:19:31 -0700377void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800378SyncLogic::onSyncCert(const shared_ptr<const ndn::Interest>& interest,
379 const shared_ptr<Data>& cert,
380 shared_ptr<ValidationRequest> previousStep)
Yingdi Yu43e71612013-10-30 22:19:31 -0700381{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800382 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert,
383 previousStep->stepCount_,
384 previousStep->onVerified_,
385 previousStep->onVerifyFailed_);
386
387 if (nextStep)
388 m_face->expressInterest
389 (*nextStep->interest_,
390 bind(&SyncLogic::onSyncCert, this, _1, _2, nextStep),
391 bind(&SyncLogic::onSyncCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
Yingdi Yu43e71612013-10-30 22:19:31 -0700392}
393
394void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800395SyncLogic::onSyncCertTimeout(const ptr_lib::shared_ptr<const ndn::Interest>& interest,
396 const OnVerifyFailed& onVerifyFailed,
397 const shared_ptr<Data>& data,
398 shared_ptr<ValidationRequest> nextStep)
399{
400 if(nextStep->retry_ > 0)
401 m_face->expressInterest(*interest,
402 bind(&SyncLogic::onSyncCert,
403 this,
404 _1,
405 _2,
406 nextStep),
407 bind(&SyncLogic::onSyncCertTimeout,
408 this,
409 _1,
410 onVerifyFailed,
411 data,
412 nextStep));
413 else
414 onVerifyFailed(data);
415}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700416
417void
418SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700419{
Yingdi Yu43e71612013-10-30 22:19:31 -0700420 _LOG_DEBUG("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700421 DigestConstPtr rootDigest;
422 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800423 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700424 rootDigest = m_state->getDigest();
425 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700426
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700427 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700428 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700429 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700430
431 SyncStateMsg ssm;
432 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800433 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700434 ssm << (*m_state);
435 }
436 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700437 return;
438 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700439
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700440 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700441 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700442 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700443 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700444 return;
445 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700446
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700447 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
448
449 if (stateInDiffLog != m_log.end ())
450 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700451 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700452
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700453 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700454 return;
455 }
456
457 if (!timedProcessing)
458 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700459 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700460 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700461 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700462 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
463 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700464 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700465
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700466 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
467 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700468
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700469 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
470 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700471 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700472 }
473 else
474 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700475 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700476
477 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700478 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700479 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800480}
481
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800482void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700483SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800484{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800485 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700486 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700487
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700488 try
489 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700490
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700491 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700492
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700493 ownInterestSatisfied = (name == m_outstandingInterestName);
494
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700495 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700496 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700497 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700498 {
499 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700500 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700501 }
502 msg >> diff;
503
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700504 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700505 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700506 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700507 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
508 BOOST_ASSERT (diffLeaf != 0);
509
510 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700511 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700512 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700513 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700514
515 bool inserted = false;
516 bool updated = false;
517 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700518 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800519 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700520 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
521 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800522
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700523 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700524 {
525 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700526 if (!oldSeq.isValid())
527 {
528 oldSeq = SeqNo(seq.getSession(), 0);
529 }
530 else
531 {
532 ++oldSeq;
533 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700534 // there is no need for application to process update on forwarder node
535 if (info->toString() != forwarderPrefix)
536 {
537 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700538 if (m_perBranch)
539 {
540 ostringstream interestName;
541 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
542 m_onUpdateBranch(interestName.str());
543 }
544 else
545 {
546 v.push_back(mdi);
547 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700548 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700549 }
550 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700551 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700552 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800553 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700554 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700555 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700556 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700557 if (!m_perBranch)
558 {
559 m_onRemove (info->toString ());
560 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700561 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700562 }
563 else
564 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700565 }
566 }
567
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700568 if (!v.empty())
569 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700570 if (!m_perBranch)
571 {
572 m_onUpdate(v);
573 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700574 }
575
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700576 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700577 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700578 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700579 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700580 _LOG_TRACE ("Something really fishy happened during state decoding " <<
581 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700582 diffLog.reset ();
583 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700584 }
585
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700586 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
587 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700588 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700589 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700590
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700591 // this is kind of wrong
592 // 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 -0700593
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700594 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700595 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700596 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700597 bind (&SyncLogic::sendSyncInterest, this),
598 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700599 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800600}
601
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800602void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700603SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800604{
Yingdi Yu43e71612013-10-30 22:19:31 -0700605 _LOG_DEBUG("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700606 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
607
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700608 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700609 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700610 _LOG_TRACE ("Could not find " << *digest << " in digest log");
611 return;
612 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700613
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700614 SyncStateMsg ssm;
615 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800616 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700617 ssm << (*m_state);
618 }
619 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700620}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700621
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700622void
623SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
624{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800625 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700626 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800627 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700628 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
629 {
630 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
631 /// @todo Impose limit on how many state info should be send out
632 }
633 }
634
635 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800636 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700637 uint32_t counter = 0;
638 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700639 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800640 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700641
642 if (!interest.m_unknown)
643 {
644 _LOG_TRACE (">> D " << interest.m_name);
645 sendSyncData (interest.m_name, interest.m_digest, diffLog);
646 }
647 else
648 {
649 _LOG_TRACE (">> D (unknown)" << interest.m_name);
650 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
651 }
652 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700653 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700654 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700655 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700656 catch (Error::InterestTableIsEmpty &e)
657 {
658 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800659 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800660}
661
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800662void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700663SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700664{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700665 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700666 if (m_log.size () > 0)
667 {
668 m_log.get<sequenced> ().front ()->setNext (diffLog);
669 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700670 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 -0700671 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700672 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700673}
674
675void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700676SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
677{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700678 DiffStatePtr diff;
679 {
680 //cout << "Add local names" <<endl;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800681 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700682 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700683
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700684 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700685
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700686 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700687 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700688
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700689 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700690
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800691 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700692 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700693 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700694 }
695
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700696 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
697 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700698}
699
700void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700701SyncLogic::remove(const string &prefix)
702{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700703 DiffStatePtr diff;
704 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800705 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700706 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700707 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700708
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700709 // increment the sequence number for the forwarder node
710 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700711
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700712 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
713 SeqNo seqNo (0);
714 if (item != m_state->getLeaves ().end ())
715 {
716 seqNo = (*item)->getSeq ();
717 ++seqNo;
718 }
719 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700720
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800721 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700722 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700723 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700724
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700725 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700726 }
727
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700728 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700729}
730
731void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800732SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800733{
Yingdi Yu43e71612013-10-30 22:19:31 -0700734 _LOG_DEBUG("sendSyncInterest");
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800735 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800736
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700737 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800738 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700739
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700740 os << m_syncPrefix << "/" << *m_state->getDigest();
741 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700742 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700743 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700744
Yingdi Yu43e71612013-10-30 22:19:31 -0700745 _LOG_DEBUG("sendSyncInterest: " << os.str());
746
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700747 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700748 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700749 bind (&SyncLogic::sendSyncInterest, this),
750 REEXPRESSING_INTEREST);
Yingdi Yu43e71612013-10-30 22:19:31 -0700751
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800752 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
753
754 OnVerified onVerified = bind(&SyncLogic::onSyncDataVerified, this, _1);
755 OnVerifyFailed onVerifyFailed = bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
756
757 m_face->expressInterest(*interest,
758 bind(&SyncLogic::onSyncData, this, _1, _2, 0, onVerified, onVerifyFailed),
759 bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, 0, onVerified, onVerifyFailed));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800760}
761
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700762void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700763SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
764{
765 ostringstream os;
766 os << m_syncPrefix << "/recovery/" << *digest;
767 _LOG_TRACE (">> I " << os.str ());
768
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700769 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
770 m_recoveryRetransmissionInterval <<= 1;
771
772 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
773 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
774 {
775 m_scheduler.schedule (nextRetransmission,
776 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
777 REEXPRESSING_RECOVERY_INTEREST);
778 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700779
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800780 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
781
782 OnVerified onVerified = bind(&SyncLogic::onSyncDataVerified, this, _1);
783 OnVerifyFailed onVerifyFailed = bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
784
785 m_face->expressInterest(*interest,
786 bind(&SyncLogic::onSyncData, this, _1, _2, 0, onVerified, onVerifyFailed),
787 bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, 0, onVerified, onVerifyFailed));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700788}
789
790
791void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700792SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
793{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700794 SyncStateMsg msg;
795 msg << (*state);
796 sendSyncData(name, digest, msg);
797}
798
799// pass in state msg instead of state, so that there is no need to lock the state until
800// this function returns
801void
802SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm)
803{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700804 _LOG_TRACE (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700805 int size = ssm.ByteSize();
806 char *wireData = new char[size];
807 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800808 Blob blob((const uint8_t*)wireData, size);
Yingdi Yu43e71612013-10-30 22:19:31 -0700809 Name dataName(name);
810 Name signingIdentity = m_policyManager->inferSigningIdentity(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800811
812 shared_ptr<Data> syncData = make_shared<Data>(dataName);
813 syncData->setContent(blob.buf(), blob.size());
814 syncData->getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
815
816 Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(signingIdentity);
817 m_identityManager->signByCertificate(*syncData, certificateName);
818
819 m_transport->send(*syncData->wireEncode());
820
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700821 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700822
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700823 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700824 bool satisfiedOwnInterest = false;
825 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800826 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700827 satisfiedOwnInterest = (m_outstandingInterestName == name);
828 }
829
830 if (satisfiedOwnInterest)
831 {
832 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
833
834 m_scheduler.cancel (REEXPRESSING_INTEREST);
835 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
836 bind (&SyncLogic::sendSyncInterest, this),
837 REEXPRESSING_INTEREST);
838 }
839}
840
Zhenkai Zhue5660932012-06-04 15:25:20 -0700841string
842SyncLogic::getRootDigest()
843{
844 ostringstream os;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800845 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhue5660932012-06-04 15:25:20 -0700846 os << *m_state->getDigest();
847 return os.str();
848}
849
Alexander Afanasyev80237382012-10-04 10:20:47 -0700850size_t
851SyncLogic::getNumberOfBranches () const
852{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800853 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700854 return m_state->getLeaves ().size ();
855}
856
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700857void
858SyncLogic::printState () const
859{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800860 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700861
862 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
863 {
864 std::cout << *leaf << std::endl;
865 }
866}
867
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700868std::map<std::string, bool>
869SyncLogic::getBranchPrefixes() const
870{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800871 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700872
873 std::map<std::string, bool> m;
874
875 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
876 {
877 std::string prefix = leaf->getInfo()->toString();
878 // do not return forwarder prefix
879 if (prefix != forwarderPrefix)
880 {
881 m.insert(pair<std::string, bool>(prefix, false));
882 }
883 }
884
885 return m;
886}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700887
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800888}