blob: 0567b60c2ef65a84351bad94566e6801db1bb2ac [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/web/index.html>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080023 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080024
Chaoyi Bian11f294f2012-03-08 14:28:06 -080025#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080026#include "sync-diff-leaf.h"
27#include "sync-full-leaf.h"
Alexander Afanasyevce001692013-07-14 11:34:41 -070028#include "sync-logging.h"
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070029#include "sync-state.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070030
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080031#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
Yingdi Yu43e71612013-10-30 22:19:31 -070035using namespace ndn;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080036
Yingdi Yu06a678a2014-08-01 17:07:08 -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) \
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070052 (time::milliseconds(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 Yu6e1c9cd2014-03-25 10:26:54 -070060const int SyncLogic::m_syncResponseFreshness = 1000; // MUST BE dividable by 1000!!!
61const int SyncLogic::m_syncInterestReexpress = 4; // seconds
62
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080063SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080064 const IdentityCertificate& myCertificate,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070065 shared_ptr<Validator> validator,
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080066 shared_ptr<Face> face,
67 LogicUpdateCallback onUpdate,
68 LogicRemoveCallback onRemove)
69 : m_state (new FullState)
Yingdi Yua2d76492014-07-02 13:43:52 -070070 , m_syncInterestTable (face->getIoService(), time::seconds(m_syncInterestReexpress))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080071 , m_syncPrefix (syncPrefix)
Yingdi Yu3da10fe2014-02-27 16:37:34 -080072 , m_myCertificate(myCertificate)
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080073 , m_onUpdate (onUpdate)
74 , m_onRemove (onRemove)
75 , m_perBranch (false)
76 , m_validator(validator)
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080077 , m_face(face)
Yingdi Yua2d76492014-07-02 13:43:52 -070078 , m_scheduler(face->getIoService())
Alexander Afanasyev531803b2014-02-05 15:57:35 -080079 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080080 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
81 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
82 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070083{
84 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
85 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -080086 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070087
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080088
Yingdi Yu280bb962014-01-30 09:52:43 -080089 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
90 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -080091
Yingdi Yu68bc51a2014-03-25 16:02:12 -070092 m_instanceId = std::string("Instance " + boost::lexical_cast<std::string>(m_instanceCounter++) + " ");
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080093}
94
Yingdi Yu43e71612013-10-30 22:19:31 -070095SyncLogic::SyncLogic (const Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080096 const IdentityCertificate& myCertificate,
Yingdi Yu280bb962014-01-30 09:52:43 -080097 shared_ptr<Validator> validator,
Yingdi Yu6e235db2013-12-27 08:40:53 +080098 shared_ptr<Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070099 LogicPerBranchCallback onUpdateBranch)
100 : m_state (new FullState)
Yingdi Yua2d76492014-07-02 13:43:52 -0700101 , m_syncInterestTable (face->getIoService(), time::seconds (m_syncInterestReexpress))
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700102 , m_syncPrefix (syncPrefix)
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800103 , m_myCertificate(myCertificate)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700104 , m_onUpdateBranch (onUpdateBranch)
105 , m_perBranch(true)
Yingdi Yu280bb962014-01-30 09:52:43 -0800106 , m_validator(validator)
Yingdi Yu6e235db2013-12-27 08:40:53 +0800107 , m_face(face)
Yingdi Yua2d76492014-07-02 13:43:52 -0700108 , m_scheduler(face->getIoService())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700109 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Yingdi Yu280bb962014-01-30 09:52:43 -0800110 , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000))
111 , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500))
Alexander Afanasyevce001692013-07-14 11:34:41 -0700112 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700113{
114 m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix,
115 bind(&SyncLogic::onSyncInterest, this, _1, _2),
Yingdi Yu51c80252014-02-10 19:32:05 -0800116 bind(&SyncLogic::onSyncRegisterFailed, this, _1, _2));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700117
Yingdi Yu280bb962014-01-30 09:52:43 -0800118 m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter
119 bind (&SyncLogic::sendSyncInterest, this));
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700120}
121
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800122SyncLogic::~SyncLogic ()
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700123{
124 m_face->unsetInterestFilter(m_syncRegisteredPrefixId);
Yingdi Yu280bb962014-01-30 09:52:43 -0800125 m_scheduler.cancelEvent (m_reexpressingInterestId);
126 m_scheduler.cancelEvent (m_delayedInterestProcessingId);
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800127}
128
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700129/**
130 * Two types of intersts
131 *
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700132 * Normal name: .../<hash>
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700133 * Recovery name: .../recovery/<hash>
134 */
135boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800136SyncLogic::convertNameToDigestAndType (const Name &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700137{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800138 BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700139
Yingdi Yu6d638f02014-01-24 11:01:21 -0800140 int nameLengthDiff = name.size() - m_syncPrefix.size();
141 BOOST_ASSERT (nameLengthDiff > 0);
142 BOOST_ASSERT (nameLengthDiff < 3);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700143
Yingdi Yua2d76492014-07-02 13:43:52 -0700144 std::string hash = name.get(-1).toUri();
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700145 std::string interestType;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700146
Yingdi Yu6d638f02014-01-24 11:01:21 -0800147 if(nameLengthDiff == 1)
148 interestType = "normal";
149 else
Yingdi Yua2d76492014-07-02 13:43:52 -0700150 interestType = name.get(-2).toUri();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700151
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800152 _LOG_DEBUG_ID (hash << ", " << interestType);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700153
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800154 DigestPtr digest = boost::make_shared<Digest> ();
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700155 std::istringstream is (hash);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700156 is >> *digest;
157
158 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700159}
160
161void
Yingdi Yu51c80252014-02-10 19:32:05 -0800162SyncLogic::onSyncInterest (const Name& prefix, const ndn::Interest& interest)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700163{
Yingdi Yu51c80252014-02-10 19:32:05 -0800164 Name name = interest.getName();
Yingdi Yu6d638f02014-01-24 11:01:21 -0800165
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800166 _LOG_DEBUG_ID("respondSyncInterest: " << name);
Yingdi Yu6d638f02014-01-24 11:01:21 -0800167
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700168 try
169 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800170 _LOG_DEBUG_ID ("<< I " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700171
Yingdi Yua2d76492014-07-02 13:43:52 -0700172 if(name.get(m_syncPrefix.size()).toUri() == "CHRONOS-INTRO-CERT")
Yingdi Yu0eee6002014-02-11 15:54:17 -0800173 // it is a certificate, validator will take care of it.
174 return;
175
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700176 DigestConstPtr digest;
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700177 std::string type;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700178 tie (digest, type) = convertNameToDigestAndType (name);
179
180 if (type == "normal") // kind of ineffective...
181 {
182 processSyncInterest (name, digest);
183 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700184 else if (type == "recovery")
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700185 {
186 processSyncRecoveryInterest (name, digest);
187 }
188 }
189 catch (Error::DigestCalculationError &e)
190 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800191 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700192 // log error. ignoring it for now, later we should log it
193 return ;
194 }
195}
196
197void
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700198SyncLogic::onSyncRegisterFailed(const Name& prefix, const std::string& msg)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800199{
Yingdi Yu51c80252014-02-10 19:32:05 -0800200 _LOG_DEBUG_ID("Sync prefix registration failed! " << msg);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800201}
202
203void
Yingdi Yu51c80252014-02-10 19:32:05 -0800204SyncLogic::onSyncData(const ndn::Interest& interest, Data& data)
205{
206 OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1);
207 OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700208 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu51c80252014-02-10 19:32:05 -0800209}
Yingdi Yu280bb962014-01-30 09:52:43 -0800210
211void
Yingdi Yu51c80252014-02-10 19:32:05 -0800212SyncLogic::onSyncTimeout(const ndn::Interest& interest)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700213{
214 // It is OK. Others will handle the time out situation.
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800215}
216
217void
Yingdi Yu280bb962014-01-30 09:52:43 -0800218SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800219{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800220 _LOG_DEBUG_ID("Sync data cannot be verified!");
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800221}
222
223void
Yingdi Yu280bb962014-01-30 09:52:43 -0800224SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700225{
Yingdi Yu6d638f02014-01-24 11:01:21 -0800226 Name name = data->getName();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800227 const char* wireData = (const char*)data->getContent().value();
228 size_t len = data->getContent().value_size();
Yingdi Yu43e71612013-10-30 22:19:31 -0700229
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700230 try
231 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800232 _LOG_DEBUG_ID ("<< D " << name);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700233
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700234 DigestConstPtr digest;
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700235 std::string type;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700236 tie (digest, type) = convertNameToDigestAndType (name);
237
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700238 if (type == "normal")
239 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700240 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700241 }
242 else
243 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700244 // timer is always restarted when we schedule recovery
Yingdi Yu280bb962014-01-30 09:52:43 -0800245 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700246 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700247 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700248 }
249 catch (Error::DigestCalculationError &e)
250 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800251 _LOG_DEBUG_ID ("Something fishy happened...");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700252 // log error. ignoring it for now, later we should log it
253 return;
254 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700255}
256
Yingdi Yu43e71612013-10-30 22:19:31 -0700257void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800258SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700259{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800260 _LOG_DEBUG_ID("processSyncInterest");
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700261 DigestConstPtr rootDigest;
262 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700263 rootDigest = m_state->getDigest();
264 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700265
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700266 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700267 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700268 {
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700269
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700270 SyncStateMsg ssm;
271 {
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 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -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
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700304 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800305 _LOG_DEBUG_ID ("Digest is not in the log. Schedule processing after small delay: " << time::milliseconds (waitDelay));
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)");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700313
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700314 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;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -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;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -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
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700341 std::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 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800356 boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700357 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800358
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700359 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700360 {
361 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700362 if (!oldSeq.isValid())
363 {
364 oldSeq = SeqNo(seq.getSession(), 0);
365 }
366 else
367 {
368 ++oldSeq;
369 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700370 // there is no need for application to process update on forwarder node
371 if (info->toString() != forwarderPrefix)
372 {
373 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800374 {
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700375 std::ostringstream interestName;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800376 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
377 _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str());
378 }
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700379 if (m_perBranch)
380 {
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700381 std::ostringstream interestName;
382 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
383 m_onUpdateBranch(interestName.str());
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700384 }
385 else
386 {
387 v.push_back(mdi);
388 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700389 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700390 }
391 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700392 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700393 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700394 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700395 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700396 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700397 if (!m_perBranch)
398 {
399 m_onRemove (info->toString ());
400 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700401 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700402 }
403 else
404 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700405 }
406 }
407
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700408 if (!v.empty())
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700409 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700410 if (!m_perBranch)
411 {
412 m_onUpdate(v);
413 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700414 }
415
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700416 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700417 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700418 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700419 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800420 _LOG_DEBUG_ID ("Something really fishy happened during state decoding " <<
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700421 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700422 diffLog.reset ();
423 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700424 }
425
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700426 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
427 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700428 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800429 _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700430 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700431
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700432 // this is kind of wrong
433 // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT, there is a point to satisfy them using new state
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700434
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700435 // if state has changed, then it is safe to express a new interest
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700436 time::system_clock::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800437 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800438 EventId eventId = m_scheduler.scheduleEvent (after,
439 bind (&SyncLogic::sendSyncInterest, this));
440
Yingdi Yu280bb962014-01-30 09:52:43 -0800441 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800442 m_reexpressingInterestId = eventId;
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700443 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800444}
445
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800446void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800447SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800448{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800449 _LOG_DEBUG_ID("processSyncRecoveryInterest");
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700450 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
451
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700452 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700453 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800454 _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log");
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700455 return;
456 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700457
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700458 SyncStateMsg ssm;
459 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700460 ssm << (*m_state);
461 }
462 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700463}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700464
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700465void
466SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
467{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800468 DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700469 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700470 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
471 {
472 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
473 /// @todo Impose limit on how many state info should be send out
474 }
475 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700476
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700477 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800478 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700479 uint32_t counter = 0;
480 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700481 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800482 Sync::Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700483
484 if (!interest.m_unknown)
485 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800486 _LOG_DEBUG_ID (">> D " << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700487 sendSyncData (interest.m_name, interest.m_digest, diffLog);
488 }
489 else
490 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800491 _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700492 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
493 }
494 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700495 }
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800496 _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700497 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700498 catch (Error::InterestTableIsEmpty &e)
499 {
500 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800501 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800502}
503
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800504void
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700505SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700506{
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700507 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700508 if (m_log.size () > 0)
509 {
510 m_log.get<sequenced> ().front ()->setNext (diffLog);
511 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700512 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 -0700513 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700514 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700515}
516
517void
Yingdi Yu280bb962014-01-30 09:52:43 -0800518SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq)
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700519{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700520 DiffStatePtr diff;
521 {
522 //cout << "Add local names" <<endl;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800523 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700524
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800525 _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700526
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700527 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700528 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700529
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800530 _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700531
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800532 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700533 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700534 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700535 }
536
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800537 // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700538 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700539}
540
541void
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700542SyncLogic::remove(const Name &prefix)
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700543{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700544 DiffStatePtr diff;
545 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800546 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700547 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700548
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700549 // increment the sequence number for the forwarder node
550 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700551
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700552 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
553 SeqNo seqNo (0);
554 if (item != m_state->getLeaves ().end ())
555 {
556 seqNo = (*item)->getSeq ();
557 ++seqNo;
558 }
559 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700560
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800561 diff = boost::make_shared<DiffState>();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700562 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700563 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700564
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700565 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700566 }
567
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700568 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700569}
570
571void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800572SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800573{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800574 _LOG_DEBUG_ID("sendSyncInterest");
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800575
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700576 {
Yingdi Yu6d638f02014-01-24 11:01:21 -0800577 m_outstandingInterestName = m_syncPrefix;
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700578 std::ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800579 os << *m_state->getDigest();
580 m_outstandingInterestName.append(os.str());
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800581 _LOG_DEBUG_ID (">> I " << m_outstandingInterestName);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700582 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700583
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800584 _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700585
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800586 EventId eventId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)),
587 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800588 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800589 m_reexpressingInterestId = eventId;
Yingdi Yu43e71612013-10-30 22:19:31 -0700590
Yingdi Yu6d638f02014-01-24 11:01:21 -0800591 ndn::Interest interest(m_outstandingInterestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800592 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800593
Yingdi Yu6d638f02014-01-24 11:01:21 -0800594 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800595 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800596 bind(&SyncLogic::onSyncTimeout, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800597}
598
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700599void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700600SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
601{
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700602 std::ostringstream os;
Yingdi Yu6d638f02014-01-24 11:01:21 -0800603 os << *digest;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700604
Yingdi Yu6d638f02014-01-24 11:01:21 -0800605 Name interestName = m_syncPrefix;
606 interestName.append("recovery").append(os.str());
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700607
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700608 time::system_clock::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter));
Yingdi Yu280bb962014-01-30 09:52:43 -0800609
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700610 m_recoveryRetransmissionInterval <<= 1;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700611
Yingdi Yu280bb962014-01-30 09:52:43 -0800612 m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId);
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700613 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
Yingdi Yu280bb962014-01-30 09:52:43 -0800614 m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission,
615 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700616
Yingdi Yu6d638f02014-01-24 11:01:21 -0800617 ndn::Interest interest(interestName);
Yingdi Yu280bb962014-01-30 09:52:43 -0800618 interest.setMustBeFresh(true);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800619
Yingdi Yu6d638f02014-01-24 11:01:21 -0800620 m_face->expressInterest(interest,
Yingdi Yu51c80252014-02-10 19:32:05 -0800621 bind(&SyncLogic::onSyncData, this, _1, _2),
Yingdi Yu280bb962014-01-30 09:52:43 -0800622 bind(&SyncLogic::onSyncTimeout, this, _1));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700623}
624
625
626void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800627SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700628{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700629 SyncStateMsg msg;
630 msg << (*state);
631 sendSyncData(name, digest, msg);
632}
633
634// pass in state msg instead of state, so that there is no need to lock the state until
635// this function returns
636void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800637SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700638{
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800639 _LOG_DEBUG_ID (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700640 int size = ssm.ByteSize();
641 char *wireData = new char[size];
Yingdi Yu280bb962014-01-30 09:52:43 -0800642 ssm.SerializeToArray(wireData, size);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800643
Yingdi Yu6d638f02014-01-24 11:01:21 -0800644 Data syncData(name);
645 syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700646 syncData.setFreshnessPeriod(time::milliseconds(m_syncResponseFreshness));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700647
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800648 m_keyChain.sign(syncData, m_myCertificate.getName());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700649
Yingdi Yu6d638f02014-01-24 11:01:21 -0800650 m_face->put(syncData);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700651
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700652 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700653
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700654 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700655 bool satisfiedOwnInterest = false;
656 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700657 satisfiedOwnInterest = (m_outstandingInterestName == name);
658 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700659
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700660 if (satisfiedOwnInterest)
661 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800662 _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700663
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700664 time::system_clock::Duration after = time::milliseconds(GET_RANDOM (m_reexpressionJitter));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800665 // cout << "------------ reexpress interest after: " << after << endl;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800666 EventId eventId = m_scheduler.scheduleEvent (after,
667 bind (&SyncLogic::sendSyncInterest, this));
Yingdi Yu280bb962014-01-30 09:52:43 -0800668 m_scheduler.cancelEvent (m_reexpressingInterestId);
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800669 m_reexpressingInterestId = eventId;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700670 }
671}
672
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700673std::string
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700674SyncLogic::getRootDigest()
Zhenkai Zhue5660932012-06-04 15:25:20 -0700675{
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700676 std::ostringstream os;
Zhenkai Zhue5660932012-06-04 15:25:20 -0700677 os << *m_state->getDigest();
678 return os.str();
679}
680
Alexander Afanasyev80237382012-10-04 10:20:47 -0700681size_t
682SyncLogic::getNumberOfBranches () const
683{
Alexander Afanasyev80237382012-10-04 10:20:47 -0700684 return m_state->getLeaves ().size ();
685}
686
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700687void
688SyncLogic::printState () const
689{
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700690 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
691 {
692 std::cout << *leaf << std::endl;
693 }
694}
695
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700696std::map<std::string, bool>
697SyncLogic::getBranchPrefixes() const
698{
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700699 std::map<std::string, bool> m;
700
701 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
702 {
703 std::string prefix = leaf->getInfo()->toString();
704 // do not return forwarder prefix
705 if (prefix != forwarderPrefix)
706 {
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700707 m.insert(std::pair<std::string, bool>(prefix, false));
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700708 }
709 }
710
711 return m;
712}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700713
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800714}