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> |
| 19 | * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| 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" |
| 26 | #include <boost/make_shared.hpp> |
| 27 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
| 29 | #include <boost/date_time/posix_time/posix_time.hpp> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 30 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace std; |
| 33 | using namespace boost; |
| 34 | |
| 35 | namespace Sync |
| 36 | { |
| 37 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 38 | SyncLogic::SyncLogic (const std::string &syncPrefix, |
| 39 | LogicUpdateCallback onUpdate, |
| 40 | LogicRemoveCallback onRemove, |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 41 | CcnxWrapperPtr ccnxHandle) |
| 42 | : m_syncPrefix (syncPrefix) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 43 | , m_onUpdate (onUpdate) |
| 44 | , m_onRemove (onRemove) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 45 | , m_ccnxHandle (ccnxHandle) |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 46 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
| 47 | , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,100)) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 49 | m_ccnxHandle->setInterestFilter (syncPrefix, |
| 50 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 53 | SyncLogic::~SyncLogic () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 54 | { |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 55 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 57 | |
| 58 | void |
| 59 | SyncLogic::respondSyncInterest (const string &interest) |
| 60 | { |
| 61 | string hash = interest.substr(interest.find_last_of("/") + 1); |
| 62 | DigestPtr digest = make_shared<Digest> (); |
| 63 | try |
| 64 | { |
| 65 | istringstream is (hash); |
| 66 | is >> *digest; |
| 67 | } |
| 68 | catch (Error::DigestCalculationError &e) |
| 69 | { |
| 70 | // log error. ignoring it for now, later we should log it |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | processSyncInterest (digest, interest); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/) |
| 79 | { |
| 80 | // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 81 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 82 | |
| 83 | if (*m_state.getDigest() == *digest) |
| 84 | { |
| 85 | m_syncInterestTable.insert (interestName); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 90 | |
| 91 | if (stateInDiffLog != m_log.end ()) |
| 92 | { |
| 93 | m_ccnxHandle->publishData (interestName, |
| 94 | lexical_cast<string> (*(*stateInDiffLog)->diff ()), |
| 95 | m_syncResponseFreshness); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (!timedProcessing) |
| 100 | { |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 101 | m_delayedChecksScheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/, |
| 102 | bind (&SyncLogic::processSyncInterest, this, digest, interestName, true)); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
| 107 | m_ccnxHandle->publishData (interestName + "/state", |
| 108 | lexical_cast<string> (m_state), |
| 109 | m_syncResponseFreshness); |
| 110 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 113 | void |
| 114 | SyncLogic::processSyncData (const string &name, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 115 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 116 | DiffStatePtr diffLog = make_shared<DiffState> (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 118 | try |
| 119 | { |
| 120 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 121 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 122 | string last = name.substr(name.find_last_of("/") + 1); |
| 123 | istringstream ss (dataBuffer); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 125 | if (last == "state") |
| 126 | { |
| 127 | FullState full; |
| 128 | ss >> full; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 129 | BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 130 | { |
| 131 | NameInfoConstPtr info = leaf->getInfo (); |
| 132 | SeqNo seq = leaf->getSeq (); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 133 | |
| 134 | bool inserted = false; |
| 135 | bool updated = false; |
| 136 | SeqNo oldSeq; |
| 137 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 139 | if (updated) |
| 140 | { |
| 141 | diffLog->update (info, seq); |
| 142 | m_onUpdate (info->toString (), seq.getSeq(), oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 145 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 146 | else |
| 147 | { |
| 148 | DiffState diff; |
| 149 | ss >> diff; |
| 150 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
| 151 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 152 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 153 | BOOST_ASSERT (diffLeaf != 0); |
| 154 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 155 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 156 | if (diffLeaf->getOperation() == UPDATE) |
| 157 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 158 | SeqNo seq = diffLeaf->getSeq(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 160 | bool inserted = false; |
| 161 | bool updated = false; |
| 162 | SeqNo oldSeq; |
| 163 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 165 | if (updated) |
| 166 | { |
| 167 | diffLog->update (info, seq); |
| 168 | m_onUpdate (info->toString (), seq.getSeq(), oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 171 | else if (diffLeaf->getOperation() == REMOVE) |
| 172 | { |
| 173 | if (m_state.remove (info)) |
| 174 | { |
| 175 | diffLog->remove (info); |
| 176 | m_onRemove (info->toString ()); |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | BOOST_ASSERT (false); // just in case |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 186 | diffLog->setDigest(m_state.getDigest()); |
Alexander Afanasyev | a36b751 | 2012-03-12 16:03:03 -0700 | [diff] [blame] | 187 | if (m_log.size () > 0) |
| 188 | { |
| 189 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 190 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 191 | m_log.insert (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 192 | } |
| 193 | catch (Error::SyncXmlDecodingFailure &e) |
| 194 | { |
| 195 | // log error |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (diffLog->getLeaves ().size () > 0) |
| 200 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 201 | sendSyncInterest(); |
| 202 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 205 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 206 | SyncLogic::processPendingSyncInterests(DiffStatePtr &diff) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 208 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 209 | diff->setDigest(m_state.getDigest()); |
| 210 | m_log.insert(diff); |
| 211 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 212 | vector<string> pis = m_syncInterestTable.fetchAll (); |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 213 | stringstream ss; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 214 | ss << *diff; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 215 | for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii) |
| 216 | { |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 217 | m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 220 | |
| 221 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 222 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 223 | { |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 224 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 225 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
| 226 | SeqNo seqN(session, seq); |
| 227 | m_state.update(info, seqN); |
| 228 | |
| 229 | DiffStatePtr diff = make_shared<DiffState>(); |
| 230 | diff->update(info, seqN); |
| 231 | |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 232 | processPendingSyncInterests(diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 236 | SyncLogic::remove(const string &prefix) |
| 237 | { |
| 238 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 239 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 240 | m_state.remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 241 | |
| 242 | DiffStatePtr diff = make_shared<DiffState>(); |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 243 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 244 | |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 245 | processPendingSyncInterests(diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 249 | SyncLogic::sendSyncInterest () |
| 250 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 251 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 252 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 253 | ostringstream os; |
Alexander Afanasyev | 8a3d913 | 2012-03-12 23:54:32 -0700 | [diff] [blame^] | 254 | os << m_syncPrefix << "/" << *m_state.getDigest(); |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 255 | |
| 256 | m_ccnxHandle->sendInterest (os.str (), |
| 257 | bind (&SyncLogic::processSyncData, this, _1, _2)); |
| 258 | } |
| 259 | |
| 260 | } |