Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 21 | * Yingdi Yu <yingdi@cs.ucla.edu> |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 22 | */ |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 23 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 24 | #include "sync-logic.h" |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 25 | #include "sync-diff-leaf.h" |
| 26 | #include "sync-full-leaf.h" |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 27 | #include "sync-logging.h" |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 28 | #include "sync-state.h" |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 29 | |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 30 | #include <boost/make_shared.hpp> |
| 31 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 32 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 33 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 34 | |
| 35 | using namespace std; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 36 | using namespace ndn; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 37 | |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 38 | INIT_LOGGER ("SyncLogic"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 40 | |
| 41 | #ifdef _DEBUG |
| 42 | #define _LOG_DEBUG_ID(v) _LOG_DEBUG(m_instanceId + v) |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 43 | #else |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 44 | #define _LOG_DEBUG_ID(v) _LOG_DEBUG(v) |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 45 | #endif |
| 46 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 47 | #define GET_RANDOM(var) var () |
| 48 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 49 | #define TIME_SECONDS_WITH_JITTER(sec) \ |
| 50 | (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
| 51 | |
Alexander Afanasyev | 4261226 | 2012-05-05 10:58:37 -0700 | [diff] [blame] | 52 | #define TIME_MILLISECONDS_WITH_JITTER(ms) \ |
| 53 | (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 54 | |
| 55 | |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 56 | namespace Sync |
| 57 | { |
| 58 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 59 | int SyncLogic::m_instanceCounter = 0; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 61 | SyncLogic::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 Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 81 | m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix, |
| 82 | bind(&SyncLogic::onSyncInterest, this, _1, _2), |
| 83 | bind(&SyncLogic::onSyncRegisterFailed, this, _1)); |
| 84 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 85 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 86 | m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter |
| 87 | bind (&SyncLogic::sendSyncInterest, this)); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 88 | |
| 89 | m_instanceId = string("Instance " + boost::lexical_cast<string>(m_instanceCounter++) + " "); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 92 | SyncLogic::SyncLogic (const Name& syncPrefix, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 93 | shared_ptr<Validator> validator, |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 94 | shared_ptr<Face> face, |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 95 | 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 Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 101 | , m_validator(validator) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 102 | , m_keyChain(new KeyChain()) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 103 | , m_face(face) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 104 | , m_scheduler(*face->ioService()) |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 105 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 106 | , m_rangeUniformRandom (m_randomGenerator, boost::uniform_int<> (200,1000)) |
| 107 | , m_reexpressionJitter (m_randomGenerator, boost::uniform_int<> (100,500)) |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 108 | , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval) |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 109 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 110 | m_syncRegisteredPrefixId = m_face->setInterestFilter (m_syncPrefix, |
| 111 | bind(&SyncLogic::onSyncInterest, this, _1, _2), |
| 112 | bind(&SyncLogic::onSyncRegisterFailed, this, _1)); |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 114 | m_reexpressingInterestId = m_scheduler.scheduleEvent (time::seconds (0), // no need to add jitter |
| 115 | bind (&SyncLogic::sendSyncInterest, this)); |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 118 | SyncLogic::~SyncLogic () |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 119 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 120 | m_face->unsetInterestFilter(m_syncRegisteredPrefixId); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 121 | } |
| 122 | |
Zhenkai Zhu | eb1d865 | 2012-12-23 15:25:39 -0800 | [diff] [blame] | 123 | void |
| 124 | SyncLogic::stop() |
| 125 | { |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 126 | m_face->unsetInterestFilter(m_syncRegisteredPrefixId); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 127 | m_scheduler.cancelEvent (m_reexpressingInterestId); |
| 128 | m_scheduler.cancelEvent (m_delayedInterestProcessingId); |
Zhenkai Zhu | eb1d865 | 2012-12-23 15:25:39 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Two types of intersts |
| 133 | * |
| 134 | * Normal name: .../<hash> |
| 135 | * Recovery name: .../recovery/<hash> |
| 136 | */ |
| 137 | boost::tuple<DigestConstPtr, std::string> |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 138 | SyncLogic::convertNameToDigestAndType (const Name &name) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 139 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 140 | BOOST_ASSERT (m_syncPrefix.isPrefixOf(name)); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 141 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 142 | 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 Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 153 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 154 | _LOG_DEBUG_ID (hash << ", " << interestType); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 155 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 156 | DigestPtr digest = boost::make_shared<Digest> (); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 157 | istringstream is (hash); |
| 158 | is >> *digest; |
| 159 | |
| 160 | return make_tuple (digest, interestType); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 164 | SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 165 | const shared_ptr<const ndn::Interest>& interest) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 166 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 167 | Name name = interest->getName(); |
| 168 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 169 | _LOG_DEBUG_ID("respondSyncInterest: " << name); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 171 | try |
| 172 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 173 | _LOG_DEBUG_ID ("<< I " << name); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 174 | |
| 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 Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 190 | _LOG_DEBUG_ID ("Something fishy happened..."); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 191 | // log error. ignoring it for now, later we should log it |
| 192 | return ; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 197 | SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix) |
| 198 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 199 | _LOG_DEBUG_ID("Sync prefix registration failed!"); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
| 203 | SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest, |
| 204 | const shared_ptr<Data>& data, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 205 | const OnDataValidated& onValidated, |
| 206 | const OnDataValidationFailed& onValidationFailed) |
| 207 | { m_validator->validate(data, onValidated, onValidationFailed); } |
| 208 | |
| 209 | void |
| 210 | SyncLogic::onSyncTimeout(const shared_ptr<const ndn::Interest>& interest) |
| 211 | { |
| 212 | // It is OK. Others will handle the time out situation. |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 216 | SyncLogic::onSyncDataValidationFailed(const shared_ptr<const Data>& data) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 217 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 218 | _LOG_DEBUG_ID("Sync data cannot be verified!"); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 222 | SyncLogic::onSyncDataValidated(const shared_ptr<const Data>& data) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 223 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 224 | Name name = data->getName(); |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 225 | const char* wireData = (const char*)data->getContent().value(); |
| 226 | size_t len = data->getContent().value_size(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 227 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 228 | try |
| 229 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 230 | _LOG_DEBUG_ID ("<< D " << name); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 231 | |
| 232 | DigestConstPtr digest; |
| 233 | string type; |
| 234 | tie (digest, type) = convertNameToDigestAndType (name); |
| 235 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 236 | if (type == "normal") |
| 237 | { |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 238 | processSyncData (name, digest, wireData, len); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 242 | // timer is always restarted when we schedule recovery |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 243 | m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 244 | processSyncData (name, digest, wireData, len); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 245 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 246 | } |
| 247 | catch (Error::DigestCalculationError &e) |
| 248 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 249 | _LOG_DEBUG_ID ("Something fishy happened..."); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 250 | // log error. ignoring it for now, later we should log it |
| 251 | return; |
| 252 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 255 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 256 | SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 257 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 258 | _LOG_DEBUG_ID("processSyncInterest"); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 259 | DigestConstPtr rootDigest; |
| 260 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 261 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 262 | rootDigest = m_state->getDigest(); |
| 263 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 264 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 265 | // Special case when state is not empty and we have received request with zero-root digest |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 266 | if (digest->isZero () && !rootDigest->isZero ()) |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 267 | { |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 268 | |
| 269 | SyncStateMsg ssm; |
| 270 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 271 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 272 | ssm << (*m_state); |
| 273 | } |
| 274 | sendSyncData (name, digest, ssm); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 277 | |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 278 | if (*rootDigest == *digest) |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 279 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 280 | _LOG_DEBUG_ID ("processSyncInterest (): Same state. Adding to PIT"); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 281 | m_syncInterestTable.insert (digest, name.toUri(), false); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 282 | return; |
| 283 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 284 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 285 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 286 | |
| 287 | if (stateInDiffLog != m_log.end ()) |
| 288 | { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 289 | DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff (); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 290 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 291 | sendSyncData (name, digest, stateDiff); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | if (!timedProcessing) |
| 296 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 297 | bool exists = m_syncInterestTable.insert (digest, name.toUri(), true); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 298 | if (exists) // somebody else replied, so restart random-game timer |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 299 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 300 | _LOG_DEBUG_ID ("Unknown digest, but somebody may have already replied, so restart our timer"); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 301 | m_scheduler.cancelEvent (m_delayedInterestProcessingId); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 302 | } |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 303 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 304 | uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 305 | _LOG_DEBUG_ID ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms"); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 306 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 307 | m_delayedInterestProcessingId = m_scheduler.scheduleEvent (time::milliseconds (waitDelay), |
| 308 | bind (&SyncLogic::processSyncInterest, this, name, digest, true)); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 309 | } |
| 310 | else |
| 311 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 312 | _LOG_DEBUG_ID (" (timed processing)"); |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 313 | |
| 314 | m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval; |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 315 | sendSyncRecoveryInterests (digest); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 316 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 319 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 320 | SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 321 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 322 | DiffStatePtr diffLog = boost::make_shared<DiffState> (); |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 323 | bool ownInterestSatisfied = false; |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 324 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 325 | try |
| 326 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 327 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 328 | m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 329 | |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 330 | ownInterestSatisfied = (name == m_outstandingInterestName); |
| 331 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 332 | DiffState diff; |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 333 | SyncStateMsg msg; |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 334 | if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized()) |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 335 | { |
| 336 | //Throw |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 337 | BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () ); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 338 | } |
| 339 | msg >> diff; |
| 340 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 341 | vector<MissingDataInfo> v; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 342 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 343 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 344 | DiffLeafConstPtr diffLeaf = boost::dynamic_pointer_cast<const DiffLeaf> (leaf); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 345 | BOOST_ASSERT (diffLeaf != 0); |
| 346 | |
| 347 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Zhenkai Zhu | ba3c1d1 | 2012-10-03 23:26:35 -0700 | [diff] [blame] | 348 | if (diffLeaf->getOperation() == UPDATE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 349 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 350 | SeqNo seq = diffLeaf->getSeq(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 351 | |
| 352 | bool inserted = false; |
| 353 | bool updated = false; |
| 354 | SeqNo oldSeq; |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 355 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 356 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 357 | boost::tie (inserted, updated, oldSeq) = m_state->update (info, seq); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 358 | } |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 359 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 360 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 361 | { |
| 362 | diffLog->update (info, seq); |
Zhenkai Zhu | 68f04d5 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 363 | if (!oldSeq.isValid()) |
| 364 | { |
| 365 | oldSeq = SeqNo(seq.getSession(), 0); |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | ++oldSeq; |
| 370 | } |
Zhenkai Zhu | ba3c1d1 | 2012-10-03 23:26:35 -0700 | [diff] [blame] | 371 | // 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 Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 375 | { |
| 376 | ostringstream interestName; |
| 377 | interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq(); |
| 378 | _LOG_DEBUG_ID("+++++++++++++++ " + interestName.str()); |
| 379 | } |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 380 | 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 Zhu | ba3c1d1 | 2012-10-03 23:26:35 -0700 | [diff] [blame] | 390 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 393 | else if (diffLeaf->getOperation() == REMOVE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 394 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 395 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 396 | if (m_state->remove (info)) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 397 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 398 | diffLog->remove (info); |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 399 | if (!m_perBranch) |
| 400 | { |
| 401 | m_onRemove (info->toString ()); |
| 402 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 403 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 410 | if (!v.empty()) |
| 411 | { |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 412 | if (!m_perBranch) |
| 413 | { |
| 414 | m_onUpdate(v); |
| 415 | } |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 418 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 419 | } |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 420 | catch (Error::SyncStateMsgDecodingFailure &e) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 421 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 422 | _LOG_DEBUG_ID ("Something really fishy happened during state decoding " << |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 423 | diagnostic_information (e)); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 424 | diffLog.reset (); |
| 425 | // don't do anything |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 428 | if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) || |
| 429 | ownInterestSatisfied) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 430 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 431 | _LOG_DEBUG_ID(" +++++++++++++++ state changed!!!"); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 432 | // Do it only if everything went fine and state changed |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 433 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 434 | // 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 Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 436 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 437 | // if state has changed, then it is safe to express a new interest |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 438 | 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 Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 443 | m_scheduler.cancelEvent (m_reexpressingInterestId); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 444 | m_reexpressingInterestId = eventId; |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 445 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 448 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 449 | SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 450 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 451 | _LOG_DEBUG_ID("processSyncRecoveryInterest"); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 452 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 453 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 454 | if (stateInDiffLog == m_log.end ()) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 455 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 456 | _LOG_DEBUG_ID ("Could not find " << *digest << " in digest log"); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 457 | return; |
| 458 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 459 | |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 460 | SyncStateMsg ssm; |
| 461 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 462 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 463 | ssm << (*m_state); |
| 464 | } |
| 465 | sendSyncData (name, digest, ssm); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 466 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 467 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 468 | void |
| 469 | SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog) |
| 470 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 471 | DiffStatePtr fullStateLog = boost::make_shared<DiffState> (); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 472 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 473 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 474 | 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 Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 482 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 483 | uint32_t counter = 0; |
| 484 | while (m_syncInterestTable.size () > 0) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 485 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 486 | Sync::Interest interest = m_syncInterestTable.pop (); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 487 | |
| 488 | if (!interest.m_unknown) |
| 489 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 490 | _LOG_DEBUG_ID (">> D " << interest.m_name); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 491 | sendSyncData (interest.m_name, interest.m_digest, diffLog); |
| 492 | } |
| 493 | else |
| 494 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 495 | _LOG_DEBUG_ID (">> D (unknown)" << interest.m_name); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 496 | sendSyncData (interest.m_name, interest.m_digest, fullStateLog); |
| 497 | } |
| 498 | counter ++; |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 499 | } |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 500 | _LOG_DEBUG_ID ("Satisfied " << counter << " pending interests"); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 501 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 502 | catch (Error::InterestTableIsEmpty &e) |
| 503 | { |
| 504 | // ok. not really an error |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 505 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 508 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 509 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 510 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 511 | diffLog->setDigest (m_state->getDigest()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 512 | if (m_log.size () > 0) |
| 513 | { |
| 514 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 515 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 516 | m_log.erase (m_state->getDigest()); // remove diff state with the same digest. next pointers are still valid |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 517 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 518 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 522 | SyncLogic::addLocalNames (const Name &prefix, uint64_t session, uint64_t seq) |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 523 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 524 | DiffStatePtr diff; |
| 525 | { |
| 526 | //cout << "Add local names" <<endl; |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 527 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 528 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 529 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 530 | _LOG_DEBUG_ID ("addLocalNames (): old state " << *m_state->getDigest ()); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 531 | |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 532 | SeqNo seqN (session, seq); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 533 | m_state->update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 534 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 535 | _LOG_DEBUG_ID ("addLocalNames (): new state " << *m_state->getDigest ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 536 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 537 | diff = boost::make_shared<DiffState>(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 538 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 539 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 542 | // _LOG_DEBUG_ID ("PIT size: " << m_syncInterestTable.size ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 543 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 547 | SyncLogic::remove(const Name &prefix) |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 548 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 549 | DiffStatePtr diff; |
| 550 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 551 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 552 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri()); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 553 | m_state->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 554 | |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 555 | // increment the sequence number for the forwarder node |
| 556 | NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix); |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 557 | |
Alexander Afanasyev | 7e1986f | 2012-10-04 10:21:10 -0700 | [diff] [blame] | 558 | 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 Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 566 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 567 | diff = boost::make_shared<DiffState>(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 568 | diff->remove(info); |
Alexander Afanasyev | 7e1986f | 2012-10-04 10:21:10 -0700 | [diff] [blame] | 569 | diff->update(forwarderInfo, seqNo); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 570 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 571 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 574 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 578 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 579 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 580 | _LOG_DEBUG_ID("sendSyncInterest"); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 581 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 582 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 583 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 584 | m_outstandingInterestName = m_syncPrefix; |
| 585 | ostringstream os; |
| 586 | os << *m_state->getDigest(); |
| 587 | m_outstandingInterestName.append(os.str()); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 588 | _LOG_DEBUG_ID (">> I " << m_outstandingInterestName); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 589 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 590 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 591 | _LOG_DEBUG_ID("sendSyncInterest: " << m_outstandingInterestName); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 592 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 593 | EventId eventId = m_scheduler.scheduleEvent (time::seconds(m_syncInterestReexpress) + time::milliseconds(GET_RANDOM (m_reexpressionJitter)), |
| 594 | bind (&SyncLogic::sendSyncInterest, this)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 595 | m_scheduler.cancelEvent (m_reexpressingInterestId); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 596 | m_reexpressingInterestId = eventId; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 597 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 598 | ndn::Interest interest(m_outstandingInterestName); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 599 | interest.setMustBeFresh(true); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 600 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 601 | OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1); |
| 602 | OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 603 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 604 | m_face->expressInterest(interest, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 605 | bind(&SyncLogic::onSyncData, this, _1, _2, onValidated, onValidationFailed), |
| 606 | bind(&SyncLogic::onSyncTimeout, this, _1)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 609 | void |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 610 | SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest) |
| 611 | { |
| 612 | ostringstream os; |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 613 | os << *digest; |
| 614 | |
| 615 | Name interestName = m_syncPrefix; |
| 616 | interestName.append("recovery").append(os.str()); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 617 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 618 | time::Duration nextRetransmission = time::milliseconds (m_recoveryRetransmissionInterval + GET_RANDOM (m_reexpressionJitter)); |
| 619 | |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 620 | m_recoveryRetransmissionInterval <<= 1; |
| 621 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 622 | m_scheduler.cancelEvent (m_reexpressingRecoveryInterestId); |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 623 | if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 624 | m_reexpressingRecoveryInterestId = m_scheduler.scheduleEvent (nextRetransmission, |
| 625 | bind (&SyncLogic::sendSyncRecoveryInterests, this, digest)); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 626 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 627 | ndn::Interest interest(interestName); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 628 | interest.setMustBeFresh(true); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 629 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 630 | OnDataValidated onValidated = bind(&SyncLogic::onSyncDataValidated, this, _1); |
| 631 | OnDataValidationFailed onValidationFailed = bind(&SyncLogic::onSyncDataValidationFailed, this, _1); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 632 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 633 | m_face->expressInterest(interest, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 634 | bind(&SyncLogic::onSyncData, this, _1, _2, onValidated, onValidationFailed), |
| 635 | bind(&SyncLogic::onSyncTimeout, this, _1)); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
| 639 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 640 | SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 641 | { |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 642 | 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 |
| 649 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 650 | SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm) |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 651 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 652 | _LOG_DEBUG_ID (">> D " << name); |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 653 | int size = ssm.ByteSize(); |
| 654 | char *wireData = new char[size]; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 655 | ssm.SerializeToArray(wireData, size); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 656 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 657 | Data syncData(name); |
| 658 | syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 659 | syncData.setFreshnessPeriod(m_syncResponseFreshness); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 660 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 661 | m_keyChain->sign(syncData); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 662 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 663 | m_face->put(syncData); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 664 | |
Chaoyi Bian | 6665d5a | 2012-06-07 14:16:44 -0700 | [diff] [blame] | 665 | delete []wireData; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 666 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 667 | // checking if our own interest got satisfied |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 668 | bool satisfiedOwnInterest = false; |
| 669 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 670 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 671 | satisfiedOwnInterest = (m_outstandingInterestName == name); |
| 672 | } |
| 673 | |
| 674 | if (satisfiedOwnInterest) |
| 675 | { |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 676 | _LOG_DEBUG_ID ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)"); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 677 | |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 678 | 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 Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 682 | m_scheduler.cancelEvent (m_reexpressingInterestId); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame^] | 683 | m_reexpressingInterestId = eventId; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 687 | string |
| 688 | SyncLogic::getRootDigest() |
| 689 | { |
| 690 | ostringstream os; |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 691 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 692 | os << *m_state->getDigest(); |
| 693 | return os.str(); |
| 694 | } |
| 695 | |
Alexander Afanasyev | 8023738 | 2012-10-04 10:20:47 -0700 | [diff] [blame] | 696 | size_t |
| 697 | SyncLogic::getNumberOfBranches () const |
| 698 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 699 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 8023738 | 2012-10-04 10:20:47 -0700 | [diff] [blame] | 700 | return m_state->getLeaves ().size (); |
| 701 | } |
| 702 | |
Alexander Afanasyev | 5548c04 | 2012-10-04 19:10:09 -0700 | [diff] [blame] | 703 | void |
| 704 | SyncLogic::printState () const |
| 705 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 706 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 5548c04 | 2012-10-04 19:10:09 -0700 | [diff] [blame] | 707 | |
| 708 | BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ()) |
| 709 | { |
| 710 | std::cout << *leaf << std::endl; |
| 711 | } |
| 712 | } |
| 713 | |
Zhenkai Zhu | d5aec4b | 2012-10-09 12:01:19 -0700 | [diff] [blame] | 714 | std::map<std::string, bool> |
| 715 | SyncLogic::getBranchPrefixes() const |
| 716 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 717 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | d5aec4b | 2012-10-09 12:01:19 -0700 | [diff] [blame] | 718 | |
| 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 Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 733 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 734 | } |