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 | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 23 | #include "logging.h" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 24 | #include "random-interval-generator.h" |
| 25 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
| 28 | INIT_LOGGER ("Sync.Core"); |
| 29 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 30 | const string SyncCore::RECOVER = "RECOVER"; |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 31 | const double SyncCore::WAIT = 0.05; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 32 | const double SyncCore::RANDOM_PERCENT = 0.5; |
| 33 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 34 | using namespace boost; |
| 35 | |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 36 | // for debugging |
| 37 | static void |
| 38 | printMsg(SyncStateMsgPtr &msg) |
| 39 | { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 40 | _LOG_TRACE (" ===== start Msg ======"); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 41 | int size = msg->state_size(); |
| 42 | if (size > 0) |
| 43 | { |
| 44 | int index = 0; |
| 45 | while (index < size) |
| 46 | { |
| 47 | SyncState state = msg->state(index); |
| 48 | string strName = state.name(); |
| 49 | string strLocator = state.locator(); |
| 50 | sqlite3_int64 seq = state.seq(); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 51 | _LOG_TRACE ("Name: " << Name((const unsigned char *)strName.c_str(), strName.size()) |
| 52 | <<", Locator: " << Name((const unsigned char *)strLocator.c_str(), strLocator.size()) |
| 53 | << ", seq: " << seq); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 54 | index ++; |
| 55 | } |
| 56 | } |
| 57 | else |
| 58 | { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 59 | _LOG_TRACE ("Msg size 0"); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 60 | } |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 61 | _LOG_TRACE (" ++++++++ end Msg ++++++++ "); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 64 | SyncCore::SyncCore(SyncLogPtr syncLog, const Name &userName, const Name &localPrefix, const Name &syncPrefix, const StateMsgCallback &callback, const CcnxWrapperPtr &handle, const SchedulerPtr &scheduler) |
| 65 | : m_log(syncLog) |
| 66 | , m_scheduler(scheduler) |
Alexander Afanasyev | a44a7a2 | 2013-01-14 17:37:06 -0800 | [diff] [blame] | 67 | , m_stateMsgCallback(callback) |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 68 | // , m_userName(userName) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 69 | , m_syncPrefix(syncPrefix) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 70 | , m_handle(handle) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 71 | , m_syncClosure (boost::bind(&SyncCore::handleSyncData, this, _1, _2), |
| 72 | boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1)) |
| 73 | , m_recoverClosure (boost::bind(&SyncCore::handleRecoverData, this, _1, _2), |
| 74 | boost::bind(&SyncCore::handleRecoverInterestTimeout, this, _1)) |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 75 | , m_recoverWaitGenerator(new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::UP)) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 76 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 77 | m_rootHash = m_log->RememberStateInStateLog(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 78 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 79 | m_handle->setInterestFilter(m_syncPrefix, boost::bind(&SyncCore::handleInterest, this, _1)); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 80 | // m_log->initYP(m_yp); |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 81 | m_log->UpdateLocalLocator (localPrefix); |
| 82 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 83 | m_scheduler->start(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 84 | sendSyncInterest(); |
| 85 | } |
| 86 | |
| 87 | SyncCore::~SyncCore() |
| 88 | { |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 89 | // need to "deregister" closures |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 92 | // Name |
| 93 | // SyncCore::yp(const Name &deviceName) |
| 94 | // { |
| 95 | // Name locator; |
| 96 | // ReadLock lock(m_ypMutex); |
| 97 | // if (m_yp.find(deviceName) != m_yp.end()) |
| 98 | // { |
| 99 | // locator = m_yp[deviceName]; |
| 100 | // } |
| 101 | // else |
| 102 | // { |
| 103 | // cout << "self: " << m_userName << ", deviceName: " << deviceName << " not found in yp " << endl; |
| 104 | // } |
| 105 | // return locator; |
| 106 | // } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 108 | // void |
| 109 | // SyncCore::updateLocalPrefix(const Name &localPrefix) |
| 110 | // { |
| 111 | // m_localPrefix = localPrefix; |
| 112 | // // optionally, we can have a sync action to announce the new prefix |
| 113 | // // we are not doing this for now |
| 114 | // } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 115 | |
| 116 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 117 | SyncCore::updateLocalState(sqlite3_int64 seqno) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 118 | { |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 119 | m_log->UpdateLocalSeqNo (seqno); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 120 | // choose to update locator everytime |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 121 | // m_log->UpdateLocator(m_userName, m_localPrefix); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 122 | // { |
| 123 | // WriteLock lock(m_ypMutex); |
| 124 | // m_yp[m_userName] = m_localPrefix; |
| 125 | // } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 126 | HashPtr oldHash = m_rootHash; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 127 | m_rootHash = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 128 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 129 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldHash, *m_rootHash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 130 | |
| 131 | // reply sync Interest with oldHash as last component |
| 132 | Name syncName = constructSyncName(oldHash); |
| 133 | Bytes syncData; |
| 134 | msgToBytes(msg, syncData); |
| 135 | m_handle->publishData(syncName, syncData, FRESHNESS); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 136 | _LOG_DEBUG (m_log->GetLocalName () << " publishes: " << *oldHash); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 137 | printMsg(msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 138 | |
| 139 | // 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; |
| 140 | // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 141 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.1, |
| 142 | bind(&SyncCore::sendSyncInterest, this), |
| 143 | lexical_cast<string> (*m_rootHash)); |
| 144 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 145 | sendSyncInterest(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 149 | SyncCore::handleInterest(const Name &name) |
| 150 | { |
| 151 | int size = name.size(); |
| 152 | int prefixSize = m_syncPrefix.size(); |
| 153 | if (size == prefixSize + 1) |
| 154 | { |
| 155 | // this is normal sync interest |
| 156 | handleSyncInterest(name); |
| 157 | } |
| 158 | else if (size == prefixSize + 2 && name.getCompAsString(m_syncPrefix.size()) == RECOVER) |
| 159 | { |
| 160 | // this is recovery interest |
| 161 | handleRecoverInterest(name); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | SyncCore::handleRecoverInterest(const Name &name) |
| 167 | { |
| 168 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 169 | // this is the hash unkonwn to the sender of the interest |
| 170 | Hash hash(head(hashBytes), hashBytes.size()); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 171 | if (m_log->LookupSyncLog(hash) > 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 172 | { |
| 173 | // we know the hash, should reply everything |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 174 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*(Hash::Origin), *m_rootHash); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 175 | // DEBUG |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 176 | /* |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 177 | assert(msg->state_size() > 0); |
| 178 | int size = msg->state_size(); |
| 179 | int index = 0; |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 180 | cerr << "Reply recover interest with: " << endl; |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 181 | while (index < size) |
| 182 | { |
| 183 | SyncState state = msg->state(index); |
| 184 | string strName = state.name(); |
| 185 | string strLoc = state.locator(); |
| 186 | cout << "Name: " << Name((const unsigned char *)strName.c_str(), strName.size()) << ", Loc: " << Name((const unsigned char *)strLoc.c_str(), strLoc.size()) << ", seq: " << state.seq() << endl; |
| 187 | if (state.type() == SyncState::UPDATE) |
| 188 | { |
| 189 | cout << "Action: update" << endl; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | cout << "Action: delete" << endl; |
| 194 | } |
| 195 | index++; |
| 196 | } |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 197 | */ |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 198 | // END DEBUG |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 199 | Bytes syncData; |
| 200 | msgToBytes(msg, syncData); |
| 201 | m_handle->publishData(name, syncData, FRESHNESS); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 202 | _LOG_DEBUG (m_log->GetLocalName () << " publishes " << hash); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 203 | printMsg(msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
| 207 | // we don't recognize this hash, can not help |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 212 | SyncCore::handleSyncInterest(const Name &name) |
| 213 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 214 | Bytes hashBytes = name.getComp(name.size() - 1); |
| 215 | HashPtr hash(new Hash(head(hashBytes), hashBytes.size())); |
| 216 | if (*hash == *m_rootHash) |
| 217 | { |
| 218 | // we have the same hash; nothing needs to be done |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 219 | _LOG_DEBUG ("same as root hash: " << *hash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 222 | else if (m_log->LookupSyncLog(*hash) > 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 223 | { |
| 224 | // we know something more |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 225 | _LOG_DEBUG ("found hash in sync log"); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 226 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*hash, *m_rootHash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 227 | |
| 228 | Bytes syncData; |
| 229 | msgToBytes(msg, syncData); |
| 230 | m_handle->publishData(name, syncData, FRESHNESS); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 231 | _LOG_DEBUG (m_log->GetLocalName () << " publishes: " << *hash); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 232 | printMsg(msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
| 236 | // 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] | 237 | double wait = m_recoverWaitGenerator->nextInterval(); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 238 | _LOG_DEBUG (m_log->GetLocalName () << ", rootHash: " << *m_rootHash << ", hash: " << *hash); |
| 239 | _LOG_DEBUG ("recover task scheduled after wait: " << wait); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 241 | Scheduler::scheduleOneTimeTask (m_scheduler, |
| 242 | wait, boost::bind(&SyncCore::recover, this, hash), |
| 243 | "r-"+lexical_cast<string> (*hash)); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 244 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | Closure::TimeoutCallbackReturnValue |
| 248 | SyncCore::handleSyncInterestTimeout(const Name &name) |
| 249 | { |
| 250 | // sendInterestInterest with the current root hash; |
| 251 | sendSyncInterest(); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 252 | return Closure::RESULT_OK; |
| 253 | } |
| 254 | |
| 255 | Closure::TimeoutCallbackReturnValue |
| 256 | SyncCore::handleRecoverInterestTimeout(const Name &name) |
| 257 | { |
| 258 | // We do not re-express recovery interest for now |
| 259 | // if difference is not resolved, the sync interest will trigger |
| 260 | // recovery anyway; so it's not so important to have recovery interest |
| 261 | // re-expressed |
| 262 | return Closure::RESULT_OK; |
| 263 | } |
| 264 | |
| 265 | void |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 266 | SyncCore::handleRecoverData(const Name &name, PcoPtr content) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 267 | { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 268 | //cout << "handle recover data" << end; |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 269 | handleStateData(*content->contentPtr ()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 270 | sendSyncInterest(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 274 | SyncCore::handleSyncData(const Name &name, PcoPtr content) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 275 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 276 | // suppress recover in interest - data out of order case |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 277 | handleStateData(*content->contentPtr ()); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 278 | |
| 279 | // resume outstanding sync interest |
| 280 | sendSyncInterest(); |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | SyncCore::handleStateData(const Bytes &content) |
| 285 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 286 | SyncStateMsgPtr msg(new SyncStateMsg); |
| 287 | bool success = msg->ParseFromArray(head(content), content.size()); |
| 288 | if(!success) |
| 289 | { |
| 290 | // ignore misformed SyncData |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 291 | _LOG_ERROR ("Misformed SyncData"); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 295 | _LOG_DEBUG (m_log->GetLocalName () << " receives Msg "); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 296 | printMsg (msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 297 | int size = msg->state_size(); |
| 298 | int index = 0; |
| 299 | while (index < size) |
| 300 | { |
| 301 | SyncState state = msg->state(index); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 302 | string devStr = state.name(); |
| 303 | Name deviceName((const unsigned char *)devStr.c_str(), devStr.size()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 304 | // cout << "Got Name: " << deviceName; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 305 | if (state.type() == SyncState::UPDATE) |
| 306 | { |
| 307 | sqlite3_int64 seqno = state.seq(); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 308 | // cout << ", Got seq: " << seqno << endl; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 309 | m_log->UpdateDeviceSeqNo(deviceName, seqno); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 310 | if (state.has_locator()) |
| 311 | { |
| 312 | string locStr = state.locator(); |
| 313 | Name locatorName((const unsigned char *)locStr.c_str(), locStr.size()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 314 | // cout << ", Got loc: " << locatorName << endl; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 315 | m_log->UpdateLocator(deviceName, locatorName); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 316 | // WriteLock lock(m_ypMutex); |
| 317 | // m_yp[deviceName] = locatorName; |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 318 | _LOG_DEBUG ("self: " << m_log->GetLocalName () << ", device: " << deviceName << " < == > " << locatorName); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | else |
| 322 | { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 323 | _LOG_TRACE ("nani"); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 324 | deregister(deviceName); |
| 325 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 326 | index++; |
| 327 | } |
| 328 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 329 | // find the actuall difference and invoke callback on the actual difference |
| 330 | HashPtr oldHash = m_rootHash; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 331 | m_rootHash = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 332 | // get diff with both new SeqNo and old SeqNo |
| 333 | SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 334 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 335 | if (diff->state_size() > 0) |
| 336 | { |
| 337 | m_stateMsgCallback(diff); |
| 338 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
| 342 | SyncCore::sendSyncInterest() |
| 343 | { |
| 344 | Name syncInterest = constructSyncName(m_rootHash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 345 | m_handle->sendInterest(syncInterest, m_syncClosure); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 346 | _LOG_DEBUG (m_log->GetLocalName () << " send SYNC interest: " << *m_rootHash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void |
| 350 | SyncCore::recover(const HashPtr &hash) |
| 351 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 352 | if (!(*hash == *m_rootHash) && m_log->LookupSyncLog(*hash) <= 0) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 353 | { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 354 | _LOG_DEBUG (m_log->GetLocalName () << ", Recover for: " << *hash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 355 | // unfortunately we still don't recognize this hash |
| 356 | Bytes bytes; |
| 357 | readRaw(bytes, (const unsigned char *)hash->GetHash(), hash->GetHashBytes()); |
| 358 | Name recoverInterest = m_syncPrefix; |
| 359 | recoverInterest.appendComp(RECOVER); |
| 360 | // append the unknown hash |
| 361 | recoverInterest.appendComp(bytes); |
| 362 | m_handle->sendInterest(recoverInterest, m_recoverClosure); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame^] | 363 | _LOG_DEBUG (m_log->GetLocalName () << " send RECOVER Interest: " << *hash); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 364 | } |
| 365 | else |
| 366 | { |
| 367 | // we already learned the hash; cheers! |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | SyncCore::deregister(const Name &name) |
| 373 | { |
| 374 | // Do nothing for now |
| 375 | // TODO: handle deregistering |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | Name |
| 379 | SyncCore::constructSyncName(const HashPtr &hash) |
| 380 | { |
| 381 | Bytes bytes; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 382 | readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes()); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 383 | Name syncName = m_syncPrefix; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 384 | syncName.appendComp(bytes); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 385 | return syncName; |
| 386 | } |
| 387 | |
| 388 | void |
| 389 | SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes) |
| 390 | { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 391 | int size = msg->ByteSize(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 392 | bytes.resize(size); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 393 | msg->SerializeToArray(head(bytes), size); |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 394 | } |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 395 | |
| 396 | sqlite3_int64 |
| 397 | SyncCore::seq(const Name &name) |
| 398 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 399 | return m_log->SeqNo(name); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 400 | } |