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