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