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