blob: 6bd7c42457544e6a83c96724a78733561aa2fe4e [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 Afanasyeva76010b2012-05-24 21:31:49 -070066 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070067#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070068 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070069 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
70 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070071#else
Alexander Afanasyev55608612012-04-30 13:24:50 -070072 , m_rangeUniformRandom (200,1000)
Alexander Afanasyeva76010b2012-05-24 21:31:49 -070073 , m_reexpressionJitter (10,500)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070074#endif
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070075{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070076#ifndef NS3_MODULE
77 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070078
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070079 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070080 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070081
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070082 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070083 bind (&SyncLogic::sendSyncInterest, this),
84 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070085#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080086}
87
Alexander Afanasyevc1030192012-03-08 22:21:28 -080088SyncLogic::~SyncLogic ()
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
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700103 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700104 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
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700118/**
119 * Two types of intersts
120 *
121 * Normal name: .../<hash>
122 * Recovery name: .../recovery/<hash>
123 */
124boost::tuple<DigestConstPtr, std::string>
125SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700126{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700127 BOOST_ASSERT (name.find (m_syncPrefix) == 0);
128
129 string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700130 if (hash[0] == '/')
131 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700132 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700133
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700134 size_t pos = hash.find ('/');
135 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700136 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700137 interestType = hash.substr (0, pos);
138 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700139 }
140
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700141 _LOG_TRACE (hash << ", " << interestType);
142
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700143 DigestPtr digest = make_shared<Digest> ();
144 istringstream is (hash);
145 is >> *digest;
146
147 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700148}
149
150void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700151SyncLogic::respondSyncInterest (const string &name)
152{
153 try
154 {
155 _LOG_TRACE ("<< I " << name);
156
157 DigestConstPtr digest;
158 string type;
159 tie (digest, type) = convertNameToDigestAndType (name);
160
161 if (type == "normal") // kind of ineffective...
162 {
163 processSyncInterest (name, digest);
164 }
165 else if (type == "recovery")
166 {
167 processSyncRecoveryInterest (name, digest);
168 }
169 }
170 catch (Error::DigestCalculationError &e)
171 {
172 _LOG_TRACE ("Something fishy happened...");
173 // log error. ignoring it for now, later we should log it
174 return ;
175 }
176}
177
178void
179SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer)
180{
181 try
182 {
183 _LOG_TRACE ("<< D " << name);
184
185 DigestConstPtr digest;
186 string type;
187 tie (digest, type) = convertNameToDigestAndType (name);
188
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700189 if (type == "normal")
190 {
191 processSyncData (name, digest, dataBuffer);
192 }
193 else
194 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700195 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700196 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
197 processSyncData (name, digest, dataBuffer);
198 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700199 }
200 catch (Error::DigestCalculationError &e)
201 {
202 _LOG_TRACE ("Something fishy happened...");
203 // log error. ignoring it for now, later we should log it
204 return;
205 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700206}
207
208
209void
210SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700211{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700212 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700213
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700214 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700215 if (digest->isZero () && !m_state->getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700216 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700217 sendSyncData (name, digest, m_state);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700218 return;
219 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700220
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700221 if (*m_state->getDigest() == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700222 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700223 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700224 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700225 return;
226 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700227
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700228 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
229
230 if (stateInDiffLog != m_log.end ())
231 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700232 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700233
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700234 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700235 return;
236 }
237
238 if (!timedProcessing)
239 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700240 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700241 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700242 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700243 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
244 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700245 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700246
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700247 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
248 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700249
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700250 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
251 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
252 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700253 }
254 else
255 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700256 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700257
258 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700259 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700260 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800261}
262
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800263void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700264SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800265{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700266 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700267 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700268
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700269 try
270 {
271 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700272
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700273 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700274
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700275 ownInterestSatisfied = (name == m_outstandingInterestName);
276
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700277 DiffState diff;
278 istringstream ss (dataBuffer);
279 ss >> diff;
280 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700281 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700282 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
283 BOOST_ASSERT (diffLeaf != 0);
284
285 NameInfoConstPtr info = diffLeaf->getInfo();
286 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700287 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700288 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700289
290 bool inserted = false;
291 bool updated = false;
292 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700293 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800294
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700295 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700296 {
297 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700298 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700299 }
300 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700301 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700302 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700303 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700304 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700305 diffLog->remove (info);
306 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700307 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700308 }
309 else
310 {
311 BOOST_ASSERT (false); // just in case
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700312 }
313 }
314
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700315 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700316 }
317 catch (Error::SyncXmlDecodingFailure &e)
318 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700319 _LOG_TRACE ("Something really fishy happened during state decoding " <<
320 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700321 diffLog.reset ();
322 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700323 }
324
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700325 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
326 ownInterestSatisfied)
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700327 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700328 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700329
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700330 // this is kind of wrong
331 // 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 -0700332
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700333 // if state has changed, then it is safe to express a new interest
334 m_scheduler.cancel (REEXPRESSING_INTEREST);
335 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
336 bind (&SyncLogic::sendSyncInterest, this),
337 REEXPRESSING_INTEREST);
338 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800339}
340
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800341void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700342SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800343{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700344 recursive_mutex::scoped_lock lock (m_stateMutex);
345
346 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
347
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700348 if (stateInDiffLog == m_log.end ())
349 {
350 _LOG_TRACE ("Could not find " << *digest << " in digest log");
351 return;
352 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700353
354 sendSyncData (name, digest, m_state);
355}
356
357void
358SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
359{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700360 DiffStatePtr fullStateLog = make_shared<DiffState> ();
361 {
362 recursive_mutex::scoped_lock lock (m_stateMutex);
363 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
364 {
365 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
366 /// @todo Impose limit on how many state info should be send out
367 }
368 }
369
370 try
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700371 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700372 uint32_t counter = 0;
373 while (m_syncInterestTable.size () > 0)
374 {
375 Interest interest = m_syncInterestTable.pop ();
376
377 if (!interest.m_unknown)
378 {
379 _LOG_TRACE (">> D " << interest.m_name);
380 sendSyncData (interest.m_name, interest.m_digest, diffLog);
381 }
382 else
383 {
384 _LOG_TRACE (">> D (unknown)" << interest.m_name);
385 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
386 }
387 counter ++;
388 }
389 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700390 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700391 catch (Error::InterestTableIsEmpty &e)
392 {
393 // ok. not really an error
394 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800395}
396
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800397void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700398SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700399{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700400 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700401 if (m_log.size () > 0)
402 {
403 m_log.get<sequenced> ().front ()->setNext (diffLog);
404 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700405 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 -0700406 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700407 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700408}
409
410void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700411SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
412{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700413 DiffStatePtr diff;
414 {
415 //cout << "Add local names" <<endl;
416 recursive_mutex::scoped_lock lock (m_stateMutex);
417 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700418
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700419 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Alexander Afanasyev42612262012-05-05 10:58:37 -0700420
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700421 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700422 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700423
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700424 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700425
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700426 diff = make_shared<DiffState>();
427 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700428 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700429 }
430
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700431 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
432 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700433}
434
435void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700436SyncLogic::remove(const string &prefix)
437{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700438 DiffStatePtr diff;
439 {
440 recursive_mutex::scoped_lock lock (m_stateMutex);
441 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700442 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700443
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700444 diff = make_shared<DiffState>();
445 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700446
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700447 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700448 }
449
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700450 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700451}
452
453void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800454SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800455{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800456 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800457
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700458 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700459 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700460
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700461 os << m_syncPrefix << "/" << *m_state->getDigest();
462 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700463 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700464 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700465
466 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700467 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700468 bind (&SyncLogic::sendSyncInterest, this),
469 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700470
471 m_ccnxHandle->sendInterest (os.str (),
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700472 bind (&SyncLogic::respondSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800473}
474
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700475void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700476SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
477{
478 ostringstream os;
479 os << m_syncPrefix << "/recovery/" << *digest;
480 _LOG_TRACE (">> I " << os.str ());
481
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700482 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
483 m_recoveryRetransmissionInterval <<= 1;
484
485 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
486 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
487 {
488 m_scheduler.schedule (nextRetransmission,
489 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
490 REEXPRESSING_RECOVERY_INTEREST);
491 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700492
493 m_ccnxHandle->sendInterest (os.str (),
494 bind (&SyncLogic::respondSyncData, this, _1, _2));
495}
496
497
498void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700499SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
500{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700501 _LOG_TRACE (">> D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700502 // sending
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700503 m_ccnxHandle->publishData (name,
504 lexical_cast<string> (*state),
505 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700506
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700507 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700508 bool satisfiedOwnInterest = false;
509 {
510 recursive_mutex::scoped_lock lock (m_stateMutex);
511 satisfiedOwnInterest = (m_outstandingInterestName == name);
512 }
513
514 if (satisfiedOwnInterest)
515 {
516 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
517
518 m_scheduler.cancel (REEXPRESSING_INTEREST);
519 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
520 bind (&SyncLogic::sendSyncInterest, this),
521 REEXPRESSING_INTEREST);
522 }
523}
524
525
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800526}