blob: f9bbdca4b479aac366e407e8175d1b9be9296140 [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/make_shared.hpp>
31#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070032#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080033#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080034
35using namespace std;
Yingdi Yu43e71612013-10-30 22:19:31 -070036using namespace ndn;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080037
Chaoyi Bianf007f922012-04-09 20:05:37 -070038INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070039
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080040
41#ifdef _DEBUG
42#define _LOG_DEBUG_ID(v) _LOG_DEBUG(m_instanceId + v)
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070043#else
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080044#define _LOG_DEBUG_ID(v) _LOG_DEBUG(v)
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070045#endif
46
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080047#define GET_RANDOM(var) var ()
48
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070049#define TIME_SECONDS_WITH_JITTER(sec) \
50 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
51
Alexander Afanasyev42612262012-05-05 10:58:37 -070052#define TIME_MILLISECONDS_WITH_JITTER(ms) \
53 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070054
55
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080056namespace Sync
57{
58
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080059int SyncLogic::m_instanceCounter = 0;
Yingdi Yu43e71612013-10-30 22:19:31 -070060
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080061SyncLogic::SyncLogic (const Name& syncPrefix,
62 shared_ptr<Validator> validator,
63 shared_ptr<Face> face,
64 LogicUpdateCallback onUpdate,
65 LogicRemoveCallback onRemove)
66 : m_state (new FullState)
67 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
68 , m_syncPrefix (syncPrefix)
69 , m_onUpdate (onUpdate)
70 , m_onRemove (onRemove)
71 , m_perBranch (false)
72 , m_validator(validator)
73 , m_keyChain(new KeyChain())
74 , m_face(face)
75 , m_scheduler(*face->ioService())
76 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
77 , 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),
83 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
84
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 Yu280bb962014-01-30 09:52:43 -080093 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080094 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070095 LogicPerBranchCallback onUpdateBranch)
96 : m_state (new FullState)
97 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
98 , m_syncPrefix (syncPrefix)
99 , m_onUpdateBranch (onUpdateBranch)
100 , m_perBranch(true)
Yingdi Yu280bb962014-01-30 09:52:43 -0800101 , m_validator(validator)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800102 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +0800103 , m_face(face)
Yingdi Yu280bb962014-01-30 09:52:43 -0800104 , m_scheduler(*face->ioService())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700105 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -0800106 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
107 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Alexander Afanasyevce001692013-07-14 11:34:41 -0700108 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700109{
Yingdi Yu280bb962014-01-30 09:52:43 -0800110 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
111 bind(&SyncLogic::onSyncInterest, this, _1, _2),
112 bind(&SyncLogic::onSyncRegisterFailed, this, _1));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700113
Yingdi Yu280bb962014-01-30 09:52:43 -0800114 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
115 bind (&SyncLogic::sendSyncInterest, this));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700116}
117
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800118SyncLogic::~SyncLogic ()
Yingdi Yu280bb962014-01-30 09:52:43 -0800119{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800120 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800121}
122
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800123void
124SyncLogic::stop()
125{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800126 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu280bb962014-01-30 09:52:43 -0800127 m_scheduler.cancelEvent (m_reexpressingInterestId);
128 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800129}
130
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700131/**
132 * Two types of intersts
133 *
134 * Normal name: .../<hash>
135 * Recovery name: .../recovery/<hash>
136 */
137boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800138SyncLogic::convertNameToDigestAndType (const Name &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700139{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800140 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700141
Yingdi Yu6d638f02014-01-24 11:01:21 -0800142 int nameLengthDiff = name.size() - m_syncPrefix.size();
143 BOOST_ASSERT (nameLengthDiff > 0);
144 BOOST_ASSERT (nameLengthDiff < 3);
145
146 string hash = name.get(-1).toEscapedString();
147 string interestType;
148
149 if(nameLengthDiff == 1)
150 interestType = "normal";
151 else
152 interestType = name.get(-2).toEscapedString();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700153
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800154 _LOG_DEBUG_ID (hash << ", " << interestType);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700155
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800156 DigestPtr digest = boost::make_shared<Digest> ();
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700157 istringstream is (hash);
158 is >> *digest;
159
160 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700161}
162
163void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800164SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix,
Yingdi Yu280bb962014-01-30 09:52:43 -0800165 const shared_ptr<const ndn::Interest>& interest)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700166{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800167 Name name = interest->getName();
168
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800169 _LOG_DEBUG_ID("respondSyncInterest: " << name);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800170
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700171 try
172 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800173 _LOG_DEBUG_ID ("<< I " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700174
175 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 Yu46c9f1a2013-12-18 15:15:46 +0800197SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix)
198{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800199 _LOG_DEBUG_ID("Sync prefix registration failed!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800200}
201
202void
203SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest,
204 const shared_ptr<Data>& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800205 const OnDataValidated& onValidated,
206 const OnDataValidationFailed& onValidationFailed)
207{ m_validator->validate(data, onValidated, onValidationFailed); }
208
209void
210SyncLogic::onSyncTimeout(const shared_ptr<const ndn::Interest>& interest)
211{
212 // It is OK. Others will handle the time out situation.
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800213}
214
215void
Yingdi Yu280bb962014-01-30 09:52:43 -0800216SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800217{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800218 _LOG_DEBUG_ID("Sync data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800219}
220
221void
Yingdi Yu280bb962014-01-30 09:52:43 -0800222SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700223{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800224 Name name = data->getName();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800225 const char* wireData = (const char*)data->getContent().value();
226 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700227
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700228 try
229 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800230 _LOG_DEBUG_ID ("<< D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700231
232 DigestConstPtr digest;
233 string type;
234 tie (digest, type) = convertNameToDigestAndType (name);
235
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700236 if (type == "normal")
237 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700238 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700239 }
240 else
241 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700242 // timer is always restarted when we schedule recovery
Yingdi Yu280bb962014-01-30 09:52:43 -0800243 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700244 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700245 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700246 }
247 catch (Error::DigestCalculationError &e)
248 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800249 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700250 // log error. ignoring it for now, later we should log it
251 return;
252 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700253}
254
Yingdi Yu43e71612013-10-30 22:19:31 -0700255void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800256SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700257{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800258 _LOG_DEBUG_ID("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700259 DigestConstPtr rootDigest;
260 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800261 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
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 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800271 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700272 ssm << (*m_state);
273 }
274 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700275 return;
276 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700277
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700278 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700279 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800280 _LOG_DEBUG_ID ("processSyncInterest (): Same state. Adding to PIT");
Yingdi Yu6d638f02014-01-24 11:01:21 -0800281 m_syncInterestTable.insert (digest, name.toUri(), false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700282 return;
283 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700284
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700285 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
286
287 if (stateInDiffLog != m_log.end ())
288 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700289 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700290
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700291 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700292 return;
293 }
294
295 if (!timedProcessing)
296 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800297 bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700298 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700299 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800300 _LOG_DEBUG_ID ("Unknown digest, but somebody may have already replied, so restart our timer");
Yingdi Yu280bb962014-01-30 09:52:43 -0800301 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700302 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700303
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700304 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800305 _LOG_DEBUG_ID ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700306
Yingdi Yu280bb962014-01-30 09:52:43 -0800307 m_delayedInterestProcessingId = m_scheduler.scheduleEvent (time::milliseconds (waitDelay),
308 bind (&SyncLogic::processSyncInterest, this, name, digest, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700309 }
310 else
311 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800312 _LOG_DEBUG_ID (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700313
314 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700315 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700316 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800317}
318
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800319void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800320SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800321{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800322 DiffStatePtr diffLog = boost::make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700323 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700324
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700325 try
326 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700327
Yingdi Yu6d638f02014-01-24 11:01:21 -0800328 m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700329
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700330 ownInterestSatisfied = (name == m_outstandingInterestName);
331
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700332 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700333 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700334 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700335 {
336 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700337 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700338 }
339 msg >> diff;
340
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700341 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700342 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700343 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800344 DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700345 BOOST_ASSERT (diffLeaf != 0);
346
347 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700348 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700349 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700350 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700351
352 bool inserted = false;
353 bool updated = false;
354 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700355 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800356 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu280bb962014-01-30 09:52:43 -0800357 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700358 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800359
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700360 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700361 {
362 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700363 if (!oldSeq.isValid())
364 {
365 oldSeq = SeqNo(seq.getSession(), 0);
366 }
367 else
368 {
369 ++oldSeq;
370 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700371 // there is no need for application to process update on forwarder node
372 if (info->toString() != forwarderPrefix)
373 {
374 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800375 {
376 ostringstream interestName;
377 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
378 _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str());
379 }
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700380 if (m_perBranch)
381 {
382 ostringstream interestName;
383 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
384 m_onUpdateBranch(interestName.str());
385 }
386 else
387 {
388 v.push_back(mdi);
389 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700390 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700391 }
392 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700393 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700394 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800395 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700396 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700397 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700398 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700399 if (!m_perBranch)
400 {
401 m_onRemove (info->toString ());
402 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700403 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700404 }
405 else
406 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700407 }
408 }
409
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700410 if (!v.empty())
411 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700412 if (!m_perBranch)
413 {
414 m_onUpdate(v);
415 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700416 }
417
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700418 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700419 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700420 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700421 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800422 _LOG_DEBUG_ID ("Something really fishy happened during state decoding " <<
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700423 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700424 diffLog.reset ();
425 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700426 }
427
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700428 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
429 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700430 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800431 _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700432 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700433
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700434 // this is kind of wrong
435 // 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 -0700436
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700437 // if state has changed, then it is safe to express a new interest
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800438 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
439 cout << "------------ reexpress interest after: " << after << " ms " << endl;
440 EventId eventId = m_scheduler.scheduleEvent (after,
441 bind (&SyncLogic::sendSyncInterest, this));
442
Yingdi Yu280bb962014-01-30 09:52:43 -0800443 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800444 m_reexpressingInterestId = eventId;
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700445 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800446}
447
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800448void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800449SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800450{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800451 _LOG_DEBUG_ID("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700452 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
453
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700454 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700455 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800456 _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700457 return;
458 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700459
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700460 SyncStateMsg ssm;
461 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800462 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700463 ssm << (*m_state);
464 }
465 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700466}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700467
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700468void
469SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
470{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800471 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700472 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800473 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700474 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
475 {
476 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
477 /// @todo Impose limit on how many state info should be send out
478 }
479 }
480
481 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800482 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700483 uint32_t counter = 0;
484 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700485 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800486 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700487
488 if (!interest.m_unknown)
489 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800490 _LOG_DEBUG_ID (">> D " << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700491 sendSyncData (interest.m_name, interest.m_digest, diffLog);
492 }
493 else
494 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800495 _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700496 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
497 }
498 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700499 }
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800500 _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700501 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700502 catch (Error::InterestTableIsEmpty &e)
503 {
504 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800505 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800506}
507
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800508void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700509SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700510{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700511 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700512 if (m_log.size () > 0)
513 {
514 m_log.get<sequenced> ().front ()->setNext (diffLog);
515 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700516 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 -0700517 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700518 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700519}
520
521void
Yingdi Yu280bb962014-01-30 09:52:43 -0800522SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700523{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700524 DiffStatePtr diff;
525 {
526 //cout << "Add local names" <<endl;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800527 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800528 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700529
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800530 _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700531
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700532 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700533 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700534
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800535 _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700536
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800537 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700538 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700539 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700540 }
541
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800542 // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700543 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700544}
545
546void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800547SyncLogic::remove(const Name &prefix)
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700548{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700549 DiffStatePtr diff;
550 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800551 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800552 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700553 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700554
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700555 // increment the sequence number for the forwarder node
556 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700557
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700558 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
559 SeqNo seqNo (0);
560 if (item != m_state->getLeaves ().end ())
561 {
562 seqNo = (*item)->getSeq ();
563 ++seqNo;
564 }
565 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700566
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800567 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700568 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700569 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700570
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700571 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700572 }
573
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700574 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700575}
576
577void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800578SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800579{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800580 _LOG_DEBUG_ID("sendSyncInterest");
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800581
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700582 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800583 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800584 m_outstandingInterestName = m_syncPrefix;
585 ostringstream os;
586 os << *m_state->getDigest();
587 m_outstandingInterestName.append(os.str());
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800588 _LOG_DEBUG_ID (">> I " << m_outstandingInterestName);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700589 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700590
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800591 _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700592
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800593 EventId eventId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
594 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800595 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800596 m_reexpressingInterestId = eventId;
Yingdi Yu43e71612013-10-30 22:19:31 -0700597
Yingdi Yu6d638f02014-01-24 11:01:21 -0800598 ndn::Interest interest(m_outstandingInterestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800599 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800600
Yingdi Yu280bb962014-01-30 09:52:43 -0800601 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
602 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800603
Yingdi Yu6d638f02014-01-24 11:01:21 -0800604 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800605 bind(&SyncLogic::onSyncData, this, _1, _2, onValidated, onValidationFailed),
606 bind(&SyncLogic::onSyncTimeout, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800607}
608
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700609void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700610SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
611{
612 ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800613 os << *digest;
614
615 Name interestName = m_syncPrefix;
616 interestName.append("recovery").append(os.str());
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700617
Yingdi Yu280bb962014-01-30 09:52:43 -0800618 time::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
619
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700620 m_recoveryRetransmissionInterval <<= 1;
621
Yingdi Yu280bb962014-01-30 09:52:43 -0800622 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700623 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
Yingdi Yu280bb962014-01-30 09:52:43 -0800624 m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission,
625 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700626
Yingdi Yu6d638f02014-01-24 11:01:21 -0800627 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800628 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800629
Yingdi Yu280bb962014-01-30 09:52:43 -0800630 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
631 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800632
Yingdi Yu6d638f02014-01-24 11:01:21 -0800633 m_face->expressInterest(interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800634 bind(&SyncLogic::onSyncData, this, _1, _2, onValidated, onValidationFailed),
635 bind(&SyncLogic::onSyncTimeout, this, _1));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700636}
637
638
639void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800640SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700641{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700642 SyncStateMsg msg;
643 msg << (*state);
644 sendSyncData(name, digest, msg);
645}
646
647// pass in state msg instead of state, so that there is no need to lock the state until
648// this function returns
649void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800650SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700651{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800652 _LOG_DEBUG_ID (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700653 int size = ssm.ByteSize();
654 char *wireData = new char[size];
Yingdi Yu280bb962014-01-30 09:52:43 -0800655 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800656
Yingdi Yu6d638f02014-01-24 11:01:21 -0800657 Data syncData(name);
658 syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800659 syncData.setFreshnessPeriod(m_syncResponseFreshness);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800660
Yingdi Yu280bb962014-01-30 09:52:43 -0800661 m_keyChain->sign(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800662
Yingdi Yu6d638f02014-01-24 11:01:21 -0800663 m_face->put(syncData);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800664
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700665 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700666
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700667 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700668 bool satisfiedOwnInterest = false;
669 {
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800670 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700671 satisfiedOwnInterest = (m_outstandingInterestName == name);
672 }
673
674 if (satisfiedOwnInterest)
675 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800676 _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700677
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800678 time::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
679 cout << "------------ reexpress interest after: " << after << " ms " << endl;
680 EventId eventId = m_scheduler.scheduleEvent (after,
681 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800682 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800683 m_reexpressingInterestId = eventId;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700684 }
685}
686
Zhenkai Zhue5660932012-06-04 15:25:20 -0700687string
688SyncLogic::getRootDigest()
689{
690 ostringstream os;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800691 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhue5660932012-06-04 15:25:20 -0700692 os << *m_state->getDigest();
693 return os.str();
694}
695
Alexander Afanasyev80237382012-10-04 10:20:47 -0700696size_t
697SyncLogic::getNumberOfBranches () const
698{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800699 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700700 return m_state->getLeaves ().size ();
701}
702
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700703void
704SyncLogic::printState () const
705{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800706 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700707
708 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
709 {
710 std::cout << *leaf << std::endl;
711 }
712}
713
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700714std::map<std::string, bool>
715SyncLogic::getBranchPrefixes() const
716{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800717 boost::recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700718
719 std::map<std::string, bool> m;
720
721 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
722 {
723 std::string prefix = leaf->getInfo()->toString();
724 // do not return forwarder prefix
725 if (prefix != forwarderPrefix)
726 {
727 m.insert(pair<std::string, bool>(prefix, false));
728 }
729 }
730
731 return m;
732}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700733
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800734}