blob: d4e5163524dd51a69d10cfee4bd29b0992eb35bb [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080020 * 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"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070026#include "sync-log.h"
27
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080028#include <boost/make_shared.hpp>
29#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030#include <boost/lexical_cast.hpp>
31#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080032#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080033
34using namespace std;
35using namespace boost;
36
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070037#ifndef _STANDALONE
38INIT_LOGGER ("SyncLogic");
39#endif
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070040
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041namespace Sync
42{
43
Alexander Afanasyev750d1872012-03-12 15:33:56 -070044SyncLogic::SyncLogic (const std::string &syncPrefix,
45 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070046 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080047 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070048 , m_onUpdate (onUpdate)
49 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070050 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070051#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070052 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev085742a2012-04-26 12:24:57 -070053 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,300))
54 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (0,100))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070055#else
Alexander Afanasyev085742a2012-04-26 12:24:57 -070056 , m_rangeUniformRandom (200,300)
57 , m_reexpressionJitter (0, 100)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070058#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080059{
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070060#ifdef _STANDALONE
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070061#ifdef _DEBUG
62#ifdef HAVE_LOG4CXX
63 // _LOG_FUNCTION (syncPrefix);
64 static int id = 0;
65 staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id));
66 id ++;
67#endif
68#endif
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070069#endif
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070070
71 _LOG_DEBUG ("syncPrefix");
72
73#ifndef NS3_MODULE
74 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070075
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070076 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070077 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070078
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070079 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070080 bind (&SyncLogic::sendSyncInterest, this),
81 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070082#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080083}
84
Alexander Afanasyevc1030192012-03-08 22:21:28 -080085SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080086{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070087 // _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070088 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080089
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070090 m_ccnxHandle.reset ();
91}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070092
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070093#ifdef NS3_MODULE
94void
95SyncLogic::StartApplication ()
96{
97 m_ccnxHandle->SetNode (GetNode ());
98 m_ccnxHandle->StartApplication ();
99
100 m_ccnxHandle->setInterestFilter (m_syncPrefix,
101 bind (&SyncLogic::respondSyncInterest, this, _1));
102
103 m_scheduler.schedule (TIME_SECONDS (0),
104 bind (&SyncLogic::sendSyncInterest, this),
105 REEXPRESSING_INTEREST);
106}
107
108void
109SyncLogic::StopApplication ()
110{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700111 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700112 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700113 m_scheduler.cancel (REEXPRESSING_INTEREST);
114 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700115}
116#endif
117
118
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700119void
120SyncLogic::respondSyncInterest (const string &interest)
121{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700122 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700123 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700124 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700125 // cout << "Received Sync Interest: " << hash << endl;
126
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700127 DigestPtr digest = make_shared<Digest> ();
128 try
129 {
130 istringstream is (hash);
131 is >> *digest;
132 }
133 catch (Error::DigestCalculationError &e)
134 {
135 // log error. ignoring it for now, later we should log it
136 return;
137 }
138
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700139 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700140}
141
142void
143SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
144{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700145 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700146
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700147 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700148 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700149 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700150 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
151
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700152 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700153 m_ccnxHandle->publishData (interestName + "/state",
154 lexical_cast<string> (m_state),
155 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700156 if (m_outstandingInterest == interestName)
157 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700158 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700159 bind (&SyncLogic::sendSyncInterest, this),
160 REEXPRESSING_INTEREST);
161 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700162 return;
163 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700164
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700165 if (*m_state.getDigest() == *digest)
166 {
167 // cout << interestName << "\n";
168 if (digest->isZero ())
169 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700170 _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700171 m_syncInterestTable.insert (interestName + "/state");
172 }
173 else
174 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700175 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700176 m_syncInterestTable.insert (interestName);
177 }
178 return;
179 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700180
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700181 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
182
183 if (stateInDiffLog != m_log.end ())
184 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700185 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
186 // string state = lexical_cast<string> (*stateDiff);
187 // erase_all (state, "\n");
188 // _LOG_TRACE (">> D " << interestName << ", state: " << state);
189 // _LOG_DEBUG ("Log size: " << m_log.size ());
190
191 // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
192 // {
193 // string state = lexical_cast<string> (*ds);
194 // erase_all (state, "\n");
195 // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ());
196 // }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700197
198 m_syncInterestTable.remove (interestName);
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700199 _LOG_DEBUG (">> D" << interestName);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700200 m_ccnxHandle->publishData (interestName,
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700201 lexical_cast<string> (*stateDiff),
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700202 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700203 if (m_outstandingInterest == interestName)
204 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700205 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700206 bind (&SyncLogic::sendSyncInterest, this),
207 REEXPRESSING_INTEREST);
208 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700209 return;
210 }
211
212 if (!timedProcessing)
213 {
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700214 UnknownDigestContainer::index<hashed>::type::iterator previousUnknownDigest = m_recentUnknownDigests.get<hashed> ().find (digest);
215 if (previousUnknownDigest != m_recentUnknownDigests.get<hashed> ().end ())
216 {
217 _LOG_DEBUG ("Digest is not in the log, but we have already seen it. (Don't do anything)");
218 }
219 else
220 {
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700221 // m_recentUnknownDigests.insert (DigestTime (digest, TIME_NOW + TIME_SECONDS (m_unknownDigestStoreTime)));
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700222
223 uint32_t waitDelay =
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700224#ifndef NS3_MODULE
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700225 m_rangeUniformRandom ()
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700226#else
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700227 m_rangeUniformRandom.GetValue ()
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700228#endif
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700229 ;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700230
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700231 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
232 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay) /*from 20 to 100ms*/,
233 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
234 DELAYED_INTEREST_PROCESSING);
235 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700236 }
237 else
238 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700239 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700240
241 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700242 m_ccnxHandle->publishData (interestName + "/state",
243 lexical_cast<string> (m_state),
244 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700245
246 if (m_outstandingInterest == interestName)
247 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700248 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700249 bind (&SyncLogic::sendSyncInterest, this),
250 REEXPRESSING_INTEREST);
251 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700252 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800253}
254
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800255void
256SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800257{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700258 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700259 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700260
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700261 try
262 {
263 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700264
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700265 string last = name.substr(name.find_last_of("/") + 1);
266 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700267
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700268 m_syncInterestTable.remove (name); // just in case, remove both interests from our interest table. No reason to keep them there
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700269 if (last == "state")
270 {
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700271 m_syncInterestTable.remove (name.substr(0, name.find_last_of("/")));
272
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700273 FullState full;
274 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700275 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700276 {
277 NameInfoConstPtr info = leaf->getInfo ();
278 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700279
280 bool inserted = false;
281 bool updated = false;
282 SeqNo oldSeq;
283 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800284
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700285 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700286 {
287 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700288 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700289 }
290 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700291 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700292 else
293 {
294 DiffState diff;
295 ss >> diff;
296 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
297 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700298 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
299 BOOST_ASSERT (diffLeaf != 0);
300
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700301 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700302 if (diffLeaf->getOperation() == UPDATE)
303 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700304 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800305
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700306 bool inserted = false;
307 bool updated = false;
308 SeqNo oldSeq;
309 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800310
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700311 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700312 {
313 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700314 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700315 }
316 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700317 else if (diffLeaf->getOperation() == REMOVE)
318 {
319 if (m_state.remove (info))
320 {
321 diffLog->remove (info);
322 m_onRemove (info->toString ());
323 }
324 }
325 else
326 {
327 BOOST_ASSERT (false); // just in case
328 }
329 }
330 }
331
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700332 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700333 }
334 catch (Error::SyncXmlDecodingFailure &e)
335 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700336 diffLog.reset ();
337 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700338 }
339
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700340 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700341 if (diffLog->getLeaves ().size () > 0)
342 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700343 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700344 bind (&SyncLogic::sendSyncInterest, this),
345 REEXPRESSING_INTEREST);
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700346 }
347 else
348 {
349 // should not reexpress the same interest. Need at least wait for data lifetime
350 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
351 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700352 // m_scheduler.schedule (posix_time::seconds (0),
Alexander Afanasyev70f32052012-04-24 14:10:38 -0700353 m_scheduler.schedule (TIME_MILLISECONDS (m_syncResponseFreshness) + TIME_MILLISECONDS (1),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700354 bind (&SyncLogic::sendSyncInterest, this),
355 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700356 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800357}
358
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800359void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700360SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800361{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800362 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700363 if (pis.size () > 0)
364 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700365 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700366 ss << *diffLog;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700367 bool satisfiedOwnInterest = false;
368
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700369 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
370 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700371 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700372 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700373
374 {
375 recursive_mutex::scoped_lock lock (m_stateMutex);
376 // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
377 satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
378 }
379 }
380
381 if (satisfiedOwnInterest)
382 {
383 _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
384 // we need to reexpress interest only if we satisfied our own interest
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700385 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700386 bind (&SyncLogic::sendSyncInterest, this),
387 REEXPRESSING_INTEREST);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700388 }
389 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800390}
391
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800392void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700393SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700394{
395 //cout << "Process Pending Interests" <<endl;
396 diffLog->setDigest (m_state.getDigest());
397 if (m_log.size () > 0)
398 {
399 m_log.get<sequenced> ().front ()->setNext (diffLog);
400 }
401 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
402 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700403 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700404 // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700405}
406
407void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700408SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
409{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700410 DiffStatePtr diff;
411 {
412 //cout << "Add local names" <<endl;
413 recursive_mutex::scoped_lock lock (m_stateMutex);
414 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700415
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700416 SeqNo seqN (session, seq);
417 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700418
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700419 _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
420
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700421 diff = make_shared<DiffState>();
422 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700423 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700424 }
425
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700426 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
427 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700428}
429
430void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700431SyncLogic::remove(const string &prefix)
432{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700433 DiffStatePtr diff;
434 {
435 recursive_mutex::scoped_lock lock (m_stateMutex);
436 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
437 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700438
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700439 diff = make_shared<DiffState>();
440 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700441
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700442 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700443 }
444
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700445 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700446}
447
448void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800449SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800450{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800451 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800452
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700453 {
454 // cout << "Sending Sync Interest" << endl;
455 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700456
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700457 os << m_syncPrefix << "/" << *m_state.getDigest();
458
459 _LOG_TRACE (">> I " << os.str ());
460
461 m_outstandingInterest = os.str ();
462 }
463
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800464 m_ccnxHandle->sendInterest (os.str (),
465 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700466
467 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700468 m_scheduler.schedule (TIME_SECONDS (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700469 bind (&SyncLogic::sendSyncInterest, this),
470 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800471}
472
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800473}