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