blob: c4a84870139ae726f255db47f217ac9c4e10f327 [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 Afanasyev750d1872012-03-12 15:33:56 -070041SyncLogic::SyncLogic (const std::string &syncPrefix,
42 LogicUpdateCallback onUpdate,
43 LogicRemoveCallback onRemove,
Alexander Afanasyevc1030192012-03-08 22:21:28 -080044 CcnxWrapperPtr ccnxHandle)
45 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070046 , m_onUpdate (onUpdate)
47 , m_onRemove (onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080048 , m_ccnxHandle (ccnxHandle)
Alexander Afanasyev387ac952012-03-11 23:49:27 -070049 , m_delayedCheckThreadRunning (true)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080050{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080051 srandom(time(NULL));
Alexander Afanasyev387ac952012-03-11 23:49:27 -070052 m_ccnxHandle->setInterestFilter (syncPrefix,
53 bind (&SyncLogic::respondSyncInterest, this, _1));
54
55 m_delayedCheckThread = thread (&SyncLogic::delayedChecksLoop, this);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080056}
57
Alexander Afanasyevc1030192012-03-08 22:21:28 -080058SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080059{
Alexander Afanasyev387ac952012-03-11 23:49:27 -070060 m_delayedCheckThreadRunning = false;
61 // cout << "Requested stop" << this_thread::get_id () << endl;
62 m_delayedCheckThread.interrupt ();
63 m_delayedCheckThread.join ();
64}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080065
Alexander Afanasyev387ac952012-03-11 23:49:27 -070066void
67SyncLogic::delayedChecksLoop ()
68{
69 while (m_delayedCheckThreadRunning)
70 {
71 try
72 {
73 DelayedChecksList::value_type tuple;
74
75 {
76 unique_lock<mutex> lock (m_listChecksMutex);
77 while (m_delayedCheckThreadRunning && m_listChecks.size () == 0)
78 {
79 m_listChecksCondition.wait (lock);
80 // cout << "Got something" << endl;
81 }
82
83 if (m_listChecks.size () == 0) continue;
84
85 tuple = m_listChecks.front ();
86 m_listChecks.pop_front ();
87 // cout << "pop" << endl;
88 // release the mutex
89 }
90
91 // waiting and calling
92
93 // cout << "Duration: " << tuple.get<0> () - get_system_time () << endl;
94 this_thread::sleep (tuple.get<0> ());
95
96 if (!m_delayedCheckThreadRunning) continue;
97 tuple.get<1> () (); // call the scheduled function
98 }
99 catch (thread_interrupted e)
100 {
101 // cout << "interrupted: " << this_thread::get_id () << endl;
102 // do nothing
103 }
104 }
105 // cout << "Exited...\n";
106}
107
108
109
110void
111SyncLogic::respondSyncInterest (const string &interest)
112{
113 string hash = interest.substr(interest.find_last_of("/") + 1);
114 DigestPtr digest = make_shared<Digest> ();
115 try
116 {
117 istringstream is (hash);
118 is >> *digest;
119 }
120 catch (Error::DigestCalculationError &e)
121 {
122 // log error. ignoring it for now, later we should log it
123 return;
124 }
125
126 processSyncInterest (digest, interest);
127}
128
129void
130SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
131{
132 // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700133 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700134
135 if (*m_state.getDigest() == *digest)
136 {
137 m_syncInterestTable.insert (interestName);
138 return;
139 }
140
141 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
142
143 if (stateInDiffLog != m_log.end ())
144 {
145 m_ccnxHandle->publishData (interestName,
146 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
147 m_syncResponseFreshness);
148 return;
149 }
150
151 if (!timedProcessing)
152 {
153 {
154 // Alex: Should we ignore interests if interest with the same digest is already in the wait queue?
155
156 lock_guard<mutex> lock (m_listChecksMutex);
157 system_time delay = get_system_time () + m_delayedCheckTime;
158 // do we need randomization??
159 // delay += boost::ptime::milliseconds (rand() % 80 + 20);
160
161 m_listChecks.push_back (make_tuple (delay,
162 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true))
163 );
164 }
165 m_listChecksCondition.notify_one ();
166 }
167 else
168 {
169 m_ccnxHandle->publishData (interestName + "/state",
170 lexical_cast<string> (m_state),
171 m_syncResponseFreshness);
172 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800173}
174
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800175void
176SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800177{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700178 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700179
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700180 try
181 {
182 recursive_mutex::scoped_lock lock (m_stateMutex);
183
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700184 string last = name.substr(name.find_last_of("/") + 1);
185 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700186
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700187 if (last == "state")
188 {
189 FullState full;
190 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700191 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700192 {
193 NameInfoConstPtr info = leaf->getInfo ();
194 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700195
196 bool inserted = false;
197 bool updated = false;
198 SeqNo oldSeq;
199 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800200
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700201 if (updated)
202 {
203 diffLog->update (info, seq);
204 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700205 }
206 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700207 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700208 else
209 {
210 DiffState diff;
211 ss >> diff;
212 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
213 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700214 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
215 BOOST_ASSERT (diffLeaf != 0);
216
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700217 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700218 if (diffLeaf->getOperation() == UPDATE)
219 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700220 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800221
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700222 bool inserted = false;
223 bool updated = false;
224 SeqNo oldSeq;
225 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800226
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700227 if (updated)
228 {
229 diffLog->update (info, seq);
230 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700231 }
232 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700233 else if (diffLeaf->getOperation() == REMOVE)
234 {
235 if (m_state.remove (info))
236 {
237 diffLog->remove (info);
238 m_onRemove (info->toString ());
239 }
240 }
241 else
242 {
243 BOOST_ASSERT (false); // just in case
244 }
245 }
246 }
247
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700248 diffLog->setDigest(m_state.getDigest());
249 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700250 }
251 catch (Error::SyncXmlDecodingFailure &e)
252 {
253 // log error
254 return;
255 }
256
257 if (diffLog->getLeaves ().size () > 0)
258 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700259 // notify upper layer
260 BOOST_FOREACH (LeafConstPtr leaf, diffLog->getLeaves ())
261 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700262 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
263 BOOST_ASSERT (diffLeaf != 0);
264
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700265 // m_fetchCallback (prefix, 1, seq.getSeq());
266 }
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800267
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700268 sendSyncInterest();
269 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800270}
271
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800272void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700273SyncLogic::processPendingSyncInterests(DiffStatePtr &diff)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800274{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700275 recursive_mutex::scoped_lock lock (m_stateMutex);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800276 diff->setDigest(m_state.getDigest());
277 m_log.insert(diff);
278
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800279 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800280 stringstream ss;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800281 ss << *diff;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800282 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
283 {
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800284 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800285 }
286}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800287
288void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700289SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
290{
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700291 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700292 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
293 SeqNo seqN(session, seq);
294 m_state.update(info, seqN);
295
296 DiffStatePtr diff = make_shared<DiffState>();
297 diff->update(info, seqN);
298
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700299 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700300}
301
302void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700303SyncLogic::remove(const string &prefix)
304{
305 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700306 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700307 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700308
309 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700310 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700311
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700312 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700313}
314
315void
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800316SyncLogic::sendSyncInterest ()
317{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700318 recursive_mutex::scoped_lock lock (m_stateMutex);
319
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800320 ostringstream os;
321 os << m_syncPrefix << "/" << m_state.getDigest();
322
323 m_ccnxHandle->sendInterest (os.str (),
324 bind (&SyncLogic::processSyncData, this, _1, _2));
325}
326
327}