blob: c81f2f194f4de99634995d60fedc0cf37a91b590 [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 Afanasyev1b449c42012-03-13 20:24:07 -070048 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070049 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070050
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070051 sendSyncInterest ();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080052}
53
Alexander Afanasyevc1030192012-03-08 22:21:28 -080054SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080055{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070056 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070058 m_ccnxHandle.reset ();
59}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070060
61void
62SyncLogic::respondSyncInterest (const string &interest)
63{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070064 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070065 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070066 // cout << "Received Sync Interest: " << hash << endl;
67
Alexander Afanasyev387ac952012-03-11 23:49:27 -070068 DigestPtr digest = make_shared<Digest> ();
69 try
70 {
71 istringstream is (hash);
72 is >> *digest;
73 }
74 catch (Error::DigestCalculationError &e)
75 {
76 // log error. ignoring it for now, later we should log it
77 return;
78 }
79
80 processSyncInterest (digest, interest);
81}
82
83void
84SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
85{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070086 // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070087 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070088
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -070089 if (*m_state.getDigest() == *digest)
90 {
91 // cout << interestName << "\n";
92 m_syncInterestTable.insert (interestName);
93 return;
94 }
95
96 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev763855a2012-03-13 14:17:37 -070097 if (digest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -070098 {
99 m_ccnxHandle->publishData (interestName + "/state",
100 lexical_cast<string> (m_state),
101 m_syncResponseFreshness);
102 return;
103 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700104
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700105
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700106 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
107
108 if (stateInDiffLog != m_log.end ())
109 {
110 m_ccnxHandle->publishData (interestName,
111 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
112 m_syncResponseFreshness);
113 return;
114 }
115
116 if (!timedProcessing)
117 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700118 m_scheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
119 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
120 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700121
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700122 }
123 else
124 {
125 m_ccnxHandle->publishData (interestName + "/state",
126 lexical_cast<string> (m_state),
127 m_syncResponseFreshness);
128 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800129}
130
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800131void
132SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800133{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700134 // cout << "Process Sync Data" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700135 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700136
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700137 try
138 {
139 recursive_mutex::scoped_lock lock (m_stateMutex);
140
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700141 string last = name.substr(name.find_last_of("/") + 1);
142 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700143
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700144 if (last == "state")
145 {
146 FullState full;
147 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700148 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700149 {
150 NameInfoConstPtr info = leaf->getInfo ();
151 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700152
153 bool inserted = false;
154 bool updated = false;
155 SeqNo oldSeq;
156 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800157
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700158 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700159 {
160 diffLog->update (info, seq);
161 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700162 }
163 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700164 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700165 else
166 {
167 DiffState diff;
168 ss >> diff;
169 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
170 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700171 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
172 BOOST_ASSERT (diffLeaf != 0);
173
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700174 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700175 if (diffLeaf->getOperation() == UPDATE)
176 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700177 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800178
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700179 bool inserted = false;
180 bool updated = false;
181 SeqNo oldSeq;
182 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800183
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700184 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700185 {
186 diffLog->update (info, seq);
187 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700188 }
189 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700190 else if (diffLeaf->getOperation() == REMOVE)
191 {
192 if (m_state.remove (info))
193 {
194 diffLog->remove (info);
195 m_onRemove (info->toString ());
196 }
197 }
198 else
199 {
200 BOOST_ASSERT (false); // just in case
201 }
202 }
203 }
204
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700205 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700206 if (m_log.size () > 0)
207 {
208 m_log.get<sequenced> ().front ()->setNext (diffLog);
209 }
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700210 m_log.erase (m_state.getDigest());
211 /// @todo Optimization
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700212 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700213 }
214 catch (Error::SyncXmlDecodingFailure &e)
215 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700216 diffLog.reset ();
217 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700218 }
219
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700220 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700221 if (diffLog->getLeaves ().size () > 0)
222 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700223 sendSyncInterest ();
224 }
225 else
226 {
227 // should not reexpress the same interest. Need at least wait for data lifetime
228 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
229 m_scheduler.cancel (REEXPRESSING_INTEREST);
230 m_scheduler.schedule (posix_time::seconds (m_syncResponseFreshness),
231 bind (&SyncLogic::sendSyncInterest, this),
232 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700233 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800234}
235
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800236void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700237SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800238{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800239 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700240 if (pis.size () > 0)
241 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700242 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700243 ss << *diffLog;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700244 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
245 {
246 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
247 }
248 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800249}
250
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800251void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700252SyncLogic::processPendingSyncInterests (DiffStatePtr diffLog)
253{
254 //cout << "Process Pending Interests" <<endl;
255 diffLog->setDigest (m_state.getDigest());
256 if (m_log.size () > 0)
257 {
258 m_log.get<sequenced> ().front ()->setNext (diffLog);
259 }
260 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
261 /// @todo Optimization
262 m_log.insert (diffLog);
263}
264
265void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700266SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
267{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700268 DiffStatePtr diff;
269 {
270 //cout << "Add local names" <<endl;
271 recursive_mutex::scoped_lock lock (m_stateMutex);
272 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700273
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700274 SeqNo seqN (session, seq);
275 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700276
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700277 diff = make_shared<DiffState>();
278 diff->update(info, seqN);
279 processPendingSyncInterests (diff);
280 }
281
282 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700283}
284
285void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700286SyncLogic::remove(const string &prefix)
287{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700288 DiffStatePtr diff;
289 {
290 recursive_mutex::scoped_lock lock (m_stateMutex);
291 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
292 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700293
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700294 diff = make_shared<DiffState>();
295 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700296
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700297 processPendingSyncInterests (diff);
298 }
299
300 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700301}
302
303void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800304SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800305{
Alexander Afanasyev848dd842012-03-13 00:05:40 -0700306 //cout << "Sending Sync Interest" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700307 recursive_mutex::scoped_lock lock (m_stateMutex);
308
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800309 ostringstream os;
Alexander Afanasyev8a3d9132012-03-12 23:54:32 -0700310 os << m_syncPrefix << "/" << *m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800311
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800312 m_ccnxHandle->sendInterest (os.str (),
313 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700314
315 m_scheduler.cancel (REEXPRESSING_INTEREST);
316 m_scheduler.schedule (posix_time::seconds (4),
317 bind (&SyncLogic::sendSyncInterest, this),
318 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800319}
320
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800321}