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> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 28 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace std; |
| 31 | using namespace boost; |
| 32 | |
| 33 | namespace Sync |
| 34 | { |
| 35 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 36 | SyncLogic::SyncLogic (const string &syncPrefix, |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 37 | LogicCallback fetch, |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 38 | CcnxWrapperPtr ccnxHandle) |
| 39 | : m_syncPrefix (syncPrefix) |
| 40 | , m_fetch (fetch) |
| 41 | , m_ccnxHandle (ccnxHandle) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 42 | { |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 43 | srandom(time(NULL)); |
Chaoyi Bian | d882ee7 | 2012-03-09 17:58:11 -0800 | [diff] [blame] | 44 | m_ccnxHandle->setInterestFilter(syncPrefix, bind(&SyncLogic::respondSyncInterest, this, _1)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 47 | SyncLogic::~SyncLogic () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 48 | { |
| 49 | |
| 50 | } |
| 51 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 52 | void |
| 53 | SyncLogic::processSyncData (const string &name, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 54 | { |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 55 | string last = name.substr(name.find_last_of("/") + 1); |
| 56 | stringstream ss(dataBuffer); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 57 | |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 58 | const LeafContainer &fullLc = m_state.getLeaves(); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 59 | DiffStatePtr diffLog = make_shared<DiffState>(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 60 | |
| 61 | if (last == "state") |
| 62 | { |
| 63 | FullState full; |
| 64 | ss >> full; |
| 65 | BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves().get<ordered>()) |
| 66 | { |
| 67 | shared_ptr<const FullLeaf> fullLeaf = dynamic_pointer_cast<const FullLeaf>(leaf); |
| 68 | const NameInfo &info = fullLeaf->getInfo(); |
| 69 | LeafContainer::iterator it = fullLc.find(info); |
| 70 | NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString()); |
| 71 | SeqNo seq = fullLeaf->getSeq(); |
| 72 | |
| 73 | if (it == fullLc.end()) |
| 74 | { |
| 75 | string prefix = info.toString(); |
| 76 | prefix += "/"; |
| 77 | prefix += seq.getSession(); |
| 78 | m_fetch(prefix, 1, seq.getSeq()); |
| 79 | m_state.update(pInfo, seq); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 80 | diffLog->update(pInfo, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 81 | } |
| 82 | else |
| 83 | { |
| 84 | SeqNo currSeq = (*it)->getSeq(); |
| 85 | if (currSeq < seq) |
| 86 | { |
| 87 | string prefix = info.toString(); |
| 88 | prefix += "/"; |
| 89 | prefix += seq.getSession(); |
| 90 | |
| 91 | if (currSeq.getSession() == seq.getSession()) |
| 92 | m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq()); |
| 93 | else |
| 94 | m_fetch(prefix, 1, seq.getSeq()); |
| 95 | |
| 96 | m_state.update(pInfo, seq); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 97 | diffLog->update(pInfo, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | DiffState diff; |
| 105 | ss >> diff; |
| 106 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
| 107 | { |
| 108 | shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf>(leaf); |
| 109 | const NameInfo &info = diffLeaf->getInfo(); |
| 110 | LeafContainer::iterator it = fullLc.find(info); |
| 111 | SeqNo seq = diffLeaf->getSeq(); |
| 112 | |
| 113 | switch (diffLeaf->getOperation()) |
| 114 | { |
| 115 | case UPDATE: |
| 116 | if (it == fullLc.end()) |
| 117 | { |
| 118 | string prefix = info.toString(); |
| 119 | prefix += "/"; |
| 120 | prefix += seq.getSession(); |
| 121 | m_fetch(prefix, 1, seq.getSeq()); |
| 122 | |
| 123 | NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString()); |
| 124 | m_state.update(pInfo, seq); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 125 | diffLog->update(pInfo, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
| 129 | SeqNo currSeq = (*it)->getSeq(); |
| 130 | if (currSeq < seq) |
| 131 | { |
| 132 | string prefix = info.toString(); |
| 133 | prefix += "/"; |
| 134 | prefix += seq.getSession(); |
| 135 | |
| 136 | if (currSeq.getSession() == seq.getSession()) |
| 137 | m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq()); |
| 138 | else |
| 139 | m_fetch(prefix, 1, seq.getSeq()); |
| 140 | |
| 141 | NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString()); |
| 142 | m_state.update(pInfo, seq); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 143 | diffLog->update(pInfo, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | break; |
| 147 | |
| 148 | case REMOVE: |
| 149 | if (it != fullLc.end()) |
| 150 | { |
| 151 | NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString()); |
| 152 | m_state.remove(pInfo); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 153 | diffLog->remove(pInfo); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 154 | } |
| 155 | break; |
| 156 | |
| 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Chaoyi Bian | 74cadb5 | 2012-03-09 17:50:29 -0800 | [diff] [blame] | 163 | diffLog->setDigest(m_state.getDigest()); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 164 | m_log.insert(diffLog); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 165 | sendSyncInterest(); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 168 | void |
| 169 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 170 | { |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 171 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
| 172 | SeqNo seqN(session, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 173 | DiffStatePtr diff = make_shared<DiffState>(); |
| 174 | diff->update(info, seqN); |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 175 | m_state.update(info, seqN); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 176 | diff->setDigest(m_state.getDigest()); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 177 | m_log.insert(diff); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 178 | |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 179 | vector<string> pis = m_syncInterestTable.fetchAll(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 180 | stringstream ss; |
| 181 | ss << *diff; |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 182 | for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii) |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 183 | { |
| 184 | m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness); |
| 185 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 188 | void |
Chaoyi Bian | 633255f | 2012-03-09 21:25:38 -0800 | [diff] [blame] | 189 | SyncLogic::checkAgain (const string &interest, DigestPtr digest) |
| 190 | { |
| 191 | int wait = rand() % 80 + 20; |
| 192 | sleep(wait/1000.0); |
| 193 | |
| 194 | if (*m_state.getDigest() == *digest) |
| 195 | { |
| 196 | m_syncInterestTable.insert(interest); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | DiffStateContainer::iterator ii = m_log.find (digest); |
| 201 | if (ii != m_log.end ()) |
| 202 | { |
| 203 | stringstream ss; |
| 204 | ss << *(*ii)->diff(); |
| 205 | m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | stringstream ss; |
| 210 | ss << m_state; |
| 211 | m_ccnxHandle->publishData(interest + "/state", ss.str(), m_syncResponseFreshness); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 216 | SyncLogic::respondSyncInterest (const string &interest) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 217 | { |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 218 | string hash = interest.substr(interest.find_last_of("/") + 1); |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 219 | DigestPtr digest = make_shared<Digest> (); |
| 220 | *digest << hash; |
| 221 | digest->finalize (); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 223 | if (*m_state.getDigest() == *digest) |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 224 | { |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 225 | m_syncInterestTable.insert (interest); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 226 | return; |
| 227 | } |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 229 | DiffStateContainer::iterator ii = m_log.find (digest); |
Chaoyi Bian | d8e1bc9 | 2012-03-09 17:48:35 -0800 | [diff] [blame] | 230 | |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 231 | if (ii != m_log.end()) |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 232 | { |
| 233 | stringstream ss; |
| 234 | ss << *(*ii)->diff(); |
| 235 | m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness); |
| 236 | } |
| 237 | else |
| 238 | { |
Chaoyi Bian | 633255f | 2012-03-09 21:25:38 -0800 | [diff] [blame] | 239 | m_thread.join(); |
| 240 | m_thread = thread(&SyncLogic::checkAgain, this, interest, digest); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 241 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 244 | void |
| 245 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 246 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 247 | ostringstream os; |
| 248 | os << m_syncPrefix << "/" << m_state.getDigest(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 249 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 250 | m_ccnxHandle->sendInterest (os.str (), |
| 251 | bind (&SyncLogic::processSyncData, this, _1, _2)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 254 | } |