blob: bd4456faaf37222e888bad9a3064eb3f71b76ebe [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
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070041
42#ifdef NS3_MODULE
43#define GET_RANDOM(var) var.GetValue ()
44#else
45#define GET_RANDOM(var) var ()
46#endif
47
48#define TIME_SECONDS_WITH_JITTER(sec) \
49 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
50
51#define TIME_MILLISECONDS_WITH_JITTER(sec) \
52 (TIME_MILLISECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
53
54
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080055namespace Sync
56{
57
Alexander Afanasyev750d1872012-03-12 15:33:56 -070058SyncLogic::SyncLogic (const std::string &syncPrefix,
59 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070060 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080061 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070062 , m_onUpdate (onUpdate)
63 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070064 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070065#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070066 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev085742a2012-04-26 12:24:57 -070067 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,300))
68 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (0,100))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070069#else
Alexander Afanasyev085742a2012-04-26 12:24:57 -070070 , m_rangeUniformRandom (200,300)
71 , m_reexpressionJitter (0, 100)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070072#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080073{
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070074#ifdef _STANDALONE
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070075#ifdef _DEBUG
76#ifdef HAVE_LOG4CXX
77 // _LOG_FUNCTION (syncPrefix);
78 static int id = 0;
79 staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id));
80 id ++;
81#endif
82#endif
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070083#endif
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070084
85 _LOG_DEBUG ("syncPrefix");
86
87#ifndef NS3_MODULE
88 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070089
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070090 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070091 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070092
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070093 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070094 bind (&SyncLogic::sendSyncInterest, this),
95 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070096#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080097}
98
Alexander Afanasyevc1030192012-03-08 22:21:28 -080099SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800100{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700101 // _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700102 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800103
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700104 m_ccnxHandle.reset ();
105}
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700106
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700107#ifdef NS3_MODULE
108void
109SyncLogic::StartApplication ()
110{
111 m_ccnxHandle->SetNode (GetNode ());
112 m_ccnxHandle->StartApplication ();
113
114 m_ccnxHandle->setInterestFilter (m_syncPrefix,
115 bind (&SyncLogic::respondSyncInterest, this, _1));
116
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700117 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700118 bind (&SyncLogic::sendSyncInterest, this),
119 REEXPRESSING_INTEREST);
120}
121
122void
123SyncLogic::StopApplication ()
124{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700125 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700126 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700127 m_scheduler.cancel (REEXPRESSING_INTEREST);
128 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700129}
130#endif
131
132
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700133void
134SyncLogic::respondSyncInterest (const string &interest)
135{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700136 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700137 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700138 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700139 // cout << "Received Sync Interest: " << hash << endl;
140
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700141 DigestPtr digest = make_shared<Digest> ();
142 try
143 {
144 istringstream is (hash);
145 is >> *digest;
146 }
147 catch (Error::DigestCalculationError &e)
148 {
149 // log error. ignoring it for now, later we should log it
150 return;
151 }
152
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700153 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700154}
155
156void
157SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
158{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700159 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700160
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700161 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700162 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700163 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700164 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
165
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700166 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700167 m_ccnxHandle->publishData (interestName + "/state",
168 lexical_cast<string> (m_state),
169 m_syncResponseFreshness);
170 return;
171 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700172
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700173 if (*m_state.getDigest() == *digest)
174 {
175 // cout << interestName << "\n";
176 if (digest->isZero ())
177 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700178 _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700179 m_syncInterestTable.insert (interestName + "/state");
180 }
181 else
182 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700183 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700184 m_syncInterestTable.insert (interestName);
185 }
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700186
187 // !!! important change !!!
188 m_scheduler.cancel (REEXPRESSING_INTEREST);
189 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
190 bind (&SyncLogic::sendSyncInterest, this),
191 REEXPRESSING_INTEREST);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700192 return;
193 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700194
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700195 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
196
197 if (stateInDiffLog != m_log.end ())
198 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700199 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
200 // string state = lexical_cast<string> (*stateDiff);
201 // erase_all (state, "\n");
202 // _LOG_TRACE (">> D " << interestName << ", state: " << state);
203 // _LOG_DEBUG ("Log size: " << m_log.size ());
204
205 // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
206 // {
207 // string state = lexical_cast<string> (*ds);
208 // erase_all (state, "\n");
209 // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ());
210 // }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700211
212 m_syncInterestTable.remove (interestName);
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700213 _LOG_DEBUG (">> D" << interestName);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700214 m_ccnxHandle->publishData (interestName,
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700215 lexical_cast<string> (*stateDiff),
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700216 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700217 if (m_outstandingInterest == interestName)
218 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700219 m_scheduler.cancel (REEXPRESSING_INTEREST);
220 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700221 bind (&SyncLogic::sendSyncInterest, this),
222 REEXPRESSING_INTEREST);
223 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700224 return;
225 }
226
227 if (!timedProcessing)
228 {
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700229 UnknownDigestContainer::index<hashed>::type::iterator previousUnknownDigest = m_recentUnknownDigests.get<hashed> ().find (digest);
230 if (previousUnknownDigest != m_recentUnknownDigests.get<hashed> ().end ())
231 {
232 _LOG_DEBUG ("Digest is not in the log, but we have already seen it. (Don't do anything)");
233 }
234 else
235 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700236 m_recentUnknownDigests.insert (DigestTime (digest, TIME_NOW + TIME_SECONDS (m_unknownDigestStoreTime)));
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700237
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700238 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700239 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700240
241 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700242 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
243 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev1f8f0682012-04-26 13:00:14 -0700244
245 // just in case, re-express our interest (e.g., probably something bad happened)
246
247 m_scheduler.cancel (REEXPRESSING_INTEREST);
248 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
249 bind (&SyncLogic::sendSyncInterest, this),
250 REEXPRESSING_INTEREST);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700251 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700252 }
253 else
254 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700255 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700256
257 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700258 m_ccnxHandle->publishData (interestName + "/state",
259 lexical_cast<string> (m_state),
260 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700261
262 if (m_outstandingInterest == interestName)
263 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700264 m_scheduler.cancel (REEXPRESSING_INTEREST);
265 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700266 bind (&SyncLogic::sendSyncInterest, this),
267 REEXPRESSING_INTEREST);
268 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700269 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800270}
271
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800272void
273SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800274{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700275 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700276 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700277
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700278 try
279 {
280 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700281
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700282 string last = name.substr(name.find_last_of("/") + 1);
283 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700284
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700285 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 -0700286 if (last == "state")
287 {
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700288 m_syncInterestTable.remove (name.substr(0, name.find_last_of("/")));
289
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700290 FullState full;
291 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700292 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700293 {
294 NameInfoConstPtr info = leaf->getInfo ();
295 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700296
297 bool inserted = false;
298 bool updated = false;
299 SeqNo oldSeq;
300 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800301
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700302 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700303 {
304 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700305 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700306 }
307 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700308 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700309 else
310 {
311 DiffState diff;
312 ss >> diff;
313 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
314 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700315 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
316 BOOST_ASSERT (diffLeaf != 0);
317
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700318 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700319 if (diffLeaf->getOperation() == UPDATE)
320 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700321 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800322
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700323 bool inserted = false;
324 bool updated = false;
325 SeqNo oldSeq;
326 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800327
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700328 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700329 {
330 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700331 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700332 }
333 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700334 else if (diffLeaf->getOperation() == REMOVE)
335 {
336 if (m_state.remove (info))
337 {
338 diffLog->remove (info);
339 m_onRemove (info->toString ());
340 }
341 }
342 else
343 {
344 BOOST_ASSERT (false); // just in case
345 }
346 }
347 }
348
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700349 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700350 }
351 catch (Error::SyncXmlDecodingFailure &e)
352 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700353 diffLog.reset ();
354 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700355 }
356
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700357 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700358 if (diffLog->getLeaves ().size () > 0)
359 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700360 m_scheduler.cancel (REEXPRESSING_INTEREST);
361 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700362 bind (&SyncLogic::sendSyncInterest, this),
363 REEXPRESSING_INTEREST);
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700364 }
365 else
366 {
367 // should not reexpress the same interest. Need at least wait for data lifetime
368 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
369 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700370 m_scheduler.schedule (TIME_MILLISECONDS_WITH_JITTER (m_syncResponseFreshness),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700371 bind (&SyncLogic::sendSyncInterest, this),
372 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700373 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800374}
375
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800376void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700377SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800378{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800379 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700380 if (pis.size () > 0)
381 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700382 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700383 ss << *diffLog;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700384 bool satisfiedOwnInterest = false;
385
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700386 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
387 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700388 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700389 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700390
391 {
392 recursive_mutex::scoped_lock lock (m_stateMutex);
393 // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
394 satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
395 }
396 }
397
398 if (satisfiedOwnInterest)
399 {
400 _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
401 // we need to reexpress interest only if we satisfied our own interest
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700402 m_scheduler.cancel (REEXPRESSING_INTEREST);
403 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700404 bind (&SyncLogic::sendSyncInterest, this),
405 REEXPRESSING_INTEREST);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700406 }
407 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800408}
409
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800410void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700411SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700412{
413 //cout << "Process Pending Interests" <<endl;
414 diffLog->setDigest (m_state.getDigest());
415 if (m_log.size () > 0)
416 {
417 m_log.get<sequenced> ().front ()->setNext (diffLog);
418 }
419 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
420 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700421 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700422 // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700423}
424
425void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700426SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
427{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700428 DiffStatePtr diff;
429 {
430 //cout << "Add local names" <<endl;
431 recursive_mutex::scoped_lock lock (m_stateMutex);
432 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700433
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700434 SeqNo seqN (session, seq);
435 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700436
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700437 _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
438
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700439 diff = make_shared<DiffState>();
440 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700441 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700442 }
443
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700444 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
445 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700446}
447
448void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700449SyncLogic::remove(const string &prefix)
450{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700451 DiffStatePtr diff;
452 {
453 recursive_mutex::scoped_lock lock (m_stateMutex);
454 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
455 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700456
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700457 diff = make_shared<DiffState>();
458 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700459
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700460 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700461 }
462
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700463 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700464}
465
466void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800467SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800468{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800469 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800470
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700471 {
472 // cout << "Sending Sync Interest" << endl;
473 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700474
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700475 os << m_syncPrefix << "/" << *m_state.getDigest();
476
477 _LOG_TRACE (">> I " << os.str ());
478
479 m_outstandingInterest = os.str ();
480 }
481
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800482 m_ccnxHandle->sendInterest (os.str (),
483 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700484
485 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700486 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700487 bind (&SyncLogic::sendSyncInterest, this),
488 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800489}
490
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800491}