blob: b72a061fedf65aaf68a4f56c6501311eba9e62cc [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 Afanasyevb6cf56e2012-05-29 19:33:49 -070023#ifdef NS3_MODULE
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070024#include <ns3/ccnx-pit.h>
25#include <ns3/ccnx.h>
Alexander Afanasyevb6cf56e2012-05-29 19:33:49 -070026#endif
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070027
Chaoyi Bian11f294f2012-03-08 14:28:06 -080028#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080029#include "sync-diff-leaf.h"
30#include "sync-full-leaf.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070031#include "sync-log.h"
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070032#include "sync-state.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070033
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080034#include <boost/make_shared.hpp>
35#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070036#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080037#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080038
39using namespace std;
40using namespace boost;
41
Chaoyi Bianf007f922012-04-09 20:05:37 -070042INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070043
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070044#ifdef NS3_MODULE
45#define GET_RANDOM(var) var.GetValue ()
46#else
47#define GET_RANDOM(var) var ()
48#endif
49
50#define TIME_SECONDS_WITH_JITTER(sec) \
51 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
52
Alexander Afanasyev42612262012-05-05 10:58:37 -070053#define TIME_MILLISECONDS_WITH_JITTER(ms) \
54 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070055
56
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057namespace Sync
58{
59
Alexander Afanasyev750d1872012-03-12 15:33:56 -070060SyncLogic::SyncLogic (const std::string &syncPrefix,
61 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070062 LogicRemoveCallback onRemove)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070063 : m_state (new FullState)
64 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
65 , m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070066 , m_onUpdate (onUpdate)
67 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070068 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyeva76010b2012-05-24 21:31:49 -070069 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070070#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070071 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070072 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
73 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070074#else
Alexander Afanasyev55608612012-04-30 13:24:50 -070075 , m_rangeUniformRandom (200,1000)
Alexander Afanasyeva76010b2012-05-24 21:31:49 -070076 , m_reexpressionJitter (10,500)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070077#endif
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070078{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070079#ifndef NS3_MODULE
80 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070081
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070082 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070083 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070084
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070085 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070086 bind (&SyncLogic::sendSyncInterest, this),
87 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070088#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080089}
90
Alexander Afanasyevc1030192012-03-08 22:21:28 -080091SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080092{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070093 m_ccnxHandle.reset ();
94}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070095
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070096#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -070097void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070098SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -070099{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700100 m_ccnxHandle->SetNode (GetNode ());
101 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700102
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700103 m_ccnxHandle->setInterestFilter (m_syncPrefix,
104 bind (&SyncLogic::respondSyncInterest, this, _1));
105
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700106 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700107 bind (&SyncLogic::sendSyncInterest, this),
108 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700109}
110
111void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700112SyncLogic::StopApplication ()
113{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700114 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700115 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700116 m_scheduler.cancel (REEXPRESSING_INTEREST);
117 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700118}
119#endif
120
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700121/**
122 * Two types of intersts
123 *
124 * Normal name: .../<hash>
125 * Recovery name: .../recovery/<hash>
126 */
127boost::tuple<DigestConstPtr, std::string>
128SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700129{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700130 BOOST_ASSERT (name.find (m_syncPrefix) == 0);
131
132 string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700133 if (hash[0] == '/')
134 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700135 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700136
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700137 size_t pos = hash.find ('/');
138 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700139 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700140 interestType = hash.substr (0, pos);
141 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700142 }
143
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700144 _LOG_TRACE (hash << ", " << interestType);
145
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700146 DigestPtr digest = make_shared<Digest> ();
147 istringstream is (hash);
148 is >> *digest;
149
150 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700151}
152
153void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700154SyncLogic::respondSyncInterest (const string &name)
155{
156 try
157 {
158 _LOG_TRACE ("<< I " << name);
159
160 DigestConstPtr digest;
161 string type;
162 tie (digest, type) = convertNameToDigestAndType (name);
163
164 if (type == "normal") // kind of ineffective...
165 {
166 processSyncInterest (name, digest);
167 }
168 else if (type == "recovery")
169 {
170 processSyncRecoveryInterest (name, digest);
171 }
172 }
173 catch (Error::DigestCalculationError &e)
174 {
175 _LOG_TRACE ("Something fishy happened...");
176 // log error. ignoring it for now, later we should log it
177 return ;
178 }
179}
180
181void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700182SyncLogic::respondSyncData (const std::string &name, const char *wireData, size_t len)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700183{
184 try
185 {
186 _LOG_TRACE ("<< D " << name);
187
188 DigestConstPtr digest;
189 string type;
190 tie (digest, type) = convertNameToDigestAndType (name);
191
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700192 if (type == "normal")
193 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700194 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700195 }
196 else
197 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700198 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700199 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700200 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700201 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700202 }
203 catch (Error::DigestCalculationError &e)
204 {
205 _LOG_TRACE ("Something fishy happened...");
206 // log error. ignoring it for now, later we should log it
207 return;
208 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700209}
210
211
212void
213SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700214{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700215 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700216
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700217 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700218 if (digest->isZero () && !m_state->getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700219 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700220 sendSyncData (name, digest, m_state);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700221 return;
222 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700223
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700224 if (*m_state->getDigest() == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700225 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700226 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700227 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700228 return;
229 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700230
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700231 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
232
233 if (stateInDiffLog != m_log.end ())
234 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700235 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700236
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700237 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700238 return;
239 }
240
241 if (!timedProcessing)
242 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700243 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700244 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700245 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700246 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
247 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700248 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700249
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700250 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
251 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700252
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700253 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
254 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700255 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700256 }
257 else
258 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700259 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700260
261 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700262 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700263 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800264}
265
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800266void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700267SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800268{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700269 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700270 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700271
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700272 try
273 {
274 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700275
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700276 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700277
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700278 ownInterestSatisfied = (name == m_outstandingInterestName);
279
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700280 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700281 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700282 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700283 {
284 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700285 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700286 }
287 msg >> diff;
288
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700289 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700290 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700291 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700292 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
293 BOOST_ASSERT (diffLeaf != 0);
294
295 NameInfoConstPtr info = diffLeaf->getInfo();
296 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700297 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700298 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700299
300 bool inserted = false;
301 bool updated = false;
302 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700303 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800304
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700305 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700306 {
307 diffLog->update (info, seq);
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700308 //m_onUpdate (info->toString (), seq, oldSeq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700309 if (!oldSeq.isValid())
310 {
311 oldSeq = SeqNo(seq.getSession(), 0);
312 }
313 else
314 {
315 ++oldSeq;
316 }
317 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700318 v.push_back(mdi);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700319 }
320 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700321 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700322 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700323 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700324 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700325 diffLog->remove (info);
326 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700327 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700328 }
329 else
330 {
331 BOOST_ASSERT (false); // just in case
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700332 }
333 }
334
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700335 if (!v.empty())
336 {
337 m_onUpdate(v);
338 }
339
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700340 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700341 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700342 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700343 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700344 _LOG_TRACE ("Something really fishy happened during state decoding " <<
345 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700346 diffLog.reset ();
347 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700348 }
349
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700350 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
351 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700352 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700353 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700354
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700355 // this is kind of wrong
356 // 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 -0700357
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700358 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700359 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700360 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700361 bind (&SyncLogic::sendSyncInterest, this),
362 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700363 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800364}
365
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800366void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700367SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800368{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700369 recursive_mutex::scoped_lock lock (m_stateMutex);
370
371 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
372
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700373 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700374 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700375 _LOG_TRACE ("Could not find " << *digest << " in digest log");
376 return;
377 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700378
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700379 sendSyncData (name, digest, m_state);
380}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700381
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700382void
383SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
384{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700385 DiffStatePtr fullStateLog = make_shared<DiffState> ();
386 {
387 recursive_mutex::scoped_lock lock (m_stateMutex);
388 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
389 {
390 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
391 /// @todo Impose limit on how many state info should be send out
392 }
393 }
394
395 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800396 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700397 uint32_t counter = 0;
398 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700399 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700400 Interest interest = m_syncInterestTable.pop ();
401
402 if (!interest.m_unknown)
403 {
404 _LOG_TRACE (">> D " << interest.m_name);
405 sendSyncData (interest.m_name, interest.m_digest, diffLog);
406 }
407 else
408 {
409 _LOG_TRACE (">> D (unknown)" << interest.m_name);
410 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
411 }
412 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700413 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700414 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700415 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700416 catch (Error::InterestTableIsEmpty &e)
417 {
418 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800419 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800420}
421
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800422void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700423SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700424{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700425 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700426 if (m_log.size () > 0)
427 {
428 m_log.get<sequenced> ().front ()->setNext (diffLog);
429 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700430 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 -0700431 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700432 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700433}
434
435void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700436SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
437{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700438 DiffStatePtr diff;
439 {
440 //cout << "Add local names" <<endl;
441 recursive_mutex::scoped_lock lock (m_stateMutex);
442 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700443
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700444 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700445
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700446 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700447 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700448
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700449 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700450
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700451 diff = make_shared<DiffState>();
452 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700453 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700454 }
455
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700456 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
457 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700458}
459
460void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700461SyncLogic::remove(const string &prefix)
462{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700463 DiffStatePtr diff;
464 {
465 recursive_mutex::scoped_lock lock (m_stateMutex);
466 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700467 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700468
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700469 diff = make_shared<DiffState>();
470 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700471
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700472 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700473 }
474
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700475 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700476}
477
478void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800479SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800480{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800481 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800482
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700483 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700484 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700485
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700486 os << m_syncPrefix << "/" << *m_state->getDigest();
487 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700488 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700489 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700490
491 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700492 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700493 bind (&SyncLogic::sendSyncInterest, this),
494 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700495
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700496 m_ccnxHandle->sendInterest (os.str (),
497 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800498}
499
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700500void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700501SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
502{
503 ostringstream os;
504 os << m_syncPrefix << "/recovery/" << *digest;
505 _LOG_TRACE (">> I " << os.str ());
506
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700507 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
508 m_recoveryRetransmissionInterval <<= 1;
509
510 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
511 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
512 {
513 m_scheduler.schedule (nextRetransmission,
514 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
515 REEXPRESSING_RECOVERY_INTEREST);
516 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700517
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700518 m_ccnxHandle->sendInterest (os.str (),
519 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700520}
521
522
523void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700524SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
525{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700526 _LOG_TRACE (">> D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700527 // sending
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700528 SyncStateMsg ssm;
529 ssm << (*state);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700530 int size = ssm.ByteSize();
531 char *wireData = new char[size];
532 ssm.SerializeToArray(wireData, size);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700533 m_ccnxHandle->publishRawData (name,
534 wireData,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700535 size,
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700536 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700537 delete wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700538
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700539 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700540 bool satisfiedOwnInterest = false;
541 {
542 recursive_mutex::scoped_lock lock (m_stateMutex);
543 satisfiedOwnInterest = (m_outstandingInterestName == name);
544 }
545
546 if (satisfiedOwnInterest)
547 {
548 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
549
550 m_scheduler.cancel (REEXPRESSING_INTEREST);
551 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
552 bind (&SyncLogic::sendSyncInterest, this),
553 REEXPRESSING_INTEREST);
554 }
555}
556
Zhenkai Zhue5660932012-06-04 15:25:20 -0700557string
558SyncLogic::getRootDigest()
559{
560 ostringstream os;
561 recursive_mutex::scoped_lock lock (m_stateMutex);
562 os << *m_state->getDigest();
563 return os.str();
564}
565
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700566
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800567}