blob: c46dbe8b5ff7edc86a500224a9336a04a2367f6c [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
52 // We need to send out our Sync Interest when we're ready
53 sendSyncInterest();
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{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -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
87 if (*m_state.getDigest() == *digest)
88 {
89 m_syncInterestTable.insert (interestName);
90 return;
91 }
92
93 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
94
95 if (stateInDiffLog != m_log.end ())
96 {
97 m_ccnxHandle->publishData (interestName,
98 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
99 m_syncResponseFreshness);
100 return;
101 }
102
103 if (!timedProcessing)
104 {
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700105 m_delayedChecksScheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
106 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700107
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700108 }
109 else
110 {
111 m_ccnxHandle->publishData (interestName + "/state",
112 lexical_cast<string> (m_state),
113 m_syncResponseFreshness);
114 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800115}
116
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800117void
118SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800119{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700120 //cout << "Process Sync Data" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700121 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700122
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700123 try
124 {
125 recursive_mutex::scoped_lock lock (m_stateMutex);
126
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700127 string last = name.substr(name.find_last_of("/") + 1);
128 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700129
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700130 if (last == "state")
131 {
132 FullState full;
133 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700134 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700135 {
136 NameInfoConstPtr info = leaf->getInfo ();
137 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700138
139 bool inserted = false;
140 bool updated = false;
141 SeqNo oldSeq;
142 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800143
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700144 if (updated)
145 {
146 diffLog->update (info, seq);
147 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700148 }
149 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700150 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700151 else
152 {
153 DiffState diff;
154 ss >> diff;
155 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
156 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700157 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
158 BOOST_ASSERT (diffLeaf != 0);
159
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700160 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700161 if (diffLeaf->getOperation() == UPDATE)
162 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700163 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800164
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700165 bool inserted = false;
166 bool updated = false;
167 SeqNo oldSeq;
168 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800169
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700170 if (updated)
171 {
172 diffLog->update (info, seq);
173 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700174 }
175 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700176 else if (diffLeaf->getOperation() == REMOVE)
177 {
178 if (m_state.remove (info))
179 {
180 diffLog->remove (info);
181 m_onRemove (info->toString ());
182 }
183 }
184 else
185 {
186 BOOST_ASSERT (false); // just in case
187 }
188 }
189 }
190
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700191 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700192 if (m_log.size () > 0)
193 {
194 m_log.get<sequenced> ().front ()->setNext (diffLog);
195 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700196 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700197 }
198 catch (Error::SyncXmlDecodingFailure &e)
199 {
200 // log error
201 return;
202 }
203
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700204 // Zhenkai: shouldn't we all ways send a new Sync Interest?
205 //if (diffLog->getLeaves ().size () > 0)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700206 {
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 Afanasyevd91497c2012-03-12 15:39:30 -0700212SyncLogic::processPendingSyncInterests(DiffStatePtr &diff)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800213{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700214 //cout << "Process Pending Interests" <<endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700215 recursive_mutex::scoped_lock lock (m_stateMutex);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800216 diff->setDigest(m_state.getDigest());
217 m_log.insert(diff);
218
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800219 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800220 stringstream ss;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800221 ss << *diff;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800222 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
223 {
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800224 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800225 }
226}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800227
228void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700229SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
230{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700231 //cout << "Add local names" <<endl;
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700232 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700233 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
234 SeqNo seqN(session, seq);
235 m_state.update(info, seqN);
236
237 DiffStatePtr diff = make_shared<DiffState>();
238 diff->update(info, seqN);
239
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700240 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700241}
242
243void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700244SyncLogic::remove(const string &prefix)
245{
246 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700247 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700248 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700249
250 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700251 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700252
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700253 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700254}
255
256void
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800257SyncLogic::sendSyncInterest ()
258{
Zhenkai Zhud010e2c2012-03-12 21:55:04 -0700259 //cout << "Sending Sync Interest" << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700260 recursive_mutex::scoped_lock lock (m_stateMutex);
261
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800262 ostringstream os;
263 os << m_syncPrefix << "/" << m_state.getDigest();
264
265 m_ccnxHandle->sendInterest (os.str (),
266 bind (&SyncLogic::processSyncData, this, _1, _2));
267}
268
269}