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