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