Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 22 | |
Alexander Afanasyev | b6cf56e | 2012-05-29 19:33:49 -0700 | [diff] [blame] | 23 | #ifdef NS3_MODULE |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 24 | #include <ns3/ccnx-pit.h> |
| 25 | #include <ns3/ccnx.h> |
Alexander Afanasyev | b6cf56e | 2012-05-29 19:33:49 -0700 | [diff] [blame] | 26 | #endif |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 27 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 28 | #include "sync-logic.h" |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 29 | #include "sync-diff-leaf.h" |
| 30 | #include "sync-full-leaf.h" |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 31 | #include "sync-logging.h" |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 32 | #include "sync-state.h" |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 33 | |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 34 | #include <boost/make_shared.hpp> |
| 35 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 36 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 37 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 38 | |
| 39 | using namespace std; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 40 | using namespace ndn; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 41 | using namespace ndn::ptr_lib; |
| 42 | using namespace ndn::func_lib; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 43 | |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 44 | INIT_LOGGER ("SyncLogic"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 46 | #ifdef NS3_MODULE |
| 47 | #define GET_RANDOM(var) var.GetValue () |
| 48 | #else |
| 49 | #define GET_RANDOM(var) var () |
| 50 | #endif |
| 51 | |
| 52 | #define TIME_SECONDS_WITH_JITTER(sec) \ |
| 53 | (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
| 54 | |
Alexander Afanasyev | 4261226 | 2012-05-05 10:58:37 -0700 | [diff] [blame] | 55 | #define TIME_MILLISECONDS_WITH_JITTER(ms) \ |
| 56 | (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 57 | |
| 58 | |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 59 | namespace Sync |
| 60 | { |
| 61 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 62 | SyncLogic::SyncLogic (const Name& syncPrefix, |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame^] | 63 | shared_ptr<SecPolicySync> policy, |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 64 | shared_ptr<Face> face, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 65 | LogicUpdateCallback onUpdate, |
| 66 | LogicRemoveCallback onRemove) |
| 67 | : m_state (new FullState) |
| 68 | , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress)) |
| 69 | , m_syncPrefix (syncPrefix) |
| 70 | , m_onUpdate (onUpdate) |
| 71 | , m_onRemove (onRemove) |
| 72 | , m_perBranch (false) |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame^] | 73 | , m_policy(policy) |
| 74 | , m_verifier(new Verifier(policy)) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 75 | , m_keyChain(new KeyChain()) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 76 | , m_face(face) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 77 | #ifndef NS3_MODULE |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 78 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
| 79 | , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000)) |
| 80 | , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500)) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 81 | #else |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 82 | , m_rangeUniformRandom (200,1000) |
| 83 | , m_reexpressionJitter (10,500) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 84 | #endif |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 85 | , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 86 | { |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 87 | #ifndef NS3_MODULE |
| 88 | // In NS3 module these functions are moved to StartApplication method |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 89 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 90 | m_syncRegisteredPrefixId = m_face->setInterestFilter(m_syncPrefix, |
| 91 | func_lib::bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4), |
| 92 | func_lib::bind(&SyncLogic::onSyncRegisterFailed, this, _1)); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 94 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 95 | m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 96 | func_lib::bind (&SyncLogic::sendSyncInterest, this), |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 97 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 98 | #endif |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 101 | SyncLogic::SyncLogic (const Name& syncPrefix, |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame^] | 102 | shared_ptr<SecPolicySync> policy, |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 103 | shared_ptr<Face> face, |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 104 | LogicPerBranchCallback onUpdateBranch) |
| 105 | : m_state (new FullState) |
| 106 | , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress)) |
| 107 | , m_syncPrefix (syncPrefix) |
| 108 | , m_onUpdateBranch (onUpdateBranch) |
| 109 | , m_perBranch(true) |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame^] | 110 | , m_policy(policy) |
| 111 | , m_verifier(new Verifier(policy)) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 112 | , m_keyChain(new KeyChain()) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 113 | , m_face(face) |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 114 | #ifndef NS3_MODULE |
| 115 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
| 116 | , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000)) |
| 117 | , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500)) |
| 118 | #else |
| 119 | , m_rangeUniformRandom (200,1000) |
| 120 | , m_reexpressionJitter (10,500) |
| 121 | #endif |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 122 | , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval) |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 123 | { |
| 124 | #ifndef NS3_MODULE |
| 125 | // In NS3 module these functions are moved to StartApplication method |
| 126 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 127 | m_syncRegisteredPrefixId = m_face->setInterestFilter(m_syncPrefix, |
| 128 | func_lib::bind(&SyncLogic::onSyncInterest, this, _1, _2, _3, _4), |
| 129 | func_lib::bind(&SyncLogic::onSyncRegisterFailed, this, _1)); |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 130 | |
| 131 | m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 132 | func_lib::bind (&SyncLogic::sendSyncInterest, this), |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 133 | REEXPRESSING_INTEREST); |
| 134 | #endif |
| 135 | } |
| 136 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 137 | SyncLogic::~SyncLogic () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 138 | { |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 139 | m_face->unsetInterestFilter(m_syncRegisteredPrefixId); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 142 | #ifdef NS3_MODULE |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 143 | void |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 144 | SyncLogic::StartApplication () |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 145 | { |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 146 | m_ccnxHandle->SetNode (GetNode ()); |
| 147 | m_ccnxHandle->StartApplication (); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 149 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 150 | func_lib::bind (&SyncLogic::respondSyncInterest, this, _1)); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 152 | m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 153 | func_lib::bind (&SyncLogic::sendSyncInterest, this), |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 154 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 158 | SyncLogic::StopApplication () |
| 159 | { |
Alexander Afanasyev | 434f161 | 2012-04-21 21:19:15 -0700 | [diff] [blame] | 160 | m_ccnxHandle->clearInterestFilter (m_syncPrefix); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 161 | m_ccnxHandle->StopApplication (); |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 162 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 163 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | |
Zhenkai Zhu | eb1d865 | 2012-12-23 15:25:39 -0800 | [diff] [blame] | 167 | void |
| 168 | SyncLogic::stop() |
| 169 | { |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 170 | m_face->unsetInterestFilter(m_syncRegisteredPrefixId); |
Zhenkai Zhu | eb1d865 | 2012-12-23 15:25:39 -0800 | [diff] [blame] | 171 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 172 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
| 173 | } |
| 174 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Two types of intersts |
| 177 | * |
| 178 | * Normal name: .../<hash> |
| 179 | * Recovery name: .../recovery/<hash> |
| 180 | */ |
| 181 | boost::tuple<DigestConstPtr, std::string> |
| 182 | SyncLogic::convertNameToDigestAndType (const std::string &name) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 183 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 184 | BOOST_ASSERT (name.find (m_syncPrefix.toUri()) == 0); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 185 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 186 | string hash = name.substr (m_syncPrefix.toUri().size (), name.size ()-m_syncPrefix.toUri().size ()); |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 187 | if (hash[0] == '/') |
| 188 | hash = hash.substr (1, hash.size ()-1); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 189 | string interestType = "normal"; |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 191 | size_t pos = hash.find ('/'); |
| 192 | if (pos != string::npos) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 193 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 194 | interestType = hash.substr (0, pos); |
| 195 | hash = hash.substr (pos + 1); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 198 | _LOG_TRACE (hash << ", " << interestType); |
| 199 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 200 | DigestPtr digest = boost::make_shared<Digest> (); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 201 | istringstream is (hash); |
| 202 | is >> *digest; |
| 203 | |
| 204 | return make_tuple (digest, interestType); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 208 | SyncLogic::onSyncInterest (const shared_ptr<const Name>& prefix, |
| 209 | const shared_ptr<const ndn::Interest>& interest, |
| 210 | Transport& transport, |
| 211 | uint64_t registeredPrefixId) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 212 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 213 | _LOG_DEBUG("respondSyncInterest: " << interest->getName().toUri()); |
| 214 | string name = interest->getName().toUri(); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 215 | try |
| 216 | { |
| 217 | _LOG_TRACE ("<< I " << name); |
| 218 | |
| 219 | DigestConstPtr digest; |
| 220 | string type; |
| 221 | tie (digest, type) = convertNameToDigestAndType (name); |
| 222 | |
| 223 | if (type == "normal") // kind of ineffective... |
| 224 | { |
| 225 | processSyncInterest (name, digest); |
| 226 | } |
| 227 | else if (type == "recovery") |
| 228 | { |
| 229 | processSyncRecoveryInterest (name, digest); |
| 230 | } |
| 231 | } |
| 232 | catch (Error::DigestCalculationError &e) |
| 233 | { |
| 234 | _LOG_TRACE ("Something fishy happened..."); |
| 235 | // log error. ignoring it for now, later we should log it |
| 236 | return ; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 241 | SyncLogic::onSyncRegisterFailed(const shared_ptr<const Name>& prefix) |
| 242 | { |
| 243 | _LOG_DEBUG("Sync prefix registration failed!"); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | SyncLogic::onSyncData(const shared_ptr<const ndn::Interest>& interest, |
| 248 | const shared_ptr<Data>& data, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 249 | const OnVerified& onVerified, |
| 250 | const OnVerifyFailed& onVerifyFailed) |
| 251 | { |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 252 | m_verifier->verifyData(data, onVerified, onVerifyFailed); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void |
| 256 | SyncLogic::onSyncDataTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 257 | int retry, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 258 | const OnVerified& onVerified, |
| 259 | const OnVerifyFailed& onVerifyFailed) |
| 260 | { |
| 261 | if(retry > 0) |
| 262 | { |
| 263 | m_face->expressInterest(*interest, |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 264 | func_lib::bind(&SyncLogic::onSyncData, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 265 | this, |
| 266 | _1, |
| 267 | _2, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 268 | onVerified, |
| 269 | onVerifyFailed), |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 270 | func_lib::bind(&SyncLogic::onSyncDataTimeout, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 271 | this, |
| 272 | _1, |
| 273 | retry - 1, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 274 | onVerified, |
| 275 | onVerifyFailed)); |
| 276 | } |
| 277 | else |
| 278 | _LOG_DEBUG("Sync interest eventually times out!"); |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | SyncLogic::onSyncDataVerifyFailed(const shared_ptr<Data>& data) |
| 283 | { |
| 284 | _LOG_DEBUG("Sync data cannot be verified!"); |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | SyncLogic::onSyncDataVerified(const shared_ptr<Data>& data) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 289 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 290 | string name = data->getName().toUri(); |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 291 | const char* wireData = (const char*)data->getContent().value(); |
| 292 | size_t len = data->getContent().value_size(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 293 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 294 | try |
| 295 | { |
| 296 | _LOG_TRACE ("<< D " << name); |
| 297 | |
| 298 | DigestConstPtr digest; |
| 299 | string type; |
| 300 | tie (digest, type) = convertNameToDigestAndType (name); |
| 301 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 302 | if (type == "normal") |
| 303 | { |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 304 | processSyncData (name, digest, wireData, len); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 305 | } |
| 306 | else |
| 307 | { |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 308 | // timer is always restarted when we schedule recovery |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 309 | m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 310 | processSyncData (name, digest, wireData, len); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 311 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 312 | } |
| 313 | catch (Error::DigestCalculationError &e) |
| 314 | { |
| 315 | _LOG_TRACE ("Something fishy happened..."); |
| 316 | // log error. ignoring it for now, later we should log it |
| 317 | return; |
| 318 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 321 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 322 | SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 323 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 324 | _LOG_DEBUG("processSyncInterest"); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 325 | DigestConstPtr rootDigest; |
| 326 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 327 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 328 | rootDigest = m_state->getDigest(); |
| 329 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 330 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 331 | // 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] | 332 | if (digest->isZero () && !rootDigest->isZero ()) |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 333 | { |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 334 | |
| 335 | SyncStateMsg ssm; |
| 336 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 337 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 338 | ssm << (*m_state); |
| 339 | } |
| 340 | sendSyncData (name, digest, ssm); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 341 | return; |
| 342 | } |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 343 | |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 344 | if (*rootDigest == *digest) |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 345 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 346 | _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT"); |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 347 | m_syncInterestTable.insert (digest, name, false); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 348 | return; |
| 349 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 350 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 351 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 352 | |
| 353 | if (stateInDiffLog != m_log.end ()) |
| 354 | { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 355 | DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff (); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 357 | sendSyncData (name, digest, stateDiff); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
| 361 | if (!timedProcessing) |
| 362 | { |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 363 | bool exists = m_syncInterestTable.insert (digest, name, true); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 364 | if (exists) // somebody else replied, so restart random-game timer |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 365 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 366 | _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer"); |
| 367 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 368 | } |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 369 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 370 | uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom); |
| 371 | _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms"); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 372 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 373 | m_scheduler.schedule (TIME_MILLISECONDS (waitDelay), |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 374 | func_lib::bind (&SyncLogic::processSyncInterest, this, name, digest, true), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 375 | DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 376 | } |
| 377 | else |
| 378 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 379 | _LOG_TRACE (" (timed processing)"); |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 380 | |
| 381 | m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval; |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 382 | sendSyncRecoveryInterests (digest); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 383 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 386 | void |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 387 | SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 388 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 389 | DiffStatePtr diffLog = boost::make_shared<DiffState> (); |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 390 | bool ownInterestSatisfied = false; |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 391 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 392 | try |
| 393 | { |
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 | m_syncInterestTable.remove (name); // Remove satisfied interest from PIT |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 396 | |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 397 | ownInterestSatisfied = (name == m_outstandingInterestName); |
| 398 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 399 | DiffState diff; |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 400 | SyncStateMsg msg; |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 401 | if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized()) |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 402 | { |
| 403 | //Throw |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 404 | BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () ); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 405 | } |
| 406 | msg >> diff; |
| 407 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 408 | vector<MissingDataInfo> v; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 409 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 410 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 411 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 412 | BOOST_ASSERT (diffLeaf != 0); |
| 413 | |
| 414 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Zhenkai Zhu | ba3c1d1 | 2012-10-03 23:26:35 -0700 | [diff] [blame] | 415 | if (diffLeaf->getOperation() == UPDATE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 416 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 417 | SeqNo seq = diffLeaf->getSeq(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 418 | |
| 419 | bool inserted = false; |
| 420 | bool updated = false; |
| 421 | SeqNo oldSeq; |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 422 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 423 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 424 | tie (inserted, updated, oldSeq) = m_state->update (info, seq); |
| 425 | } |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 426 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 427 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 428 | { |
| 429 | diffLog->update (info, seq); |
Zhenkai Zhu | 68f04d5 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 430 | if (!oldSeq.isValid()) |
| 431 | { |
| 432 | oldSeq = SeqNo(seq.getSession(), 0); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | ++oldSeq; |
| 437 | } |
Zhenkai Zhu | ba3c1d1 | 2012-10-03 23:26:35 -0700 | [diff] [blame] | 438 | // there is no need for application to process update on forwarder node |
| 439 | if (info->toString() != forwarderPrefix) |
| 440 | { |
| 441 | MissingDataInfo mdi = {info->toString(), oldSeq, seq}; |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 442 | if (m_perBranch) |
| 443 | { |
| 444 | ostringstream interestName; |
| 445 | interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq(); |
| 446 | m_onUpdateBranch(interestName.str()); |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | v.push_back(mdi); |
| 451 | } |
Zhenkai Zhu | ba3c1d1 | 2012-10-03 23:26:35 -0700 | [diff] [blame] | 452 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 455 | else if (diffLeaf->getOperation() == REMOVE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 456 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 457 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 458 | if (m_state->remove (info)) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 459 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 460 | diffLog->remove (info); |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 461 | if (!m_perBranch) |
| 462 | { |
| 463 | m_onRemove (info->toString ()); |
| 464 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 465 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 466 | } |
| 467 | else |
| 468 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 472 | if (!v.empty()) |
| 473 | { |
Zhenkai Zhu | b9f1959 | 2012-10-16 14:27:38 -0700 | [diff] [blame] | 474 | if (!m_perBranch) |
| 475 | { |
| 476 | m_onUpdate(v); |
| 477 | } |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 480 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 481 | } |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 482 | catch (Error::SyncStateMsgDecodingFailure &e) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 483 | { |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 484 | _LOG_TRACE ("Something really fishy happened during state decoding " << |
| 485 | diagnostic_information (e)); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 486 | diffLog.reset (); |
| 487 | // don't do anything |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 490 | if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) || |
| 491 | ownInterestSatisfied) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 492 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 493 | // Do it only if everything went fine and state changed |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 494 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 495 | // this is kind of wrong |
| 496 | // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT, there is a point to satisfy them using new state |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 497 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 498 | // if state has changed, then it is safe to express a new interest |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 499 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 500 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 501 | func_lib::bind (&SyncLogic::sendSyncInterest, this), |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 502 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 503 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 506 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 507 | SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 508 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 509 | _LOG_DEBUG("processSyncRecoveryInterest"); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 510 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 511 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 512 | if (stateInDiffLog == m_log.end ()) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 513 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 514 | _LOG_TRACE ("Could not find " << *digest << " in digest log"); |
| 515 | return; |
| 516 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 517 | |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 518 | SyncStateMsg ssm; |
| 519 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 520 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 521 | ssm << (*m_state); |
| 522 | } |
| 523 | sendSyncData (name, digest, ssm); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 524 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 525 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 526 | void |
| 527 | SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog) |
| 528 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 529 | DiffStatePtr fullStateLog = boost::make_shared<DiffState> (); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 530 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 531 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 532 | BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/) |
| 533 | { |
| 534 | fullStateLog->update (leaf->getInfo (), leaf->getSeq ()); |
| 535 | /// @todo Impose limit on how many state info should be send out |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | try |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 540 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 541 | uint32_t counter = 0; |
| 542 | while (m_syncInterestTable.size () > 0) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 543 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 544 | Sync::Interest interest = m_syncInterestTable.pop (); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 545 | |
| 546 | if (!interest.m_unknown) |
| 547 | { |
| 548 | _LOG_TRACE (">> D " << interest.m_name); |
| 549 | sendSyncData (interest.m_name, interest.m_digest, diffLog); |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | _LOG_TRACE (">> D (unknown)" << interest.m_name); |
| 554 | sendSyncData (interest.m_name, interest.m_digest, fullStateLog); |
| 555 | } |
| 556 | counter ++; |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 557 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 558 | _LOG_DEBUG ("Satisfied " << counter << " pending interests"); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 559 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 560 | catch (Error::InterestTableIsEmpty &e) |
| 561 | { |
| 562 | // ok. not really an error |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 563 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 566 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 567 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 568 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 569 | diffLog->setDigest (m_state->getDigest()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 570 | if (m_log.size () > 0) |
| 571 | { |
| 572 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 573 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 574 | 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] | 575 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 576 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 580 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 581 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 582 | DiffStatePtr diff; |
| 583 | { |
| 584 | //cout << "Add local names" <<endl; |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 585 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 586 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 587 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 588 | _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ()); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 589 | |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 590 | SeqNo seqN (session, seq); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 591 | m_state->update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 592 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 593 | _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 594 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 595 | diff = boost::make_shared<DiffState>(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 596 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 597 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 600 | // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ()); |
| 601 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 605 | SyncLogic::remove(const string &prefix) |
| 606 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 607 | DiffStatePtr diff; |
| 608 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 609 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 610 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 611 | m_state->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 612 | |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 613 | // increment the sequence number for the forwarder node |
| 614 | NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix); |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 615 | |
Alexander Afanasyev | 7e1986f | 2012-10-04 10:21:10 -0700 | [diff] [blame] | 616 | LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo); |
| 617 | SeqNo seqNo (0); |
| 618 | if (item != m_state->getLeaves ().end ()) |
| 619 | { |
| 620 | seqNo = (*item)->getSeq (); |
| 621 | ++seqNo; |
| 622 | } |
| 623 | m_state->update (forwarderInfo, seqNo); |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 624 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 625 | diff = boost::make_shared<DiffState>(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 626 | diff->remove(info); |
Alexander Afanasyev | 7e1986f | 2012-10-04 10:21:10 -0700 | [diff] [blame] | 627 | diff->update(forwarderInfo, seqNo); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 628 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 629 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 632 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 636 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 637 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 638 | _LOG_DEBUG("sendSyncInterest"); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 639 | ostringstream os; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 640 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 641 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 642 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 643 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 644 | os << m_syncPrefix << "/" << *m_state->getDigest(); |
| 645 | m_outstandingInterestName = os.str (); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 646 | _LOG_TRACE (">> I " << os.str ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 647 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 648 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 649 | _LOG_DEBUG("sendSyncInterest: " << os.str()); |
| 650 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 651 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 652 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress), |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 653 | func_lib::bind (&SyncLogic::sendSyncInterest, this), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 654 | REEXPRESSING_INTEREST); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 655 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 656 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str()); |
| 657 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 658 | OnVerified onVerified = func_lib::bind(&SyncLogic::onSyncDataVerified, this, _1); |
| 659 | OnVerifyFailed onVerifyFailed = func_lib::bind(&SyncLogic::onSyncDataVerifyFailed, this, _1); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 660 | |
| 661 | m_face->expressInterest(*interest, |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 662 | func_lib::bind(&SyncLogic::onSyncData, this, _1, _2, onVerified, onVerifyFailed), |
| 663 | func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, onVerified, onVerifyFailed)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 666 | void |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 667 | SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest) |
| 668 | { |
| 669 | ostringstream os; |
| 670 | os << m_syncPrefix << "/recovery/" << *digest; |
| 671 | _LOG_TRACE (">> I " << os.str ()); |
| 672 | |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 673 | TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval); |
| 674 | m_recoveryRetransmissionInterval <<= 1; |
| 675 | |
| 676 | m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST); |
| 677 | if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds |
| 678 | { |
| 679 | m_scheduler.schedule (nextRetransmission, |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 680 | func_lib::bind (&SyncLogic::sendSyncRecoveryInterests, this, digest), |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 681 | REEXPRESSING_RECOVERY_INTEREST); |
| 682 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 683 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 684 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str()); |
| 685 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 686 | OnVerified onVerified = func_lib::bind(&SyncLogic::onSyncDataVerified, this, _1); |
| 687 | OnVerifyFailed onVerifyFailed = func_lib::bind(&SyncLogic::onSyncDataVerifyFailed, this, _1); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 688 | |
| 689 | m_face->expressInterest(*interest, |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 690 | func_lib::bind(&SyncLogic::onSyncData, this, _1, _2, onVerified, onVerifyFailed), |
| 691 | func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, onVerified, onVerifyFailed)); |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
| 695 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 696 | SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state) |
| 697 | { |
Zhenkai Zhu | 2d3e270 | 2012-10-15 14:18:05 -0700 | [diff] [blame] | 698 | SyncStateMsg msg; |
| 699 | msg << (*state); |
| 700 | sendSyncData(name, digest, msg); |
| 701 | } |
| 702 | |
| 703 | // pass in state msg instead of state, so that there is no need to lock the state until |
| 704 | // this function returns |
| 705 | void |
| 706 | SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm) |
| 707 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 708 | _LOG_TRACE (">> D " << name); |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 709 | int size = ssm.ByteSize(); |
| 710 | char *wireData = new char[size]; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 711 | Name dataName(name); |
Yingdi Yu | 5e0af3e | 2014-01-15 19:33:25 -0800 | [diff] [blame^] | 712 | Name signingIdentity = m_policy->inferSigningIdentity(dataName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 713 | |
| 714 | shared_ptr<Data> syncData = make_shared<Data>(dataName); |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 715 | syncData->setContent(reinterpret_cast<const uint8_t*>(wireData), size); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 716 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 717 | Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(signingIdentity); |
| 718 | m_keyChain->sign(*syncData, certificateName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 719 | |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 720 | m_face->put(*syncData); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 721 | |
Chaoyi Bian | 6665d5a | 2012-06-07 14:16:44 -0700 | [diff] [blame] | 722 | delete []wireData; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 723 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 724 | // checking if our own interest got satisfied |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 725 | bool satisfiedOwnInterest = false; |
| 726 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 727 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 728 | satisfiedOwnInterest = (m_outstandingInterestName == name); |
| 729 | } |
| 730 | |
| 731 | if (satisfiedOwnInterest) |
| 732 | { |
| 733 | _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)"); |
| 734 | |
| 735 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 736 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
| 737 | bind (&SyncLogic::sendSyncInterest, this), |
| 738 | REEXPRESSING_INTEREST); |
| 739 | } |
| 740 | } |
| 741 | |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 742 | string |
| 743 | SyncLogic::getRootDigest() |
| 744 | { |
| 745 | ostringstream os; |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 746 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 747 | os << *m_state->getDigest(); |
| 748 | return os.str(); |
| 749 | } |
| 750 | |
Alexander Afanasyev | 8023738 | 2012-10-04 10:20:47 -0700 | [diff] [blame] | 751 | size_t |
| 752 | SyncLogic::getNumberOfBranches () const |
| 753 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 754 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 8023738 | 2012-10-04 10:20:47 -0700 | [diff] [blame] | 755 | return m_state->getLeaves ().size (); |
| 756 | } |
| 757 | |
Alexander Afanasyev | 5548c04 | 2012-10-04 19:10:09 -0700 | [diff] [blame] | 758 | void |
| 759 | SyncLogic::printState () const |
| 760 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 761 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 5548c04 | 2012-10-04 19:10:09 -0700 | [diff] [blame] | 762 | |
| 763 | BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ()) |
| 764 | { |
| 765 | std::cout << *leaf << std::endl; |
| 766 | } |
| 767 | } |
| 768 | |
Zhenkai Zhu | d5aec4b | 2012-10-09 12:01:19 -0700 | [diff] [blame] | 769 | std::map<std::string, bool> |
| 770 | SyncLogic::getBranchPrefixes() const |
| 771 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 772 | boost::recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | d5aec4b | 2012-10-09 12:01:19 -0700 | [diff] [blame] | 773 | |
| 774 | std::map<std::string, bool> m; |
| 775 | |
| 776 | BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ()) |
| 777 | { |
| 778 | std::string prefix = leaf->getInfo()->toString(); |
| 779 | // do not return forwarder prefix |
| 780 | if (prefix != forwarderPrefix) |
| 781 | { |
| 782 | m.insert(pair<std::string, bool>(prefix, false)); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return m; |
| 787 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 788 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 789 | } |