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