blob: e947eda0660f7b6ad847d6bd11626303a6beb2b0 [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
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070023#include <ns3/ccnx-pit.h>
24#include <ns3/ccnx.h>
25
Chaoyi Bian11f294f2012-03-08 14:28:06 -080026#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080027#include "sync-diff-leaf.h"
28#include "sync-full-leaf.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070029#include "sync-log.h"
30
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080031#include <boost/make_shared.hpp>
32#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070033#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080034#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080035
36using namespace std;
37using namespace boost;
38
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070039INIT_LOGGER ("SyncLogic");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070040
41#ifdef NS3_MODULE
42#define GET_RANDOM(var) var.GetValue ()
43#else
44#define GET_RANDOM(var) var ()
45#endif
46
47#define TIME_SECONDS_WITH_JITTER(sec) \
48 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
49
Alexander Afanasyev42612262012-05-05 10:58:37 -070050#define TIME_MILLISECONDS_WITH_JITTER(ms) \
51 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070052
53
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080054namespace Sync
55{
56
Alexander Afanasyev750d1872012-03-12 15:33:56 -070057SyncLogic::SyncLogic (const std::string &syncPrefix,
58 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070059 LogicRemoveCallback onRemove)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070060 : m_state (new FullState)
61 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
62 , m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070063 , m_onUpdate (onUpdate)
64 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070065 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070066#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070067 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070068 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
69 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070070#else
Alexander Afanasyev55608612012-04-30 13:24:50 -070071 , m_rangeUniformRandom (200,1000)
72 , m_reexpressionJitter (100,500)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070073#endif
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070074{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070075#ifndef NS3_MODULE
76 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070077
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070078 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070079 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070080
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070081 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070082 bind (&SyncLogic::sendSyncInterest, this),
83 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070084#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080085}
86
Alexander Afanasyevc1030192012-03-08 22:21:28 -080087SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080088{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070089 m_ccnxHandle.reset ();
90}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070091
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070092#ifdef NS3_MODULE
93void
94SyncLogic::StartApplication ()
95{
96 m_ccnxHandle->SetNode (GetNode ());
97 m_ccnxHandle->StartApplication ();
98
99 m_ccnxHandle->setInterestFilter (m_syncPrefix,
100 bind (&SyncLogic::respondSyncInterest, this, _1));
101
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700102 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700103 bind (&SyncLogic::sendSyncInterest, this),
104 REEXPRESSING_INTEREST);
105}
106
107void
108SyncLogic::StopApplication ()
109{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700110 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700111 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700112 m_scheduler.cancel (REEXPRESSING_INTEREST);
113 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700114}
115#endif
116
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700117/**
118 * Two types of intersts
119 *
120 * Normal name: .../<hash>
121 * Recovery name: .../recovery/<hash>
122 */
123boost::tuple<DigestConstPtr, std::string>
124SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700125{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700126 BOOST_ASSERT (name.find (m_syncPrefix) == 0);
127
128 string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700129 if (hash[0] == '/')
130 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700131 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700132
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700133 size_t pos = hash.find ('/');
134 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700135 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700136 interestType = hash.substr (0, pos);
137 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700138 }
139
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700140 _LOG_TRACE (hash << ", " << interestType);
141
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700142 DigestPtr digest = make_shared<Digest> ();
143 istringstream is (hash);
144 is >> *digest;
145
146 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700147}
148
149void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700150SyncLogic::respondSyncInterest (const string &name)
151{
152 try
153 {
154 _LOG_TRACE ("<< I " << name);
155
156 DigestConstPtr digest;
157 string type;
158 tie (digest, type) = convertNameToDigestAndType (name);
159
160 if (type == "normal") // kind of ineffective...
161 {
162 processSyncInterest (name, digest);
163 }
164 else if (type == "recovery")
165 {
166 processSyncRecoveryInterest (name, digest);
167 }
168 }
169 catch (Error::DigestCalculationError &e)
170 {
171 _LOG_TRACE ("Something fishy happened...");
172 // log error. ignoring it for now, later we should log it
173 return ;
174 }
175}
176
177void
178SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer)
179{
180 try
181 {
182 _LOG_TRACE ("<< D " << name);
183
184 DigestConstPtr digest;
185 string type;
186 tie (digest, type) = convertNameToDigestAndType (name);
187
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700188 if (type == "normal")
189 {
190 processSyncData (name, digest, dataBuffer);
191 }
192 else
193 {
194 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
195 processSyncData (name, digest, dataBuffer);
196 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700197 }
198 catch (Error::DigestCalculationError &e)
199 {
200 _LOG_TRACE ("Something fishy happened...");
201 // log error. ignoring it for now, later we should log it
202 return;
203 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700204}
205
206
207void
208SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700209{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700210 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700211
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700212 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700213 if (digest->isZero () && !m_state->getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700214 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700215 sendSyncData (name, digest, m_state);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700216 return;
217 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700218
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700219 if (*m_state->getDigest() == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700220 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700221 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700222 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700223 return;
224 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700225
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700226 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
227
228 if (stateInDiffLog != m_log.end ())
229 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700230 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700231
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700232 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700233 return;
234 }
235
236 if (!timedProcessing)
237 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700238 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700239 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700240 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700241 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
242 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700243 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700244
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700245 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
246 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700247
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700248 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
249 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
250 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700251 }
252 else
253 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700254 _LOG_TRACE (" (timed processing)");
255 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700256 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800257}
258
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800259void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700260SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800261{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700262 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700263 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700264
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700265 try
266 {
267 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700268
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700269 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700270
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700271 ownInterestSatisfied = (name == m_outstandingInterestName);
272
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700273 DiffState diff;
274 istringstream ss (dataBuffer);
275 ss >> diff;
276 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700277 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700278 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
279 BOOST_ASSERT (diffLeaf != 0);
280
281 NameInfoConstPtr info = diffLeaf->getInfo();
282 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700283 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700284 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700285
286 bool inserted = false;
287 bool updated = false;
288 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700289 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800290
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700291 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700292 {
293 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700294 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700295 }
296 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700297 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700298 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700299 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700300 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700301 diffLog->remove (info);
302 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700303 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700304 }
305 else
306 {
307 BOOST_ASSERT (false); // just in case
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700308 }
309 }
310
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700311 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700312 }
313 catch (Error::SyncXmlDecodingFailure &e)
314 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700315 _LOG_TRACE ("Something really fishy happened during state decoding " <<
316 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700317 diffLog.reset ();
318 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700319 }
320
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700321 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
322 ownInterestSatisfied)
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700323 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700324 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700325
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700326 // this is kind of wrong
327 // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT, there is a point to satisfy them using new state
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700328
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700329 // if state has changed, then it is safe to express a new interest
330 m_scheduler.cancel (REEXPRESSING_INTEREST);
331 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
332 bind (&SyncLogic::sendSyncInterest, this),
333 REEXPRESSING_INTEREST);
334 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800335}
336
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800337void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700338SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800339{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700340 recursive_mutex::scoped_lock lock (m_stateMutex);
341
342 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
343
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700344 if (stateInDiffLog == m_log.end ())
345 {
346 _LOG_TRACE ("Could not find " << *digest << " in digest log");
347 return;
348 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700349
350 sendSyncData (name, digest, m_state);
351}
352
353void
354SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
355{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700356 DiffStatePtr fullStateLog = make_shared<DiffState> ();
357 {
358 recursive_mutex::scoped_lock lock (m_stateMutex);
359 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
360 {
361 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
362 /// @todo Impose limit on how many state info should be send out
363 }
364 }
365
366 try
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700367 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700368 uint32_t counter = 0;
369 while (m_syncInterestTable.size () > 0)
370 {
371 Interest interest = m_syncInterestTable.pop ();
372
373 if (!interest.m_unknown)
374 {
375 _LOG_TRACE (">> D " << interest.m_name);
376 sendSyncData (interest.m_name, interest.m_digest, diffLog);
377 }
378 else
379 {
380 _LOG_TRACE (">> D (unknown)" << interest.m_name);
381 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
382 }
383 counter ++;
384 }
385 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700386 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700387 catch (Error::InterestTableIsEmpty &e)
388 {
389 // ok. not really an error
390 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800391}
392
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800393void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700394SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700395{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700396 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700397 if (m_log.size () > 0)
398 {
399 m_log.get<sequenced> ().front ()->setNext (diffLog);
400 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700401 m_log.erase (m_state->getDigest()); // remove diff state with the same digest. next pointers are still valid
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700402 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700403 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700404}
405
406void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700407SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
408{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700409 DiffStatePtr diff;
410 {
411 //cout << "Add local names" <<endl;
412 recursive_mutex::scoped_lock lock (m_stateMutex);
413 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700414
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700415 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Alexander Afanasyev42612262012-05-05 10:58:37 -0700416
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700417 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700418 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700419
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700420 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700421
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700422 diff = make_shared<DiffState>();
423 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700424 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700425 }
426
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700427 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
428 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700429}
430
431void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700432SyncLogic::remove(const string &prefix)
433{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700434 DiffStatePtr diff;
435 {
436 recursive_mutex::scoped_lock lock (m_stateMutex);
437 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700438 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700439
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700440 diff = make_shared<DiffState>();
441 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700442
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700443 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700444 }
445
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700446 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700447}
448
449void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800450SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800451{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800452 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800453
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700454 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700455 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700456
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700457 os << m_syncPrefix << "/" << *m_state->getDigest();
458 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700459 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700460 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700461
462 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700463 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700464 bind (&SyncLogic::sendSyncInterest, this),
465 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700466
467 m_ccnxHandle->sendInterest (os.str (),
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700468 bind (&SyncLogic::respondSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800469}
470
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700471void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700472SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
473{
474 ostringstream os;
475 os << m_syncPrefix << "/recovery/" << *digest;
476 _LOG_TRACE (">> I " << os.str ());
477
478 // no need for retransmission
479 // m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
480 // m_scheduler.schedule (TIME_SECONDS_WITH_JITTER(1.0),//TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
481 // bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
482 // REEXPRESSING_RECOVERY_INTEREST);
483
484 m_ccnxHandle->sendInterest (os.str (),
485 bind (&SyncLogic::respondSyncData, this, _1, _2));
486}
487
488
489void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700490SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
491{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700492 _LOG_TRACE (">> D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700493 // sending
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700494 m_ccnxHandle->publishData (name,
495 lexical_cast<string> (*state),
496 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700497
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700498 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700499 bool satisfiedOwnInterest = false;
500 {
501 recursive_mutex::scoped_lock lock (m_stateMutex);
502 satisfiedOwnInterest = (m_outstandingInterestName == name);
503 }
504
505 if (satisfiedOwnInterest)
506 {
507 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
508
509 m_scheduler.cancel (REEXPRESSING_INTEREST);
510 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
511 bind (&SyncLogic::sendSyncInterest, this),
512 REEXPRESSING_INTEREST);
513 }
514}
515
516
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800517}