blob: a07259bb4b60ba9b6030ee08d7dfc20e3867cc07 [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();
Chaoyi Biand8e1bc92012-03-09 17:48:35 -080058 DiffStatePtr diffLog = make_shared<DiffState>();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080059
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 Biand8e1bc92012-03-09 17:48:35 -080079 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080080 }
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 Biand8e1bc92012-03-09 17:48:35 -080096 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080097 }
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 Biand8e1bc92012-03-09 17:48:35 -0800124 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800125 }
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 Biand8e1bc92012-03-09 17:48:35 -0800142 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800143 }
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 Biand8e1bc92012-03-09 17:48:35 -0800152 diffLog->remove(pInfo);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800153 }
154 break;
155
156 default:
157 break;
158 }
159 }
160 }
161
Chaoyi Bian74cadb52012-03-09 17:50:29 -0800162 diffLog->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800163 m_log.insert(diffLog);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800164 sendSyncInterest();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800165}
166
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800167void
168SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800169{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800170 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
171 SeqNo seqN(session, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800172 DiffStatePtr diff = make_shared<DiffState>();
173 diff->update(info, seqN);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800174 m_state.update(info, seqN);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800175 diff->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800176 m_log.insert(diff);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800177
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800178 vector<string> pis = m_syncInterestTable.fetchAll();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800179 stringstream ss;
180 ss << *diff;
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800181 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800182 {
183 m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness);
184 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800185}
186
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800187void
188SyncLogic::respondSyncInterest (const string &interest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800189{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800190 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800191 DigestPtr digest = make_shared<Digest> ();
192 *digest << hash;
193 digest->finalize ();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800194
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800195 if (*m_state.getDigest() == *digest)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800196 {
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800197 m_syncInterestTable.insert (interest);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800198 return;
199 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800200
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800201 DiffStateContainer::iterator ii = m_log.find (digest);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800202
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800203 if (ii != m_log.end())
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800204 {
205 stringstream ss;
206 ss << *(*ii)->diff();
207 m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness);
208 }
209 else
210 {
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800211 int wait = rand() % 80 + 20;
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800212 sleep(wait/1000.0); // ??? sleep in this thread???
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800213 }
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800214
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800215 if (*m_state.getDigest() == *digest)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800216 {
217 m_syncInterestTable.insert(interest);
218 return;
219 }
220
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800221 ii = m_log.find (digest);
222 if (ii != m_log.end ())
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800223 {
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 Bian44fff0c2012-03-07 21:07:22 -0800234}
235
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800236void
237SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800238{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800239 ostringstream os;
240 os << m_syncPrefix << "/" << m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800241
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800242 m_ccnxHandle->sendInterest (os.str (),
243 bind (&SyncLogic::processSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800244}
245
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800246}