blob: 7235220dd98fc8f63af0dd0a7d55b312b2f54551 [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));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080051}
52
Alexander Afanasyevc1030192012-03-08 22:21:28 -080053SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080054{
Alexander Afanasyev387ac952012-03-11 23:49:27 -070055}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080056
Alexander Afanasyev387ac952012-03-11 23:49:27 -070057
58void
59SyncLogic::respondSyncInterest (const string &interest)
60{
61 string hash = interest.substr(interest.find_last_of("/") + 1);
62 DigestPtr digest = make_shared<Digest> ();
63 try
64 {
65 istringstream is (hash);
66 is >> *digest;
67 }
68 catch (Error::DigestCalculationError &e)
69 {
70 // log error. ignoring it for now, later we should log it
71 return;
72 }
73
74 processSyncInterest (digest, interest);
75}
76
77void
78SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
79{
80 // cout << "SyncLogic::processSyncInterest " << timedProcessing << endl;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070081 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -070082
83 if (*m_state.getDigest() == *digest)
84 {
85 m_syncInterestTable.insert (interestName);
86 return;
87 }
88
89 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
90
91 if (stateInDiffLog != m_log.end ())
92 {
93 m_ccnxHandle->publishData (interestName,
94 lexical_cast<string> (*(*stateInDiffLog)->diff ()),
95 m_syncResponseFreshness);
96 return;
97 }
98
99 if (!timedProcessing)
100 {
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700101 m_delayedChecksScheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
102 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true));
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700103
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700104 }
105 else
106 {
107 m_ccnxHandle->publishData (interestName + "/state",
108 lexical_cast<string> (m_state),
109 m_syncResponseFreshness);
110 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800111}
112
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800113void
114SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800115{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700116 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700117
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700118 try
119 {
120 recursive_mutex::scoped_lock lock (m_stateMutex);
121
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700122 string last = name.substr(name.find_last_of("/") + 1);
123 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700124
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700125 if (last == "state")
126 {
127 FullState full;
128 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700129 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700130 {
131 NameInfoConstPtr info = leaf->getInfo ();
132 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700133
134 bool inserted = false;
135 bool updated = false;
136 SeqNo oldSeq;
137 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800138
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700139 if (updated)
140 {
141 diffLog->update (info, seq);
142 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700143 }
144 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700145 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700146 else
147 {
148 DiffState diff;
149 ss >> diff;
150 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
151 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700152 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
153 BOOST_ASSERT (diffLeaf != 0);
154
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700155 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700156 if (diffLeaf->getOperation() == UPDATE)
157 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700158 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800159
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700160 bool inserted = false;
161 bool updated = false;
162 SeqNo oldSeq;
163 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800164
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700165 if (updated)
166 {
167 diffLog->update (info, seq);
168 m_onUpdate (info->toString (), seq.getSeq(), oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700169 }
170 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700171 else if (diffLeaf->getOperation() == REMOVE)
172 {
173 if (m_state.remove (info))
174 {
175 diffLog->remove (info);
176 m_onRemove (info->toString ());
177 }
178 }
179 else
180 {
181 BOOST_ASSERT (false); // just in case
182 }
183 }
184 }
185
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700186 diffLog->setDigest(m_state.getDigest());
Alexander Afanasyeva36b7512012-03-12 16:03:03 -0700187 if (m_log.size () > 0)
188 {
189 m_log.get<sequenced> ().front ()->setNext (diffLog);
190 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700191 m_log.insert (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700192 }
193 catch (Error::SyncXmlDecodingFailure &e)
194 {
195 // log error
196 return;
197 }
198
199 if (diffLog->getLeaves ().size () > 0)
200 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700201 sendSyncInterest();
202 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800203}
204
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800205void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700206SyncLogic::processPendingSyncInterests(DiffStatePtr &diff)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800207{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700208 recursive_mutex::scoped_lock lock (m_stateMutex);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800209 diff->setDigest(m_state.getDigest());
210 m_log.insert(diff);
211
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800212 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800213 stringstream ss;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800214 ss << *diff;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800215 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
216 {
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800217 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800218 }
219}
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800220
221void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700222SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
223{
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700224 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700225 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
226 SeqNo seqN(session, seq);
227 m_state.update(info, seqN);
228
229 DiffStatePtr diff = make_shared<DiffState>();
230 diff->update(info, seqN);
231
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700232 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700233}
234
235void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700236SyncLogic::remove(const string &prefix)
237{
238 recursive_mutex::scoped_lock lock (m_stateMutex);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700239 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700240 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700241
242 DiffStatePtr diff = make_shared<DiffState>();
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700243 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700244
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700245 processPendingSyncInterests(diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700246}
247
248void
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800249SyncLogic::sendSyncInterest ()
250{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700251 recursive_mutex::scoped_lock lock (m_stateMutex);
252
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800253 ostringstream os;
Alexander Afanasyev8a3d9132012-03-12 23:54:32 -0700254 os << m_syncPrefix << "/" << *m_state.getDigest();
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800255
256 m_ccnxHandle->sendInterest (os.str (),
257 bind (&SyncLogic::processSyncData, this, _1, _2));
258}
259
260}