blob: 240e9dffa75cf0383dac8207c3570e4628267c5d [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
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080034#include <boost/make_shared.hpp>
35#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070036#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080037#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080038
39using namespace std;
Yingdi Yu43e71612013-10-30 22:19:31 -070040using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080041using namespace ndn::ptr_lib;
42using namespace ndn::func_lib;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080043
Chaoyi Bianf007f922012-04-09 20:05:37 -070044INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070045
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070046#ifdef NS3_MODULE
47#define GET_RANDOM(var) var.GetValue ()
48#else
49#define GET_RANDOM(var) var ()
50#endif
51
52#define TIME_SECONDS_WITH_JITTER(sec) \
53 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
54
Alexander Afanasyev42612262012-05-05 10:58:37 -070055#define TIME_MILLISECONDS_WITH_JITTER(ms) \
56 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070057
58
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080059namespace Sync
60{
61
Yingdi Yu43e71612013-10-30 22:19:31 -070062 SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080063 shared_ptr<SecPolicySync> policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080064 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070065 LogicUpdateCallback onUpdate,
66 LogicRemoveCallback onRemove)
67 : m_state (new FullState)
68 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
69 , m_syncPrefix (syncPrefix)
70 , m_onUpdate (onUpdate)
71 , m_onRemove (onRemove)
72 , m_perBranch (false)
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080073 , m_policy(policy)
74 , m_verifier(new Verifier(policy))
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080075 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +080076 , m_face(face)
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 Yu0cb0f2b2014-01-09 13:51:16 -080090 m_syncRegisteredPrefixId = m_face->setInterestFilter(m_syncPrefix,
91 func_lib::bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4),
92 func_lib::bind(&SyncLogic::onSyncRegisterFailed, this, _1));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080093
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070094
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070095 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080096 func_lib::bind (&SyncLogic::sendSyncInterest, this),
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070097 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070098#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080099}
100
Yingdi Yu43e71612013-10-30 22:19:31 -0700101SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800102 shared_ptr<SecPolicySync> policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +0800103 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700104 LogicPerBranchCallback onUpdateBranch)
105 : m_state (new FullState)
106 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
107 , m_syncPrefix (syncPrefix)
108 , m_onUpdateBranch (onUpdateBranch)
109 , m_perBranch(true)
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800110 , m_policy(policy)
111 , m_verifier(new Verifier(policy))
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800112 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +0800113 , m_face(face)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700114#ifndef NS3_MODULE
115 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
116 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
117 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
118#else
119 , m_rangeUniformRandom (200,1000)
120 , m_reexpressionJitter (10,500)
121#endif
Alexander Afanasyevce001692013-07-14 11:34:41 -0700122 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700123{
124#ifndef NS3_MODULE
125 // In NS3 module these functions are moved to StartApplication method
126
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800127 m_syncRegisteredPrefixId = m_face->setInterestFilter(m_syncPrefix,
128 func_lib::bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4),
129 func_lib::bind(&SyncLogic::onSyncRegisterFailed, this, _1));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700130
131 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800132 func_lib::bind (&SyncLogic::sendSyncInterest, this),
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700133 REEXPRESSING_INTEREST);
134#endif
135}
136
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800137SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800138{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800139 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800140}
141
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700142#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700143void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700144SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700145{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700146 m_ccnxHandle->SetNode (GetNode ());
147 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700148
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700149 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800150 func_lib::bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700151
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700152 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800153 func_lib::bind (&SyncLogic::sendSyncInterest, this),
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700154 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700155}
156
157void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700158SyncLogic::StopApplication ()
159{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700160 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700161 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700162 m_scheduler.cancel (REEXPRESSING_INTEREST);
163 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700164}
165#endif
166
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800167void
168SyncLogic::stop()
169{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800170 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800171 m_scheduler.cancel (REEXPRESSING_INTEREST);
172 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
173}
174
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700175/**
176 * Two types of intersts
177 *
178 * Normal name: .../<hash>
179 * Recovery name: .../recovery/<hash>
180 */
181boost::tuple<DigestConstPtr, std::string>
182SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700183{
Yingdi Yu43e71612013-10-30 22:19:31 -0700184 BOOST_ASSERT (name.find (m_syncPrefix.toUri()) == 0);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700185
Yingdi Yu43e71612013-10-30 22:19:31 -0700186 string hash = name.substr (m_syncPrefix.toUri().size (), name.size ()-m_syncPrefix.toUri().size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700187 if (hash[0] == '/')
188 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700189 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700190
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700191 size_t pos = hash.find ('/');
192 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700193 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700194 interestType = hash.substr (0, pos);
195 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700196 }
197
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700198 _LOG_TRACE (hash << ", " << interestType);
199
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800200 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700201 istringstream is (hash);
202 is >> *digest;
203
204 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700205}
206
207void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800208SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix,
209 const shared_ptr<const ndn::Interest>& interest,
210 Transport& transport,
211 uint64_t registeredPrefixId)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700212{
Yingdi Yu43e71612013-10-30 22:19:31 -0700213 _LOG_DEBUG("respondSyncInterest: " << interest->getName().toUri());
214 string name = interest->getName().toUri();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700215 try
216 {
217 _LOG_TRACE ("<< I " << name);
218
219 DigestConstPtr digest;
220 string type;
221 tie (digest, type) = convertNameToDigestAndType (name);
222
223 if (type == "normal") // kind of ineffective...
224 {
225 processSyncInterest (name, digest);
226 }
227 else if (type == "recovery")
228 {
229 processSyncRecoveryInterest (name, digest);
230 }
231 }
232 catch (Error::DigestCalculationError &e)
233 {
234 _LOG_TRACE ("Something fishy happened...");
235 // log error. ignoring it for now, later we should log it
236 return ;
237 }
238}
239
240void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800241SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix)
242{
243 _LOG_DEBUG("Sync prefix registration failed!");
244}
245
246void
247SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest,
248 const shared_ptr<Data>& data,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800249 const OnVerified& onVerified,
250 const OnVerifyFailed& onVerifyFailed)
251{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800252 m_verifier->verifyData(data, onVerified, onVerifyFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800253}
254
255void
256SyncLogic::onSyncDataTimeout(const shared_ptr<const ndn::Interest>& interest,
257 int retry,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800258 const OnVerified& onVerified,
259 const OnVerifyFailed& onVerifyFailed)
260{
261 if(retry > 0)
262 {
263 m_face->expressInterest(*interest,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800264 func_lib::bind(&SyncLogic::onSyncData,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800265 this,
266 _1,
267 _2,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800268 onVerified,
269 onVerifyFailed),
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800270 func_lib::bind(&SyncLogic::onSyncDataTimeout,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800271 this,
272 _1,
273 retry - 1,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800274 onVerified,
275 onVerifyFailed));
276 }
277 else
278 _LOG_DEBUG("Sync interest eventually times out!");
279}
280
281void
282SyncLogic::onSyncDataVerifyFailed(const shared_ptr<Data>& data)
283{
284 _LOG_DEBUG("Sync data cannot be verified!");
285}
286
287void
288SyncLogic::onSyncDataVerified(const shared_ptr<Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700289{
Yingdi Yu43e71612013-10-30 22:19:31 -0700290 string name = data->getName().toUri();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800291 const char* wireData = (const char*)data->getContent().value();
292 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700293
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700294 try
295 {
296 _LOG_TRACE ("<< D " << name);
297
298 DigestConstPtr digest;
299 string type;
300 tie (digest, type) = convertNameToDigestAndType (name);
301
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700302 if (type == "normal")
303 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700304 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700305 }
306 else
307 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700308 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700309 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700310 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700311 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700312 }
313 catch (Error::DigestCalculationError &e)
314 {
315 _LOG_TRACE ("Something fishy happened...");
316 // log error. ignoring it for now, later we should log it
317 return;
318 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700319}
320
Yingdi Yu43e71612013-10-30 22:19:31 -0700321void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700322SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700323{
Yingdi Yu43e71612013-10-30 22:19:31 -0700324 _LOG_DEBUG("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700325 DigestConstPtr rootDigest;
326 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800327 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700328 rootDigest = m_state->getDigest();
329 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700330
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700331 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700332 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700333 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700334
335 SyncStateMsg ssm;
336 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800337 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700338 ssm << (*m_state);
339 }
340 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700341 return;
342 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700343
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700344 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700345 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700346 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700347 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700348 return;
349 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700350
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700351 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
352
353 if (stateInDiffLog != m_log.end ())
354 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700355 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700356
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700357 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700358 return;
359 }
360
361 if (!timedProcessing)
362 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700363 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700364 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700365 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700366 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
367 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700368 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700369
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700370 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
371 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700372
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700373 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800374 func_lib::bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700375 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700376 }
377 else
378 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700379 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700380
381 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700382 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700383 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800384}
385
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800386void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700387SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800388{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800389 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700390 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700391
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700392 try
393 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700394
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700395 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700396
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700397 ownInterestSatisfied = (name == m_outstandingInterestName);
398
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700399 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700400 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700401 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700402 {
403 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700404 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700405 }
406 msg >> diff;
407
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700408 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700409 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700410 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700411 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
412 BOOST_ASSERT (diffLeaf != 0);
413
414 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700415 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700416 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700417 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700418
419 bool inserted = false;
420 bool updated = false;
421 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700422 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800423 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700424 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
425 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800426
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700427 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700428 {
429 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700430 if (!oldSeq.isValid())
431 {
432 oldSeq = SeqNo(seq.getSession(), 0);
433 }
434 else
435 {
436 ++oldSeq;
437 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700438 // there is no need for application to process update on forwarder node
439 if (info->toString() != forwarderPrefix)
440 {
441 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700442 if (m_perBranch)
443 {
444 ostringstream interestName;
445 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
446 m_onUpdateBranch(interestName.str());
447 }
448 else
449 {
450 v.push_back(mdi);
451 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700452 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700453 }
454 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700455 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700456 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800457 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700458 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700459 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700460 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700461 if (!m_perBranch)
462 {
463 m_onRemove (info->toString ());
464 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700465 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700466 }
467 else
468 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700469 }
470 }
471
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700472 if (!v.empty())
473 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700474 if (!m_perBranch)
475 {
476 m_onUpdate(v);
477 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700478 }
479
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700480 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700481 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700482 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700483 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700484 _LOG_TRACE ("Something really fishy happened during state decoding " <<
485 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700486 diffLog.reset ();
487 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700488 }
489
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700490 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
491 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700492 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700493 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700494
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700495 // this is kind of wrong
496 // 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 -0700497
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700498 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700499 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700500 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800501 func_lib::bind (&SyncLogic::sendSyncInterest, this),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700502 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700503 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800504}
505
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800506void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700507SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800508{
Yingdi Yu43e71612013-10-30 22:19:31 -0700509 _LOG_DEBUG("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700510 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
511
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700512 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700513 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700514 _LOG_TRACE ("Could not find " << *digest << " in digest log");
515 return;
516 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700517
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700518 SyncStateMsg ssm;
519 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800520 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700521 ssm << (*m_state);
522 }
523 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700524}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700525
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700526void
527SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
528{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800529 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700530 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800531 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700532 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
533 {
534 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
535 /// @todo Impose limit on how many state info should be send out
536 }
537 }
538
539 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800540 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700541 uint32_t counter = 0;
542 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700543 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800544 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700545
546 if (!interest.m_unknown)
547 {
548 _LOG_TRACE (">> D " << interest.m_name);
549 sendSyncData (interest.m_name, interest.m_digest, diffLog);
550 }
551 else
552 {
553 _LOG_TRACE (">> D (unknown)" << interest.m_name);
554 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
555 }
556 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700557 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700558 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700559 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700560 catch (Error::InterestTableIsEmpty &e)
561 {
562 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800563 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800564}
565
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800566void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700567SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700568{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700569 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700570 if (m_log.size () > 0)
571 {
572 m_log.get<sequenced> ().front ()->setNext (diffLog);
573 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700574 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 -0700575 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700576 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700577}
578
579void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700580SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
581{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700582 DiffStatePtr diff;
583 {
584 //cout << "Add local names" <<endl;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800585 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700586 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700587
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700588 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700589
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700590 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700591 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700592
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700593 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700594
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800595 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700596 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700597 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700598 }
599
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700600 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
601 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700602}
603
604void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700605SyncLogic::remove(const string &prefix)
606{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700607 DiffStatePtr diff;
608 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800609 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700610 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700611 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700612
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700613 // increment the sequence number for the forwarder node
614 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700615
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700616 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
617 SeqNo seqNo (0);
618 if (item != m_state->getLeaves ().end ())
619 {
620 seqNo = (*item)->getSeq ();
621 ++seqNo;
622 }
623 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700624
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800625 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700626 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700627 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700628
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700629 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700630 }
631
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700632 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700633}
634
635void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800636SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800637{
Yingdi Yu43e71612013-10-30 22:19:31 -0700638 _LOG_DEBUG("sendSyncInterest");
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800639 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800640
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700641 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800642 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700643
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700644 os << m_syncPrefix << "/" << *m_state->getDigest();
645 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700646 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700647 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700648
Yingdi Yu43e71612013-10-30 22:19:31 -0700649 _LOG_DEBUG("sendSyncInterest: " << os.str());
650
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700651 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700652 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800653 func_lib::bind (&SyncLogic::sendSyncInterest, this),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700654 REEXPRESSING_INTEREST);
Yingdi Yu43e71612013-10-30 22:19:31 -0700655
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800656 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
657
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800658 OnVerified onVerified = func_lib::bind(&SyncLogic::onSyncDataVerified, this, _1);
659 OnVerifyFailed onVerifyFailed = func_lib::bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800660
661 m_face->expressInterest(*interest,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800662 func_lib::bind(&SyncLogic::onSyncData, this, _1, _2, onVerified, onVerifyFailed),
663 func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, onVerified, onVerifyFailed));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800664}
665
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700666void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700667SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
668{
669 ostringstream os;
670 os << m_syncPrefix << "/recovery/" << *digest;
671 _LOG_TRACE (">> I " << os.str ());
672
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700673 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
674 m_recoveryRetransmissionInterval <<= 1;
675
676 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
677 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
678 {
679 m_scheduler.schedule (nextRetransmission,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800680 func_lib::bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700681 REEXPRESSING_RECOVERY_INTEREST);
682 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700683
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800684 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
685
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800686 OnVerified onVerified = func_lib::bind(&SyncLogic::onSyncDataVerified, this, _1);
687 OnVerifyFailed onVerifyFailed = func_lib::bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800688
689 m_face->expressInterest(*interest,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800690 func_lib::bind(&SyncLogic::onSyncData, this, _1, _2, onVerified, onVerifyFailed),
691 func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, onVerified, onVerifyFailed));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700692}
693
694
695void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700696SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
697{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700698 SyncStateMsg msg;
699 msg << (*state);
700 sendSyncData(name, digest, msg);
701}
702
703// pass in state msg instead of state, so that there is no need to lock the state until
704// this function returns
705void
706SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm)
707{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700708 _LOG_TRACE (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700709 int size = ssm.ByteSize();
710 char *wireData = new char[size];
Yingdi Yu43e71612013-10-30 22:19:31 -0700711 Name dataName(name);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800712 Name signingIdentity = m_policy->inferSigningIdentity(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800713
714 shared_ptr<Data> syncData = make_shared<Data>(dataName);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800715 syncData->setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800716
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800717 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(signingIdentity);
718 m_keyChain->sign(*syncData, certificateName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800719
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800720 m_face->put(*syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800721
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700722 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700723
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700724 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700725 bool satisfiedOwnInterest = false;
726 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800727 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700728 satisfiedOwnInterest = (m_outstandingInterestName == name);
729 }
730
731 if (satisfiedOwnInterest)
732 {
733 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
734
735 m_scheduler.cancel (REEXPRESSING_INTEREST);
736 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
737 bind (&SyncLogic::sendSyncInterest, this),
738 REEXPRESSING_INTEREST);
739 }
740}
741
Zhenkai Zhue5660932012-06-04 15:25:20 -0700742string
743SyncLogic::getRootDigest()
744{
745 ostringstream os;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800746 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhue5660932012-06-04 15:25:20 -0700747 os << *m_state->getDigest();
748 return os.str();
749}
750
Alexander Afanasyev80237382012-10-04 10:20:47 -0700751size_t
752SyncLogic::getNumberOfBranches () const
753{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800754 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700755 return m_state->getLeaves ().size ();
756}
757
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700758void
759SyncLogic::printState () const
760{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800761 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700762
763 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
764 {
765 std::cout << *leaf << std::endl;
766 }
767}
768
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700769std::map<std::string, bool>
770SyncLogic::getBranchPrefixes() const
771{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800772 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700773
774 std::map<std::string, bool> m;
775
776 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
777 {
778 std::string prefix = leaf->getInfo()->toString();
779 // do not return forwarder prefix
780 if (prefix != forwarderPrefix)
781 {
782 m.insert(pair<std::string, bool>(prefix, false));
783 }
784 }
785
786 return m;
787}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700788
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800789}