blob: 7eba7c61f7020c6c190bf0e21875dd4f20349034 [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
95 if (digest->zero ())
96 {
97 m_ccnxHandle->publishData (interestName + "/state",
98 lexical_cast<string> (m_state),
99 m_syncResponseFreshness);
100 return;
101 }
102
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700103 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
104
105 if (stateInDiffLog != m_log.end ())
106 {
107 m_ccnxHandle->publishData (interestName,
108 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
109 m_syncResponseFreshness);
110 return;
111 }
112
113 if (!timedProcessing)
114 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700115 m_scheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
116 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
117 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700118
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700119 }
120 else
121 {
122 m_ccnxHandle->publishData (interestName + "/state",
123 lexical_cast<string> (m_state),
124 m_syncResponseFreshness);
125 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800126}
127
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800128void
129SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800130{
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700131 //cout << "Process Sync Data" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700132 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700133
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700134 try
135 {
136 recursive_mutex::scoped_lock lock (m_stateMutex);
137
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700138 string last = name.substr(name.find_last_of("/") + 1);
139 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700140
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700141 if (last == "state")
142 {
143 FullState full;
144 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700145 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700146 {
147 NameInfoConstPtr info = leaf->getInfo ();
148 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700149
150 bool inserted = false;
151 bool updated = false;
152 SeqNo oldSeq;
153 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800154
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700155 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700156 {
157 diffLog->update (info, seq);
158 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700159 }
160 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700161 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700162 else
163 {
164 DiffState diff;
165 ss >> diff;
166 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
167 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700168 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
169 BOOST_ASSERT (diffLeaf != 0);
170
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700171 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700172 if (diffLeaf->getOperation() == UPDATE)
173 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700174 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800175
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700176 bool inserted = false;
177 bool updated = false;
178 SeqNo oldSeq;
179 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800180
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700181 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700182 {
183 diffLog->update (info, seq);
184 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700185 }
186 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700187 else if (diffLeaf->getOperation() == REMOVE)
188 {
189 if (m_state.remove (info))
190 {
191 diffLog->remove (info);
192 m_onRemove (info->toString ());
193 }
194 }
195 else
196 {
197 BOOST_ASSERT (false); // just in case
198 }
199 }
200 }
201
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700202 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700203 if (m_log.size () > 0)
204 {
205 m_log.get<sequenced> ().front ()->setNext (diffLog);
206 }
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700207 m_log.erase (m_state.getDigest());
208 /// @todo Optimization
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700209 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700210 }
211 catch (Error::SyncXmlDecodingFailure &e)
212 {
213 // log error
214 return;
215 }
216
217 if (diffLog->getLeaves ().size () > 0)
218 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700219 sendSyncInterest();
220 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800221}
222
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800223void
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700224SyncLogic::processPendingSyncInterests (DiffStatePtr &diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800225{
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700226 //cout << "Process Pending Interests" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700227 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700228
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700229 diffLog->setDigest (m_state.getDigest());
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700230 if (m_log.size () > 0)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700231 {
232 m_log.get<sequenced> ().front ()->setNext (diffLog);
233 }
Alexander Afanasyevf0519d22012-03-13 14:00:44 -0700234 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
235 /// @todo Optimization
236 m_log.insert (diffLog);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800237
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800238 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700239 if (pis.size () > 0)
240 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700241 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700242 ss << *diffLog;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700243 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
244 {
245 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
246 }
247 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800248}
249
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800250void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700251SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
252{
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700253 //cout << "Add local names" <<endl;
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700254 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700255 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700256
257 SeqNo seqN (session, seq);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700258 m_state.update(info, seqN);
259
260 DiffStatePtr diff = make_shared<DiffState>();
261 diff->update(info, seqN);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700262 processPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700263}
264
265void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700266SyncLogic::remove(const string &prefix)
267{
268 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700269 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700270 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700271
272 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700273 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700274
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700275 processPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700276}
277
278void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800279SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800280{
Alexander Afanasyev848dd842012-03-13 00:05:40 -0700281 //cout << "Sending Sync Interest" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700282 recursive_mutex::scoped_lock lock (m_stateMutex);
283
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800284 ostringstream os;
Alexander Afanasyev8a3d9132012-03-12 23:54:32 -0700285 os << m_syncPrefix << "/" << *m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800286
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800287 m_ccnxHandle->sendInterest (os.str (),
288 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700289
290 m_scheduler.cancel (REEXPRESSING_INTEREST);
291 m_scheduler.schedule (posix_time::seconds (4),
292 bind (&SyncLogic::sendSyncInterest, this),
293 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800294}
295
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800296}