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