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