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