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