blob: 293d90cd30e5976c4905643a60fa64a97dde6e92 [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>
Yingdi Yu6d638f02014-01-24 11:01:21 -080021 * Yingdi Yu <yingdi@cs.ucla.edu>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080022 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080023
Alexander Afanasyevb6cf56e2012-05-29 19:33:49 -070024#ifdef NS3_MODULE
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070025#include <ns3/ccnx-pit.h>
26#include <ns3/ccnx.h>
Alexander Afanasyevb6cf56e2012-05-29 19:33:49 -070027#endif
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070028
Chaoyi Bian11f294f2012-03-08 14:28:06 -080029#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080030#include "sync-diff-leaf.h"
31#include "sync-full-leaf.h"
Alexander Afanasyevce001692013-07-14 11:34:41 -070032#include "sync-logging.h"
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070033#include "sync-state.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070034
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080035#include <boost/make_shared.hpp>
36#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070037#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080038#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080039
40using namespace std;
Yingdi Yu43e71612013-10-30 22:19:31 -070041using namespace ndn;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080042
Chaoyi Bianf007f922012-04-09 20:05:37 -070043INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070044
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070045#ifdef NS3_MODULE
46#define GET_RANDOM(var) var.GetValue ()
47#else
48#define GET_RANDOM(var) var ()
49#endif
50
51#define TIME_SECONDS_WITH_JITTER(sec) \
52 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
53
Alexander Afanasyev42612262012-05-05 10:58:37 -070054#define TIME_MILLISECONDS_WITH_JITTER(ms) \
55 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070056
57
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080058namespace Sync
59{
60
Yingdi Yu43e71612013-10-30 22:19:31 -070061 SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080062 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080063 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070064 LogicUpdateCallback onUpdate,
65 LogicRemoveCallback onRemove)
66 : m_state (new FullState)
67 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
68 , m_syncPrefix (syncPrefix)
69 , m_onUpdate (onUpdate)
70 , m_onRemove (onRemove)
71 , m_perBranch (false)
Yingdi Yu280bb962014-01-30 09:52:43 -080072 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080073 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +080074 , m_face(face)
Yingdi Yu280bb962014-01-30 09:52:43 -080075 , m_scheduler(*face->ioService())
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070076#ifndef NS3_MODULE
Yingdi Yu43e71612013-10-30 22:19:31 -070077 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -080078 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
79 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070080#else
Yingdi Yu43e71612013-10-30 22:19:31 -070081 , m_rangeUniformRandom (200,1000)
82 , m_reexpressionJitter (10,500)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070083#endif
Yingdi Yu43e71612013-10-30 22:19:31 -070084 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070085{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070086#ifndef NS3_MODULE
87 // In NS3 module these functions are moved to StartApplication method
Yingdi Yu43e71612013-10-30 22:19:31 -070088
Yingdi Yu280bb962014-01-30 09:52:43 -080089 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
90 bind(&SyncLogic::onSyncInterest, this, _1, _2),
91 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
92
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080093
Yingdi Yu280bb962014-01-30 09:52:43 -080094 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
95 bind (&SyncLogic::sendSyncInterest, this));
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070096#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080097}
98
Yingdi Yu43e71612013-10-30 22:19:31 -070099SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -0800100 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +0800101 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700102 LogicPerBranchCallback onUpdateBranch)
103 : m_state (new FullState)
104 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
105 , m_syncPrefix (syncPrefix)
106 , m_onUpdateBranch (onUpdateBranch)
107 , m_perBranch(true)
Yingdi Yu280bb962014-01-30 09:52:43 -0800108 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800109 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +0800110 , m_face(face)
Yingdi Yu280bb962014-01-30 09:52:43 -0800111 , m_scheduler(*face->ioService())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700112#ifndef NS3_MODULE
113 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -0800114 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
115 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700116#else
117 , m_rangeUniformRandom (200,1000)
118 , m_reexpressionJitter (10,500)
119#endif
Alexander Afanasyevce001692013-07-14 11:34:41 -0700120 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700121{
122#ifndef NS3_MODULE
123 // In NS3 module these functions are moved to StartApplication method
124
Yingdi Yu280bb962014-01-30 09:52:43 -0800125 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
126 bind(&SyncLogic::onSyncInterest, this, _1, _2),
127 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700128
Yingdi Yu280bb962014-01-30 09:52:43 -0800129 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
130 bind (&SyncLogic::sendSyncInterest, this));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700131#endif
132}
133
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800134SyncLogic::~SyncLogic ()
Yingdi Yu280bb962014-01-30 09:52:43 -0800135{
136 // m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800137}
138
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700139#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700140void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700141SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700142{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700143 m_ccnxHandle->SetNode (GetNode ());
144 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700145
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700146 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800147 func_lib::bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700148
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700149 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800150 func_lib::bind (&SyncLogic::sendSyncInterest, this),
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700151 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700152}
153
154void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700155SyncLogic::StopApplication ()
156{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700157 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700158 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700159 m_scheduler.cancel (REEXPRESSING_INTEREST);
160 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700161}
162#endif
163
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800164void
165SyncLogic::stop()
166{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800167 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu280bb962014-01-30 09:52:43 -0800168 m_scheduler.cancelEvent (m_reexpressingInterestId);
169 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800170}
171
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700172/**
173 * Two types of intersts
174 *
175 * Normal name: .../<hash>
176 * Recovery name: .../recovery/<hash>
177 */
178boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800179SyncLogic::convertNameToDigestAndType (const Name &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700180{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800181 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700182
Yingdi Yu6d638f02014-01-24 11:01:21 -0800183 int nameLengthDiff = name.size() - m_syncPrefix.size();
184 BOOST_ASSERT (nameLengthDiff > 0);
185 BOOST_ASSERT (nameLengthDiff < 3);
186
187 string hash = name.get(-1).toEscapedString();
188 string interestType;
189
190 if(nameLengthDiff == 1)
191 interestType = "normal";
192 else
193 interestType = name.get(-2).toEscapedString();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700194
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700195 _LOG_TRACE (hash << ", " << interestType);
196
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800197 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700198 istringstream is (hash);
199 is >> *digest;
200
201 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700202}
203
204void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800205SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix,
Yingdi Yu280bb962014-01-30 09:52:43 -0800206 const shared_ptr<const ndn::Interest>& interest)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700207{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800208 Name name = interest->getName();
209
210 _LOG_DEBUG("respondSyncInterest: " << name);
211
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700212 try
213 {
214 _LOG_TRACE ("<< I " << name);
215
216 DigestConstPtr digest;
217 string type;
218 tie (digest, type) = convertNameToDigestAndType (name);
219
220 if (type == "normal") // kind of ineffective...
221 {
222 processSyncInterest (name, digest);
223 }
224 else if (type == "recovery")
225 {
226 processSyncRecoveryInterest (name, digest);
227 }
228 }
229 catch (Error::DigestCalculationError &e)
230 {
231 _LOG_TRACE ("Something fishy happened...");
232 // log error. ignoring it for now, later we should log it
233 return ;
234 }
235}
236
237void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800238SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix)
239{
240 _LOG_DEBUG("Sync prefix registration failed!");
241}
242
243void
244SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest,
245 const shared_ptr<Data>& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800246 const OnDataValidated& onValidated,
247 const OnDataValidationFailed& onValidationFailed)
248{ m_validator->validate(data, onValidated, onValidationFailed); }
249
250void
251SyncLogic::onSyncTimeout(const shared_ptr<const ndn::Interest>& interest)
252{
253 // It is OK. Others will handle the time out situation.
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800254}
255
256void
Yingdi Yu280bb962014-01-30 09:52:43 -0800257SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800258{
259 _LOG_DEBUG("Sync data cannot be verified!");
260}
261
262void
Yingdi Yu280bb962014-01-30 09:52:43 -0800263SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700264{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800265 Name name = data->getName();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800266 const char* wireData = (const char*)data->getContent().value();
267 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700268
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700269 try
270 {
271 _LOG_TRACE ("<< D " << name);
272
273 DigestConstPtr digest;
274 string type;
275 tie (digest, type) = convertNameToDigestAndType (name);
276
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700277 if (type == "normal")
278 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700279 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700280 }
281 else
282 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700283 // timer is always restarted when we schedule recovery
Yingdi Yu280bb962014-01-30 09:52:43 -0800284 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700285 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700286 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700287 }
288 catch (Error::DigestCalculationError &e)
289 {
290 _LOG_TRACE ("Something fishy happened...");
291 // log error. ignoring it for now, later we should log it
292 return;
293 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700294}
295
Yingdi Yu43e71612013-10-30 22:19:31 -0700296void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800297SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700298{
Yingdi Yu43e71612013-10-30 22:19:31 -0700299 _LOG_DEBUG("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700300 DigestConstPtr rootDigest;
301 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800302 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700303 rootDigest = m_state->getDigest();
304 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700305
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700306 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700307 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700308 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700309
310 SyncStateMsg ssm;
311 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800312 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700313 ssm << (*m_state);
314 }
315 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700316 return;
317 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700318
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700319 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700320 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700321 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Yingdi Yu6d638f02014-01-24 11:01:21 -0800322 m_syncInterestTable.insert (digest, name.toUri(), false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700323 return;
324 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700325
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700326 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
327
328 if (stateInDiffLog != m_log.end ())
329 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700330 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700331
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700332 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700333 return;
334 }
335
336 if (!timedProcessing)
337 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800338 bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700339 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700340 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700341 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
Yingdi Yu280bb962014-01-30 09:52:43 -0800342 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700343 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700344
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700345 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
346 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700347
Yingdi Yu280bb962014-01-30 09:52:43 -0800348 m_delayedInterestProcessingId = m_scheduler.scheduleEvent (time::milliseconds (waitDelay),
349 bind (&SyncLogic::processSyncInterest, this, name, digest, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700350 }
351 else
352 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700353 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700354
355 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700356 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700357 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800358}
359
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800360void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800361SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800362{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800363 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700364 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700365
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700366 try
367 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700368
Yingdi Yu6d638f02014-01-24 11:01:21 -0800369 m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700370
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700371 ownInterestSatisfied = (name == m_outstandingInterestName);
372
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700373 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700374 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700375 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700376 {
377 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700378 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700379 }
380 msg >> diff;
381
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700382 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700383 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700384 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800385 DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700386 BOOST_ASSERT (diffLeaf != 0);
387
388 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700389 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700390 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700391 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700392
393 bool inserted = false;
394 bool updated = false;
395 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700396 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800397 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu280bb962014-01-30 09:52:43 -0800398 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700399 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800400
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700401 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700402 {
403 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700404 if (!oldSeq.isValid())
405 {
406 oldSeq = SeqNo(seq.getSession(), 0);
407 }
408 else
409 {
410 ++oldSeq;
411 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700412 // there is no need for application to process update on forwarder node
413 if (info->toString() != forwarderPrefix)
414 {
415 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700416 if (m_perBranch)
417 {
418 ostringstream interestName;
419 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
420 m_onUpdateBranch(interestName.str());
421 }
422 else
423 {
424 v.push_back(mdi);
425 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700426 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700427 }
428 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700429 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700430 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800431 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700432 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700433 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700434 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700435 if (!m_perBranch)
436 {
437 m_onRemove (info->toString ());
438 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700439 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700440 }
441 else
442 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700443 }
444 }
445
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700446 if (!v.empty())
447 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700448 if (!m_perBranch)
449 {
450 m_onUpdate(v);
451 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700452 }
453
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700454 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700455 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700456 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700457 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700458 _LOG_TRACE ("Something really fishy happened during state decoding " <<
459 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700460 diffLog.reset ();
461 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700462 }
463
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700464 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
465 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700466 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700467 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700468
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700469 // this is kind of wrong
470 // 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 -0700471
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700472 // if state has changed, then it is safe to express a new interest
Yingdi Yu280bb962014-01-30 09:52:43 -0800473 m_scheduler.cancelEvent (m_reexpressingInterestId);
474 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
475 bind (&SyncLogic::sendSyncInterest, this));
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700476 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800477}
478
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800479void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800480SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800481{
Yingdi Yu43e71612013-10-30 22:19:31 -0700482 _LOG_DEBUG("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700483 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
484
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700485 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700486 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700487 _LOG_TRACE ("Could not find " << *digest << " in digest log");
488 return;
489 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700490
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700491 SyncStateMsg ssm;
492 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800493 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700494 ssm << (*m_state);
495 }
496 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700497}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700498
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700499void
500SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
501{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800502 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700503 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800504 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700505 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
506 {
507 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
508 /// @todo Impose limit on how many state info should be send out
509 }
510 }
511
512 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800513 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700514 uint32_t counter = 0;
515 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700516 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800517 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700518
519 if (!interest.m_unknown)
520 {
521 _LOG_TRACE (">> D " << interest.m_name);
522 sendSyncData (interest.m_name, interest.m_digest, diffLog);
523 }
524 else
525 {
526 _LOG_TRACE (">> D (unknown)" << interest.m_name);
527 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
528 }
529 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700530 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700531 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700532 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700533 catch (Error::InterestTableIsEmpty &e)
534 {
535 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800536 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800537}
538
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800539void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700540SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700541{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700542 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700543 if (m_log.size () > 0)
544 {
545 m_log.get<sequenced> ().front ()->setNext (diffLog);
546 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700547 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 -0700548 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700549 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700550}
551
552void
Yingdi Yu280bb962014-01-30 09:52:43 -0800553SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700554{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700555 DiffStatePtr diff;
556 {
557 //cout << "Add local names" <<endl;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800558 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800559 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700560
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700561 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700562
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700563 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700564 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700565
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700566 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700567
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800568 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700569 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700570 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700571 }
572
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700573 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
574 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700575}
576
577void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800578SyncLogic::remove(const Name &prefix)
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700579{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700580 DiffStatePtr diff;
581 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800582 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800583 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700584 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700585
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700586 // increment the sequence number for the forwarder node
587 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700588
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700589 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
590 SeqNo seqNo (0);
591 if (item != m_state->getLeaves ().end ())
592 {
593 seqNo = (*item)->getSeq ();
594 ++seqNo;
595 }
596 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700597
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800598 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700599 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700600 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700601
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700602 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700603 }
604
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700605 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700606}
607
608void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800609SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800610{
Yingdi Yu43e71612013-10-30 22:19:31 -0700611 _LOG_DEBUG("sendSyncInterest");
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800612
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700613 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800614 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800615 m_outstandingInterestName = m_syncPrefix;
616 ostringstream os;
617 os << *m_state->getDigest();
618 m_outstandingInterestName.append(os.str());
619 _LOG_TRACE (">> I " << m_outstandingInterestName);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700620 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700621
Yingdi Yu6d638f02014-01-24 11:01:21 -0800622 _LOG_DEBUG("sendSyncInterest: " << m_outstandingInterestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700623
Yingdi Yu280bb962014-01-30 09:52:43 -0800624 m_scheduler.cancelEvent (m_reexpressingInterestId);
625 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress)+time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
626 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu43e71612013-10-30 22:19:31 -0700627
Yingdi Yu6d638f02014-01-24 11:01:21 -0800628 ndn::Interest interest(m_outstandingInterestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800629 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800630
Yingdi Yu280bb962014-01-30 09:52:43 -0800631 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
632 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800633
Yingdi Yu6d638f02014-01-24 11:01:21 -0800634 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800635 bind(&SyncLogic::onSyncData, this, _1, _2, onValidated, onValidationFailed),
636 bind(&SyncLogic::onSyncTimeout, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800637}
638
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700639void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700640SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
641{
642 ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800643 os << *digest;
644
645 Name interestName = m_syncPrefix;
646 interestName.append("recovery").append(os.str());
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700647
Yingdi Yu280bb962014-01-30 09:52:43 -0800648 time::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
649
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700650 m_recoveryRetransmissionInterval <<= 1;
651
Yingdi Yu280bb962014-01-30 09:52:43 -0800652 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700653 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
Yingdi Yu280bb962014-01-30 09:52:43 -0800654 m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission,
655 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700656
Yingdi Yu6d638f02014-01-24 11:01:21 -0800657 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800658 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800659
Yingdi Yu280bb962014-01-30 09:52:43 -0800660 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
661 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800662
Yingdi Yu6d638f02014-01-24 11:01:21 -0800663 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800664 bind(&SyncLogic::onSyncData, this, _1, _2, onValidated, onValidationFailed),
665 bind(&SyncLogic::onSyncTimeout, this, _1));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700666}
667
668
669void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800670SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700671{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700672 SyncStateMsg msg;
673 msg << (*state);
674 sendSyncData(name, digest, msg);
675}
676
677// pass in state msg instead of state, so that there is no need to lock the state until
678// this function returns
679void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800680SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700681{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700682 _LOG_TRACE (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700683 int size = ssm.ByteSize();
684 char *wireData = new char[size];
Yingdi Yu280bb962014-01-30 09:52:43 -0800685 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800686
Yingdi Yu6d638f02014-01-24 11:01:21 -0800687 Data syncData(name);
688 syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800689
Yingdi Yu280bb962014-01-30 09:52:43 -0800690 m_keyChain->sign(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800691
Yingdi Yu6d638f02014-01-24 11:01:21 -0800692 m_face->put(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800693
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700694 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700695
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700696 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700697 bool satisfiedOwnInterest = false;
698 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800699 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700700 satisfiedOwnInterest = (m_outstandingInterestName == name);
701 }
702
703 if (satisfiedOwnInterest)
704 {
705 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
706
Yingdi Yu280bb962014-01-30 09:52:43 -0800707 m_scheduler.cancelEvent (m_reexpressingInterestId);
708 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
709 bind (&SyncLogic::sendSyncInterest, this));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700710 }
711}
712
Zhenkai Zhue5660932012-06-04 15:25:20 -0700713string
714SyncLogic::getRootDigest()
715{
716 ostringstream os;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800717 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhue5660932012-06-04 15:25:20 -0700718 os << *m_state->getDigest();
719 return os.str();
720}
721
Alexander Afanasyev80237382012-10-04 10:20:47 -0700722size_t
723SyncLogic::getNumberOfBranches () const
724{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800725 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700726 return m_state->getLeaves ().size ();
727}
728
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700729void
730SyncLogic::printState () const
731{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800732 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700733
734 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
735 {
736 std::cout << *leaf << std::endl;
737 }
738}
739
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700740std::map<std::string, bool>
741SyncLogic::getBranchPrefixes() const
742{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800743 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700744
745 std::map<std::string, bool> m;
746
747 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
748 {
749 std::string prefix = leaf->getInfo()->toString();
750 // do not return forwarder prefix
751 if (prefix != forwarderPrefix)
752 {
753 m.insert(pair<std::string, bool>(prefix, false));
754 }
755 }
756
757 return m;
758}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700759
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800760}