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