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