blob: 3098b00fae30700908284e013df7f4b2ff89eee1 [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 Afanasyev0ac399e2012-04-27 13:28:28 -070068 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (500,1000))
Alexander Afanasyev085742a2012-04-26 12:24:57 -070069 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (0,100))
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");
222 m_syncInterestTable.insert (digest, name);
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 Afanasyevf3c03a92012-05-09 12:00:37 -0700238 bool exists = m_syncInterestTable.insert (digest, name);
239 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 Afanasyeve4e2bf72012-03-12 12:44:54 -0700263
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700264 try
265 {
266 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700267
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700268 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700269
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700270 DiffState diff;
271 istringstream ss (dataBuffer);
272 ss >> diff;
273 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700274 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700275 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
276 BOOST_ASSERT (diffLeaf != 0);
277
278 NameInfoConstPtr info = diffLeaf->getInfo();
279 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700280 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700281 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700282
283 bool inserted = false;
284 bool updated = false;
285 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700286 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800287
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700288 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700289 {
290 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700291 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700292 }
293 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700294 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700295 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700296 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700297 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700298 diffLog->remove (info);
299 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700300 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700301 }
302 else
303 {
304 BOOST_ASSERT (false); // just in case
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700305 }
306 }
307
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700308 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700309 }
310 catch (Error::SyncXmlDecodingFailure &e)
311 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700312 _LOG_TRACE ("Something really fishy happened during state decoding " <<
313 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700314 diffLog.reset ();
315 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700316 }
317
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700318 if (diffLog != 0 && diffLog->getLeaves ().size () > 0)
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700319 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700320 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700321
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700322 // this is kind of wrong
323 // 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 -0700324
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700325 // if state has changed, then it is safe to express a new interest
326 m_scheduler.cancel (REEXPRESSING_INTEREST);
327 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
328 bind (&SyncLogic::sendSyncInterest, this),
329 REEXPRESSING_INTEREST);
330 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800331}
332
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800333void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700334SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800335{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700336 recursive_mutex::scoped_lock lock (m_stateMutex);
337
338 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
339
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700340 if (stateInDiffLog == m_log.end ())
341 {
342 _LOG_TRACE ("Could not find " << *digest << " in digest log");
343 return;
344 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700345
346 sendSyncData (name, digest, m_state);
347}
348
349void
350SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
351{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700352 DiffStatePtr fullStateLog = make_shared<DiffState> ();
353 {
354 recursive_mutex::scoped_lock lock (m_stateMutex);
355 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
356 {
357 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
358 /// @todo Impose limit on how many state info should be send out
359 }
360 }
361
362 try
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700363 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700364 uint32_t counter = 0;
365 while (m_syncInterestTable.size () > 0)
366 {
367 Interest interest = m_syncInterestTable.pop ();
368
369 if (!interest.m_unknown)
370 {
371 _LOG_TRACE (">> D " << interest.m_name);
372 sendSyncData (interest.m_name, interest.m_digest, diffLog);
373 }
374 else
375 {
376 _LOG_TRACE (">> D (unknown)" << interest.m_name);
377 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
378 }
379 counter ++;
380 }
381 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700382 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700383 catch (Error::InterestTableIsEmpty &e)
384 {
385 // ok. not really an error
386 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800387}
388
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800389void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700390SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700391{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700392 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700393 if (m_log.size () > 0)
394 {
395 m_log.get<sequenced> ().front ()->setNext (diffLog);
396 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700397 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 -0700398 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700399 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700400}
401
402void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700403SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
404{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700405 DiffStatePtr diff;
406 {
407 //cout << "Add local names" <<endl;
408 recursive_mutex::scoped_lock lock (m_stateMutex);
409 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700410
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700411 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Alexander Afanasyev42612262012-05-05 10:58:37 -0700412
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700413 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700414 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700415
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700416 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700417
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700418 diff = make_shared<DiffState>();
419 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700420 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700421 }
422
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700423 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
424 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700425}
426
427void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700428SyncLogic::remove(const string &prefix)
429{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700430 DiffStatePtr diff;
431 {
432 recursive_mutex::scoped_lock lock (m_stateMutex);
433 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700434 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700435
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700436 diff = make_shared<DiffState>();
437 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700438
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700439 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700440 }
441
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700442 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700443}
444
445void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800446SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800447{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800448 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800449
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700450 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700451 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700452
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700453 os << m_syncPrefix << "/" << *m_state->getDigest();
454 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700455 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700456 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700457
458 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700459 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700460 bind (&SyncLogic::sendSyncInterest, this),
461 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700462
463 m_ccnxHandle->sendInterest (os.str (),
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700464 bind (&SyncLogic::respondSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800465}
466
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700467void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700468SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
469{
470 ostringstream os;
471 os << m_syncPrefix << "/recovery/" << *digest;
472 _LOG_TRACE (">> I " << os.str ());
473
474 // no need for retransmission
475 // m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
476 // m_scheduler.schedule (TIME_SECONDS_WITH_JITTER(1.0),//TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
477 // bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
478 // REEXPRESSING_RECOVERY_INTEREST);
479
480 m_ccnxHandle->sendInterest (os.str (),
481 bind (&SyncLogic::respondSyncData, this, _1, _2));
482}
483
484
485void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700486SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
487{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700488 _LOG_TRACE (">> D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700489 // sending
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700490 m_ccnxHandle->publishData (name,
491 lexical_cast<string> (*state),
492 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700493
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700494 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700495 bool satisfiedOwnInterest = false;
496 {
497 recursive_mutex::scoped_lock lock (m_stateMutex);
498 satisfiedOwnInterest = (m_outstandingInterestName == name);
499 }
500
501 if (satisfiedOwnInterest)
502 {
503 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
504
505 m_scheduler.cancel (REEXPRESSING_INTEREST);
506 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
507 bind (&SyncLogic::sendSyncInterest, this),
508 REEXPRESSING_INTEREST);
509 }
510}
511
512
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800513}