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