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