blob: 7f24e04e8d8d69f1fb6809e11783819776ba8dd6 [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));
Zhenkai Zhud010e2c2012-03-12 21:55:04 -070051
Zhenkai Zhu88916c22012-03-12 22:10:41 -070052
53}
54
55void SyncLogic::start()
56{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -070057 // We need to send out our Sync Interest when we're ready
58 sendSyncInterest();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080059}
60
Alexander Afanasyevc1030192012-03-08 22:21:28 -080061SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080062{
Alexander Afanasyev387ac952012-03-11 23:49:27 -070063}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080064
Alexander Afanasyev387ac952012-03-11 23:49:27 -070065
66void
67SyncLogic::respondSyncInterest (const string &interest)
68{
Zhenkai Zhuc81e5d32012-03-12 22:14:09 -070069 cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070070 string hash = interest.substr(interest.find_last_of("/") + 1);
71 DigestPtr digest = make_shared<Digest> ();
72 try
73 {
74 istringstream is (hash);
75 is >> *digest;
76 }
77 catch (Error::DigestCalculationError &e)
78 {
79 // log error. ignoring it for now, later we should log it
80 return;
81 }
82
83 processSyncInterest (digest, interest);
84}
85
86void
87SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
88{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -070089 //cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070090 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070091
92 if (*m_state.getDigest() == *digest)
93 {
94 m_syncInterestTable.insert (interestName);
95 return;
96 }
97
98 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
99
100 if (stateInDiffLog != m_log.end ())
101 {
102 m_ccnxHandle->publishData (interestName,
103 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
104 m_syncResponseFreshness);
105 return;
106 }
107
108 if (!timedProcessing)
109 {
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700110 m_delayedChecksScheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
111 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700112
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700113 }
114 else
115 {
116 m_ccnxHandle->publishData (interestName + "/state",
117 lexical_cast<string> (m_state),
118 m_syncResponseFreshness);
119 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800120}
121
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800122void
123SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800124{
Zhenkai Zhuc81e5d32012-03-12 22:14:09 -0700125 cout << "Process Sync Data" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700126 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700127
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700128 try
129 {
130 recursive_mutex::scoped_lock lock (m_stateMutex);
131
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700132 string last = name.substr(name.find_last_of("/") + 1);
133 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700134
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700135 if (last == "state")
136 {
137 FullState full;
138 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700139 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700140 {
141 NameInfoConstPtr info = leaf->getInfo ();
142 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700143
144 bool inserted = false;
145 bool updated = false;
146 SeqNo oldSeq;
147 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800148
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700149 if (updated)
150 {
151 diffLog->update (info, seq);
152 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700153 }
154 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700155 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700156 else
157 {
158 DiffState diff;
159 ss >> diff;
160 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
161 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700162 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
163 BOOST_ASSERT (diffLeaf != 0);
164
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700165 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700166 if (diffLeaf->getOperation() == UPDATE)
167 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700168 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800169
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700170 bool inserted = false;
171 bool updated = false;
172 SeqNo oldSeq;
173 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800174
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700175 if (updated)
176 {
177 diffLog->update (info, seq);
178 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700179 }
180 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700181 else if (diffLeaf->getOperation() == REMOVE)
182 {
183 if (m_state.remove (info))
184 {
185 diffLog->remove (info);
186 m_onRemove (info->toString ());
187 }
188 }
189 else
190 {
191 BOOST_ASSERT (false); // just in case
192 }
193 }
194 }
195
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700196 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700197 if (m_log.size () > 0)
198 {
199 m_log.get<sequenced> ().front ()->setNext (diffLog);
200 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700201 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700202 }
203 catch (Error::SyncXmlDecodingFailure &e)
204 {
205 // log error
206 return;
207 }
208
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700209 // Zhenkai: shouldn't we all ways send a new Sync Interest?
210 //if (diffLog->getLeaves ().size () > 0)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700211 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700212 sendSyncInterest();
213 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800214}
215
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800216void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700217SyncLogic::processPendingSyncInterests(DiffStatePtr &diff)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800218{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700219 //cout << "Process Pending Interests" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700220 recursive_mutex::scoped_lock lock (m_stateMutex);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800221 diff->setDigest(m_state.getDigest());
222 m_log.insert(diff);
223
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800224 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800225 stringstream ss;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800226 ss << *diff;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800227 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
228 {
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800229 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800230 }
231}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800232
233void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700234SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
235{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700236 //cout << "Add local names" <<endl;
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700237 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700238 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
239 SeqNo seqN(session, seq);
240 m_state.update(info, seqN);
241
242 DiffStatePtr diff = make_shared<DiffState>();
243 diff->update(info, seqN);
244
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700245 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700246}
247
248void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700249SyncLogic::remove(const string &prefix)
250{
251 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700252 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700253 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700254
255 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700256 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700257
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700258 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700259}
260
261void
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800262SyncLogic::sendSyncInterest ()
263{
Zhenkai Zhuc81e5d32012-03-12 22:14:09 -0700264 cout << "Sending Sync Interest" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700265 recursive_mutex::scoped_lock lock (m_stateMutex);
266
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800267 ostringstream os;
268 os << m_syncPrefix << "/" << m_state.getDigest();
269
270 m_ccnxHandle->sendInterest (os.str (),
271 bind (&SyncLogic::processSyncData, this, _1, _2));
272}
273
274}