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