Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "sync-core.hpp" |
| 22 | #include "sync-state-helper.hpp" |
| 23 | #include "logging.hpp" |
| 24 | #include "random-interval-generator.hpp" |
| 25 | #include "simple-interval-generator.hpp" |
| 26 | #include "periodic-task.hpp" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 29 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 30 | |
| 31 | INIT_LOGGER ("Sync.Core"); |
| 32 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 33 | const string SyncCore::RECOVER = "RECOVER"; |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 34 | const double SyncCore::WAIT = 0.05; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 35 | const double SyncCore::RANDOM_PERCENT = 0.5; |
| 36 | |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 37 | const std::string SYNC_INTEREST_TAG = "send-sync-interest"; |
| 38 | const std::string SYNC_INTEREST_TAG2 = "send-sync-interest2"; |
| 39 | |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 40 | const std::string LOCAL_STATE_CHANGE_DELAYED_TAG = "local-state-changed"; |
| 41 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 42 | using namespace boost; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 43 | using namespace Ndnx; |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 45 | SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix, |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 46 | const StateMsgCallback &callback, NdnxWrapperPtr ndnx, double syncInterestInterval/*= -1.0*/) |
| 47 | : m_ndnx (ndnx) |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 48 | , m_log(syncLog) |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 49 | , m_scheduler(new Scheduler ()) |
| 50 | , m_stateMsgCallback(callback) |
| 51 | , m_syncPrefix(syncPrefix) |
| 52 | , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP)) |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 53 | , m_syncInterestInterval(syncInterestInterval) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 54 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 55 | m_rootHash = m_log->RememberStateInStateLog(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 57 | m_ndnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1)); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 58 | // m_log->initYP(m_yp); |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 59 | m_log->UpdateLocalLocator (localPrefix); |
| 60 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 61 | m_scheduler->start(); |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 62 | |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 63 | double interval = (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) ? m_syncInterestInterval : 4.0; |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 64 | m_sendSyncInterestTask = make_shared<PeriodicTask>(bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG, m_scheduler, make_shared<SimpleIntervalGenerator>(interval)); |
| 65 | // sendSyncInterest(); |
| 66 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.1, bind(&SyncCore::sendSyncInterest, this), SYNC_INTEREST_TAG2); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SyncCore::~SyncCore() |
| 70 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 71 | m_scheduler->shutdown (); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 72 | // need to "deregister" closures |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 75 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 76 | SyncCore::updateLocalState(sqlite3_int64 seqno) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 78 | m_log->UpdateLocalSeqNo (seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 79 | localStateChanged(); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 80 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 81 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 82 | void |
| 83 | SyncCore::localStateChanged() |
| 84 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 85 | HashPtr oldHash = m_rootHash; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 86 | m_rootHash = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 87 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 88 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 89 | |
| 90 | // reply sync Interest with oldHash as last component |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 91 | Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes()); |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 92 | BytesPtr syncData = serializeGZipMsg (*msg); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 94 | m_ndnx->publishData(syncName, *syncData, FRESHNESS); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 95 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged "); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 96 | _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 97 | // _LOG_TRACE (msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 99 | m_scheduler->deleteTask (SYNC_INTEREST_TAG2); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 100 | // 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; |
| 101 | // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 102 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.05, |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 103 | bind(&SyncCore::sendSyncInterest, this), |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 104 | SYNC_INTEREST_TAG2); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 105 | |
Zhenkai Zhu | d276f1e | 2013-01-24 17:37:31 -0800 | [diff] [blame] | 106 | //sendSyncInterest(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 110 | SyncCore::localStateChangedDelayed () |
| 111 | { |
| 112 | // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one localStateChanged calls |
| 113 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
| 114 | bind (&SyncCore::localStateChanged, this), |
| 115 | LOCAL_STATE_CHANGE_DELAYED_TAG); |
| 116 | } |
| 117 | |
| 118 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 119 | SyncCore::handleInterest(const Name &name) |
| 120 | { |
| 121 | int size = name.size(); |
| 122 | int prefixSize = m_syncPrefix.size(); |
| 123 | if (size == prefixSize + 1) |
| 124 | { |
| 125 | // this is normal sync interest |
| 126 | handleSyncInterest(name); |
| 127 | } |
| 128 | else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER) |
| 129 | { |
| 130 | // this is recovery interest |
| 131 | handleRecoverInterest(name); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | SyncCore::handleRecoverInterest(const Name &name) |
| 137 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 138 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name); |
| 139 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 140 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 141 | // this is the hash unkonwn to the sender of the interest |
| 142 | Hash hash(head(hashBytes), hashBytes.size()); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 143 | if (m_log->LookupSyncLog(hash) > 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 144 | { |
| 145 | // we know the hash, should reply everything |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 146 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 147 | |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 148 | BytesPtr syncData = serializeGZipMsg (*msg); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 149 | m_ndnx->publishData(name, *syncData, FRESHNESS); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 150 | _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 151 | // _LOG_TRACE (msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 152 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 153 | else |
| 154 | { |
| 155 | // we don't recognize this hash, can not help |
| 156 | } |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 160 | SyncCore::handleSyncInterest(const Name &name) |
| 161 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 162 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name); |
| 163 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 164 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 165 | HashPtr hash(new Hash(head(hashBytes), hashBytes.size())); |
| 166 | if (*hash == *m_rootHash) |
| 167 | { |
| 168 | // we have the same hash; nothing needs to be done |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 169 | _LOG_TRACE ("same as root hash: " << hash->shortHash ()); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 172 | else if (m_log->LookupSyncLog(*hash) > 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 173 | { |
| 174 | // we know something more |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 175 | _LOG_TRACE ("found hash in sync log"); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 176 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 177 | |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 178 | BytesPtr syncData = serializeGZipMsg (*msg); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 179 | m_ndnx->publishData(name, *syncData, FRESHNESS); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 180 | _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 181 | _LOG_TRACE (msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
| 185 | // we don't recognize the hash, send recover Interest if still don't know the hash after a randomized wait period |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 186 | double wait = m_recoverWaitGenerator->nextInterval(); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 187 | _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 188 | _LOG_TRACE ("recover task scheduled after wait: " << wait); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 189 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 190 | Scheduler::scheduleOneTimeTask (m_scheduler, |
| 191 | wait, boost::bind(&SyncCore::recover, this, hash), |
| 192 | "r-"+lexical_cast<string> (*hash)); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 193 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 196 | void |
| 197 | SyncCore::handleSyncInterestTimeout(const Name &name, const Closure &closure, Selectors selectors) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 198 | { |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 199 | // sync interest will be resent by scheduler |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 202 | void |
| 203 | SyncCore::handleRecoverInterestTimeout(const Name &name, const Closure &closure, Selectors selectors) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 204 | { |
| 205 | // We do not re-express recovery interest for now |
| 206 | // if difference is not resolved, the sync interest will trigger |
| 207 | // recovery anyway; so it's not so important to have recovery interest |
| 208 | // re-expressed |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 212 | SyncCore::handleRecoverData(const Name &name, PcoPtr content) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 213 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 214 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 215 | //cout << "handle recover data" << end; |
Alexander Afanasyev | 30ef922 | 2013-01-26 10:41:38 -0800 | [diff] [blame] | 216 | if (content && content->contentPtr () && content->contentPtr ()->size () > 0) |
| 217 | { |
| 218 | handleStateData(*content->contentPtr ()); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | _LOG_ERROR ("Got recovery DATA with empty content"); |
| 223 | } |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 224 | |
| 225 | // sendSyncInterest(); |
| 226 | m_scheduler->deleteTask (SYNC_INTEREST_TAG2); |
| 227 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
| 228 | bind(&SyncCore::sendSyncInterest, this), |
| 229 | SYNC_INTEREST_TAG2); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 233 | SyncCore::handleSyncData(const Name &name, PcoPtr content) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 234 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 235 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name); |
| 236 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 237 | // suppress recover in interest - data out of order case |
Alexander Afanasyev | 30ef922 | 2013-01-26 10:41:38 -0800 | [diff] [blame] | 238 | if (content && content->contentPtr () && content->contentPtr ()->size () > 0) |
| 239 | { |
| 240 | handleStateData(*content->contentPtr ()); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | _LOG_ERROR ("Got sync DATA with empty content"); |
| 245 | } |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 246 | |
| 247 | // resume outstanding sync interest |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 248 | // sendSyncInterest(); |
| 249 | |
| 250 | m_scheduler->deleteTask (SYNC_INTEREST_TAG2); |
| 251 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
| 252 | bind(&SyncCore::sendSyncInterest, this), |
| 253 | SYNC_INTEREST_TAG2); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void |
| 257 | SyncCore::handleStateData(const Bytes &content) |
| 258 | { |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 259 | SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content); |
| 260 | if(!(msg)) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 261 | { |
| 262 | // ignore misformed SyncData |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 263 | _LOG_ERROR ("Misformed SyncData"); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 267 | _LOG_TRACE (m_log->GetLocalName () << " receives Msg "); |
| 268 | _LOG_TRACE (msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 269 | int size = msg->state_size(); |
| 270 | int index = 0; |
| 271 | while (index < size) |
| 272 | { |
| 273 | SyncState state = msg->state(index); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 274 | string devStr = state.name(); |
| 275 | Name deviceName((const unsigned char *)devStr.c_str(), devStr.size()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 276 | // cout << "Got Name: " << deviceName; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 277 | if (state.type() == SyncState::UPDATE) |
| 278 | { |
| 279 | sqlite3_int64 seqno = state.seq(); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 280 | // cout << ", Got seq: " << seqno << endl; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 281 | m_log->UpdateDeviceSeqNo(deviceName, seqno); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 282 | if (state.has_locator()) |
| 283 | { |
| 284 | string locStr = state.locator(); |
| 285 | Name locatorName((const unsigned char *)locStr.c_str(), locStr.size()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 286 | // cout << ", Got loc: " << locatorName << endl; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 287 | m_log->UpdateLocator(deviceName, locatorName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 288 | |
| 289 | _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | else |
| 293 | { |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 294 | _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet"); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 295 | deregister(deviceName); |
| 296 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 297 | index++; |
| 298 | } |
| 299 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 300 | // find the actuall difference and invoke callback on the actual difference |
| 301 | HashPtr oldHash = m_rootHash; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 302 | m_rootHash = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 303 | // get diff with both new SeqNo and old SeqNo |
| 304 | SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 305 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 306 | if (diff->state_size() > 0) |
| 307 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 308 | m_stateMsgCallback (diff); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 309 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void |
| 313 | SyncCore::sendSyncInterest() |
| 314 | { |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 315 | Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes()); |
| 316 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 317 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 318 | |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 319 | Selectors selectors; |
| 320 | if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) |
| 321 | { |
| 322 | selectors.interestLifetime(m_syncInterestInterval); |
| 323 | } |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 324 | m_ndnx->sendInterest(syncInterest, |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 325 | Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2), |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 326 | boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1, _2, _3)), |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 327 | selectors); |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 328 | |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 329 | // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds from now |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 330 | // if no such task exists, it will be added |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 331 | m_scheduler->rescheduleTask(m_sendSyncInterestTask); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 335 | SyncCore::recover(HashPtr hash) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 336 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 337 | if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 338 | { |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 339 | _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ()); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 340 | // unfortunately we still don't recognize this hash |
| 341 | Bytes bytes; |
| 342 | readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 343 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 344 | // append the unknown hash |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 345 | Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes); |
| 346 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 347 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 348 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 349 | m_ndnx->sendInterest(recoverInterest, |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 350 | Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2), |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 351 | boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1, _2, _3))); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 352 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 353 | } |
| 354 | else |
| 355 | { |
| 356 | // we already learned the hash; cheers! |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void |
| 361 | SyncCore::deregister(const Name &name) |
| 362 | { |
| 363 | // Do nothing for now |
| 364 | // TODO: handle deregistering |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 367 | sqlite3_int64 |
| 368 | SyncCore::seq(const Name &name) |
| 369 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 370 | return m_log->SeqNo(name); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 371 | } |