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