| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /** |
| * Copyright (c) 2013-2016, Regents of the University of California. |
| * |
| * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| * |
| * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, either |
| * version 3 of the License, or (at your option) any later version. |
| * |
| * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received copies of the GNU General Public License along with |
| * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| */ |
| |
| #include "sync-core.hpp" |
| #include "logging.hpp" |
| #include "periodic-task.hpp" |
| #include "random-interval-generator.hpp" |
| #include "simple-interval-generator.hpp" |
| #include "sync-state-helper.hpp" |
| |
| #include <boost/lexical_cast.hpp> |
| #include <boost/make_shared.hpp> |
| |
| INIT_LOGGER("Sync.Core"); |
| |
| const string SyncCore::RECOVER = "RECOVER"; |
| const double SyncCore::WAIT = 0.05; |
| const double SyncCore::RANDOM_PERCENT = 0.5; |
| |
| const std::string SYNC_INTEREST_TAG = "send-sync-interest"; |
| const std::string SYNC_INTEREST_TAG2 = "send-sync-interest2"; |
| |
| const std::string LOCAL_STATE_CHANGE_DELAYED_TAG = "local-state-changed"; |
| |
| using namespace boost; |
| using namespace Ndnx; |
| |
| SyncCore::SyncCore(SyncLogPtr syncLog, const Name& userName, const Name& localPrefix, |
| const Name& syncPrefix, const StateMsgCallback& callback, CcnxWrapperPtr ccnx, |
| double syncInterestInterval /*= -1.0*/) |
| : m_ccnx(ccnx) |
| , m_log(syncLog) |
| , m_scheduler(new Scheduler()) |
| , m_stateMsgCallback(callback) |
| , m_syncPrefix(syncPrefix) |
| , m_recoverWaitGenerator( |
| new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP)) |
| , m_syncInterestInterval(syncInterestInterval) |
| { |
| m_rootHash = m_log->RememberStateInStateLog(); |
| |
| m_ndnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1)); |
| // m_log->initYP(m_yp); |
| m_log->UpdateLocalLocator(localPrefix); |
| |
| m_scheduler->start(); |
| |
| double interval = |
| (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) ? m_syncInterestInterval : 4.0; |
| m_sendSyncInterestTask = |
| make_shared<PeriodicTask>(bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG, |
| m_scheduler, make_shared<SimpleIntervalGenerator>(interval)); |
| // sendSyncInterest(); |
| Scheduler::scheduleOneTimeTask(m_scheduler, 0.1, bind(&SyncCore::sendSyncInterest, this), |
| SYNC_INTEREST_TAG2); |
| } |
| |
| SyncCore::~SyncCore() |
| { |
| m_scheduler->shutdown(); |
| // need to "deregister" closures |
| } |
| |
| void |
| SyncCore::updateLocalState(sqlite3_int64 seqno) |
| { |
| m_log->UpdateLocalSeqNo(seqno); |
| localStateChanged(); |
| } |
| |
| void |
| SyncCore::localStateChanged() |
| { |
| HashPtr oldHash = m_rootHash; |
| m_rootHash = m_log->RememberStateInStateLog(); |
| |
| SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash); |
| |
| // reply sync Interest with oldHash as last component |
| Name syncName = Name(m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes()); |
| BytesPtr syncData = serializeGZipMsg(*msg); |
| |
| m_ccnx->publishData(syncName, *syncData, FRESHNESS); |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] localStateChanged "); |
| _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes: " << oldHash->shortHash()); |
| // _LOG_TRACE (msg); |
| |
| m_scheduler->deleteTask(SYNC_INTEREST_TAG2); |
| // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no problem, we know the new root hash already; |
| // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers |
| Scheduler::scheduleOneTimeTask(m_scheduler, 0.05, bind(&SyncCore::sendSyncInterest, this), |
| SYNC_INTEREST_TAG2); |
| |
| // sendSyncInterest(); |
| } |
| |
| void |
| SyncCore::localStateChangedDelayed() |
| { |
| // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one localStateChanged calls |
| Scheduler::scheduleOneTimeTask(m_scheduler, 0.5, bind(&SyncCore::localStateChanged, this), |
| LOCAL_STATE_CHANGE_DELAYED_TAG); |
| } |
| |
| void |
| SyncCore::handleInterest(const Name& name) |
| { |
| int size = name.size(); |
| int prefixSize = m_syncPrefix.size(); |
| if (size == prefixSize + 1) { |
| // this is normal sync interest |
| handleSyncInterest(name); |
| } |
| else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER) { |
| // this is recovery interest |
| handleRecoverInterest(name); |
| } |
| } |
| |
| void |
| SyncCore::handleRecoverInterest(const Name& name) |
| { |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< RECOVER Interest with name " << name); |
| |
| Bytes hashBytes = name.getComp(name.size() - 1); |
| // this is the hash unkonwn to the sender of the interest |
| Hash hash(head(hashBytes), hashBytes.size()); |
| if (m_log->LookupSyncLog(hash) > 0) { |
| // we know the hash, should reply everything |
| SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash); |
| |
| BytesPtr syncData = serializeGZipMsg(*msg); |
| m_ccnx->publishData(name, *syncData, FRESHNESS); |
| _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes " << hash.shortHash()); |
| // _LOG_TRACE (msg); |
| } |
| else { |
| // we don't recognize this hash, can not help |
| } |
| } |
| |
| void |
| SyncCore::handleSyncInterest(const Name& name) |
| { |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< SYNC Interest with name " << name); |
| |
| Bytes hashBytes = name.getComp(name.size() - 1); |
| HashPtr hash(new Hash(head(hashBytes), hashBytes.size())); |
| if (*hash == *m_rootHash) { |
| // we have the same hash; nothing needs to be done |
| _LOG_TRACE("same as root hash: " << hash->shortHash()); |
| return; |
| } |
| else if (m_log->LookupSyncLog(*hash) > 0) { |
| // we know something more |
| _LOG_TRACE("found hash in sync log"); |
| SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash); |
| |
| BytesPtr syncData = serializeGZipMsg(*msg); |
| m_ccnx->publishData(name, *syncData, FRESHNESS); |
| _LOG_TRACE(m_log->GetLocalName() << " publishes: " << hash->shortHash()); |
| _LOG_TRACE(msg); |
| } |
| else { |
| // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period |
| double wait = m_recoverWaitGenerator->nextInterval(); |
| _LOG_TRACE(m_log->GetLocalName() << ", rootHash: " << *m_rootHash |
| << ", hash: " << hash->shortHash()); |
| _LOG_TRACE("recover task scheduled after wait: " << wait); |
| |
| Scheduler::scheduleOneTimeTask(m_scheduler, wait, boost::bind(&SyncCore::recover, this, hash), |
| "r-" + lexical_cast<string>(*hash)); |
| } |
| } |
| |
| void |
| SyncCore::handleSyncInterestTimeout(const Name& name, const Closure& closure, Selectors selectors) |
| { |
| // sync interest will be resent by scheduler |
| } |
| |
| void |
| SyncCore::handleRecoverInterestTimeout(const Name& name, const Closure& closure, Selectors selectors) |
| { |
| // We do not re-express recovery interest for now |
| // if difference is not resolved, the sync interest will trigger |
| // recovery anyway; so it's not so important to have recovery interest |
| // re-expressed |
| } |
| |
| void |
| SyncCore::handleRecoverData(const Name& name, PcoPtr content) |
| { |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< RECOVER DATA with name: " << name); |
| //cout << "handle recover data" << end; |
| if (content && content->contentPtr() && content->contentPtr()->size() > 0) { |
| handleStateData(*content->contentPtr()); |
| } |
| else { |
| _LOG_ERROR("Got recovery DATA with empty content"); |
| } |
| |
| // sendSyncInterest(); |
| m_scheduler->deleteTask(SYNC_INTEREST_TAG2); |
| Scheduler::scheduleOneTimeTask(m_scheduler, 0, bind(&SyncCore::sendSyncInterest, this), |
| SYNC_INTEREST_TAG2); |
| } |
| |
| void |
| SyncCore::handleSyncData(const Name& name, PcoPtr content) |
| { |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< SYNC DATA with name: " << name); |
| |
| // suppress recover in interest - data out of order case |
| if (content && content->contentPtr() && content->contentPtr()->size() > 0) { |
| handleStateData(*content->contentPtr()); |
| } |
| else { |
| _LOG_ERROR("Got sync DATA with empty content"); |
| } |
| |
| // resume outstanding sync interest |
| // sendSyncInterest(); |
| |
| m_scheduler->deleteTask(SYNC_INTEREST_TAG2); |
| Scheduler::scheduleOneTimeTask(m_scheduler, 0, bind(&SyncCore::sendSyncInterest, this), |
| SYNC_INTEREST_TAG2); |
| } |
| |
| void |
| SyncCore::handleStateData(const Bytes& content) |
| { |
| SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content); |
| if (!(msg)) { |
| // ignore misformed SyncData |
| _LOG_ERROR("Misformed SyncData"); |
| return; |
| } |
| |
| _LOG_TRACE(m_log->GetLocalName() << " receives Msg "); |
| _LOG_TRACE(msg); |
| int size = msg->state_size(); |
| int index = 0; |
| while (index < size) { |
| SyncState state = msg->state(index); |
| string devStr = state.name(); |
| Name deviceName((const unsigned char*)devStr.c_str(), devStr.size()); |
| // cout << "Got Name: " << deviceName; |
| if (state.type() == SyncState::UPDATE) { |
| sqlite3_int64 seqno = state.seq(); |
| m_log->UpdateDeviceSeqNo(deviceName, seqno); |
| if (state.has_locator()) { |
| string locStr = state.locator(); |
| Name locatorName((const unsigned char*)locStr.c_str(), locStr.size()); |
| // cout << ", Got loc: " << locatorName << endl; |
| m_log->UpdateLocator(deviceName, locatorName); |
| |
| _LOG_TRACE("self: " << m_log->GetLocalName() << ", device: " << deviceName << " < == > " |
| << locatorName); |
| } |
| } |
| else { |
| _LOG_ERROR("Receive SYNC DELETE, but we don't support it yet"); |
| deregister(deviceName); |
| } |
| index++; |
| } |
| |
| // find the actuall difference and invoke callback on the actual difference |
| HashPtr oldHash = m_rootHash; |
| m_rootHash = m_log->RememberStateInStateLog(); |
| // get diff with both new SeqNo and old SeqNo |
| SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true); |
| |
| if (diff->state_size() > 0) { |
| m_stateMsgCallback(diff); |
| } |
| } |
| |
| void |
| SyncCore::sendSyncInterest() |
| { |
| Name syncInterest = Name(m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes()); |
| |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> SYNC Interest for " << m_rootHash->shortHash() |
| << ": " |
| << syncInterest); |
| |
| Selectors selectors; |
| if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) { |
| selectors.interestLifetime(m_syncInterestInterval); |
| } |
| m_ccnx->sendInterest(syncInterest, |
| Closure(boost::bind(&SyncCore::handleSyncData, this, _1, _2), |
| boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1, _2, _3)), |
| selectors); |
| |
| // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now |
| // if no such task exists, it will be added |
| m_scheduler->rescheduleTask(m_sendSyncInterestTask); |
| } |
| |
| void |
| SyncCore::recover(HashPtr hash) |
| { |
| if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0) { |
| _LOG_TRACE(m_log->GetLocalName() << ", Recover for: " << hash->shortHash()); |
| // unfortunately we still don't recognize this hash |
| Bytes bytes; |
| readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes()); |
| |
| // append the unknown hash |
| Name recoverInterest = Name(m_syncPrefix)(RECOVER)(bytes); |
| |
| _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> RECOVER Interests for " << hash->shortHash()); |
| |
| m_ccnx->sendInterest(recoverInterest, |
| Closure(boost::bind(&SyncCore::handleRecoverData, this, _1, _2), |
| boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1, _2, |
| _3))); |
| } |
| else { |
| // we already learned the hash; cheers! |
| } |
| } |
| |
| void |
| SyncCore::deregister(const Name& name) |
| { |
| // Do nothing for now |
| // TODO: handle deregistering |
| } |
| |
| sqlite3_int64 |
| SyncCore::seq(const Name& name) |
| { |
| return m_log->SeqNo(name); |
| } |