blob: 6b11c9a613c0f732eb4106ac714a6b093428a58f [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 Afanasyeve4e2bf72012-03-12 12:44:54 -070042 LogicCallback fetchCallback,
Alexander Afanasyevc1030192012-03-08 22:21:28 -080043 CcnxWrapperPtr ccnxHandle)
44 : m_syncPrefix (syncPrefix)
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070045 , m_fetchCallback (fetchCallback)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080046 , 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);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700176 istringstream ss (dataBuffer);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800177
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800178 DiffStatePtr diffLog = make_shared<DiffState>();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800179
180 if (last == "state")
181 {
182 FullState full;
183 ss >> full;
184 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves().get<ordered>())
185 {
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700186 NameInfoConstPtr info = leaf->getInfo ();
187 LeafContainer::iterator it = m_state.getLeaves().find (info);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800188
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700189 SeqNo seq = leaf->getSeq ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800190
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700191 // if (it == m_state.getLeaves().end())
192 // {
193 // string prefix = info.toString();
194 // prefix += "/";
195 // prefix += seq.getSession();
196 // m_fetchCallback (prefix, 1, seq.getSeq());
197 // m_state.update(pInfo, seq);
198 // diffLog->update(pInfo, seq);
199 // }
200 // else
201 // {
202 // SeqNo currSeq = (*it)->getSeq();
203 // if (currSeq < seq)
204 // {
205 // string prefix = info.toString();
206 // prefix += "/";
207 // prefix += seq.getSession();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800208
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700209 // if (currSeq.getSession() == seq.getSession())
210 // m_fetchCallback(prefix, currSeq.getSeq() + 1, seq.getSeq());
211 // else
212 // m_fetchCallback(prefix, 1, seq.getSeq());
213
214 // m_state.update(pInfo, seq);
215 // diffLog->update(pInfo, seq);
216 // }
217 // }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800218 }
219 }
220 else
221 {
222 DiffState diff;
223 ss >> diff;
224 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
225 {
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700226 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
227 if (diffLeaf == 0)
228 {
229 return;
230 /// \todo Log the error
231 }
232 NameInfoConstPtr info = diffLeaf->getInfo();
233 LeafContainer::iterator it = m_state.getLeaves().find (info);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800234 SeqNo seq = diffLeaf->getSeq();
235
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700236 // switch (diffLeaf->getOperation())
237 // {
238 // case UPDATE:
239 // if (it == m_state.getLeaves().end())
240 // {
241 // string prefix = info.toString();
242 // prefix += "/";
243 // prefix += seq.getSession();
244 // m_fetchCallback(prefix, 1, seq.getSeq());
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800245
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700246 // NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
247 // m_state.update(pInfo, seq);
248 // diffLog->update(pInfo, seq);
249 // }
250 // else
251 // {
252 // SeqNo currSeq = (*it)->getSeq();
253 // if (currSeq < seq)
254 // {
255 // string prefix = info.toString();
256 // prefix += "/";
257 // prefix += seq.getSession();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800258
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700259 // if (currSeq.getSession() == seq.getSession())
260 // m_fetchCallback(prefix, currSeq.getSeq() + 1, seq.getSeq());
261 // else
262 // m_fetchCallback(prefix, 1, seq.getSeq());
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800263
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700264 // NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
265 // m_state.update(pInfo, seq);
266 // diffLog->update(pInfo, seq);
267 // }
268 // }
269 // break;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800270
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700271 // case REMOVE:
272 // if (it != m_state.getLeaves().end())
273 // {
274 // NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
275 // m_state.remove(pInfo);
276 // diffLog->remove(pInfo);
277 // }
278 // break;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800279
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700280 // default:
281 // break;
282 // }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800283 }
284 }
285
Chaoyi Bian74cadb52012-03-09 17:50:29 -0800286 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700287 m_log.insert (diffLog);
288
289 // notify upper layer
290 BOOST_FOREACH (LeafConstPtr leaf, diffLog->getLeaves ())
291 {
292 }
293
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800294 sendSyncInterest();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800295}
296
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800297void
298SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800299{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800300 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
301 SeqNo seqN(session, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800302 DiffStatePtr diff = make_shared<DiffState>();
303 diff->update(info, seqN);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800304 m_state.update(info, seqN);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800305 diff->setDigest(m_state.getDigest());
Chaoyi Biand8e1bc92012-03-09 17:48:35 -0800306 m_log.insert(diff);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800307
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700308 vector<string> pis = m_syncInterestTable.fetchAll ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800309 stringstream ss;
310 ss << *diff;
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800311 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800312 {
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700313 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800314 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800315}
316
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800317void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800318SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800319{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800320 ostringstream os;
321 os << m_syncPrefix << "/" << m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800322
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800323 m_ccnxHandle->sendInterest (os.str (),
324 bind (&SyncLogic::processSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800325}
326
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800327}