blob: 13b66f51efd74878fd7a7cae43a8ae6406aa3960 [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 Afanasyevf3c03a92012-05-09 12:00:37 -0700136 hash = name.substr (pos + 1);
137 interestType = name.substr (0, pos);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700138 }
139
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700140 DigestPtr digest = make_shared<Digest> ();
141 istringstream is (hash);
142 is >> *digest;
143
144 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700145}
146
147void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700148SyncLogic::respondSyncInterest (const string &name)
149{
150 try
151 {
152 _LOG_TRACE ("<< I " << name);
153
154 DigestConstPtr digest;
155 string type;
156 tie (digest, type) = convertNameToDigestAndType (name);
157
158 if (type == "normal") // kind of ineffective...
159 {
160 processSyncInterest (name, digest);
161 }
162 else if (type == "recovery")
163 {
164 processSyncRecoveryInterest (name, digest);
165 }
166 }
167 catch (Error::DigestCalculationError &e)
168 {
169 _LOG_TRACE ("Something fishy happened...");
170 // log error. ignoring it for now, later we should log it
171 return ;
172 }
173}
174
175void
176SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer)
177{
178 try
179 {
180 _LOG_TRACE ("<< D " << name);
181
182 DigestConstPtr digest;
183 string type;
184 tie (digest, type) = convertNameToDigestAndType (name);
185
186 processSyncData (name, digest, dataBuffer);
187 }
188 catch (Error::DigestCalculationError &e)
189 {
190 _LOG_TRACE ("Something fishy happened...");
191 // log error. ignoring it for now, later we should log it
192 return;
193 }
194
195}
196
197
198void
199SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700200{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700201 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700202
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700203 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700204 if (digest->isZero () && !m_state->getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700205 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700206 sendSyncData (name, digest, m_state);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700207 return;
208 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700209
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700210 if (*m_state->getDigest() == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700211 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700212 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
213 m_syncInterestTable.insert (digest, name);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700214 return;
215 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700216
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700217 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
218
219 if (stateInDiffLog != m_log.end ())
220 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700221 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700222
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700223 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700224 return;
225 }
226
227 if (!timedProcessing)
228 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700229 bool exists = m_syncInterestTable.insert (digest, name);
230 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700231 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700232 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
233 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700234 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700235
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700236 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
237 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700238
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700239 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
240 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
241 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700242 }
243 else
244 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700245 BOOST_ASSERT (false); // not implemented yet
246
247 // _LOG_TRACE (">> D " << *digest << " (timed processing)");
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700248 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800249}
250
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800251void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700252SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800253{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700254 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700255
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700256 try
257 {
258 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700259
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700260 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700261
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700262 DiffState diff;
263 istringstream ss (dataBuffer);
264 ss >> diff;
265 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700266 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700267 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
268 BOOST_ASSERT (diffLeaf != 0);
269
270 NameInfoConstPtr info = diffLeaf->getInfo();
271 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700272 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700273 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700274
275 bool inserted = false;
276 bool updated = false;
277 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700278 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800279
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700280 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700281 {
282 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700283 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700284 }
285 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700286 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700287 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700288 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700289 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700290 diffLog->remove (info);
291 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700292 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700293 }
294 else
295 {
296 BOOST_ASSERT (false); // just in case
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700297 }
298 }
299
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700300 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700301 }
302 catch (Error::SyncXmlDecodingFailure &e)
303 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700304 _LOG_TRACE ("Something really fishy happened during state decoding " <<
305 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700306 diffLog.reset ();
307 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700308 }
309
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700310 if (diffLog != 0)
311 {
312 BOOST_ASSERT (diffLog->getLeaves ().size () > 0);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700313
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700314 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 -0700315
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700316 // if state has changed, then it is safe to express a new interest
317 m_scheduler.cancel (REEXPRESSING_INTEREST);
318 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
319 bind (&SyncLogic::sendSyncInterest, this),
320 REEXPRESSING_INTEREST);
321 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800322}
323
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800324void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700325SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800326{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700327 recursive_mutex::scoped_lock lock (m_stateMutex);
328
329 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
330
331 if (stateInDiffLog == m_log.end ()) return;
332
333 sendSyncData (name, digest, m_state);
334}
335
336void
337SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
338{
339 uint32_t counter = 0;
340 while (m_syncInterestTable.size () > 0)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700341 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700342 Interest interest = m_syncInterestTable.pop ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700343
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700344 _LOG_TRACE (">> D " << interest.m_name);
345 sendSyncData (interest.m_name, interest.m_digest, diffLog);
346 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700347 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700348 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800349}
350
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800351void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700352SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700353{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700354 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700355 if (m_log.size () > 0)
356 {
357 m_log.get<sequenced> ().front ()->setNext (diffLog);
358 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700359 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 -0700360 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700361 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700362}
363
364void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700365SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
366{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700367 DiffStatePtr diff;
368 {
369 //cout << "Add local names" <<endl;
370 recursive_mutex::scoped_lock lock (m_stateMutex);
371 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700372
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700373 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Alexander Afanasyev42612262012-05-05 10:58:37 -0700374
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700375 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700376 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700377
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700378 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700379
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700380 diff = make_shared<DiffState>();
381 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700382 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700383 }
384
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700385 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
386 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700387}
388
389void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700390SyncLogic::remove(const string &prefix)
391{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700392 DiffStatePtr diff;
393 {
394 recursive_mutex::scoped_lock lock (m_stateMutex);
395 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700396 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700397
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700398 diff = make_shared<DiffState>();
399 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700400
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700401 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700402 }
403
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700404 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700405}
406
407void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800408SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800409{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800410 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800411
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700412 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700413 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700414
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700415 os << m_syncPrefix << "/" << *m_state->getDigest();
416 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700417 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700418 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700419
420 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700421 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700422 bind (&SyncLogic::sendSyncInterest, this),
423 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700424
425 m_ccnxHandle->sendInterest (os.str (),
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700426 bind (&SyncLogic::respondSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800427}
428
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700429void
430SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
431{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700432 // sending
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700433 m_ccnxHandle->publishData (name,
434 lexical_cast<string> (*state),
435 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700436
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700437 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700438 bool satisfiedOwnInterest = false;
439 {
440 recursive_mutex::scoped_lock lock (m_stateMutex);
441 satisfiedOwnInterest = (m_outstandingInterestName == name);
442 }
443
444 if (satisfiedOwnInterest)
445 {
446 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
447
448 m_scheduler.cancel (REEXPRESSING_INTEREST);
449 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
450 bind (&SyncLogic::sendSyncInterest, this),
451 REEXPRESSING_INTEREST);
452 }
453}
454
455
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800456}