blob: a3fe621ab71527c2a6e9ce1b93a13308ad4adc35 [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>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070028#include <boost/lexical_cast.hpp>
29#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080030#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080031
32using namespace std;
33using namespace boost;
34
35namespace Sync
36{
37
Alexander Afanasyev387ac952012-03-11 23:49:27 -070038const boost::posix_time::time_duration SyncLogic::m_delayedCheckTime = boost::posix_time::seconds (4.0);
39
40
Alexander Afanasyevc1030192012-03-08 22:21:28 -080041SyncLogic::SyncLogic (const string &syncPrefix,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080042 LogicCallback fetch,
Alexander Afanasyevc1030192012-03-08 22:21:28 -080043 CcnxWrapperPtr ccnxHandle)
44 : m_syncPrefix (syncPrefix)
45 , m_fetch (fetch)
46 , m_ccnxHandle (ccnxHandle)
Alexander Afanasyev387ac952012-03-11 23:49:27 -070047 , m_delayedCheckThreadRunning (true)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080048{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080049 srandom(time(NULL));
Alexander Afanasyev387ac952012-03-11 23:49:27 -070050 m_ccnxHandle->setInterestFilter (syncPrefix,
51 bind (&SyncLogic::respondSyncInterest, this, _1));
52
53 m_delayedCheckThread = thread (&SyncLogic::delayedChecksLoop, this);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080054}
55
Alexander Afanasyevc1030192012-03-08 22:21:28 -080056SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057{
Alexander Afanasyev387ac952012-03-11 23:49:27 -070058 m_delayedCheckThreadRunning = false;
59 // cout << "Requested stop" << this_thread::get_id () << endl;
60 m_delayedCheckThread.interrupt ();
61 m_delayedCheckThread.join ();
62}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080063
Alexander Afanasyev387ac952012-03-11 23:49:27 -070064void
65SyncLogic::delayedChecksLoop ()
66{
67 while (m_delayedCheckThreadRunning)
68 {
69 try
70 {
71 DelayedChecksList::value_type tuple;
72
73 {
74 unique_lock<mutex> lock (m_listChecksMutex);
75 while (m_delayedCheckThreadRunning && m_listChecks.size () == 0)
76 {
77 m_listChecksCondition.wait (lock);
78 // cout << "Got something" << endl;
79 }
80
81 if (m_listChecks.size () == 0) continue;
82
83 tuple = m_listChecks.front ();
84 m_listChecks.pop_front ();
85 // cout << "pop" << endl;
86 // release the mutex
87 }
88
89 // waiting and calling
90
91 // cout << "Duration: " << tuple.get<0> () - get_system_time () << endl;
92 this_thread::sleep (tuple.get<0> ());
93
94 if (!m_delayedCheckThreadRunning) continue;
95 tuple.get<1> () (); // call the scheduled function
96 }
97 catch (thread_interrupted e)
98 {
99 // cout << "interrupted: " << this_thread::get_id () << endl;
100 // do nothing
101 }
102 }
103 // cout << "Exited...\n";
104}
105
106
107
108void
109SyncLogic::respondSyncInterest (const string &interest)
110{
111 string hash = interest.substr(interest.find_last_of("/") + 1);
112 DigestPtr digest = make_shared<Digest> ();
113 try
114 {
115 istringstream is (hash);
116 is >> *digest;
117 }
118 catch (Error::DigestCalculationError &e)
119 {
120 // log error. ignoring it for now, later we should log it
121 return;
122 }
123
124 processSyncInterest (digest, interest);
125}
126
127void
128SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
129{
130 // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
131
132 if (*m_state.getDigest() == *digest)
133 {
134 m_syncInterestTable.insert (interestName);
135 return;
136 }
137
138 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
139
140 if (stateInDiffLog != m_log.end ())
141 {
142 m_ccnxHandle->publishData (interestName,
143 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
144 m_syncResponseFreshness);
145 return;
146 }
147
148 if (!timedProcessing)
149 {
150 {
151 // Alex: Should we ignore interests if interest with the same digest is already in the wait queue?
152
153 lock_guard<mutex> lock (m_listChecksMutex);
154 system_time delay = get_system_time () + m_delayedCheckTime;
155 // do we need randomization??
156 // delay += boost::ptime::milliseconds (rand() % 80 + 20);
157
158 m_listChecks.push_back (make_tuple (delay,
159 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true))
160 );
161 }
162 m_listChecksCondition.notify_one ();
163 }
164 else
165 {
166 m_ccnxHandle->publishData (interestName + "/state",
167 lexical_cast<string> (m_state),
168 m_syncResponseFreshness);
169 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800170}
171
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800172void
173SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800174{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800175 string last = name.substr(name.find_last_of("/") + 1);
176 stringstream ss(dataBuffer);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800177
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800178 const LeafContainer &fullLc = m_state.getLeaves();
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800179 DiffStatePtr diffLog = make_shared<DiffState>();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800180
181 if (last == "state")
182 {
183 FullState full;
184 ss >> full;
185 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves().get<ordered>())
186 {
187 shared_ptr<const FullLeaf> fullLeaf = dynamic_pointer_cast<const FullLeaf>(leaf);
188 const NameInfo &info = fullLeaf->getInfo();
189 LeafContainer::iterator it = fullLc.find(info);
190 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
191 SeqNo seq = fullLeaf->getSeq();
192
193 if (it == fullLc.end())
194 {
195 string prefix = info.toString();
196 prefix += "/";
197 prefix += seq.getSession();
198 m_fetch(prefix, 1, seq.getSeq());
199 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800200 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800201 }
202 else
203 {
204 SeqNo currSeq = (*it)->getSeq();
205 if (currSeq < seq)
206 {
207 string prefix = info.toString();
208 prefix += "/";
209 prefix += seq.getSession();
210
211 if (currSeq.getSession() == seq.getSession())
212 m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
213 else
214 m_fetch(prefix, 1, seq.getSeq());
215
216 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800217 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800218 }
219 }
220 }
221 }
222 else
223 {
224 DiffState diff;
225 ss >> diff;
226 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
227 {
228 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf>(leaf);
229 const NameInfo &info = diffLeaf->getInfo();
230 LeafContainer::iterator it = fullLc.find(info);
231 SeqNo seq = diffLeaf->getSeq();
232
233 switch (diffLeaf->getOperation())
234 {
235 case UPDATE:
236 if (it == fullLc.end())
237 {
238 string prefix = info.toString();
239 prefix += "/";
240 prefix += seq.getSession();
241 m_fetch(prefix, 1, seq.getSeq());
242
243 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
244 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800245 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800246 }
247 else
248 {
249 SeqNo currSeq = (*it)->getSeq();
250 if (currSeq < seq)
251 {
252 string prefix = info.toString();
253 prefix += "/";
254 prefix += seq.getSession();
255
256 if (currSeq.getSession() == seq.getSession())
257 m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
258 else
259 m_fetch(prefix, 1, seq.getSeq());
260
261 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
262 m_state.update(pInfo, seq);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800263 diffLog->update(pInfo, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800264 }
265 }
266 break;
267
268 case REMOVE:
269 if (it != fullLc.end())
270 {
271 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
272 m_state.remove(pInfo);
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800273 diffLog->remove(pInfo);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800274 }
275 break;
276
277 default:
278 break;
279 }
280 }
281 }
282
Chaoyi Bian74cadb52012-03-09 17:50:29 -0800283 diffLog->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800284 m_log.insert(diffLog);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800285 sendSyncInterest();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800286}
287
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800288void
289SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800290{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800291 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
292 SeqNo seqN(session, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800293 DiffStatePtr diff = make_shared<DiffState>();
294 diff->update(info, seqN);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800295 m_state.update(info, seqN);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800296 diff->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800297 m_log.insert(diff);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800298
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700299 vector<string> pis = m_syncInterestTable.fetchAll ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800300 stringstream ss;
301 ss << *diff;
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800302 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800303 {
304 m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness);
305 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800306}
307
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800308void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800309SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800310{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800311 ostringstream os;
312 os << m_syncPrefix << "/" << m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800313
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800314 m_ccnxHandle->sendInterest (os.str (),
315 bind (&SyncLogic::processSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800316}
317
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800318}