blob: ea341e134f66fa7040012622fa26fc6d4faba947 [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 Biand882ee72012-03-09 17:58:11 -080044 m_ccnxHandle->setInterestFilter(syncPrefix, bind(&SyncLogic::respondSyncInterest, this, _1));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080045}
46
Alexander Afanasyevc1030192012-03-08 22:21:28 -080047SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080048{
49
50}
51
Alexander Afanasyevc1030192012-03-08 22:21:28 -080052void
53SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080054{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080055 string last = name.substr(name.find_last_of("/") + 1);
56 stringstream ss(dataBuffer);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080058 const LeafContainer &fullLc = m_state.getLeaves();
Chaoyi Biand8e1bc92012-03-09 17:48:35 -080059 DiffStatePtr diffLog = make_shared<DiffState>();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080060
61 if (last == "state")
62 {
63 FullState full;
64 ss >> full;
65 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves().get<ordered>())
66 {
67 shared_ptr<const FullLeaf> fullLeaf = dynamic_pointer_cast<const FullLeaf>(leaf);
68 const NameInfo &info = fullLeaf->getInfo();
69 LeafContainer::iterator it = fullLc.find(info);
70 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
71 SeqNo seq = fullLeaf->getSeq();
72
73 if (it == fullLc.end())
74 {
75 string prefix = info.toString();
76 prefix += "/";
77 prefix += seq.getSession();
78 m_fetch(prefix, 1, seq.getSeq());
79 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -080080 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080081 }
82 else
83 {
84 SeqNo currSeq = (*it)->getSeq();
85 if (currSeq < seq)
86 {
87 string prefix = info.toString();
88 prefix += "/";
89 prefix += seq.getSession();
90
91 if (currSeq.getSession() == seq.getSession())
92 m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
93 else
94 m_fetch(prefix, 1, seq.getSeq());
95
96 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -080097 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080098 }
99 }
100 }
101 }
102 else
103 {
104 DiffState diff;
105 ss >> diff;
106 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
107 {
108 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf>(leaf);
109 const NameInfo &info = diffLeaf->getInfo();
110 LeafContainer::iterator it = fullLc.find(info);
111 SeqNo seq = diffLeaf->getSeq();
112
113 switch (diffLeaf->getOperation())
114 {
115 case UPDATE:
116 if (it == fullLc.end())
117 {
118 string prefix = info.toString();
119 prefix += "/";
120 prefix += seq.getSession();
121 m_fetch(prefix, 1, seq.getSeq());
122
123 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
124 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800125 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800126 }
127 else
128 {
129 SeqNo currSeq = (*it)->getSeq();
130 if (currSeq < seq)
131 {
132 string prefix = info.toString();
133 prefix += "/";
134 prefix += seq.getSession();
135
136 if (currSeq.getSession() == seq.getSession())
137 m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
138 else
139 m_fetch(prefix, 1, seq.getSeq());
140
141 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
142 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800143 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800144 }
145 }
146 break;
147
148 case REMOVE:
149 if (it != fullLc.end())
150 {
151 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
152 m_state.remove(pInfo);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800153 diffLog->remove(pInfo);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800154 }
155 break;
156
157 default:
158 break;
159 }
160 }
161 }
162
Chaoyi Bian74cadb52012-03-09 17:50:29 -0800163 diffLog->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800164 m_log.insert(diffLog);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800165 sendSyncInterest();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800166}
167
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800168void
169SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800170{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800171 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
172 SeqNo seqN(session, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800173 DiffStatePtr diff = make_shared<DiffState>();
174 diff->update(info, seqN);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800175 m_state.update(info, seqN);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800176 diff->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800177 m_log.insert(diff);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800178
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800179 vector<string> pis = m_syncInterestTable.fetchAll();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800180 stringstream ss;
181 ss << *diff;
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800182 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800183 {
184 m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness);
185 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800186}
187
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800188void
Chaoyi Bian633255f2012-03-09 21:25:38 -0800189SyncLogic::checkAgain (const string &interest, DigestPtr digest)
190{
191 int wait = rand() % 80 + 20;
192 sleep(wait/1000.0);
193
194 if (*m_state.getDigest() == *digest)
195 {
196 m_syncInterestTable.insert(interest);
197 return;
198 }
199
200 DiffStateContainer::iterator ii = m_log.find (digest);
201 if (ii != m_log.end ())
202 {
203 stringstream ss;
204 ss << *(*ii)->diff();
205 m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness);
206 }
207 else
208 {
209 stringstream ss;
210 ss << m_state;
211 m_ccnxHandle->publishData(interest + "/state", ss.str(), m_syncResponseFreshness);
212 }
213}
214
215void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800216SyncLogic::respondSyncInterest (const string &interest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800217{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800218 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800219 DigestPtr digest = make_shared<Digest> ();
220 *digest << hash;
221 digest->finalize ();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800222
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800223 if (*m_state.getDigest() == *digest)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800224 {
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800225 m_syncInterestTable.insert (interest);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800226 return;
227 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800228
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800229 DiffStateContainer::iterator ii = m_log.find (digest);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800230
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800231 if (ii != m_log.end())
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800232 {
233 stringstream ss;
234 ss << *(*ii)->diff();
235 m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness);
236 }
237 else
238 {
Chaoyi Bian633255f2012-03-09 21:25:38 -0800239 m_thread.join();
240 m_thread = thread(&SyncLogic::checkAgain, this, interest, digest);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800241 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800242}
243
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800244void
245SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800246{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800247 ostringstream os;
248 os << m_syncPrefix << "/" << m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800249
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800250 m_ccnxHandle->sendInterest (os.str (),
251 bind (&SyncLogic::processSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800252}
253
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800254}