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