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); |
| 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 | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 240 | if (last == "state") |
| 241 | { |
| 242 | FullState full; |
| 243 | ss >> full; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 244 | BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 245 | { |
| 246 | NameInfoConstPtr info = leaf->getInfo (); |
| 247 | SeqNo seq = leaf->getSeq (); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 248 | |
| 249 | bool inserted = false; |
| 250 | bool updated = false; |
| 251 | SeqNo oldSeq; |
| 252 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 253 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 254 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 255 | { |
| 256 | diffLog->update (info, seq); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 257 | m_onUpdate (info->toString (), seq, oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 260 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 261 | else |
| 262 | { |
| 263 | DiffState diff; |
| 264 | ss >> diff; |
| 265 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
| 266 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 267 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 268 | BOOST_ASSERT (diffLeaf != 0); |
| 269 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 270 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 271 | if (diffLeaf->getOperation() == UPDATE) |
| 272 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 273 | SeqNo seq = diffLeaf->getSeq(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 274 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 275 | bool inserted = false; |
| 276 | bool updated = false; |
| 277 | SeqNo oldSeq; |
| 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 | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 286 | else if (diffLeaf->getOperation() == REMOVE) |
| 287 | { |
| 288 | if (m_state.remove (info)) |
| 289 | { |
| 290 | diffLog->remove (info); |
| 291 | m_onRemove (info->toString ()); |
| 292 | } |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | BOOST_ASSERT (false); // just in case |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 301 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 302 | } |
| 303 | catch (Error::SyncXmlDecodingFailure &e) |
| 304 | { |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 305 | diffLog.reset (); |
| 306 | // don't do anything |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 309 | // 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] | 310 | if (diffLog->getLeaves ().size () > 0) |
| 311 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 312 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 313 | bind (&SyncLogic::sendSyncInterest, this), |
| 314 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | { |
| 318 | // should not reexpress the same interest. Need at least wait for data lifetime |
| 319 | // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization |
| 320 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 321 | // m_scheduler.schedule (posix_time::seconds (0), |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 322 | m_scheduler.schedule (TIME_SECONDS (m_syncResponseFreshness) + TIME_MILLISECONDS (1), |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 323 | bind (&SyncLogic::sendSyncInterest, this), |
| 324 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 325 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 328 | void |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 329 | SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 330 | { |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 331 | vector<string> pis = m_syncInterestTable.fetchAll (); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 332 | if (pis.size () > 0) |
| 333 | { |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 334 | stringstream ss; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 335 | ss << *diffLog; |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 336 | bool satisfiedOwnInterest = false; |
| 337 | |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 338 | for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii) |
| 339 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 340 | _LOG_TRACE (">> D " << *ii); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 341 | m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 342 | |
| 343 | { |
| 344 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 345 | // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest)); |
| 346 | satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state")); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (satisfiedOwnInterest) |
| 351 | { |
| 352 | _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression"); |
| 353 | // we need to reexpress interest only if we satisfied our own interest |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 354 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 355 | bind (&SyncLogic::sendSyncInterest, this), |
| 356 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 361 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 362 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 363 | { |
| 364 | //cout << "Process Pending Interests" <<endl; |
| 365 | diffLog->setDigest (m_state.getDigest()); |
| 366 | if (m_log.size () > 0) |
| 367 | { |
| 368 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 369 | } |
| 370 | m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid |
| 371 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 372 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 373 | // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 377 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 378 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 379 | DiffStatePtr diff; |
| 380 | { |
| 381 | //cout << "Add local names" <<endl; |
| 382 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 383 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 384 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 385 | SeqNo seqN (session, seq); |
| 386 | m_state.update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 387 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 388 | _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ()); |
| 389 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 390 | diff = make_shared<DiffState>(); |
| 391 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 392 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 395 | // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ()); |
| 396 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 400 | SyncLogic::remove(const string &prefix) |
| 401 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 402 | DiffStatePtr diff; |
| 403 | { |
| 404 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 405 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
| 406 | m_state.remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 407 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 408 | diff = make_shared<DiffState>(); |
| 409 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 410 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 411 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 414 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 418 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 419 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 420 | ostringstream os; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 421 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 422 | { |
| 423 | // cout << "Sending Sync Interest" << endl; |
| 424 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 425 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 426 | os << m_syncPrefix << "/" << *m_state.getDigest(); |
| 427 | |
| 428 | _LOG_TRACE (">> I " << os.str ()); |
| 429 | |
| 430 | m_outstandingInterest = os.str (); |
| 431 | } |
| 432 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 433 | m_ccnxHandle->sendInterest (os.str (), |
| 434 | bind (&SyncLogic::processSyncData, this, _1, _2)); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 435 | |
| 436 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 437 | m_scheduler.schedule (TIME_SECONDS (4), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 438 | bind (&SyncLogic::sendSyncInterest, this), |
| 439 | REEXPRESSING_INTEREST); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 442 | } |