blob: a954db880ce23b25b637be50ef77ffa18200ec8b [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 Afanasyev750d1872012-03-12 15:33:56 -070038SyncLogic::SyncLogic (const std::string &syncPrefix,
39 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070040 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080041 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070042 , m_onUpdate (onUpdate)
43 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070044 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev45fba082012-03-12 18:05:24 -070045 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
46 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,100))
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080047{
Alexander Afanasyev387ac952012-03-11 23:49:27 -070048 m_ccnxHandle->setInterestFilter (syncPrefix,
49 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070050
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070051 m_scheduler.schedule (posix_time::seconds (4),
52 bind (&SyncLogic::sendSyncInterest, this),
53 REEXPRESSING_INTEREST);
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}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080059
Alexander Afanasyev387ac952012-03-11 23:49:27 -070060
61void
62SyncLogic::respondSyncInterest (const string &interest)
63{
Alexander Afanasyev848dd842012-03-13 00:05:40 -070064 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070065 string hash = interest.substr(interest.find_last_of("/") + 1);
66 DigestPtr digest = make_shared<Digest> ();
67 try
68 {
69 istringstream is (hash);
70 is >> *digest;
71 }
72 catch (Error::DigestCalculationError &e)
73 {
74 // log error. ignoring it for now, later we should log it
75 return;
76 }
77
78 processSyncInterest (digest, interest);
79}
80
81void
82SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
83{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -070084 //cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070085 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070086
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -070087 if (*m_state.getDigest() == *digest)
88 {
89 // cout << interestName << "\n";
90 m_syncInterestTable.insert (interestName);
91 return;
92 }
93
94 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev763855a2012-03-13 14:17:37 -070095 if (digest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -070096 {
97 m_ccnxHandle->publishData (interestName + "/state",
98 lexical_cast<string> (m_state),
99 m_syncResponseFreshness);
100 return;
101 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700102
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700103
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700104 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
105
106 if (stateInDiffLog != m_log.end ())
107 {
108 m_ccnxHandle->publishData (interestName,
109 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
110 m_syncResponseFreshness);
111 return;
112 }
113
114 if (!timedProcessing)
115 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700116 m_scheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
117 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
118 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700119
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700120 }
121 else
122 {
123 m_ccnxHandle->publishData (interestName + "/state",
124 lexical_cast<string> (m_state),
125 m_syncResponseFreshness);
126 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800127}
128
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800129void
130SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800131{
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700132 //cout << "Process Sync Data" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700133 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700134
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700135 try
136 {
137 recursive_mutex::scoped_lock lock (m_stateMutex);
138
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700139 string last = name.substr(name.find_last_of("/") + 1);
140 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700141
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700142 if (last == "state")
143 {
144 FullState full;
145 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700146 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700147 {
148 NameInfoConstPtr info = leaf->getInfo ();
149 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700150
151 bool inserted = false;
152 bool updated = false;
153 SeqNo oldSeq;
154 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800155
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700156 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700157 {
158 diffLog->update (info, seq);
159 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700160 }
161 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700162 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700163 else
164 {
165 DiffState diff;
166 ss >> diff;
167 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
168 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700169 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
170 BOOST_ASSERT (diffLeaf != 0);
171
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700172 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700173 if (diffLeaf->getOperation() == UPDATE)
174 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700175 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800176
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700177 bool inserted = false;
178 bool updated = false;
179 SeqNo oldSeq;
180 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800181
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700182 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700183 {
184 diffLog->update (info, seq);
185 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700186 }
187 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700188 else if (diffLeaf->getOperation() == REMOVE)
189 {
190 if (m_state.remove (info))
191 {
192 diffLog->remove (info);
193 m_onRemove (info->toString ());
194 }
195 }
196 else
197 {
198 BOOST_ASSERT (false); // just in case
199 }
200 }
201 }
202
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700203 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700204 if (m_log.size () > 0)
205 {
206 m_log.get<sequenced> ().front ()->setNext (diffLog);
207 }
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700208 m_log.erase (m_state.getDigest());
209 /// @todo Optimization
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700210 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700211 }
212 catch (Error::SyncXmlDecodingFailure &e)
213 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700214 diffLog.reset ();
215 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700216 }
217
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700218 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700219 if (diffLog->getLeaves ().size () > 0)
220 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700221 sendSyncInterest ();
222 }
223 else
224 {
225 // should not reexpress the same interest. Need at least wait for data lifetime
226 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
227 m_scheduler.cancel (REEXPRESSING_INTEREST);
228 m_scheduler.schedule (posix_time::seconds (m_syncResponseFreshness),
229 bind (&SyncLogic::sendSyncInterest, this),
230 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700231 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800232}
233
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800234void
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700235SyncLogic::processPendingSyncInterests (DiffStatePtr &diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800236{
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700237 //cout << "Process Pending Interests" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700238 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700239
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700240 diffLog->setDigest (m_state.getDigest());
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700241 if (m_log.size () > 0)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700242 {
243 m_log.get<sequenced> ().front ()->setNext (diffLog);
244 }
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700245 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
246 /// @todo Optimization
247 m_log.insert (diffLog);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800248
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800249 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700250 if (pis.size () > 0)
251 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700252 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700253 ss << *diffLog;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700254 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
255 {
256 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
257 }
258 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800259}
260
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800261void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700262SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
263{
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700264 //cout << "Add local names" <<endl;
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700265 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700266 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700267
268 SeqNo seqN (session, seq);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700269 m_state.update(info, seqN);
270
271 DiffStatePtr diff = make_shared<DiffState>();
272 diff->update(info, seqN);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700273 processPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700274}
275
276void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700277SyncLogic::remove(const string &prefix)
278{
279 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700280 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700281 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700282
283 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700284 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700285
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700286 processPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700287}
288
289void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800290SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800291{
Alexander Afanasyev848dd842012-03-13 00:05:40 -0700292 //cout << "Sending Sync Interest" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700293 recursive_mutex::scoped_lock lock (m_stateMutex);
294
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800295 ostringstream os;
Alexander Afanasyev8a3d9132012-03-12 23:54:32 -0700296 os << m_syncPrefix << "/" << *m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800297
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800298 m_ccnxHandle->sendInterest (os.str (),
299 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700300
301 m_scheduler.cancel (REEXPRESSING_INTEREST);
302 m_scheduler.schedule (posix_time::seconds (4),
303 bind (&SyncLogic::sendSyncInterest, this),
304 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800305}
306
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800307}