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