blob: 37c02f45a5e65d0eaed219fcacaf6eac1de8a19e [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- 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 Bian44fff0c2012-03-07 21:07:22 -080022
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080024#include "sync-diff-leaf.h"
25#include "sync-full-leaf.h"
26#include <boost/make_shared.hpp>
27#include <boost/foreach.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080028#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080029
30using namespace std;
31using namespace boost;
32
33namespace Sync
34{
35
Alexander Afanasyevc1030192012-03-08 22:21:28 -080036SyncLogic::SyncLogic (const string &syncPrefix,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080037 LogicCallback fetch,
Alexander Afanasyevc1030192012-03-08 22:21:28 -080038 CcnxWrapperPtr ccnxHandle)
39 : m_syncPrefix (syncPrefix)
40 , m_fetch (fetch)
41 , m_ccnxHandle (ccnxHandle)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080042{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080043 srandom(time(NULL));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080044}
45
Alexander Afanasyevc1030192012-03-08 22:21:28 -080046SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080047{
48
49}
50
Alexander Afanasyevc1030192012-03-08 22:21:28 -080051void
52SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080053{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080054 string last = name.substr(name.find_last_of("/") + 1);
55 stringstream ss(dataBuffer);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080056
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080057 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 Bian44fff0c2012-03-07 21:07:22 -0800157}
158
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800159void
160SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800161{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800162 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
163 SeqNo seqN(session, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800164 DiffStatePtr diff = make_shared<DiffState>();
165 diff->update(info, seqN);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800166 m_state.update(info, seqN);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800167 diff->setDigest(m_state.getDigest());
168
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800169 vector<string> pis = m_syncInterestTable.fetchAll();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800170 stringstream ss;
171 ss << *diff;
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800172 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800173 {
174 m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness);
175 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800176}
177
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800178void
179SyncLogic::respondSyncInterest (const string &interest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800180{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800181 string hash = interest.substr(interest.find_last_of("/") + 1);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800182 Digest digest;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800183 digest << hash;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800184
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800185 if (*m_state.getDigest() == digest)
186 {
187 m_syncInterestTable.insert(interest);
188 return;
189 }
Chaoyi Bian5badd712012-03-09 14:38:40 -0800190/*
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800191 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 Bian5badd712012-03-09 14:38:40 -0800225 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800226}
227
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800228void
229SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800230{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800231 ostringstream os;
232 os << m_syncPrefix << "/" << m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800233
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800234 m_ccnxHandle->sendInterest (os.str (),
235 bind (&SyncLogic::processSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800236}
237
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800238}