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 | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 38 | const boost::posix_time::time_duration SyncLogic::m_delayedCheckTime = boost::posix_time::seconds (4.0); |
| 39 | |
| 40 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 41 | SyncLogic::SyncLogic (const std::string &syncPrefix, |
| 42 | LogicUpdateCallback onUpdate, |
| 43 | LogicRemoveCallback onRemove, |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 44 | CcnxWrapperPtr ccnxHandle) |
| 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 | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 48 | , m_ccnxHandle (ccnxHandle) |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 49 | , m_delayedCheckThreadRunning (true) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 50 | { |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 51 | srandom(time(NULL)); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 52 | m_ccnxHandle->setInterestFilter (syncPrefix, |
| 53 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
| 54 | |
| 55 | m_delayedCheckThread = thread (&SyncLogic::delayedChecksLoop, this); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 58 | SyncLogic::~SyncLogic () |
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 | m_delayedCheckThreadRunning = false; |
| 61 | // cout << "Requested stop" << this_thread::get_id () << endl; |
| 62 | m_delayedCheckThread.interrupt (); |
| 63 | m_delayedCheckThread.join (); |
| 64 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 66 | void |
| 67 | SyncLogic::delayedChecksLoop () |
| 68 | { |
| 69 | while (m_delayedCheckThreadRunning) |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | DelayedChecksList::value_type tuple; |
| 74 | |
| 75 | { |
| 76 | unique_lock<mutex> lock (m_listChecksMutex); |
| 77 | while (m_delayedCheckThreadRunning && m_listChecks.size () == 0) |
| 78 | { |
| 79 | m_listChecksCondition.wait (lock); |
| 80 | // cout << "Got something" << endl; |
| 81 | } |
| 82 | |
| 83 | if (m_listChecks.size () == 0) continue; |
| 84 | |
| 85 | tuple = m_listChecks.front (); |
| 86 | m_listChecks.pop_front (); |
| 87 | // cout << "pop" << endl; |
| 88 | // release the mutex |
| 89 | } |
| 90 | |
| 91 | // waiting and calling |
| 92 | |
| 93 | // cout << "Duration: " << tuple.get<0> () - get_system_time () << endl; |
| 94 | this_thread::sleep (tuple.get<0> ()); |
| 95 | |
| 96 | if (!m_delayedCheckThreadRunning) continue; |
| 97 | tuple.get<1> () (); // call the scheduled function |
| 98 | } |
| 99 | catch (thread_interrupted e) |
| 100 | { |
| 101 | // cout << "interrupted: " << this_thread::get_id () << endl; |
| 102 | // do nothing |
| 103 | } |
| 104 | } |
| 105 | // cout << "Exited...\n"; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | void |
| 111 | SyncLogic::respondSyncInterest (const string &interest) |
| 112 | { |
| 113 | string hash = interest.substr(interest.find_last_of("/") + 1); |
| 114 | DigestPtr digest = make_shared<Digest> (); |
| 115 | try |
| 116 | { |
| 117 | istringstream is (hash); |
| 118 | is >> *digest; |
| 119 | } |
| 120 | catch (Error::DigestCalculationError &e) |
| 121 | { |
| 122 | // log error. ignoring it for now, later we should log it |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | processSyncInterest (digest, interest); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/) |
| 131 | { |
| 132 | // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 133 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 134 | |
| 135 | if (*m_state.getDigest() == *digest) |
| 136 | { |
| 137 | m_syncInterestTable.insert (interestName); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 142 | |
| 143 | if (stateInDiffLog != m_log.end ()) |
| 144 | { |
| 145 | m_ccnxHandle->publishData (interestName, |
| 146 | lexical_cast<string> (*(*stateInDiffLog)->diff ()), |
| 147 | m_syncResponseFreshness); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if (!timedProcessing) |
| 152 | { |
| 153 | { |
| 154 | // Alex: Should we ignore interests if interest with the same digest is already in the wait queue? |
| 155 | |
| 156 | lock_guard<mutex> lock (m_listChecksMutex); |
| 157 | system_time delay = get_system_time () + m_delayedCheckTime; |
| 158 | // do we need randomization?? |
| 159 | // delay += boost::ptime::milliseconds (rand() % 80 + 20); |
| 160 | |
| 161 | m_listChecks.push_back (make_tuple (delay, |
| 162 | bind (&SyncLogic::processSyncInterest, this, digest, interestName, true)) |
| 163 | ); |
| 164 | } |
| 165 | m_listChecksCondition.notify_one (); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | m_ccnxHandle->publishData (interestName + "/state", |
| 170 | lexical_cast<string> (m_state), |
| 171 | m_syncResponseFreshness); |
| 172 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 175 | void |
| 176 | SyncLogic::processSyncData (const string &name, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 177 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 178 | DiffStatePtr diffLog = make_shared<DiffState> (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 180 | try |
| 181 | { |
| 182 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 183 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 184 | string last = name.substr(name.find_last_of("/") + 1); |
| 185 | istringstream ss (dataBuffer); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 187 | if (last == "state") |
| 188 | { |
| 189 | FullState full; |
| 190 | ss >> full; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 191 | BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 192 | { |
| 193 | NameInfoConstPtr info = leaf->getInfo (); |
| 194 | SeqNo seq = leaf->getSeq (); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 195 | |
| 196 | bool inserted = false; |
| 197 | bool updated = false; |
| 198 | SeqNo oldSeq; |
| 199 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 201 | if (updated) |
| 202 | { |
| 203 | diffLog->update (info, seq); |
| 204 | m_onUpdate (info->toString (), seq.getSeq(), oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 205 | } |
| 206 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 207 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 208 | else |
| 209 | { |
| 210 | DiffState diff; |
| 211 | ss >> diff; |
| 212 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
| 213 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 214 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 215 | BOOST_ASSERT (diffLeaf != 0); |
| 216 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 217 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 218 | if (diffLeaf->getOperation() == UPDATE) |
| 219 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 220 | SeqNo seq = diffLeaf->getSeq(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 221 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 222 | bool inserted = false; |
| 223 | bool updated = false; |
| 224 | SeqNo oldSeq; |
| 225 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 226 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 227 | if (updated) |
| 228 | { |
| 229 | diffLog->update (info, seq); |
| 230 | m_onUpdate (info->toString (), seq.getSeq(), oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 231 | } |
| 232 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 233 | else if (diffLeaf->getOperation() == REMOVE) |
| 234 | { |
| 235 | if (m_state.remove (info)) |
| 236 | { |
| 237 | diffLog->remove (info); |
| 238 | m_onRemove (info->toString ()); |
| 239 | } |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | BOOST_ASSERT (false); // just in case |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 248 | diffLog->setDigest(m_state.getDigest()); |
| 249 | m_log.insert (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 250 | } |
| 251 | catch (Error::SyncXmlDecodingFailure &e) |
| 252 | { |
| 253 | // log error |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (diffLog->getLeaves ().size () > 0) |
| 258 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 259 | // notify upper layer |
| 260 | BOOST_FOREACH (LeafConstPtr leaf, diffLog->getLeaves ()) |
| 261 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 262 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 263 | BOOST_ASSERT (diffLeaf != 0); |
| 264 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 265 | // m_fetchCallback (prefix, 1, seq.getSeq()); |
| 266 | } |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 267 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame^] | 268 | sendSyncInterest(); |
| 269 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 272 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 273 | SyncLogic::processPendingSyncInterests(DiffStatePtr &diff) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 274 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 275 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 276 | diff->setDigest(m_state.getDigest()); |
| 277 | m_log.insert(diff); |
| 278 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 279 | vector<string> pis = m_syncInterestTable.fetchAll (); |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 280 | stringstream ss; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 281 | ss << *diff; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 282 | for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii) |
| 283 | { |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 284 | m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 287 | |
| 288 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 289 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 290 | { |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 291 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 292 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
| 293 | SeqNo seqN(session, seq); |
| 294 | m_state.update(info, seqN); |
| 295 | |
| 296 | DiffStatePtr diff = make_shared<DiffState>(); |
| 297 | diff->update(info, seqN); |
| 298 | |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 299 | processPendingSyncInterests(diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 303 | SyncLogic::remove(const string &prefix) |
| 304 | { |
| 305 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 306 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 307 | m_state.remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 308 | |
| 309 | DiffStatePtr diff = make_shared<DiffState>(); |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 310 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 311 | |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 312 | processPendingSyncInterests(diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 316 | SyncLogic::sendSyncInterest () |
| 317 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 318 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 319 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 320 | ostringstream os; |
| 321 | os << m_syncPrefix << "/" << m_state.getDigest(); |
| 322 | |
| 323 | m_ccnxHandle->sendInterest (os.str (), |
| 324 | bind (&SyncLogic::processSyncData, this, _1, _2)); |
| 325 | } |
| 326 | |
| 327 | } |