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