blob: ec4d63e883662ec5184a7490c3f3a7ba089a7fd9 [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,
61 shared_ptr<Validator> validator,
62 shared_ptr<Face> face,
63 LogicUpdateCallback onUpdate,
64 LogicRemoveCallback onRemove)
65 : m_state (new FullState)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080066 , m_syncInterestTable (*face->ioService(), time::seconds(m_syncInterestReexpress))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080067 , m_syncPrefix (syncPrefix)
68 , m_onUpdate (onUpdate)
69 , m_onRemove (onRemove)
70 , m_perBranch (false)
71 , m_validator(validator)
72 , m_keyChain(new KeyChain())
73 , m_face(face)
74 , m_scheduler(*face->ioService())
Alexander Afanasyev531803b2014-02-05 15:57:35 -080075 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080076 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
77 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
78 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
79{
Yingdi Yu280bb962014-01-30 09:52:43 -080080 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
81 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -080082 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Yingdi Yu280bb962014-01-30 09:52:43 -080083
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080084
Yingdi Yu280bb962014-01-30 09:52:43 -080085 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
86 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080087
88 m_instanceId = string("Instance " + boost::lexical_cast<string>(m_instanceCounter++) + " ");
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080089}
90
Yingdi Yu43e71612013-10-30 22:19:31 -070091SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080092 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080093 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070094 LogicPerBranchCallback onUpdateBranch)
95 : m_state (new FullState)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080096 , m_syncInterestTable (*face->ioService(), time::seconds (m_syncInterestReexpress))
Zhenkai Zhub9f19592012-10-16 14:27:38 -070097 , m_syncPrefix (syncPrefix)
98 , m_onUpdateBranch (onUpdateBranch)
99 , m_perBranch(true)
Yingdi Yu280bb962014-01-30 09:52:43 -0800100 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800101 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +0800102 , m_face(face)
Yingdi Yu280bb962014-01-30 09:52:43 -0800103 , m_scheduler(*face->ioService())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700104 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -0800105 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
106 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Alexander Afanasyevce001692013-07-14 11:34:41 -0700107 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700108{
Yingdi Yu280bb962014-01-30 09:52:43 -0800109 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
110 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -0800111 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700112
Yingdi Yu280bb962014-01-30 09:52:43 -0800113 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
114 bind (&SyncLogic::sendSyncInterest, this));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700115}
116
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800117SyncLogic::~SyncLogic ()
Yingdi Yu280bb962014-01-30 09:52:43 -0800118{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800119 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu280bb962014-01-30 09:52:43 -0800120 m_scheduler.cancelEvent (m_reexpressingInterestId);
121 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800122}
123
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700124/**
125 * Two types of intersts
126 *
127 * Normal name: .../<hash>
128 * Recovery name: .../recovery/<hash>
129 */
130boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800131SyncLogic::convertNameToDigestAndType (const Name &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700132{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800133 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700134
Yingdi Yu6d638f02014-01-24 11:01:21 -0800135 int nameLengthDiff = name.size() - m_syncPrefix.size();
136 BOOST_ASSERT (nameLengthDiff > 0);
137 BOOST_ASSERT (nameLengthDiff < 3);
138
139 string hash = name.get(-1).toEscapedString();
140 string interestType;
141
142 if(nameLengthDiff == 1)
143 interestType = "normal";
144 else
145 interestType = name.get(-2).toEscapedString();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700146
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800147 _LOG_DEBUG_ID (hash << ", " << interestType);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700148
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800149 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700150 istringstream is (hash);
151 is >> *digest;
152
153 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700154}
155
156void
Yingdi Yu51c80252014-02-10 19:32:05 -0800157SyncLogic::onSyncInterest (const Name& prefix, const ndn::Interest& interest)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700158{
Yingdi Yu51c80252014-02-10 19:32:05 -0800159 Name name = interest.getName();
Yingdi Yu6d638f02014-01-24 11:01:21 -0800160
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800161 _LOG_DEBUG_ID("respondSyncInterest: " << name);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800162
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700163 try
164 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800165 _LOG_DEBUG_ID ("<< I " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700166
167 DigestConstPtr digest;
168 string type;
169 tie (digest, type) = convertNameToDigestAndType (name);
170
171 if (type == "normal") // kind of ineffective...
172 {
173 processSyncInterest (name, digest);
174 }
175 else if (type == "recovery")
176 {
177 processSyncRecoveryInterest (name, digest);
178 }
179 }
180 catch (Error::DigestCalculationError &e)
181 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800182 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700183 // log error. ignoring it for now, later we should log it
184 return ;
185 }
186}
187
188void
Yingdi Yu51c80252014-02-10 19:32:05 -0800189SyncLogic::onSyncRegisterFailed(const Name& prefix, const string& msg)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800190{
Yingdi Yu51c80252014-02-10 19:32:05 -0800191 _LOG_DEBUG_ID("Sync prefix registration failed! " << msg);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800192}
193
194void
Yingdi Yu51c80252014-02-10 19:32:05 -0800195SyncLogic::onSyncData(const ndn::Interest& interest, Data& data)
196{
197 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
198 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
199 m_validator->validate(data, onValidated, onValidationFailed);
200}
Yingdi Yu280bb962014-01-30 09:52:43 -0800201
202void
Yingdi Yu51c80252014-02-10 19:32:05 -0800203SyncLogic::onSyncTimeout(const ndn::Interest& interest)
Yingdi Yu280bb962014-01-30 09:52:43 -0800204{
205 // It is OK. Others will handle the time out situation.
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800206}
207
208void
Yingdi Yu280bb962014-01-30 09:52:43 -0800209SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800210{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800211 _LOG_DEBUG_ID("Sync data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800212}
213
214void
Yingdi Yu280bb962014-01-30 09:52:43 -0800215SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700216{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800217 Name name = data->getName();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800218 const char* wireData = (const char*)data->getContent().value();
219 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700220
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700221 try
222 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800223 _LOG_DEBUG_ID ("<< D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700224
225 DigestConstPtr digest;
226 string type;
227 tie (digest, type) = convertNameToDigestAndType (name);
228
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700229 if (type == "normal")
230 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700231 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700232 }
233 else
234 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700235 // timer is always restarted when we schedule recovery
Yingdi Yu280bb962014-01-30 09:52:43 -0800236 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700237 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700238 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700239 }
240 catch (Error::DigestCalculationError &e)
241 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800242 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700243 // log error. ignoring it for now, later we should log it
244 return;
245 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700246}
247
Yingdi Yu43e71612013-10-30 22:19:31 -0700248void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800249SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700250{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800251 _LOG_DEBUG_ID("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700252 DigestConstPtr rootDigest;
253 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700254 rootDigest = m_state->getDigest();
255 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700256
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700257 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700258 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700259 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700260
261 SyncStateMsg ssm;
262 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700263 ssm << (*m_state);
264 }
265 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700266 return;
267 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700268
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700269 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700270 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800271 _LOG_DEBUG_ID ("processSyncInterest (): Same state. Adding to PIT");
Yingdi Yu6d638f02014-01-24 11:01:21 -0800272 m_syncInterestTable.insert (digest, name.toUri(), false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700273 return;
274 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700275
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700276 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
277
278 if (stateInDiffLog != m_log.end ())
279 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700280 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700281
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700282 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700283 return;
284 }
285
286 if (!timedProcessing)
287 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800288 bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700289 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700290 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800291 _LOG_DEBUG_ID ("Unknown digest, but somebody may have already replied, so restart our timer");
Yingdi Yu280bb962014-01-30 09:52:43 -0800292 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700293 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700294
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700295 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800296 _LOG_DEBUG_ID ("Digest is not in the log. Schedule processing after small delay: " << time::milliseconds (waitDelay));
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700297
Yingdi Yu280bb962014-01-30 09:52:43 -0800298 m_delayedInterestProcessingId = m_scheduler.scheduleEvent (time::milliseconds (waitDelay),
299 bind (&SyncLogic::processSyncInterest, this, name, digest, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700300 }
301 else
302 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800303 _LOG_DEBUG_ID (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700304
305 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700306 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700307 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800308}
309
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800310void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800311SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800312{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800313 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700314 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700315
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700316 try
317 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700318
Yingdi Yu6d638f02014-01-24 11:01:21 -0800319 m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700320
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700321 ownInterestSatisfied = (name == m_outstandingInterestName);
322
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700323 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700324 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700325 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700326 {
327 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700328 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700329 }
330 msg >> diff;
331
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700332 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700333 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700334 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800335 DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700336 BOOST_ASSERT (diffLeaf != 0);
337
338 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700339 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700340 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700341 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700342
343 bool inserted = false;
344 bool updated = false;
345 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700346 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800347 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700348 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800349
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700350 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700351 {
352 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700353 if (!oldSeq.isValid())
354 {
355 oldSeq = SeqNo(seq.getSession(), 0);
356 }
357 else
358 {
359 ++oldSeq;
360 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700361 // there is no need for application to process update on forwarder node
362 if (info->toString() != forwarderPrefix)
363 {
364 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800365 {
366 ostringstream interestName;
367 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
368 _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str());
369 }
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700370 if (m_perBranch)
371 {
372 ostringstream interestName;
373 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
374 m_onUpdateBranch(interestName.str());
375 }
376 else
377 {
378 v.push_back(mdi);
379 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700380 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700381 }
382 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700383 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700384 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700385 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700386 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700387 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700388 if (!m_perBranch)
389 {
390 m_onRemove (info->toString ());
391 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700392 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700393 }
394 else
395 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700396 }
397 }
398
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700399 if (!v.empty())
400 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700401 if (!m_perBranch)
402 {
403 m_onUpdate(v);
404 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700405 }
406
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700407 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700408 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700409 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700410 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800411 _LOG_DEBUG_ID ("Something really fishy happened during state decoding " <<
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700412 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700413 diffLog.reset ();
414 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700415 }
416
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700417 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
418 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700419 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800420 _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700421 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700422
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700423 // this is kind of wrong
424 // 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 -0700425
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700426 // if state has changed, then it is safe to express a new interest
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800427 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800428 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800429 EventId eventId = m_scheduler.scheduleEvent (after,
430 bind (&SyncLogic::sendSyncInterest, this));
431
Yingdi Yu280bb962014-01-30 09:52:43 -0800432 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800433 m_reexpressingInterestId = eventId;
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700434 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800435}
436
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800437void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800438SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800439{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800440 _LOG_DEBUG_ID("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700441 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
442
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700443 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700444 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800445 _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700446 return;
447 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700448
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700449 SyncStateMsg ssm;
450 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700451 ssm << (*m_state);
452 }
453 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700454}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700455
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700456void
457SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
458{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800459 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700460 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700461 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
462 {
463 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
464 /// @todo Impose limit on how many state info should be send out
465 }
466 }
467
468 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800469 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700470 uint32_t counter = 0;
471 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700472 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800473 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700474
475 if (!interest.m_unknown)
476 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800477 _LOG_DEBUG_ID (">> D " << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700478 sendSyncData (interest.m_name, interest.m_digest, diffLog);
479 }
480 else
481 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800482 _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700483 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
484 }
485 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700486 }
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800487 _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700488 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700489 catch (Error::InterestTableIsEmpty &e)
490 {
491 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800492 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800493}
494
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800495void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700496SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700497{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700498 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700499 if (m_log.size () > 0)
500 {
501 m_log.get<sequenced> ().front ()->setNext (diffLog);
502 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700503 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 -0700504 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700505 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700506}
507
508void
Yingdi Yu280bb962014-01-30 09:52:43 -0800509SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700510{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700511 DiffStatePtr diff;
512 {
513 //cout << "Add local names" <<endl;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800514 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700515
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800516 _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700517
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700518 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700519 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700520
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800521 _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700522
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800523 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700524 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700525 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700526 }
527
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800528 // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700529 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700530}
531
532void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800533SyncLogic::remove(const Name &prefix)
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700534{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700535 DiffStatePtr diff;
536 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800537 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700538 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700539
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700540 // increment the sequence number for the forwarder node
541 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700542
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700543 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
544 SeqNo seqNo (0);
545 if (item != m_state->getLeaves ().end ())
546 {
547 seqNo = (*item)->getSeq ();
548 ++seqNo;
549 }
550 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700551
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800552 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700553 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700554 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700555
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700556 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700557 }
558
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700559 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700560}
561
562void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800563SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800564{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800565 _LOG_DEBUG_ID("sendSyncInterest");
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800566
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700567 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800568 m_outstandingInterestName = m_syncPrefix;
569 ostringstream os;
570 os << *m_state->getDigest();
571 m_outstandingInterestName.append(os.str());
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800572 _LOG_DEBUG_ID (">> I " << m_outstandingInterestName);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700573 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700574
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800575 _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700576
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800577 EventId eventId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
578 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800579 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800580 m_reexpressingInterestId = eventId;
Yingdi Yu43e71612013-10-30 22:19:31 -0700581
Yingdi Yu6d638f02014-01-24 11:01:21 -0800582 ndn::Interest interest(m_outstandingInterestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800583 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800584
Yingdi Yu6d638f02014-01-24 11:01:21 -0800585 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800586 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800587 bind(&SyncLogic::onSyncTimeout, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800588}
589
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700590void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700591SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
592{
593 ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800594 os << *digest;
595
596 Name interestName = m_syncPrefix;
597 interestName.append("recovery").append(os.str());
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700598
Yingdi Yu280bb962014-01-30 09:52:43 -0800599 time::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
600
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700601 m_recoveryRetransmissionInterval <<= 1;
602
Yingdi Yu280bb962014-01-30 09:52:43 -0800603 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700604 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
Yingdi Yu280bb962014-01-30 09:52:43 -0800605 m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission,
606 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700607
Yingdi Yu6d638f02014-01-24 11:01:21 -0800608 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800609 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800610
Yingdi Yu6d638f02014-01-24 11:01:21 -0800611 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800612 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800613 bind(&SyncLogic::onSyncTimeout, this, _1));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700614}
615
616
617void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800618SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700619{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700620 SyncStateMsg msg;
621 msg << (*state);
622 sendSyncData(name, digest, msg);
623}
624
625// pass in state msg instead of state, so that there is no need to lock the state until
626// this function returns
627void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800628SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700629{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800630 _LOG_DEBUG_ID (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700631 int size = ssm.ByteSize();
632 char *wireData = new char[size];
Yingdi Yu280bb962014-01-30 09:52:43 -0800633 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800634
Yingdi Yu6d638f02014-01-24 11:01:21 -0800635 Data syncData(name);
636 syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800637 syncData.setFreshnessPeriod(m_syncResponseFreshness);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800638
Yingdi Yu280bb962014-01-30 09:52:43 -0800639 m_keyChain->sign(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800640
Yingdi Yu6d638f02014-01-24 11:01:21 -0800641 m_face->put(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800642
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700643 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700644
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700645 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700646 bool satisfiedOwnInterest = false;
647 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700648 satisfiedOwnInterest = (m_outstandingInterestName == name);
649 }
650
651 if (satisfiedOwnInterest)
652 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800653 _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700654
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800655 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800656 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800657 EventId eventId = m_scheduler.scheduleEvent (after,
658 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800659 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800660 m_reexpressingInterestId = eventId;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700661 }
662}
663
Zhenkai Zhue5660932012-06-04 15:25:20 -0700664string
665SyncLogic::getRootDigest()
666{
667 ostringstream os;
Zhenkai Zhue5660932012-06-04 15:25:20 -0700668 os << *m_state->getDigest();
669 return os.str();
670}
671
Alexander Afanasyev80237382012-10-04 10:20:47 -0700672size_t
673SyncLogic::getNumberOfBranches () const
674{
Alexander Afanasyev80237382012-10-04 10:20:47 -0700675 return m_state->getLeaves ().size ();
676}
677
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700678void
679SyncLogic::printState () const
680{
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700681 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
682 {
683 std::cout << *leaf << std::endl;
684 }
685}
686
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700687std::map<std::string, bool>
688SyncLogic::getBranchPrefixes() const
689{
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700690 std::map<std::string, bool> m;
691
692 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
693 {
694 std::string prefix = leaf->getInfo()->toString();
695 // do not return forwarder prefix
696 if (prefix != forwarderPrefix)
697 {
698 m.insert(pair<std::string, bool>(prefix, false));
699 }
700 }
701
702 return m;
703}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700704
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800705}