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