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