blob: 5d201438d87861becbd0a398bcbedacd5061a6ad [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 Yu3da10fe2014-02-27 16:37:34 -080061 const IdentityCertificate& myCertificate,
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 Yu3da10fe2014-02-27 16:37:34 -080069 , m_myCertificate(myCertificate)
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080070 , m_onUpdate (onUpdate)
71 , m_onRemove (onRemove)
72 , m_perBranch (false)
73 , m_validator(validator)
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080074 , m_face(face)
75 , m_scheduler(*face->ioService())
Alexander Afanasyev531803b2014-02-05 15:57:35 -080076 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080077 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
78 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
79 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
80{
Yingdi Yu280bb962014-01-30 09:52:43 -080081 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
82 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -080083 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Yingdi Yu280bb962014-01-30 09:52:43 -080084
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080085
Yingdi Yu280bb962014-01-30 09:52:43 -080086 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
87 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080088
89 m_instanceId = string("Instance " + boost::lexical_cast<string>(m_instanceCounter++) + " ");
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080090}
91
Yingdi Yu43e71612013-10-30 22:19:31 -070092SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080093 const IdentityCertificate& myCertificate,
Yingdi Yu280bb962014-01-30 09:52:43 -080094 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080095 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070096 LogicPerBranchCallback onUpdateBranch)
97 : m_state (new FullState)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080098 , m_syncInterestTable (*face->ioService(), time::seconds (m_syncInterestReexpress))
Zhenkai Zhub9f19592012-10-16 14:27:38 -070099 , m_syncPrefix (syncPrefix)
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800100 , m_myCertificate(myCertificate)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700101 , m_onUpdateBranch (onUpdateBranch)
102 , m_perBranch(true)
Yingdi Yu280bb962014-01-30 09:52:43 -0800103 , m_validator(validator)
Yingdi Yu6e235db2013-12-27 08:40:53 +0800104 , m_face(face)
Yingdi Yu280bb962014-01-30 09:52:43 -0800105 , m_scheduler(*face->ioService())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700106 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -0800107 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
108 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Alexander Afanasyevce001692013-07-14 11:34:41 -0700109 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700110{
Yingdi Yu280bb962014-01-30 09:52:43 -0800111 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
112 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -0800113 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700114
Yingdi Yu280bb962014-01-30 09:52:43 -0800115 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
116 bind (&SyncLogic::sendSyncInterest, this));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700117}
118
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800119SyncLogic::~SyncLogic ()
Yingdi Yu280bb962014-01-30 09:52:43 -0800120{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800121 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu280bb962014-01-30 09:52:43 -0800122 m_scheduler.cancelEvent (m_reexpressingInterestId);
123 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800124}
125
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700126/**
127 * Two types of intersts
128 *
129 * Normal name: .../<hash>
130 * Recovery name: .../recovery/<hash>
131 */
132boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800133SyncLogic::convertNameToDigestAndType (const Name &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700134{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800135 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700136
Yingdi Yu6d638f02014-01-24 11:01:21 -0800137 int nameLengthDiff = name.size() - m_syncPrefix.size();
138 BOOST_ASSERT (nameLengthDiff > 0);
139 BOOST_ASSERT (nameLengthDiff < 3);
140
141 string hash = name.get(-1).toEscapedString();
142 string interestType;
143
144 if(nameLengthDiff == 1)
145 interestType = "normal";
146 else
147 interestType = name.get(-2).toEscapedString();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700148
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800149 _LOG_DEBUG_ID (hash << ", " << interestType);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700150
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800151 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700152 istringstream is (hash);
153 is >> *digest;
154
155 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700156}
157
158void
Yingdi Yu51c80252014-02-10 19:32:05 -0800159SyncLogic::onSyncInterest (const Name& prefix, const ndn::Interest& interest)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700160{
Yingdi Yu51c80252014-02-10 19:32:05 -0800161 Name name = interest.getName();
Yingdi Yu6d638f02014-01-24 11:01:21 -0800162
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800163 _LOG_DEBUG_ID("respondSyncInterest: " << name);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800164
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700165 try
166 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800167 _LOG_DEBUG_ID ("<< I " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700168
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800169 if(name.get(m_syncPrefix.size()).toEscapedString() == "CHRONOS-INTRO-CERT")
Yingdi Yu0eee6002014-02-11 15:54:17 -0800170 // it is a certificate, validator will take care of it.
171 return;
172
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700173 DigestConstPtr digest;
174 string type;
175 tie (digest, type) = convertNameToDigestAndType (name);
176
177 if (type == "normal") // kind of ineffective...
178 {
179 processSyncInterest (name, digest);
180 }
181 else if (type == "recovery")
182 {
183 processSyncRecoveryInterest (name, digest);
184 }
185 }
186 catch (Error::DigestCalculationError &e)
187 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800188 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700189 // log error. ignoring it for now, later we should log it
190 return ;
191 }
192}
193
194void
Yingdi Yu51c80252014-02-10 19:32:05 -0800195SyncLogic::onSyncRegisterFailed(const Name& prefix, const string& msg)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800196{
Yingdi Yu51c80252014-02-10 19:32:05 -0800197 _LOG_DEBUG_ID("Sync prefix registration failed! " << msg);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800198}
199
200void
Yingdi Yu51c80252014-02-10 19:32:05 -0800201SyncLogic::onSyncData(const ndn::Interest& interest, Data& data)
202{
203 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
204 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
205 m_validator->validate(data, onValidated, onValidationFailed);
206}
Yingdi Yu280bb962014-01-30 09:52:43 -0800207
208void
Yingdi Yu51c80252014-02-10 19:32:05 -0800209SyncLogic::onSyncTimeout(const ndn::Interest& interest)
Yingdi Yu280bb962014-01-30 09:52:43 -0800210{
211 // It is OK. Others will handle the time out situation.
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800212}
213
214void
Yingdi Yu280bb962014-01-30 09:52:43 -0800215SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800216{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800217 _LOG_DEBUG_ID("Sync data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800218}
219
220void
Yingdi Yu280bb962014-01-30 09:52:43 -0800221SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700222{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800223 Name name = data->getName();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800224 const char* wireData = (const char*)data->getContent().value();
225 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700226
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700227 try
228 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800229 _LOG_DEBUG_ID ("<< D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700230
231 DigestConstPtr digest;
232 string type;
233 tie (digest, type) = convertNameToDigestAndType (name);
234
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700235 if (type == "normal")
236 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700237 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700238 }
239 else
240 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700241 // timer is always restarted when we schedule recovery
Yingdi Yu280bb962014-01-30 09:52:43 -0800242 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700243 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700244 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700245 }
246 catch (Error::DigestCalculationError &e)
247 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800248 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700249 // log error. ignoring it for now, later we should log it
250 return;
251 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700252}
253
Yingdi Yu43e71612013-10-30 22:19:31 -0700254void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800255SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700256{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800257 _LOG_DEBUG_ID("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700258 DigestConstPtr rootDigest;
259 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700260 rootDigest = m_state->getDigest();
261 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700262
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700263 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700264 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700265 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700266
267 SyncStateMsg ssm;
268 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700269 ssm << (*m_state);
270 }
271 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700272 return;
273 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700274
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700275 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700276 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800277 _LOG_DEBUG_ID ("processSyncInterest (): Same state. Adding to PIT");
Yingdi Yu6d638f02014-01-24 11:01:21 -0800278 m_syncInterestTable.insert (digest, name.toUri(), false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700279 return;
280 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700281
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700282 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
283
284 if (stateInDiffLog != m_log.end ())
285 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700286 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700287
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700288 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700289 return;
290 }
291
292 if (!timedProcessing)
293 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800294 bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700295 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700296 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800297 _LOG_DEBUG_ID ("Unknown digest, but somebody may have already replied, so restart our timer");
Yingdi Yu280bb962014-01-30 09:52:43 -0800298 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700299 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700300
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700301 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800302 _LOG_DEBUG_ID ("Digest is not in the log. Schedule processing after small delay: " << time::milliseconds (waitDelay));
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700303
Yingdi Yu280bb962014-01-30 09:52:43 -0800304 m_delayedInterestProcessingId = m_scheduler.scheduleEvent (time::milliseconds (waitDelay),
305 bind (&SyncLogic::processSyncInterest, this, name, digest, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700306 }
307 else
308 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800309 _LOG_DEBUG_ID (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700310
311 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700312 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700313 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800314}
315
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800316void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800317SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800318{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800319 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700320 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700321
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700322 try
323 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700324
Yingdi Yu6d638f02014-01-24 11:01:21 -0800325 m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700326
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700327 ownInterestSatisfied = (name == m_outstandingInterestName);
328
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700329 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700330 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700331 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700332 {
333 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700334 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700335 }
336 msg >> diff;
337
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700338 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700339 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700340 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800341 DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700342 BOOST_ASSERT (diffLeaf != 0);
343
344 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700345 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700346 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700347 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700348
349 bool inserted = false;
350 bool updated = false;
351 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700352 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800353 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700354 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800355
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700356 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700357 {
358 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700359 if (!oldSeq.isValid())
360 {
361 oldSeq = SeqNo(seq.getSession(), 0);
362 }
363 else
364 {
365 ++oldSeq;
366 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700367 // there is no need for application to process update on forwarder node
368 if (info->toString() != forwarderPrefix)
369 {
370 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800371 {
372 ostringstream interestName;
373 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
374 _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str());
375 }
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700376 if (m_perBranch)
377 {
378 ostringstream interestName;
379 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
380 m_onUpdateBranch(interestName.str());
381 }
382 else
383 {
384 v.push_back(mdi);
385 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700386 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700387 }
388 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700389 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700390 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700391 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700392 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700393 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700394 if (!m_perBranch)
395 {
396 m_onRemove (info->toString ());
397 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700398 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700399 }
400 else
401 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700402 }
403 }
404
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700405 if (!v.empty())
406 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700407 if (!m_perBranch)
408 {
409 m_onUpdate(v);
410 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700411 }
412
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700413 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700414 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700415 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700416 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800417 _LOG_DEBUG_ID ("Something really fishy happened during state decoding " <<
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700418 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700419 diffLog.reset ();
420 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700421 }
422
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700423 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
424 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700425 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800426 _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700427 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700428
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700429 // this is kind of wrong
430 // 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 -0700431
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700432 // if state has changed, then it is safe to express a new interest
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800433 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800434 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800435 EventId eventId = m_scheduler.scheduleEvent (after,
436 bind (&SyncLogic::sendSyncInterest, this));
437
Yingdi Yu280bb962014-01-30 09:52:43 -0800438 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800439 m_reexpressingInterestId = eventId;
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700440 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800441}
442
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800443void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800444SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800445{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800446 _LOG_DEBUG_ID("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700447 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
448
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700449 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700450 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800451 _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700452 return;
453 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700454
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700455 SyncStateMsg ssm;
456 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700457 ssm << (*m_state);
458 }
459 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700460}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700461
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700462void
463SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
464{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800465 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700466 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700467 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
468 {
469 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
470 /// @todo Impose limit on how many state info should be send out
471 }
472 }
473
474 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800475 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700476 uint32_t counter = 0;
477 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700478 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800479 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700480
481 if (!interest.m_unknown)
482 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800483 _LOG_DEBUG_ID (">> D " << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700484 sendSyncData (interest.m_name, interest.m_digest, diffLog);
485 }
486 else
487 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800488 _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700489 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
490 }
491 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700492 }
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800493 _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700494 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700495 catch (Error::InterestTableIsEmpty &e)
496 {
497 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800498 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800499}
500
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800501void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700502SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700503{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700504 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700505 if (m_log.size () > 0)
506 {
507 m_log.get<sequenced> ().front ()->setNext (diffLog);
508 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700509 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 -0700510 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700511 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700512}
513
514void
Yingdi Yu280bb962014-01-30 09:52:43 -0800515SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700516{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700517 DiffStatePtr diff;
518 {
519 //cout << "Add local names" <<endl;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800520 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700521
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800522 _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700523
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700524 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700525 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700526
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800527 _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700528
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800529 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700530 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700531 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700532 }
533
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800534 // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700535 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700536}
537
538void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800539SyncLogic::remove(const Name &prefix)
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700540{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700541 DiffStatePtr diff;
542 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800543 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700544 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700545
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700546 // increment the sequence number for the forwarder node
547 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700548
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700549 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
550 SeqNo seqNo (0);
551 if (item != m_state->getLeaves ().end ())
552 {
553 seqNo = (*item)->getSeq ();
554 ++seqNo;
555 }
556 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700557
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800558 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700559 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700560 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700561
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700562 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700563 }
564
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700565 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700566}
567
568void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800569SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800570{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800571 _LOG_DEBUG_ID("sendSyncInterest");
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800572
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700573 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800574 m_outstandingInterestName = m_syncPrefix;
575 ostringstream os;
576 os << *m_state->getDigest();
577 m_outstandingInterestName.append(os.str());
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800578 _LOG_DEBUG_ID (">> I " << m_outstandingInterestName);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700579 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700580
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800581 _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700582
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800583 EventId eventId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
584 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800585 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800586 m_reexpressingInterestId = eventId;
Yingdi Yu43e71612013-10-30 22:19:31 -0700587
Yingdi Yu6d638f02014-01-24 11:01:21 -0800588 ndn::Interest interest(m_outstandingInterestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800589 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800590
Yingdi Yu6d638f02014-01-24 11:01:21 -0800591 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800592 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800593 bind(&SyncLogic::onSyncTimeout, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800594}
595
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700596void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700597SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
598{
599 ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800600 os << *digest;
601
602 Name interestName = m_syncPrefix;
603 interestName.append("recovery").append(os.str());
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700604
Yingdi Yu280bb962014-01-30 09:52:43 -0800605 time::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
606
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700607 m_recoveryRetransmissionInterval <<= 1;
608
Yingdi Yu280bb962014-01-30 09:52:43 -0800609 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700610 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
Yingdi Yu280bb962014-01-30 09:52:43 -0800611 m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission,
612 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700613
Yingdi Yu6d638f02014-01-24 11:01:21 -0800614 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800615 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800616
Yingdi Yu6d638f02014-01-24 11:01:21 -0800617 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800618 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800619 bind(&SyncLogic::onSyncTimeout, this, _1));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700620}
621
622
623void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800624SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700625{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700626 SyncStateMsg msg;
627 msg << (*state);
628 sendSyncData(name, digest, msg);
629}
630
631// pass in state msg instead of state, so that there is no need to lock the state until
632// this function returns
633void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800634SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700635{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800636 _LOG_DEBUG_ID (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700637 int size = ssm.ByteSize();
638 char *wireData = new char[size];
Yingdi Yu280bb962014-01-30 09:52:43 -0800639 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800640
Yingdi Yu6d638f02014-01-24 11:01:21 -0800641 Data syncData(name);
642 syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800643 syncData.setFreshnessPeriod(m_syncResponseFreshness);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800644
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800645 m_keyChain.sign(syncData, m_myCertificate.getName());
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800646
Yingdi Yu6d638f02014-01-24 11:01:21 -0800647 m_face->put(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800648
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700649 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700650
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700651 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700652 bool satisfiedOwnInterest = false;
653 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700654 satisfiedOwnInterest = (m_outstandingInterestName == name);
655 }
656
657 if (satisfiedOwnInterest)
658 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800659 _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700660
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800661 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800662 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800663 EventId eventId = m_scheduler.scheduleEvent (after,
664 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800665 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800666 m_reexpressingInterestId = eventId;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700667 }
668}
669
Zhenkai Zhue5660932012-06-04 15:25:20 -0700670string
671SyncLogic::getRootDigest()
672{
673 ostringstream os;
Zhenkai Zhue5660932012-06-04 15:25:20 -0700674 os << *m_state->getDigest();
675 return os.str();
676}
677
Alexander Afanasyev80237382012-10-04 10:20:47 -0700678size_t
679SyncLogic::getNumberOfBranches () const
680{
Alexander Afanasyev80237382012-10-04 10:20:47 -0700681 return m_state->getLeaves ().size ();
682}
683
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700684void
685SyncLogic::printState () const
686{
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700687 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
688 {
689 std::cout << *leaf << std::endl;
690 }
691}
692
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700693std::map<std::string, bool>
694SyncLogic::getBranchPrefixes() const
695{
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700696 std::map<std::string, bool> m;
697
698 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
699 {
700 std::string prefix = leaf->getInfo()->toString();
701 // do not return forwarder prefix
702 if (prefix != forwarderPrefix)
703 {
704 m.insert(pair<std::string, bool>(prefix, false));
705 }
706 }
707
708 return m;
709}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700710
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800711}