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> |
| 34 | #include <boost/date_time/posix_time/posix_time.hpp> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 35 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 36 | |
| 37 | using namespace std; |
| 38 | using namespace boost; |
| 39 | |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 40 | #ifndef _STANDALONE |
| 41 | INIT_LOGGER ("SyncLogic"); |
| 42 | #endif |
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 | |
| 45 | #ifdef NS3_MODULE |
| 46 | #define GET_RANDOM(var) var.GetValue () |
| 47 | #else |
| 48 | #define GET_RANDOM(var) var () |
| 49 | #endif |
| 50 | |
| 51 | #define TIME_SECONDS_WITH_JITTER(sec) \ |
| 52 | (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
| 53 | |
| 54 | #define TIME_MILLISECONDS_WITH_JITTER(sec) \ |
| 55 | (TIME_MILLISECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter))) |
| 56 | |
| 57 | |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 58 | namespace Sync |
| 59 | { |
| 60 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 61 | SyncLogic::SyncLogic (const std::string &syncPrefix, |
| 62 | LogicUpdateCallback onUpdate, |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 63 | LogicRemoveCallback onRemove) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 64 | : m_syncPrefix (syncPrefix) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 65 | , m_onUpdate (onUpdate) |
| 66 | , m_onRemove (onRemove) |
Alexander Afanasyev | 3f93c2b | 2012-03-13 00:02:31 -0700 | [diff] [blame] | 67 | , m_ccnxHandle(new CcnxWrapper()) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 68 | #ifndef NS3_MODULE |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 69 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 70 | , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (500,1000)) |
Alexander Afanasyev | 085742a | 2012-04-26 12:24:57 -0700 | [diff] [blame] | 71 | , m_reexpressionJitter (m_randomGenerator, uniform_int<> (0,100)) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 72 | #else |
Alexander Afanasyev | 5560861 | 2012-04-30 13:24:50 -0700 | [diff] [blame] | 73 | , m_rangeUniformRandom (200,1000) |
| 74 | , m_reexpressionJitter (100,500) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 75 | #endif |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 76 | { |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 77 | #ifdef _STANDALONE |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 78 | #ifdef _DEBUG |
| 79 | #ifdef HAVE_LOG4CXX |
| 80 | // _LOG_FUNCTION (syncPrefix); |
| 81 | static int id = 0; |
| 82 | staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id)); |
| 83 | id ++; |
| 84 | #endif |
| 85 | #endif |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 86 | #endif |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 87 | |
| 88 | #ifndef NS3_MODULE |
| 89 | // In NS3 module these functions are moved to StartApplication method |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 91 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 92 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 94 | m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 95 | bind (&SyncLogic::sendSyncInterest, this), |
| 96 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 97 | #endif |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 100 | SyncLogic::~SyncLogic () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 102 | // _LOG_FUNCTION (this); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 103 | // cout << "SyncLogic::~SyncLogic ()" << endl; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 105 | m_ccnxHandle.reset (); |
| 106 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 108 | #ifdef NS3_MODULE |
| 109 | void |
| 110 | SyncLogic::StartApplication () |
| 111 | { |
| 112 | m_ccnxHandle->SetNode (GetNode ()); |
| 113 | m_ccnxHandle->StartApplication (); |
| 114 | |
| 115 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
| 116 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
| 117 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 118 | 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] | 119 | bind (&SyncLogic::sendSyncInterest, this), |
| 120 | REEXPRESSING_INTEREST); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | SyncLogic::StopApplication () |
| 125 | { |
Alexander Afanasyev | 434f161 | 2012-04-21 21:19:15 -0700 | [diff] [blame] | 126 | m_ccnxHandle->clearInterestFilter (m_syncPrefix); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 127 | m_ccnxHandle->StopApplication (); |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 128 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 129 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 130 | } |
| 131 | #endif |
| 132 | |
| 133 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 134 | void |
| 135 | SyncLogic::respondSyncInterest (const string &interest) |
| 136 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 137 | _LOG_TRACE ("<< I " << interest); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 138 | //cout << "Respond Sync Interest" << endl; |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 139 | string hash = interest.substr(interest.find_last_of("/") + 1); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 140 | // cout << "Received Sync Interest: " << hash << endl; |
| 141 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 142 | DigestPtr digest = make_shared<Digest> (); |
| 143 | try |
| 144 | { |
| 145 | istringstream is (hash); |
| 146 | is >> *digest; |
| 147 | } |
| 148 | catch (Error::DigestCalculationError &e) |
| 149 | { |
| 150 | // log error. ignoring it for now, later we should log it |
| 151 | return; |
| 152 | } |
| 153 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 154 | processSyncInterest (digest, interest, false); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
| 158 | SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/) |
| 159 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 160 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 162 | // Special case when state is not empty and we have received request with zero-root digest |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 163 | if (digest->isZero () && !m_state.getDigest()->isZero ()) |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 164 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 165 | _LOG_TRACE (">> D " << interestName << "/state" << " (zero)"); |
| 166 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 167 | m_syncInterestTable.remove (interestName + "/state"); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 168 | m_ccnxHandle->publishData (interestName + "/state", |
| 169 | lexical_cast<string> (m_state), |
| 170 | m_syncResponseFreshness); |
| 171 | return; |
| 172 | } |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 174 | if (*m_state.getDigest() == *digest) |
| 175 | { |
| 176 | // cout << interestName << "\n"; |
| 177 | if (digest->isZero ()) |
| 178 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 179 | _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 180 | m_syncInterestTable.insert (interestName + "/state"); |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 181 | // _LOG_TRACE (*GetNode ()->GetObject<ns3::Ccnx> ()->GetPit ()); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 185 | _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 186 | m_syncInterestTable.insert (interestName); |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 187 | // _LOG_TRACE (*GetNode ()->GetObject<ns3::Ccnx> ()->GetPit ()); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 188 | } |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 189 | |
| 190 | if (m_outstandingInterest == interestName) |
| 191 | { |
| 192 | _LOG_DEBUG ("Cancelling interest reexpressing 1"); |
| 193 | // !!! important change !!! |
| 194 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 195 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress), |
| 196 | bind (&SyncLogic::sendSyncInterest, this), |
| 197 | REEXPRESSING_INTEREST); |
| 198 | } |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 201 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 202 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 203 | |
| 204 | if (stateInDiffLog != m_log.end ()) |
| 205 | { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 206 | DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff (); |
| 207 | // string state = lexical_cast<string> (*stateDiff); |
| 208 | // erase_all (state, "\n"); |
| 209 | // _LOG_TRACE (">> D " << interestName << ", state: " << state); |
| 210 | // _LOG_DEBUG ("Log size: " << m_log.size ()); |
| 211 | |
| 212 | // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ()) |
| 213 | // { |
| 214 | // string state = lexical_cast<string> (*ds); |
| 215 | // erase_all (state, "\n"); |
| 216 | // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ()); |
| 217 | // } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 218 | |
| 219 | m_syncInterestTable.remove (interestName); |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 220 | _LOG_DEBUG (">> D" << interestName); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 221 | m_ccnxHandle->publishData (interestName, |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 222 | lexical_cast<string> (*stateDiff), |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 223 | m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 224 | if (m_outstandingInterest == interestName) |
| 225 | { |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 226 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 227 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 228 | bind (&SyncLogic::sendSyncInterest, this), |
| 229 | REEXPRESSING_INTEREST); |
| 230 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (!timedProcessing) |
| 235 | { |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 236 | UnknownDigestContainer::index<hashed>::type::iterator previousUnknownDigest = m_recentUnknownDigests.get<hashed> ().find (digest); |
| 237 | if (previousUnknownDigest != m_recentUnknownDigests.get<hashed> ().end ()) |
| 238 | { |
| 239 | _LOG_DEBUG ("Digest is not in the log, but we have already seen it. (Don't do anything)"); |
| 240 | } |
| 241 | else |
| 242 | { |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame^] | 243 | bool exists = m_syncInterestTable.remove (interestName); |
| 244 | if (exists) |
| 245 | return; |
| 246 | |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 247 | // m_recentUnknownDigests.insert (DigestTime (digest, TIME_NOW + TIME_SECONDS (m_unknownDigestStoreTime))); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 248 | |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 249 | uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 250 | _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms"); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 251 | |
| 252 | m_scheduler.schedule (TIME_MILLISECONDS (waitDelay), |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 253 | bind (&SyncLogic::processSyncInterest, this, digest, interestName, true), |
| 254 | DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame^] | 255 | |
Alexander Afanasyev | 1f8f068 | 2012-04-26 13:00:14 -0700 | [diff] [blame] | 256 | // just in case, re-express our interest (e.g., probably something bad happened) |
| 257 | |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 258 | // m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 259 | // m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
| 260 | // bind (&SyncLogic::sendSyncInterest, this), |
| 261 | // REEXPRESSING_INTEREST); |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 262 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
Alexander Afanasyev | 280882c | 2012-04-30 02:33:00 -0700 | [diff] [blame] | 266 | // _LOG_TRACE (" (timed processing)"); |
| 267 | _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)"); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 268 | |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame^] | 269 | m_syncInterestTable.remove (interestName); |
Alexander Afanasyev | 280882c | 2012-04-30 02:33:00 -0700 | [diff] [blame] | 270 | m_ccnxHandle->publishData (interestName + "/state", |
| 271 | lexical_cast<string> (m_state), |
| 272 | m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 273 | |
Alexander Afanasyev | 0ac399e | 2012-04-27 13:28:28 -0700 | [diff] [blame] | 274 | // exit (1); |
| 275 | // if (m_outstandingInterest == interestName) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 276 | { |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 277 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 278 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 279 | bind (&SyncLogic::sendSyncInterest, this), |
| 280 | REEXPRESSING_INTEREST); |
| 281 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 282 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 285 | void |
| 286 | SyncLogic::processSyncData (const string &name, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 287 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 288 | _LOG_TRACE ("<< D " << name); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 289 | DiffStatePtr diffLog = make_shared<DiffState> (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 290 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 291 | try |
| 292 | { |
| 293 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 294 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 295 | string last = name.substr(name.find_last_of("/") + 1); |
| 296 | istringstream ss (dataBuffer); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 297 | |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 298 | m_syncInterestTable.remove (name); // just in case, remove both interests from our interest table. No reason to keep them there |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 299 | if (last == "state") |
| 300 | { |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 301 | m_syncInterestTable.remove (name.substr(0, name.find_last_of("/"))); |
| 302 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 303 | FullState full; |
| 304 | ss >> full; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 305 | BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 306 | { |
| 307 | NameInfoConstPtr info = leaf->getInfo (); |
| 308 | SeqNo seq = leaf->getSeq (); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 309 | |
| 310 | bool inserted = false; |
| 311 | bool updated = false; |
| 312 | SeqNo oldSeq; |
| 313 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 314 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 315 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 316 | { |
| 317 | diffLog->update (info, seq); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 318 | m_onUpdate (info->toString (), seq, oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 321 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 322 | else |
| 323 | { |
| 324 | DiffState diff; |
| 325 | ss >> diff; |
| 326 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
| 327 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 328 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 329 | BOOST_ASSERT (diffLeaf != 0); |
| 330 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 331 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 332 | if (diffLeaf->getOperation() == UPDATE) |
| 333 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 334 | SeqNo seq = diffLeaf->getSeq(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 335 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 336 | bool inserted = false; |
| 337 | bool updated = false; |
| 338 | SeqNo oldSeq; |
| 339 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 340 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 341 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 342 | { |
| 343 | diffLog->update (info, seq); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 344 | m_onUpdate (info->toString (), seq, oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 347 | else if (diffLeaf->getOperation() == REMOVE) |
| 348 | { |
| 349 | if (m_state.remove (info)) |
| 350 | { |
| 351 | diffLog->remove (info); |
| 352 | m_onRemove (info->toString ()); |
| 353 | } |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | BOOST_ASSERT (false); // just in case |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 362 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 363 | } |
| 364 | catch (Error::SyncXmlDecodingFailure &e) |
| 365 | { |
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 | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 370 | // if state has changed, then it is safe to express a new interest |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 371 | if (diffLog->getLeaves ().size () > 0) |
| 372 | { |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 373 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 374 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 375 | bind (&SyncLogic::sendSyncInterest, this), |
| 376 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 377 | } |
| 378 | else |
| 379 | { |
| 380 | // should not reexpress the same interest. Need at least wait for data lifetime |
| 381 | // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization |
| 382 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 383 | m_scheduler.schedule (TIME_MILLISECONDS_WITH_JITTER (m_syncResponseFreshness), |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 384 | bind (&SyncLogic::sendSyncInterest, this), |
| 385 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 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 | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 390 | SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 391 | { |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 392 | vector<string> pis = m_syncInterestTable.fetchAll (); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 393 | if (pis.size () > 0) |
| 394 | { |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 395 | stringstream ss; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 396 | ss << *diffLog; |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 397 | bool satisfiedOwnInterest = false; |
| 398 | |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 399 | for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii) |
| 400 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 401 | _LOG_TRACE (">> D " << *ii); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 402 | m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 403 | |
| 404 | { |
| 405 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 406 | // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest)); |
| 407 | satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state")); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (satisfiedOwnInterest) |
| 412 | { |
| 413 | _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression"); |
| 414 | // we need to reexpress interest only if we satisfied our own interest |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 415 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 416 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 417 | bind (&SyncLogic::sendSyncInterest, this), |
| 418 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 423 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 424 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 425 | { |
| 426 | //cout << "Process Pending Interests" <<endl; |
| 427 | diffLog->setDigest (m_state.getDigest()); |
| 428 | if (m_log.size () > 0) |
| 429 | { |
| 430 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 431 | } |
| 432 | m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid |
| 433 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 434 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 435 | // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 439 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 440 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 441 | DiffStatePtr diff; |
| 442 | { |
| 443 | //cout << "Add local names" <<endl; |
| 444 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 445 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 446 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 447 | SeqNo seqN (session, seq); |
| 448 | m_state.update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 449 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 450 | _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ()); |
| 451 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 452 | diff = make_shared<DiffState>(); |
| 453 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 454 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 457 | // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ()); |
| 458 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 462 | SyncLogic::remove(const string &prefix) |
| 463 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 464 | DiffStatePtr diff; |
| 465 | { |
| 466 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 467 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
| 468 | m_state.remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 469 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 470 | diff = make_shared<DiffState>(); |
| 471 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 472 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 473 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 476 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 480 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 481 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 482 | ostringstream os; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 483 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 484 | { |
| 485 | // cout << "Sending Sync Interest" << endl; |
| 486 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 487 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 488 | os << m_syncPrefix << "/" << *m_state.getDigest(); |
| 489 | |
| 490 | _LOG_TRACE (">> I " << os.str ()); |
| 491 | |
| 492 | m_outstandingInterest = os.str (); |
| 493 | } |
| 494 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 495 | m_ccnxHandle->sendInterest (os.str (), |
| 496 | bind (&SyncLogic::processSyncData, this, _1, _2)); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 497 | |
| 498 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 983b0e9 | 2012-04-26 12:44:18 -0700 | [diff] [blame] | 499 | m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 500 | bind (&SyncLogic::sendSyncInterest, this), |
| 501 | REEXPRESSING_INTEREST); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 504 | } |