Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | a9369b4 | 2017-01-11 11:58:00 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "sync-core.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 22 | #include "sync-state-helper.hpp" |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 23 | #include "core/logging.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/util/string-helper.hpp> |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace chronoshare { |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 32 | _LOG_INIT(Sync.Core); |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 33 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 34 | const int SyncCore::FRESHNESS = 2; |
| 35 | const std::string SyncCore::RECOVER = "RECOVER"; |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 36 | const double SyncCore::WAIT = 0.05; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 37 | const double SyncCore::RANDOM_PERCENT = 0.5; |
| 38 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 39 | SyncCore::SyncCore(Face& face, SyncLogPtr syncLog, const Name& userName, const Name& localPrefix, |
| 40 | const Name& syncPrefix, const StateMsgCallback& callback, |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 41 | time::seconds syncInterestInterval /*= -1.0*/) |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 42 | : m_face(face) |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 43 | , m_log(syncLog) |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 44 | , m_scheduler(m_face.getIoService()) |
| 45 | , m_syncInterestEvent(m_scheduler) |
| 46 | , m_periodicInterestEvent(m_scheduler) |
| 47 | , m_localStateDelayedEvent(m_scheduler) |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 48 | , m_stateMsgCallback(callback) |
| 49 | , m_syncPrefix(syncPrefix) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 50 | , m_recoverWaitGenerator( |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 51 | new RandomIntervalGenerator(WAIT, RANDOM_PERCENT, RandomIntervalGenerator::Direction::UP)) |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 52 | , m_syncInterestInterval(syncInterestInterval) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 53 | { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 54 | m_rootDigest = m_log->RememberStateInStateLog(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 55 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 56 | m_registeredPrefixId = |
| 57 | m_face.setInterestFilter(m_syncPrefix, bind(&SyncCore::handleInterest, this, _1, _2), |
| 58 | RegisterPrefixSuccessCallback(), |
| 59 | bind(&SyncCore::onRegisterFailed, this, _1, _2)); |
| 60 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | m_log->UpdateLocalLocator(localPrefix); |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 62 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 63 | time::seconds interval = |
| 64 | (m_syncInterestInterval > time::seconds(0) && m_syncInterestInterval < time::seconds(30)) ? |
| 65 | m_syncInterestInterval : time::seconds(4); |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 66 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 67 | m_periodicInterestEvent = |
| 68 | m_scheduler.scheduleEvent(interval, bind(&SyncCore::sendPeriodicSyncInterest, this, interval)); |
| 69 | |
| 70 | m_syncInterestEvent = |
| 71 | m_scheduler.scheduleEvent(time::milliseconds(100), bind(&SyncCore::sendSyncInterest, this)); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | SyncCore::sendPeriodicSyncInterest(const time::seconds& interval) |
| 76 | { |
| 77 | sendSyncInterest(); |
| 78 | m_periodicInterestEvent = |
| 79 | m_scheduler.scheduleEvent(interval, bind(&SyncCore::sendPeriodicSyncInterest, this, interval)); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | SyncCore::~SyncCore() |
| 83 | { |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 84 | // need to "deregister" closures |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 85 | m_face.unsetInterestFilter(m_registeredPrefixId); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 88 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 89 | SyncCore::updateLocalState(sqlite3_int64 seqno) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 90 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 91 | m_log->UpdateLocalSeqNo(seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 92 | localStateChanged(); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 93 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 94 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 95 | void |
| 96 | SyncCore::localStateChanged() |
| 97 | { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 98 | ConstBufferPtr oldDigest = m_rootDigest; |
| 99 | m_rootDigest = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] localStateChanged "); |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 102 | _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes: oldDigest--" << toHex(*oldDigest) |
| 103 | << " newDigest--" |
| 104 | << toHex(*m_rootDigest)); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 105 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 106 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*oldDigest, *m_rootDigest); |
| 107 | |
| 108 | // reply sync Interest with oldDigest as last component |
| 109 | |
| 110 | Name syncName(m_syncPrefix); |
| 111 | syncName.appendImplicitSha256Digest(oldDigest); |
| 112 | |
| 113 | BufferPtr syncData = serializeGZipMsg(*msg); |
| 114 | |
| 115 | // Create Data packet |
| 116 | shared_ptr<Data> data = make_shared<Data>(); |
| 117 | data->setName(syncName); |
| 118 | data->setFreshnessPeriod(time::seconds(FRESHNESS)); |
| 119 | data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size()); |
| 120 | m_keyChain.sign(*data); |
| 121 | m_face.put(*data); |
| 122 | |
| 123 | _LOG_TRACE(msg); |
| 124 | |
| 125 | // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no |
| 126 | // problem, we know the new root digest already; |
| 127 | // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets |
| 128 | // reversed at receivers |
| 129 | m_syncInterestEvent = |
| 130 | m_scheduler.scheduleEvent(time::milliseconds(50), bind(&SyncCore::sendSyncInterest, this)); |
| 131 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 133 | // sendSyncInterest(); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 137 | SyncCore::localStateChangedDelayed() |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 138 | { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 139 | // many calls to localStateChangedDelayed within 0.5 second will be suppressed to one |
| 140 | // localStateChanged calls |
| 141 | m_localStateDelayedEvent = |
| 142 | m_scheduler.scheduleEvent(time::milliseconds(500), bind(&SyncCore::localStateChanged, this)); |
| 143 | } |
| 144 | |
| 145 | // ------------------------------------------------------------------------------------ send & |
| 146 | // handle interest |
| 147 | |
| 148 | void |
| 149 | SyncCore::sendSyncInterest() |
| 150 | { |
| 151 | Name syncInterest(m_syncPrefix); |
| 152 | // syncInterest.append(name::Component(*m_rootDigest)); |
| 153 | syncInterest.appendImplicitSha256Digest(m_rootDigest); |
| 154 | |
| 155 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> send SYNC Interest for " << toHex(*m_rootDigest) |
| 156 | << ": " |
| 157 | << syncInterest); |
| 158 | |
| 159 | Interest interest(syncInterest); |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 160 | if (m_syncInterestInterval > time::seconds(0) && m_syncInterestInterval < time::seconds(30)) { |
| 161 | interest.setInterestLifetime(m_syncInterestInterval); |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | m_face.expressInterest(interest, bind(&SyncCore::handleSyncData, this, _1, _2), |
| 165 | bind(&SyncCore::handleSyncInterestTimeout, this, _1)); |
| 166 | |
| 167 | // // if there is a pending syncSyncInterest task, reschedule it to be m_syncInterestInterval seconds |
| 168 | // // from now |
| 169 | // // if no such task exists, it will be added |
| 170 | // m_scheduler->rescheduleTask(m_sendSyncInterestTask); |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 174 | SyncCore::recover(ConstBufferPtr digest) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 175 | { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 176 | if (!(*digest == *m_rootDigest) && m_log->LookupSyncLog(*digest) <= 0) { |
| 177 | _LOG_TRACE(m_log->GetLocalName() << ", Recover for received_Digest " << toHex(*digest)); |
| 178 | // unfortunately we still don't recognize this digest |
| 179 | // append the unknown digest |
| 180 | Name recoverInterest(m_syncPrefix); |
| 181 | recoverInterest.append(RECOVER).appendImplicitSha256Digest(digest); |
| 182 | |
| 183 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] >>> send RECOVER Interests for " << toHex(*digest)); |
| 184 | |
| 185 | m_face.expressInterest(recoverInterest, |
| 186 | bind(&SyncCore::handleRecoverData, this, _1, _2), |
| 187 | bind(&SyncCore::handleRecoverInterestTimeout, this, _1)); |
| 188 | } |
| 189 | else { |
| 190 | // we already learned the digest; cheers! |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | SyncCore::handleInterest(const InterestFilter& filter, const Interest& interest) |
| 196 | { |
| 197 | Name name = interest.getName(); |
| 198 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<< handleInterest with Name: " << name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 199 | int size = name.size(); |
| 200 | int prefixSize = m_syncPrefix.size(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 201 | if (size == prefixSize + 1) { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 202 | // this is normal sync interest |
| 203 | handleSyncInterest(name); |
| 204 | } |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 205 | else if (size == prefixSize + 2 && name.get(m_syncPrefix.size()).toUri() == RECOVER) { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 206 | // this is recovery interest |
| 207 | handleRecoverInterest(name); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 212 | SyncCore::handleSyncInterest(const Name& name) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 213 | { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 214 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< handle SYNC Interest with Name: " << name); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 215 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 216 | ConstBufferPtr digest = make_shared<Buffer>(name.get(-1).value(), name.get(-1).value_size()); |
| 217 | if (*digest == *m_rootDigest) { |
| 218 | // we have the same digest; nothing needs to be done |
| 219 | _LOG_TRACE("same as root digest: " << toHex(*digest)); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 222 | else if (m_log->LookupSyncLog(*digest) > 0) { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 223 | // we know something more |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 224 | _LOG_TRACE("found digest in sync log"); |
| 225 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*digest, *m_rootDigest); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 226 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 227 | BufferPtr syncData = serializeGZipMsg(*msg); |
| 228 | shared_ptr<Data> data = make_shared<Data>(); |
| 229 | data->setName(name); |
| 230 | data->setFreshnessPeriod(time::seconds(FRESHNESS)); |
| 231 | data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size()); |
| 232 | m_keyChain.sign(*data); |
| 233 | m_face.put(*data); |
| 234 | |
| 235 | _LOG_TRACE(m_log->GetLocalName() << " publishes: " << toHex(*digest) << " my_rootDigest:" |
| 236 | << toHex(*m_rootDigest)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 237 | _LOG_TRACE(msg); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 238 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 239 | else { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 240 | // we don't recognize the digest, send recover Interest if still don't know the digest after a |
| 241 | // randomized wait period |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 242 | double wait = m_recoverWaitGenerator->nextInterval(); |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 243 | _LOG_TRACE(m_log->GetLocalName() << ", my_rootDigest: " << toHex(*m_rootDigest) |
| 244 | << ", received_Digest: " |
| 245 | << toHex(*digest)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 246 | _LOG_TRACE("recover task scheduled after wait: " << wait); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 247 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 248 | // @todo Figure out how to cancel scheduled events when class is destroyed |
| 249 | m_scheduler.scheduleEvent(time::milliseconds(static_cast<int>(wait * 1000)), |
| 250 | bind(&SyncCore::recover, this, digest)); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 251 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 254 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 255 | SyncCore::handleRecoverInterest(const Name& name) |
| 256 | { |
| 257 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< handle RECOVER Interest with name " << name); |
| 258 | |
| 259 | ConstBufferPtr digest = make_shared<Buffer>(name.get(-1).value(), name.get(-1).value_size()); |
| 260 | // this is the digest unkonwn to the sender of the interest |
| 261 | _LOG_DEBUG("rootDigest: " << toHex(*digest)); |
| 262 | if (m_log->LookupSyncLog(*digest) > 0) { |
| 263 | _LOG_DEBUG("Find in our sync_log! " << toHex(*digest)); |
| 264 | // we know the digest, should reply everything and the newest thing, but not the digest!!! This |
| 265 | // is important |
| 266 | unsigned char _origin = 0; |
| 267 | BufferPtr origin = make_shared<Buffer>(_origin); |
| 268 | // std::cout << "size of origin " << origin->size() << std::endl; |
| 269 | SyncStateMsgPtr msg = m_log->FindStateDifferences(*origin, *m_rootDigest); |
| 270 | |
| 271 | BufferPtr syncData = serializeGZipMsg(*msg); |
| 272 | shared_ptr<Data> data = make_shared<Data>(); |
| 273 | data->setName(name); |
| 274 | data->setFreshnessPeriod(time::seconds(FRESHNESS)); |
| 275 | data->setContent(reinterpret_cast<const uint8_t*>(syncData->buf()), syncData->size()); |
| 276 | m_keyChain.sign(*data); |
| 277 | m_face.put(*data); |
| 278 | |
| 279 | _LOG_TRACE("[" << m_log->GetLocalName() << "] publishes " << toHex(*digest) |
| 280 | << " FindStateDifferences(0, m_rootDigest/" |
| 281 | << toHex(*m_rootDigest) |
| 282 | << ")"); |
| 283 | _LOG_TRACE(msg); |
| 284 | } |
| 285 | else { |
| 286 | // we don't recognize this digest, can not help |
| 287 | _LOG_DEBUG("we don't recognize this digest, can not help"); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | SyncCore::handleSyncInterestTimeout(const Interest& interest) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 293 | { |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 294 | // sync interest will be resent by scheduler |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 297 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 298 | SyncCore::handleRecoverInterestTimeout(const Interest& interest) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 299 | { |
| 300 | // We do not re-express recovery interest for now |
| 301 | // if difference is not resolved, the sync interest will trigger |
| 302 | // recovery anyway; so it's not so important to have recovery interest |
| 303 | // re-expressed |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 307 | SyncCore::handleSyncData(const Interest& interest, Data& data) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 308 | { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 309 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< receive SYNC DATA with interest: " |
| 310 | << interest.toUri()); |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 311 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 312 | const Block& content = data.getContent(); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 313 | // suppress recover in interest - data out of order case |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 314 | if (data.getContent().value() && content.size() > 0) { |
| 315 | handleStateData(Buffer(content.value(), content.value_size())); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 316 | } |
| 317 | else { |
| 318 | _LOG_ERROR("Got sync DATA with empty content"); |
| 319 | } |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 320 | |
| 321 | // resume outstanding sync interest |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 322 | m_syncInterestEvent = |
| 323 | m_scheduler.scheduleEvent(time::milliseconds(0), bind(&SyncCore::sendSyncInterest, this)); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 327 | SyncCore::handleRecoverData(const Interest& interest, Data& data) |
| 328 | { |
| 329 | _LOG_DEBUG("[" << m_log->GetLocalName() << "] <<<<< receive RECOVER DATA with interest: " |
| 330 | << interest.toUri()); |
| 331 | // cout << "handle recover data" << end; |
| 332 | const Block& content = data.getContent(); |
| 333 | if (content.value() && content.size() > 0) { |
| 334 | handleStateData(Buffer(content.value(), content.value_size())); |
| 335 | } |
| 336 | else { |
| 337 | _LOG_ERROR("Got recovery DATA with empty content"); |
| 338 | } |
| 339 | |
| 340 | m_syncInterestEvent = |
| 341 | m_scheduler.scheduleEvent(time::milliseconds(0), bind(&SyncCore::sendSyncInterest, this)); |
| 342 | } |
| 343 | |
| 344 | void |
| 345 | SyncCore::handleStateData(const Buffer& content) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 346 | { |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 347 | SyncStateMsgPtr msg = deserializeGZipMsg<SyncStateMsg>(content); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 348 | if (!(msg)) { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 349 | // ignore misformed SyncData |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 350 | _LOG_ERROR("Misformed SyncData"); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 354 | _LOG_TRACE("[" << m_log->GetLocalName() << "]" |
| 355 | << " receives Msg "); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 356 | _LOG_TRACE(msg); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 357 | int size = msg->state_size(); |
| 358 | int index = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 359 | while (index < size) { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 360 | SyncState state = msg->state(index); |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 361 | std::string devStr = state.name(); |
| 362 | Name deviceName(Block((const unsigned char*)devStr.c_str(), devStr.size())); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 363 | if (state.type() == SyncState::UPDATE) { |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 364 | sqlite3_int64 seqno = state.seq(); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 365 | m_log->UpdateDeviceSeqNo(deviceName, seqno); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 366 | if (state.has_locator()) { |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 367 | std::string locStr = state.locator(); |
| 368 | Name locatorName(Block((const unsigned char*)locStr.c_str(), locStr.size())); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 369 | m_log->UpdateLocator(deviceName, locatorName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 370 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 371 | _LOG_TRACE("self: " << m_log->GetLocalName() << ", device: " << deviceName << " < == > " |
| 372 | << locatorName); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 375 | else { |
| 376 | _LOG_ERROR("Receive SYNC DELETE, but we don't support it yet"); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 377 | deregister(deviceName); |
| 378 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 379 | index++; |
| 380 | } |
| 381 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 382 | // find the actuall difference and invoke callback on the actual difference |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 383 | ConstBufferPtr oldDigest = m_rootDigest; |
| 384 | m_rootDigest = m_log->RememberStateInStateLog(); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 385 | // get diff with both new SeqNo and old SeqNo |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 386 | SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldDigest, *m_rootDigest, true); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 387 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 388 | if (diff->state_size() > 0) { |
| 389 | m_stateMsgCallback(diff); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 390 | } |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 394 | SyncCore::deregister(const Name& name) |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 395 | { |
| 396 | // Do nothing for now |
| 397 | // TODO: handle deregistering |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 400 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 401 | SyncCore::seq(const Name& name) |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 402 | { |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 403 | return m_log->SeqNo(name); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 404 | } |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 405 | |
| 406 | } // namespace chronoshare |
| 407 | } // namespace ndn |