blob: 5bbd83ce3198ad24d558c3471cc02f768fd2ebcd [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
Chaoyi Bian11f294f2012-03-08 14:28:06 -080024#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080025#include "sync-diff-leaf.h"
26#include "sync-full-leaf.h"
Alexander Afanasyevce001692013-07-14 11:34:41 -070027#include "sync-logging.h"
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070028#include "sync-state.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070029
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080030#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070031#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080032#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080033
34using namespace std;
Yingdi Yu43e71612013-10-30 22:19:31 -070035using namespace ndn;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080036
Chaoyi Bianf007f922012-04-09 20:05:37 -070037INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070038
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080039
40#ifdef _DEBUG
Alexander Afanasyev531803b2014-02-05 15:57:35 -080041#define _LOG_DEBUG_ID(v) _LOG_DEBUG(m_instanceId << " " << v)
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070042#else
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080043#define _LOG_DEBUG_ID(v) _LOG_DEBUG(v)
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070044#endif
45
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080046#define GET_RANDOM(var) var ()
47
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070048#define TIME_SECONDS_WITH_JITTER(sec) \
Alexander Afanasyev531803b2014-02-05 15:57:35 -080049 (time::seconds(sec) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070050
Alexander Afanasyev42612262012-05-05 10:58:37 -070051#define TIME_MILLISECONDS_WITH_JITTER(ms) \
Alexander Afanasyev531803b2014-02-05 15:57:35 -080052 (time::seconds(ms) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070053
Alexander Afanasyev531803b2014-02-05 15:57:35 -080054namespace Sync {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070055
Alexander Afanasyev531803b2014-02-05 15:57:35 -080056using ndn::shared_ptr;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080058int SyncLogic::m_instanceCounter = 0;
Yingdi Yu43e71612013-10-30 22:19:31 -070059
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080060SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu0eee6002014-02-11 15:54:17 -080061 const Name& identity,
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080062 shared_ptr<Validator> validator,
63 shared_ptr<Face> face,
64 LogicUpdateCallback onUpdate,
65 LogicRemoveCallback onRemove)
66 : m_state (new FullState)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080067 , m_syncInterestTable (*face->ioService(), time::seconds(m_syncInterestReexpress))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080068 , m_syncPrefix (syncPrefix)
Yingdi Yu0eee6002014-02-11 15:54:17 -080069 , m_identity (identity)
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080070 , m_onUpdate (onUpdate)
71 , m_onRemove (onRemove)
72 , m_perBranch (false)
73 , m_validator(validator)
74 , m_keyChain(new KeyChain())
75 , m_face(face)
76 , m_scheduler(*face->ioService())
Alexander Afanasyev531803b2014-02-05 15:57:35 -080077 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080078 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
79 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
80 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
81{
Yingdi Yu280bb962014-01-30 09:52:43 -080082 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
83 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -080084 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Yingdi Yu280bb962014-01-30 09:52:43 -080085
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080086
Yingdi Yu280bb962014-01-30 09:52:43 -080087 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
88 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080089
90 m_instanceId = string("Instance " + boost::lexical_cast<string>(m_instanceCounter++) + " ");
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080091}
92
Yingdi Yu43e71612013-10-30 22:19:31 -070093SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu0eee6002014-02-11 15:54:17 -080094 const Name& identity,
Yingdi Yu280bb962014-01-30 09:52:43 -080095 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080096 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070097 LogicPerBranchCallback onUpdateBranch)
98 : m_state (new FullState)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080099 , m_syncInterestTable (*face->ioService(), time::seconds (m_syncInterestReexpress))
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700100 , m_syncPrefix (syncPrefix)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800101 , m_identity (identity)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700102 , m_onUpdateBranch (onUpdateBranch)
103 , m_perBranch(true)
Yingdi Yu280bb962014-01-30 09:52:43 -0800104 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800105 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +0800106 , m_face(face)
Yingdi Yu280bb962014-01-30 09:52:43 -0800107 , m_scheduler(*face->ioService())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700108 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -0800109 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
110 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Alexander Afanasyevce001692013-07-14 11:34:41 -0700111 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700112{
Yingdi Yu280bb962014-01-30 09:52:43 -0800113 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
114 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -0800115 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700116
Yingdi Yu280bb962014-01-30 09:52:43 -0800117 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
118 bind (&SyncLogic::sendSyncInterest, this));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700119}
120
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800121SyncLogic::~SyncLogic ()
Yingdi Yu280bb962014-01-30 09:52:43 -0800122{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800123 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu280bb962014-01-30 09:52:43 -0800124 m_scheduler.cancelEvent (m_reexpressingInterestId);
125 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800126}
127
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700128/**
129 * Two types of intersts
130 *
131 * Normal name: .../<hash>
132 * Recovery name: .../recovery/<hash>
133 */
134boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800135SyncLogic::convertNameToDigestAndType (const Name &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700136{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800137 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700138
Yingdi Yu6d638f02014-01-24 11:01:21 -0800139 int nameLengthDiff = name.size() - m_syncPrefix.size();
140 BOOST_ASSERT (nameLengthDiff > 0);
141 BOOST_ASSERT (nameLengthDiff < 3);
142
143 string hash = name.get(-1).toEscapedString();
144 string interestType;
145
146 if(nameLengthDiff == 1)
147 interestType = "normal";
148 else
149 interestType = name.get(-2).toEscapedString();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700150
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800151 _LOG_DEBUG_ID (hash << ", " << interestType);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700152
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800153 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700154 istringstream is (hash);
155 is >> *digest;
156
157 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700158}
159
160void
Yingdi Yu51c80252014-02-10 19:32:05 -0800161SyncLogic::onSyncInterest (const Name& prefix, const ndn::Interest& interest)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700162{
Yingdi Yu51c80252014-02-10 19:32:05 -0800163 Name name = interest.getName();
Yingdi Yu6d638f02014-01-24 11:01:21 -0800164
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800165 _LOG_DEBUG_ID("respondSyncInterest: " << name);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800166
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700167 try
168 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800169 _LOG_DEBUG_ID ("<< I " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700170
Yingdi Yu0eee6002014-02-11 15:54:17 -0800171 if(name.get(m_syncPrefix.size()).toEscapedString() == "intro-cert")
172 // it is a certificate, validator will take care of it.
173 return;
174
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700175 DigestConstPtr digest;
176 string type;
177 tie (digest, type) = convertNameToDigestAndType (name);
178
179 if (type == "normal") // kind of ineffective...
180 {
181 processSyncInterest (name, digest);
182 }
183 else if (type == "recovery")
184 {
185 processSyncRecoveryInterest (name, digest);
186 }
187 }
188 catch (Error::DigestCalculationError &e)
189 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800190 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700191 // log error. ignoring it for now, later we should log it
192 return ;
193 }
194}
195
196void
Yingdi Yu51c80252014-02-10 19:32:05 -0800197SyncLogic::onSyncRegisterFailed(const Name& prefix, const string& msg)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800198{
Yingdi Yu51c80252014-02-10 19:32:05 -0800199 _LOG_DEBUG_ID("Sync prefix registration failed! " << msg);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800200}
201
202void
Yingdi Yu51c80252014-02-10 19:32:05 -0800203SyncLogic::onSyncData(const ndn::Interest& interest, Data& data)
204{
205 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
206 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
207 m_validator->validate(data, onValidated, onValidationFailed);
208}
Yingdi Yu280bb962014-01-30 09:52:43 -0800209
210void
Yingdi Yu51c80252014-02-10 19:32:05 -0800211SyncLogic::onSyncTimeout(const ndn::Interest& interest)
Yingdi Yu280bb962014-01-30 09:52:43 -0800212{
213 // It is OK. Others will handle the time out situation.
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800214}
215
216void
Yingdi Yu280bb962014-01-30 09:52:43 -0800217SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800218{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800219 _LOG_DEBUG_ID("Sync data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800220}
221
222void
Yingdi Yu280bb962014-01-30 09:52:43 -0800223SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700224{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800225 Name name = data->getName();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800226 const char* wireData = (const char*)data->getContent().value();
227 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700228
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700229 try
230 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800231 _LOG_DEBUG_ID ("<< D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700232
233 DigestConstPtr digest;
234 string type;
235 tie (digest, type) = convertNameToDigestAndType (name);
236
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700237 if (type == "normal")
238 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700239 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700240 }
241 else
242 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700243 // timer is always restarted when we schedule recovery
Yingdi Yu280bb962014-01-30 09:52:43 -0800244 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700245 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700246 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700247 }
248 catch (Error::DigestCalculationError &e)
249 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800250 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700251 // log error. ignoring it for now, later we should log it
252 return;
253 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700254}
255
Yingdi Yu43e71612013-10-30 22:19:31 -0700256void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800257SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700258{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800259 _LOG_DEBUG_ID("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700260 DigestConstPtr rootDigest;
261 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700262 rootDigest = m_state->getDigest();
263 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700264
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700265 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700266 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700267 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700268
269 SyncStateMsg ssm;
270 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700271 ssm << (*m_state);
272 }
273 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700274 return;
275 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700276
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700277 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700278 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800279 _LOG_DEBUG_ID ("processSyncInterest (): Same state. Adding to PIT");
Yingdi Yu6d638f02014-01-24 11:01:21 -0800280 m_syncInterestTable.insert (digest, name.toUri(), false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700281 return;
282 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700283
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700284 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
285
286 if (stateInDiffLog != m_log.end ())
287 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700288 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700289
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700290 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700291 return;
292 }
293
294 if (!timedProcessing)
295 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800296 bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700297 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700298 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800299 _LOG_DEBUG_ID ("Unknown digest, but somebody may have already replied, so restart our timer");
Yingdi Yu280bb962014-01-30 09:52:43 -0800300 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700301 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700302
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700303 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800304 _LOG_DEBUG_ID ("Digest is not in the log. Schedule processing after small delay: " << time::milliseconds (waitDelay));
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700305
Yingdi Yu280bb962014-01-30 09:52:43 -0800306 m_delayedInterestProcessingId = m_scheduler.scheduleEvent (time::milliseconds (waitDelay),
307 bind (&SyncLogic::processSyncInterest, this, name, digest, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700308 }
309 else
310 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800311 _LOG_DEBUG_ID (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700312
313 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700314 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700315 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800316}
317
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800318void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800319SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800320{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800321 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700322 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700323
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700324 try
325 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700326
Yingdi Yu6d638f02014-01-24 11:01:21 -0800327 m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700328
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700329 ownInterestSatisfied = (name == m_outstandingInterestName);
330
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700331 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700332 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700333 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700334 {
335 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700336 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700337 }
338 msg >> diff;
339
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700340 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700341 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700342 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800343 DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700344 BOOST_ASSERT (diffLeaf != 0);
345
346 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700347 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700348 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700349 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700350
351 bool inserted = false;
352 bool updated = false;
353 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700354 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800355 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700356 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800357
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700358 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700359 {
360 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700361 if (!oldSeq.isValid())
362 {
363 oldSeq = SeqNo(seq.getSession(), 0);
364 }
365 else
366 {
367 ++oldSeq;
368 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700369 // there is no need for application to process update on forwarder node
370 if (info->toString() != forwarderPrefix)
371 {
372 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800373 {
374 ostringstream interestName;
375 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
376 _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str());
377 }
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700378 if (m_perBranch)
379 {
380 ostringstream interestName;
381 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
382 m_onUpdateBranch(interestName.str());
383 }
384 else
385 {
386 v.push_back(mdi);
387 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700388 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700389 }
390 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700391 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700392 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700393 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700394 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700395 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700396 if (!m_perBranch)
397 {
398 m_onRemove (info->toString ());
399 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700400 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700401 }
402 else
403 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700404 }
405 }
406
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700407 if (!v.empty())
408 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700409 if (!m_perBranch)
410 {
411 m_onUpdate(v);
412 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700413 }
414
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700415 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700416 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700417 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700418 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800419 _LOG_DEBUG_ID ("Something really fishy happened during state decoding " <<
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700420 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700421 diffLog.reset ();
422 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700423 }
424
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700425 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
426 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700427 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800428 _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700429 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700430
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700431 // this is kind of wrong
432 // 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 -0700433
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700434 // if state has changed, then it is safe to express a new interest
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800435 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800436 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800437 EventId eventId = m_scheduler.scheduleEvent (after,
438 bind (&SyncLogic::sendSyncInterest, this));
439
Yingdi Yu280bb962014-01-30 09:52:43 -0800440 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800441 m_reexpressingInterestId = eventId;
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700442 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800443}
444
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800445void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800446SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800447{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800448 _LOG_DEBUG_ID("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700449 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
450
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700451 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700452 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800453 _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700454 return;
455 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700456
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700457 SyncStateMsg ssm;
458 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700459 ssm << (*m_state);
460 }
461 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700462}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700463
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700464void
465SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
466{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800467 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700468 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700469 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
470 {
471 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
472 /// @todo Impose limit on how many state info should be send out
473 }
474 }
475
476 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800477 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700478 uint32_t counter = 0;
479 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700480 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800481 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700482
483 if (!interest.m_unknown)
484 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800485 _LOG_DEBUG_ID (">> D " << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700486 sendSyncData (interest.m_name, interest.m_digest, diffLog);
487 }
488 else
489 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800490 _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700491 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
492 }
493 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700494 }
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800495 _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700496 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700497 catch (Error::InterestTableIsEmpty &e)
498 {
499 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800500 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800501}
502
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800503void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700504SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700505{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700506 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700507 if (m_log.size () > 0)
508 {
509 m_log.get<sequenced> ().front ()->setNext (diffLog);
510 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700511 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 -0700512 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700513 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700514}
515
516void
Yingdi Yu280bb962014-01-30 09:52:43 -0800517SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700518{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700519 DiffStatePtr diff;
520 {
521 //cout << "Add local names" <<endl;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800522 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700523
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800524 _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700525
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700526 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700527 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700528
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800529 _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700530
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800531 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700532 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700533 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700534 }
535
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800536 // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700537 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700538}
539
540void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800541SyncLogic::remove(const Name &prefix)
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700542{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700543 DiffStatePtr diff;
544 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800545 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700546 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700547
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700548 // increment the sequence number for the forwarder node
549 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700550
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700551 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
552 SeqNo seqNo (0);
553 if (item != m_state->getLeaves ().end ())
554 {
555 seqNo = (*item)->getSeq ();
556 ++seqNo;
557 }
558 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700559
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800560 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700561 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700562 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700563
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700564 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700565 }
566
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700567 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700568}
569
570void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800571SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800572{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800573 _LOG_DEBUG_ID("sendSyncInterest");
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800574
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700575 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800576 m_outstandingInterestName = m_syncPrefix;
577 ostringstream os;
578 os << *m_state->getDigest();
579 m_outstandingInterestName.append(os.str());
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800580 _LOG_DEBUG_ID (">> I " << m_outstandingInterestName);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700581 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700582
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800583 _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700584
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800585 EventId eventId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
586 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800587 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800588 m_reexpressingInterestId = eventId;
Yingdi Yu43e71612013-10-30 22:19:31 -0700589
Yingdi Yu6d638f02014-01-24 11:01:21 -0800590 ndn::Interest interest(m_outstandingInterestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800591 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800592
Yingdi Yu6d638f02014-01-24 11:01:21 -0800593 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800594 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800595 bind(&SyncLogic::onSyncTimeout, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800596}
597
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700598void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700599SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
600{
601 ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800602 os << *digest;
603
604 Name interestName = m_syncPrefix;
605 interestName.append("recovery").append(os.str());
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700606
Yingdi Yu280bb962014-01-30 09:52:43 -0800607 time::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
608
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700609 m_recoveryRetransmissionInterval <<= 1;
610
Yingdi Yu280bb962014-01-30 09:52:43 -0800611 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700612 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
Yingdi Yu280bb962014-01-30 09:52:43 -0800613 m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission,
614 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700615
Yingdi Yu6d638f02014-01-24 11:01:21 -0800616 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800617 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800618
Yingdi Yu6d638f02014-01-24 11:01:21 -0800619 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800620 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800621 bind(&SyncLogic::onSyncTimeout, this, _1));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700622}
623
624
625void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800626SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700627{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700628 SyncStateMsg msg;
629 msg << (*state);
630 sendSyncData(name, digest, msg);
631}
632
633// pass in state msg instead of state, so that there is no need to lock the state until
634// this function returns
635void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800636SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700637{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800638 _LOG_DEBUG_ID (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700639 int size = ssm.ByteSize();
640 char *wireData = new char[size];
Yingdi Yu280bb962014-01-30 09:52:43 -0800641 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800642
Yingdi Yu6d638f02014-01-24 11:01:21 -0800643 Data syncData(name);
644 syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800645 syncData.setFreshnessPeriod(m_syncResponseFreshness);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800646
Yingdi Yu0eee6002014-02-11 15:54:17 -0800647 m_keyChain->signByIdentity(syncData, m_identity);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800648
Yingdi Yu6d638f02014-01-24 11:01:21 -0800649 m_face->put(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800650
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700651 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700652
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700653 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700654 bool satisfiedOwnInterest = false;
655 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700656 satisfiedOwnInterest = (m_outstandingInterestName == name);
657 }
658
659 if (satisfiedOwnInterest)
660 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800661 _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700662
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800663 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800664 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800665 EventId eventId = m_scheduler.scheduleEvent (after,
666 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800667 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800668 m_reexpressingInterestId = eventId;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700669 }
670}
671
Zhenkai Zhue5660932012-06-04 15:25:20 -0700672string
673SyncLogic::getRootDigest()
674{
675 ostringstream os;
Zhenkai Zhue5660932012-06-04 15:25:20 -0700676 os << *m_state->getDigest();
677 return os.str();
678}
679
Alexander Afanasyev80237382012-10-04 10:20:47 -0700680size_t
681SyncLogic::getNumberOfBranches () const
682{
Alexander Afanasyev80237382012-10-04 10:20:47 -0700683 return m_state->getLeaves ().size ();
684}
685
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700686void
687SyncLogic::printState () const
688{
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700689 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
690 {
691 std::cout << *leaf << std::endl;
692 }
693}
694
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700695std::map<std::string, bool>
696SyncLogic::getBranchPrefixes() const
697{
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700698 std::map<std::string, bool> m;
699
700 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
701 {
702 std::string prefix = leaf->getInfo()->toString();
703 // do not return forwarder prefix
704 if (prefix != forwarderPrefix)
705 {
706 m.insert(pair<std::string, bool>(prefix, false));
707 }
708 }
709
710 return m;
711}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700712
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800713}