Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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 | * |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "sync-core.h" |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 23 | #include "sync-state-helper.h" |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 24 | #include "logging.h" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 25 | #include "random-interval-generator.h" |
Zhenkai Zhu | 3566f2f | 2013-01-25 23:47:59 -0800 | [diff] [blame^] | 26 | #include "simple-interval-generator.h" |
| 27 | #include "periodic-task.h" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 30 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 31 | |
| 32 | INIT_LOGGER ("Sync.Core"); |
| 33 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 34 | const string SyncCore::RECOVER = "RECOVER"; |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 35 | const double SyncCore::WAIT = 0.05; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 36 | const double SyncCore::RANDOM_PERCENT = 0.5; |
| 37 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 38 | using namespace boost; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 39 | using namespace Ccnx; |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 41 | SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix, |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 42 | const StateMsgCallback &callback, CcnxWrapperPtr ccnx, double syncInterestInterval/*= -1.0*/) |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 43 | : m_ccnx (ccnx) |
| 44 | , m_log(syncLog) |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 45 | , m_scheduler(new Scheduler ()) |
| 46 | , m_stateMsgCallback(callback) |
| 47 | , m_syncPrefix(syncPrefix) |
| 48 | , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP)) |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 49 | , m_syncInterestInterval(syncInterestInterval) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 50 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 51 | m_rootHash = m_log->RememberStateInStateLog(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 53 | m_ccnx->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1)); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 54 | // m_log->initYP(m_yp); |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 55 | m_log->UpdateLocalLocator (localPrefix); |
| 56 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 57 | m_scheduler->start(); |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 58 | string tag = userName.toString() + "send-sync-interest"; |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 59 | double interval = (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) ? m_syncInterestInterval : 4.0; |
Zhenkai Zhu | 3566f2f | 2013-01-25 23:47:59 -0800 | [diff] [blame^] | 60 | m_sendSyncInterestTask = make_shared<PeriodicTask>(bind(&SyncCore::sendSyncInterest, this), tag, m_scheduler, make_shared<SimpleIntervalGenerator>(interval)); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 61 | sendSyncInterest(); |
| 62 | } |
| 63 | |
| 64 | SyncCore::~SyncCore() |
| 65 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 66 | // m_scheduler->shutdown (); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 67 | // need to "deregister" closures |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 70 | void |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 71 | SyncCore::updateLocalPrefix (const Name &localPrefix) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 73 | m_log->UpdateLocalLocator (localPrefix); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 77 | SyncCore::updateLocalState(sqlite3_int64 seqno) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 78 | { |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 79 | m_log->UpdateLocalSeqNo (seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 80 | localStateChanged(); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 81 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 82 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 83 | void |
| 84 | SyncCore::localStateChanged() |
| 85 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 86 | HashPtr oldHash = m_rootHash; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 87 | m_rootHash = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 88 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 89 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 90 | |
| 91 | // reply sync Interest with oldHash as last component |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 92 | Name syncName = Name (m_syncPrefix)(oldHash->GetHash(), oldHash->GetHashBytes()); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 93 | BytesPtr syncData = serializeMsg (*msg); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 94 | |
| 95 | m_ccnx->publishData(syncName, *syncData, FRESHNESS); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 96 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] localStateChanged "); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 97 | _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes: " << oldHash->shortHash ()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 98 | // _LOG_TRACE (msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 99 | |
| 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), |
| 104 | lexical_cast<string> (*m_rootHash)); |
| 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 |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 110 | SyncCore::handleInterest(const Name &name) |
| 111 | { |
| 112 | int size = name.size(); |
| 113 | int prefixSize = m_syncPrefix.size(); |
| 114 | if (size == prefixSize + 1) |
| 115 | { |
| 116 | // this is normal sync interest |
| 117 | handleSyncInterest(name); |
| 118 | } |
| 119 | else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER) |
| 120 | { |
| 121 | // this is recovery interest |
| 122 | handleRecoverInterest(name); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | SyncCore::handleRecoverInterest(const Name &name) |
| 128 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 129 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER Interest with name " << name); |
| 130 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 131 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 132 | // this is the hash unkonwn to the sender of the interest |
| 133 | Hash hash(head(hashBytes), hashBytes.size()); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 134 | if (m_log->LookupSyncLog(hash) > 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 135 | { |
| 136 | // we know the hash, should reply everything |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 137 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 139 | BytesPtr syncData = serializeMsg (*msg); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 140 | m_ccnx->publishData(name, *syncData, FRESHNESS); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 141 | _LOG_TRACE ("[" << m_log->GetLocalName () << "] publishes " << hash.shortHash ()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 142 | // _LOG_TRACE (msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 143 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 144 | else |
| 145 | { |
| 146 | // we don't recognize this hash, can not help |
| 147 | } |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 151 | SyncCore::handleSyncInterest(const Name &name) |
| 152 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 153 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC Interest with name " << name); |
| 154 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 155 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 156 | HashPtr hash(new Hash(head(hashBytes), hashBytes.size())); |
| 157 | if (*hash == *m_rootHash) |
| 158 | { |
| 159 | // we have the same hash; nothing needs to be done |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 160 | _LOG_TRACE ("same as root hash: " << hash->shortHash ()); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 161 | return; |
| 162 | } |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 163 | else if (m_log->LookupSyncLog(*hash) > 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 164 | { |
| 165 | // we know something more |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 166 | _LOG_TRACE ("found hash in sync log"); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 167 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 169 | BytesPtr syncData = serializeMsg (*msg); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 170 | m_ccnx->publishData(name, *syncData, FRESHNESS); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 171 | _LOG_TRACE (m_log->GetLocalName () << " publishes: " << hash->shortHash ()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 172 | _LOG_TRACE (msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
| 176 | // 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] | 177 | double wait = m_recoverWaitGenerator->nextInterval(); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 178 | _LOG_TRACE (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << hash->shortHash ()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 179 | _LOG_TRACE ("recover task scheduled after wait: " << wait); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 181 | Scheduler::scheduleOneTimeTask (m_scheduler, |
| 182 | wait, boost::bind(&SyncCore::recover, this, hash), |
| 183 | "r-"+lexical_cast<string> (*hash)); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 184 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | Closure::TimeoutCallbackReturnValue |
| 188 | SyncCore::handleSyncInterestTimeout(const Name &name) |
| 189 | { |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 190 | // sync interest will be resent by scheduler |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 191 | return Closure::RESULT_OK; |
| 192 | } |
| 193 | |
| 194 | Closure::TimeoutCallbackReturnValue |
| 195 | SyncCore::handleRecoverInterestTimeout(const Name &name) |
| 196 | { |
| 197 | // We do not re-express recovery interest for now |
| 198 | // if difference is not resolved, the sync interest will trigger |
| 199 | // recovery anyway; so it's not so important to have recovery interest |
| 200 | // re-expressed |
| 201 | return Closure::RESULT_OK; |
| 202 | } |
| 203 | |
| 204 | void |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 205 | SyncCore::handleRecoverData(const Name &name, PcoPtr content) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 206 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 207 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< RECOVER DATA with name: " << name); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 208 | //cout << "handle recover data" << end; |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 209 | handleStateData(*content->contentPtr ()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 210 | sendSyncInterest(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 214 | SyncCore::handleSyncData(const Name &name, PcoPtr content) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 215 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 216 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] <<<<< SYNC DATA with name: " << name); |
| 217 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 218 | // suppress recover in interest - data out of order case |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 219 | handleStateData(*content->contentPtr ()); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 220 | |
| 221 | // resume outstanding sync interest |
| 222 | sendSyncInterest(); |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | SyncCore::handleStateData(const Bytes &content) |
| 227 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 228 | SyncStateMsgPtr msg(new SyncStateMsg); |
| 229 | bool success = msg->ParseFromArray(head(content), content.size()); |
| 230 | if(!success) |
| 231 | { |
| 232 | // ignore misformed SyncData |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 233 | _LOG_ERROR ("Misformed SyncData"); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 237 | _LOG_TRACE (m_log->GetLocalName () << " receives Msg "); |
| 238 | _LOG_TRACE (msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 239 | int size = msg->state_size(); |
| 240 | int index = 0; |
| 241 | while (index < size) |
| 242 | { |
| 243 | SyncState state = msg->state(index); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 244 | string devStr = state.name(); |
| 245 | Name deviceName((const unsigned char *)devStr.c_str(), devStr.size()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 246 | // cout << "Got Name: " << deviceName; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 247 | if (state.type() == SyncState::UPDATE) |
| 248 | { |
| 249 | sqlite3_int64 seqno = state.seq(); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 250 | // cout << ", Got seq: " << seqno << endl; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 251 | m_log->UpdateDeviceSeqNo(deviceName, seqno); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 252 | if (state.has_locator()) |
| 253 | { |
| 254 | string locStr = state.locator(); |
| 255 | Name locatorName((const unsigned char *)locStr.c_str(), locStr.size()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 256 | // cout << ", Got loc: " << locatorName << endl; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 257 | m_log->UpdateLocator(deviceName, locatorName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 258 | |
| 259 | _LOG_TRACE ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | else |
| 263 | { |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 264 | _LOG_ERROR ("Receive SYNC DELETE, but we don't support it yet"); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 265 | deregister(deviceName); |
| 266 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 267 | index++; |
| 268 | } |
| 269 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 270 | // find the actuall difference and invoke callback on the actual difference |
| 271 | HashPtr oldHash = m_rootHash; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 272 | m_rootHash = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 273 | // get diff with both new SeqNo and old SeqNo |
| 274 | SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 275 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 276 | if (diff->state_size() > 0) |
| 277 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 278 | m_stateMsgCallback (diff); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 279 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void |
| 283 | SyncCore::sendSyncInterest() |
| 284 | { |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 285 | Name syncInterest = Name (m_syncPrefix)(m_rootHash->GetHash(), m_rootHash->GetHashBytes()); |
| 286 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 287 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> SYNC Interest for " << m_rootHash->shortHash () << ": " << syncInterest); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 288 | |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 289 | Selectors selectors; |
| 290 | if (m_syncInterestInterval > 0 && m_syncInterestInterval < 30.0) |
| 291 | { |
| 292 | selectors.interestLifetime(m_syncInterestInterval); |
| 293 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 294 | m_ccnx->sendInterest(syncInterest, |
| 295 | Closure (boost::bind(&SyncCore::handleSyncData, this, _1, _2), |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 296 | boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)), |
| 297 | selectors); |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 298 | |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 299 | // 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] | 300 | // if no such task exists, it will be added |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 301 | m_scheduler->rescheduleTask(m_sendSyncInterestTask); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void |
| 305 | SyncCore::recover(const HashPtr &hash) |
| 306 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 307 | if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 308 | { |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 309 | _LOG_TRACE (m_log->GetLocalName () << ", Recover for: " << hash->shortHash ()); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 310 | // unfortunately we still don't recognize this hash |
| 311 | Bytes bytes; |
| 312 | readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 313 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 314 | // append the unknown hash |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 315 | Name recoverInterest = Name (m_syncPrefix)(RECOVER)(bytes); |
| 316 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 317 | _LOG_DEBUG ("[" << m_log->GetLocalName () << "] >>> RECOVER Interests for " << hash->shortHash ()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 318 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 319 | m_ccnx->sendInterest(recoverInterest, |
| 320 | Closure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2), |
| 321 | boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1))); |
| 322 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 323 | } |
| 324 | else |
| 325 | { |
| 326 | // we already learned the hash; cheers! |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | SyncCore::deregister(const Name &name) |
| 332 | { |
| 333 | // Do nothing for now |
| 334 | // TODO: handle deregistering |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 337 | sqlite3_int64 |
| 338 | SyncCore::seq(const Name &name) |
| 339 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 340 | return m_log->SeqNo(name); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 341 | } |