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