blob: 4a243a433a6018a6ac56a62fa72462b2c3a0d963 [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,
40 LogicRemoveCallback onRemove,
Alexander Afanasyevc1030192012-03-08 22:21:28 -080041 CcnxWrapperPtr ccnxHandle)
42 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070043 , m_onUpdate (onUpdate)
44 , m_onRemove (onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080045 , m_ccnxHandle (ccnxHandle)
Alexander Afanasyev45fba082012-03-12 18:05:24 -070046 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
47 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,100))
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080048{
Alexander Afanasyev387ac952012-03-11 23:49:27 -070049 m_ccnxHandle->setInterestFilter (syncPrefix,
50 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070051
52 sendSyncInterest ();
53 m_scheduler.schedule (posix_time::seconds (4),
54 bind (&SyncLogic::sendSyncInterest, this),
55 REEXPRESSING_INTEREST);
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}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080061
Alexander Afanasyev387ac952012-03-11 23:49:27 -070062
63void
64SyncLogic::respondSyncInterest (const string &interest)
65{
66 string hash = interest.substr(interest.find_last_of("/") + 1);
67 DigestPtr digest = make_shared<Digest> ();
68 try
69 {
70 istringstream is (hash);
71 is >> *digest;
72 }
73 catch (Error::DigestCalculationError &e)
74 {
75 // log error. ignoring it for now, later we should log it
76 return;
77 }
78
79 processSyncInterest (digest, interest);
80}
81
82void
83SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
84{
85 // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070086 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070087
88 if (*m_state.getDigest() == *digest)
89 {
90 m_syncInterestTable.insert (interestName);
91 return;
92 }
93
94 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
95
96 if (stateInDiffLog != m_log.end ())
97 {
98 m_ccnxHandle->publishData (interestName,
99 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
100 m_syncResponseFreshness);
101 return;
102 }
103
104 if (!timedProcessing)
105 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700106 m_scheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
107 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
108 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700109
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700110 }
111 else
112 {
113 m_ccnxHandle->publishData (interestName + "/state",
114 lexical_cast<string> (m_state),
115 m_syncResponseFreshness);
116 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800117}
118
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800119void
120SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800121{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700122 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700123
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700124 try
125 {
126 recursive_mutex::scoped_lock lock (m_stateMutex);
127
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700128 string last = name.substr(name.find_last_of("/") + 1);
129 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700130
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700131 if (last == "state")
132 {
133 FullState full;
134 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700135 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700136 {
137 NameInfoConstPtr info = leaf->getInfo ();
138 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700139
140 bool inserted = false;
141 bool updated = false;
142 SeqNo oldSeq;
143 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800144
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700145 if (updated)
146 {
147 diffLog->update (info, seq);
148 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700149 }
150 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700151 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700152 else
153 {
154 DiffState diff;
155 ss >> diff;
156 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
157 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700158 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
159 BOOST_ASSERT (diffLeaf != 0);
160
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700161 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700162 if (diffLeaf->getOperation() == UPDATE)
163 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700164 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800165
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700166 bool inserted = false;
167 bool updated = false;
168 SeqNo oldSeq;
169 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800170
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700171 if (updated)
172 {
173 diffLog->update (info, seq);
174 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700175 }
176 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700177 else if (diffLeaf->getOperation() == REMOVE)
178 {
179 if (m_state.remove (info))
180 {
181 diffLog->remove (info);
182 m_onRemove (info->toString ());
183 }
184 }
185 else
186 {
187 BOOST_ASSERT (false); // just in case
188 }
189 }
190 }
191
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700192 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700193 if (m_log.size () > 0)
194 {
195 m_log.get<sequenced> ().front ()->setNext (diffLog);
196 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700197 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700198 }
199 catch (Error::SyncXmlDecodingFailure &e)
200 {
201 // log error
202 return;
203 }
204
205 if (diffLog->getLeaves ().size () > 0)
206 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700207 sendSyncInterest();
208 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800209}
210
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800211void
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700212SyncLogic::processPendingSyncInterests (DiffStatePtr &diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800213{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700214 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700215
216 diffLog->setDigest(m_state.getDigest());
217 if (m_log.size () > 0)
218 {
219 m_log.get<sequenced> ().front ()->setNext (diffLog);
220 }
221 m_log.insert (diffLog);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800222
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800223 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700224 if (pis.size () > 0)
225 {
226 stringstream ss;
227 ss << *diffLog;
228 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
229 {
230 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
231 }
232 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800233}
234
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800235void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700236SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
237{
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700238 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700239 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
240 SeqNo seqN(session, seq);
241 m_state.update(info, seqN);
242
243 DiffStatePtr diff = make_shared<DiffState>();
244 diff->update(info, seqN);
245
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700246 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700247}
248
249void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700250SyncLogic::remove(const string &prefix)
251{
252 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700253 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700254 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700255
256 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700257 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700258
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700259 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700260}
261
262void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800263SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800264{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700265 recursive_mutex::scoped_lock lock (m_stateMutex);
266
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800267 ostringstream os;
Alexander Afanasyev8a3d9132012-03-12 23:54:32 -0700268 os << m_syncPrefix << "/" << *m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800269
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800270 m_ccnxHandle->sendInterest (os.str (),
271 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700272
273 m_scheduler.cancel (REEXPRESSING_INTEREST);
274 m_scheduler.schedule (posix_time::seconds (4),
275 bind (&SyncLogic::sendSyncInterest, this),
276 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800277}
278
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800279}