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