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 | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 136 | hash = name.substr (pos + 1); |
| 137 | interestType = name.substr (0, pos); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 140 | DigestPtr digest = make_shared<Digest> (); |
| 141 | istringstream is (hash); |
| 142 | is >> *digest; |
| 143 | |
| 144 | return make_tuple (digest, interestType); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 148 | SyncLogic::respondSyncInterest (const string &name) |
| 149 | { |
| 150 | try |
| 151 | { |
| 152 | _LOG_TRACE ("<< I " << name); |
| 153 | |
| 154 | DigestConstPtr digest; |
| 155 | string type; |
| 156 | tie (digest, type) = convertNameToDigestAndType (name); |
| 157 | |
| 158 | if (type == "normal") // kind of ineffective... |
| 159 | { |
| 160 | processSyncInterest (name, digest); |
| 161 | } |
| 162 | else if (type == "recovery") |
| 163 | { |
| 164 | processSyncRecoveryInterest (name, digest); |
| 165 | } |
| 166 | } |
| 167 | catch (Error::DigestCalculationError &e) |
| 168 | { |
| 169 | _LOG_TRACE ("Something fishy happened..."); |
| 170 | // log error. ignoring it for now, later we should log it |
| 171 | return ; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer) |
| 177 | { |
| 178 | try |
| 179 | { |
| 180 | _LOG_TRACE ("<< D " << name); |
| 181 | |
| 182 | DigestConstPtr digest; |
| 183 | string type; |
| 184 | tie (digest, type) = convertNameToDigestAndType (name); |
| 185 | |
| 186 | processSyncData (name, digest, dataBuffer); |
| 187 | } |
| 188 | catch (Error::DigestCalculationError &e) |
| 189 | { |
| 190 | _LOG_TRACE ("Something fishy happened..."); |
| 191 | // log error. ignoring it for now, later we should log it |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void |
| 199 | SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 200 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 201 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 202 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 203 | // 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] | 204 | if (digest->isZero () && !m_state->getDigest()->isZero ()) |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 205 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 206 | sendSyncData (name, digest, m_state); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 207 | return; |
| 208 | } |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 209 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 210 | if (*m_state->getDigest() == *digest) |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 211 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 212 | _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT"); |
| 213 | m_syncInterestTable.insert (digest, name); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 214 | return; |
| 215 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 217 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 218 | |
| 219 | if (stateInDiffLog != m_log.end ()) |
| 220 | { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 221 | DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff (); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 222 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 223 | sendSyncData (name, digest, stateDiff); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
| 227 | if (!timedProcessing) |
| 228 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 229 | bool exists = m_syncInterestTable.insert (digest, name); |
| 230 | if (exists) // somebody else replied, so restart random-game timer |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 231 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 232 | _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer"); |
| 233 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 234 | } |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 235 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 236 | uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom); |
| 237 | _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] | 238 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 239 | m_scheduler.schedule (TIME_MILLISECONDS (waitDelay), |
| 240 | bind (&SyncLogic::processSyncInterest, this, name, digest, true), |
| 241 | DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 245 | BOOST_ASSERT (false); // not implemented yet |
| 246 | |
| 247 | // _LOG_TRACE (">> D " << *digest << " (timed processing)"); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 248 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 251 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 252 | SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 253 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 254 | DiffStatePtr diffLog = make_shared<DiffState> (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 255 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 256 | try |
| 257 | { |
| 258 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 259 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 260 | m_syncInterestTable.remove (name); // Remove satisfied interest from PIT |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 261 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 262 | DiffState diff; |
| 263 | istringstream ss (dataBuffer); |
| 264 | ss >> diff; |
| 265 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 266 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 267 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 268 | BOOST_ASSERT (diffLeaf != 0); |
| 269 | |
| 270 | NameInfoConstPtr info = diffLeaf->getInfo(); |
| 271 | if (diffLeaf->getOperation() == UPDATE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 272 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 273 | SeqNo seq = diffLeaf->getSeq(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 274 | |
| 275 | bool inserted = false; |
| 276 | bool updated = false; |
| 277 | SeqNo oldSeq; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 278 | tie (inserted, updated, oldSeq) = m_state->update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 279 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 280 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 281 | { |
| 282 | diffLog->update (info, seq); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 283 | m_onUpdate (info->toString (), seq, oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 286 | else if (diffLeaf->getOperation() == REMOVE) |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 287 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 288 | if (m_state->remove (info)) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 289 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 290 | diffLog->remove (info); |
| 291 | m_onRemove (info->toString ()); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 292 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | { |
| 296 | BOOST_ASSERT (false); // just in case |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 300 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 301 | } |
| 302 | catch (Error::SyncXmlDecodingFailure &e) |
| 303 | { |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame^] | 304 | _LOG_TRACE ("Something really fishy happened during state decoding " << |
| 305 | diagnostic_information (e)); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 306 | diffLog.reset (); |
| 307 | // don't do anything |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame^] | 310 | if (diffLog != 0) |
| 311 | { |
| 312 | BOOST_ASSERT (diffLog->getLeaves ().size () > 0); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 313 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame^] | 314 | 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] | 315 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame^] | 316 | // if state has changed, then it is safe to express a new interest |
| 317 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 318 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
| 319 | bind (&SyncLogic::sendSyncInterest, this), |
| 320 | REEXPRESSING_INTEREST); |
| 321 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 324 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 325 | SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 326 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 327 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 328 | |
| 329 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 330 | |
| 331 | if (stateInDiffLog == m_log.end ()) return; |
| 332 | |
| 333 | sendSyncData (name, digest, m_state); |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog) |
| 338 | { |
| 339 | uint32_t counter = 0; |
| 340 | while (m_syncInterestTable.size () > 0) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 341 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 342 | Interest interest = m_syncInterestTable.pop (); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 343 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 344 | _LOG_TRACE (">> D " << interest.m_name); |
| 345 | sendSyncData (interest.m_name, interest.m_digest, diffLog); |
| 346 | counter ++; |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 347 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 348 | _LOG_DEBUG ("Satisfied " << counter << " pending interests"); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 351 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 352 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 353 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 354 | diffLog->setDigest (m_state->getDigest()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 355 | if (m_log.size () > 0) |
| 356 | { |
| 357 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 358 | } |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 359 | 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] | 360 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 361 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 365 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 366 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 367 | DiffStatePtr diff; |
| 368 | { |
| 369 | //cout << "Add local names" <<endl; |
| 370 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 371 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 372 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 373 | _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ()); |
Alexander Afanasyev | 4261226 | 2012-05-05 10:58:37 -0700 | [diff] [blame] | 374 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 375 | SeqNo seqN (session, seq); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 376 | m_state->update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 377 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 378 | _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 379 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 380 | diff = make_shared<DiffState>(); |
| 381 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 382 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 385 | // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ()); |
| 386 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 390 | SyncLogic::remove(const string &prefix) |
| 391 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 392 | DiffStatePtr diff; |
| 393 | { |
| 394 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 395 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 396 | m_state->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 397 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 398 | diff = make_shared<DiffState>(); |
| 399 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 400 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 401 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 404 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 408 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 409 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 410 | ostringstream os; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 411 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 412 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 413 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 414 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 415 | os << m_syncPrefix << "/" << *m_state->getDigest(); |
| 416 | m_outstandingInterestName = os.str (); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 417 | _LOG_TRACE (">> I " << os.str ()); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 418 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 419 | |
| 420 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 421 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 422 | bind (&SyncLogic::sendSyncInterest, this), |
| 423 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 4261226 | 2012-05-05 10:58:37 -0700 | [diff] [blame] | 424 | |
| 425 | m_ccnxHandle->sendInterest (os.str (), |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 426 | bind (&SyncLogic::respondSyncData, this, _1, _2)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 429 | void |
| 430 | SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state) |
| 431 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 432 | // sending |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame^] | 433 | m_ccnxHandle->publishData (name, |
| 434 | lexical_cast<string> (*state), |
| 435 | m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 436 | |
Alexander Afanasyev | 282a10b | 2012-05-09 12:56:38 -0700 | [diff] [blame^] | 437 | // checking if our own interest got satisfied |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 438 | bool satisfiedOwnInterest = false; |
| 439 | { |
| 440 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 441 | satisfiedOwnInterest = (m_outstandingInterestName == name); |
| 442 | } |
| 443 | |
| 444 | if (satisfiedOwnInterest) |
| 445 | { |
| 446 | _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)"); |
| 447 | |
| 448 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 449 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
| 450 | bind (&SyncLogic::sendSyncInterest, this), |
| 451 | REEXPRESSING_INTEREST); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 456 | } |