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