Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 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 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 22 | |
Alexander Afanasyev | b6cf56e | 2012-05-29 19:33:49 -0700 | [diff] [blame] | 23 | #ifdef NS3_MODULE |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 24 | #include <ns3/ccnx-pit.h> |
| 25 | #include <ns3/ccnx.h> |
Alexander Afanasyev | b6cf56e | 2012-05-29 19:33:49 -0700 | [diff] [blame] | 26 | #endif |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 27 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 28 | #include "sync-logic.h" |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 29 | #include "sync-diff-leaf.h" |
| 30 | #include "sync-full-leaf.h" |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 31 | #include "sync-log.h" |
| 32 | |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 33 | #include <boost/make_shared.hpp> |
| 34 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 35 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 36 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 37 | |
| 38 | using namespace std; |
| 39 | using namespace boost; |
| 40 | |
Chaoyi Bian | f007f92 | 2012-04-09 20:05:37 -0700 | [diff] [blame] | 41 | INIT_LOGGER ("SyncLogic"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 43 | #ifdef NS3_MODULE |
| 44 | #define GET_RANDOM(var) var.GetValue () |
| 45 | #else |
| 46 | #define GET_RANDOM(var) var () |
| 47 | #endif |
| 48 | |
| 49 | #define TIME_SECONDS_WITH_JITTER(sec) \ |
| 50 | (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
| 51 | |
Alexander Afanasyev | 4261226 | 2012-05-05 10:58:37 -0700 | [diff] [blame] | 52 | #define TIME_MILLISECONDS_WITH_JITTER(ms) \ |
| 53 | (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 54 | |
| 55 | |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 56 | namespace Sync |
| 57 | { |
| 58 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 59 | SyncLogic::SyncLogic (const std::string &syncPrefix, |
| 60 | LogicUpdateCallback onUpdate, |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 61 | LogicRemoveCallback onRemove) |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 62 | : m_state (new FullState) |
| 63 | , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress)) |
| 64 | , m_syncPrefix (syncPrefix) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 65 | , m_onUpdate (onUpdate) |
| 66 | , m_onRemove (onRemove) |
Alexander Afanasyev | 3f93c2b | 2012-03-13 00:02:31 -0700 | [diff] [blame] | 67 | , m_ccnxHandle(new CcnxWrapper()) |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 68 | , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 69 | #ifndef NS3_MODULE |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 70 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 71 | , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000)) |
| 72 | , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500)) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 73 | #else |
Alexander Afanasyev | 5560861 | 2012-04-30 13:24:50 -0700 | [diff] [blame] | 74 | , m_rangeUniformRandom (200,1000) |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 75 | , m_reexpressionJitter (10,500) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 76 | #endif |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 77 | { |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 78 | #ifndef NS3_MODULE |
| 79 | // In NS3 module these functions are moved to StartApplication method |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 81 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 82 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 84 | m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 85 | bind (&SyncLogic::sendSyncInterest, this), |
| 86 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 87 | #endif |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 90 | SyncLogic::~SyncLogic () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 91 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 92 | m_ccnxHandle.reset (); |
| 93 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 94 | |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 95 | #ifdef NS3_MODULE |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 96 | void |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 97 | SyncLogic::StartApplication () |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 98 | { |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 99 | m_ccnxHandle->SetNode (GetNode ()); |
| 100 | m_ccnxHandle->StartApplication (); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 101 | |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 102 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
| 103 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
| 104 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 105 | m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 106 | bind (&SyncLogic::sendSyncInterest, this), |
| 107 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 111 | SyncLogic::StopApplication () |
| 112 | { |
Alexander Afanasyev | 434f161 | 2012-04-21 21:19:15 -0700 | [diff] [blame] | 113 | m_ccnxHandle->clearInterestFilter (m_syncPrefix); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 114 | m_ccnxHandle->StopApplication (); |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 115 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 116 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 117 | } |
| 118 | #endif |
| 119 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Two types of intersts |
| 122 | * |
| 123 | * Normal name: .../<hash> |
| 124 | * Recovery name: .../recovery/<hash> |
| 125 | */ |
| 126 | boost::tuple<DigestConstPtr, std::string> |
| 127 | SyncLogic::convertNameToDigestAndType (const std::string &name) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 128 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 129 | BOOST_ASSERT (name.find (m_syncPrefix) == 0); |
| 130 | |
| 131 | string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ()); |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 132 | if (hash[0] == '/') |
| 133 | hash = hash.substr (1, hash.size ()-1); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 134 | string interestType = "normal"; |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 136 | size_t pos = hash.find ('/'); |
| 137 | if (pos != string::npos) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 138 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 139 | interestType = hash.substr (0, pos); |
| 140 | hash = hash.substr (pos + 1); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 143 | _LOG_TRACE (hash << ", " << interestType); |
| 144 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 145 | DigestPtr digest = make_shared<Digest> (); |
| 146 | istringstream is (hash); |
| 147 | is >> *digest; |
| 148 | |
| 149 | return make_tuple (digest, interestType); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 153 | SyncLogic::respondSyncInterest (const string &name) |
| 154 | { |
| 155 | try |
| 156 | { |
| 157 | _LOG_TRACE ("<< I " << name); |
| 158 | |
| 159 | DigestConstPtr digest; |
| 160 | string type; |
| 161 | tie (digest, type) = convertNameToDigestAndType (name); |
| 162 | |
| 163 | if (type == "normal") // kind of ineffective... |
| 164 | { |
| 165 | processSyncInterest (name, digest); |
| 166 | } |
| 167 | else if (type == "recovery") |
| 168 | { |
| 169 | processSyncRecoveryInterest (name, digest); |
| 170 | } |
| 171 | } |
| 172 | catch (Error::DigestCalculationError &e) |
| 173 | { |
| 174 | _LOG_TRACE ("Something fishy happened..."); |
| 175 | // log error. ignoring it for now, later we should log it |
| 176 | return ; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer) |
| 182 | { |
| 183 | try |
| 184 | { |
| 185 | _LOG_TRACE ("<< D " << name); |
| 186 | |
| 187 | DigestConstPtr digest; |
| 188 | string type; |
| 189 | tie (digest, type) = convertNameToDigestAndType (name); |
| 190 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 191 | if (type == "normal") |
| 192 | { |
| 193 | processSyncData (name, digest, dataBuffer); |
| 194 | } |
| 195 | else |
| 196 | { |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 197 | // timer is always restarted when we schedule recovery |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 198 | m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST); |
| 199 | processSyncData (name, digest, dataBuffer); |
| 200 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 201 | } |
| 202 | catch (Error::DigestCalculationError &e) |
| 203 | { |
| 204 | _LOG_TRACE ("Something fishy happened..."); |
| 205 | // log error. ignoring it for now, later we should log it |
| 206 | return; |
| 207 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | void |
| 212 | SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 213 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 214 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 215 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 216 | // Special case when state is not empty and we have received request with zero-root digest |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 217 | if (digest->isZero () && !m_state->getDigest()->isZero ()) |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 218 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 219 | sendSyncData (name, digest, m_state); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 220 | return; |
| 221 | } |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 222 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 223 | if (*m_state->getDigest() == *digest) |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 224 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 225 | _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT"); |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 226 | m_syncInterestTable.insert (digest, name, false); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 230 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 231 | |
| 232 | if (stateInDiffLog != m_log.end ()) |
| 233 | { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 234 | DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff (); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 235 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 236 | sendSyncData (name, digest, stateDiff); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
| 240 | if (!timedProcessing) |
| 241 | { |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 242 | bool exists = m_syncInterestTable.insert (digest, name, true); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 243 | if (exists) // somebody else replied, so restart random-game timer |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 244 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 245 | _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer"); |
| 246 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 247 | } |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 248 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 249 | uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom); |
| 250 | _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms"); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 251 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 252 | m_scheduler.schedule (TIME_MILLISECONDS (waitDelay), |
| 253 | bind (&SyncLogic::processSyncInterest, this, name, digest, true), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 254 | DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 258 | _LOG_TRACE (" (timed processing)"); |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 259 | |
| 260 | m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval; |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 261 | sendSyncRecoveryInterests (digest); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 262 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 265 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 266 | SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 267 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 268 | DiffStatePtr diffLog = make_shared<DiffState> (); |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 269 | bool ownInterestSatisfied = false; |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 270 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 271 | try |
| 272 | { |
| 273 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 274 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 275 | m_syncInterestTable.remove (name); // Remove satisfied interest from PIT |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 276 | |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 277 | ownInterestSatisfied = (name == m_outstandingInterestName); |
| 278 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 279 | DiffState diff; |
| 280 | istringstream ss (dataBuffer); |
| 281 | ss >> diff; |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 282 | vector<MissingDataInfo> v; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 283 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 284 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 285 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 286 | BOOST_ASSERT (diffLeaf != 0); |
| 287 | |
| 288 | NameInfoConstPtr info = diffLeaf->getInfo(); |
| 289 | if (diffLeaf->getOperation() == UPDATE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 290 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 291 | SeqNo seq = diffLeaf->getSeq(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 292 | |
| 293 | bool inserted = false; |
| 294 | bool updated = false; |
| 295 | SeqNo oldSeq; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 296 | tie (inserted, updated, oldSeq) = m_state->update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 297 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 298 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 299 | { |
| 300 | diffLog->update (info, seq); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 301 | //m_onUpdate (info->toString (), seq, oldSeq); |
Zhenkai Zhu | 68f04d5 | 2012-06-05 14:07:41 -0700 | [diff] [blame^] | 302 | if (!oldSeq.isValid()) |
| 303 | { |
| 304 | oldSeq = SeqNo(seq.getSession(), 0); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | ++oldSeq; |
| 309 | } |
| 310 | MissingDataInfo mdi = {info->toString(), oldSeq, seq}; |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 311 | v.push_back(mdi); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 314 | else if (diffLeaf->getOperation() == REMOVE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 315 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 316 | if (m_state->remove (info)) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 317 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 318 | diffLog->remove (info); |
| 319 | m_onRemove (info->toString ()); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 320 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 321 | } |
| 322 | else |
| 323 | { |
| 324 | BOOST_ASSERT (false); // just in case |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 328 | if (!v.empty()) |
| 329 | { |
| 330 | m_onUpdate(v); |
| 331 | } |
| 332 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 333 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 334 | } |
| 335 | catch (Error::SyncXmlDecodingFailure &e) |
| 336 | { |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 337 | _LOG_TRACE ("Something really fishy happened during state decoding " << |
| 338 | diagnostic_information (e)); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 339 | diffLog.reset (); |
| 340 | // don't do anything |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 343 | if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) || |
| 344 | ownInterestSatisfied) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 345 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 346 | // Do it only if everything went fine and state changed |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 347 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 348 | // this is kind of wrong |
| 349 | // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT, there is a point to satisfy them using new state |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 350 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 351 | // if state has changed, then it is safe to express a new interest |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 352 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 353 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 354 | bind (&SyncLogic::sendSyncInterest, this), |
| 355 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 356 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 359 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 360 | SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 361 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 362 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 363 | |
| 364 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 365 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 366 | if (stateInDiffLog == m_log.end ()) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 367 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 368 | _LOG_TRACE ("Could not find " << *digest << " in digest log"); |
| 369 | return; |
| 370 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 371 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 372 | sendSyncData (name, digest, m_state); |
| 373 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 374 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 375 | void |
| 376 | SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog) |
| 377 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 378 | DiffStatePtr fullStateLog = make_shared<DiffState> (); |
| 379 | { |
| 380 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 381 | BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/) |
| 382 | { |
| 383 | fullStateLog->update (leaf->getInfo (), leaf->getSeq ()); |
| 384 | /// @todo Impose limit on how many state info should be send out |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | try |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 389 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 390 | uint32_t counter = 0; |
| 391 | while (m_syncInterestTable.size () > 0) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 392 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 393 | Interest interest = m_syncInterestTable.pop (); |
| 394 | |
| 395 | if (!interest.m_unknown) |
| 396 | { |
| 397 | _LOG_TRACE (">> D " << interest.m_name); |
| 398 | sendSyncData (interest.m_name, interest.m_digest, diffLog); |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | _LOG_TRACE (">> D (unknown)" << interest.m_name); |
| 403 | sendSyncData (interest.m_name, interest.m_digest, fullStateLog); |
| 404 | } |
| 405 | counter ++; |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 406 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 407 | _LOG_DEBUG ("Satisfied " << counter << " pending interests"); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 408 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 409 | catch (Error::InterestTableIsEmpty &e) |
| 410 | { |
| 411 | // ok. not really an error |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 412 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 415 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 416 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 417 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 418 | diffLog->setDigest (m_state->getDigest()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 419 | if (m_log.size () > 0) |
| 420 | { |
| 421 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 422 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 423 | m_log.erase (m_state->getDigest()); // remove diff state with the same digest. next pointers are still valid |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 424 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 425 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 429 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 430 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 431 | DiffStatePtr diff; |
| 432 | { |
| 433 | //cout << "Add local names" <<endl; |
| 434 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 435 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 436 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 437 | _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ()); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 438 | |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 439 | SeqNo seqN (session, seq); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 440 | m_state->update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 441 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 442 | _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 443 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 444 | diff = make_shared<DiffState>(); |
| 445 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 446 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 449 | // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ()); |
| 450 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 454 | SyncLogic::remove(const string &prefix) |
| 455 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 456 | DiffStatePtr diff; |
| 457 | { |
| 458 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 459 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 460 | m_state->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 461 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 462 | diff = make_shared<DiffState>(); |
| 463 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 464 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 465 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 468 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 472 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 473 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 474 | ostringstream os; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 475 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 476 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 477 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 478 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 479 | os << m_syncPrefix << "/" << *m_state->getDigest(); |
| 480 | m_outstandingInterestName = os.str (); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 481 | _LOG_TRACE (">> I " << os.str ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 482 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 483 | |
| 484 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 485 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 486 | bind (&SyncLogic::sendSyncInterest, this), |
| 487 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 4261226 | 2012-05-05 10:58:37 -0700 | [diff] [blame] | 488 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 489 | m_ccnxHandle->sendInterestForString (os.str (), |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 490 | bind (&SyncLogic::respondSyncData, this, _1, _2)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 493 | void |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 494 | SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest) |
| 495 | { |
| 496 | ostringstream os; |
| 497 | os << m_syncPrefix << "/recovery/" << *digest; |
| 498 | _LOG_TRACE (">> I " << os.str ()); |
| 499 | |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 500 | TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval); |
| 501 | m_recoveryRetransmissionInterval <<= 1; |
| 502 | |
| 503 | m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST); |
| 504 | if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds |
| 505 | { |
| 506 | m_scheduler.schedule (nextRetransmission, |
| 507 | bind (&SyncLogic::sendSyncRecoveryInterests, this, digest), |
| 508 | REEXPRESSING_RECOVERY_INTEREST); |
| 509 | } |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 510 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 511 | m_ccnxHandle->sendInterestForString (os.str (), |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 512 | bind (&SyncLogic::respondSyncData, this, _1, _2)); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 517 | SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state) |
| 518 | { |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 519 | _LOG_TRACE (">> D " << name); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 520 | // sending |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 521 | m_ccnxHandle->publishStringData (name, |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 522 | lexical_cast<string> (*state), |
| 523 | m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 524 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame] | 525 | // checking if our own interest got satisfied |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 526 | bool satisfiedOwnInterest = false; |
| 527 | { |
| 528 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 529 | satisfiedOwnInterest = (m_outstandingInterestName == name); |
| 530 | } |
| 531 | |
| 532 | if (satisfiedOwnInterest) |
| 533 | { |
| 534 | _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)"); |
| 535 | |
| 536 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 537 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
| 538 | bind (&SyncLogic::sendSyncInterest, this), |
| 539 | REEXPRESSING_INTEREST); |
| 540 | } |
| 541 | } |
| 542 | |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 543 | string |
| 544 | SyncLogic::getRootDigest() |
| 545 | { |
| 546 | ostringstream os; |
| 547 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 548 | os << *m_state->getDigest(); |
| 549 | return os.str(); |
| 550 | } |
| 551 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 552 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 553 | } |