blob: 98160b50cff1b2c4b138fe428a05c4c093cabbff [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 Afanasyevce001692013-07-14 11:34:41 -070031#include "sync-logging.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 Afanasyevce001692013-07-14 11:34:41 -070068 , m_perBranch (false)
Zhenkai Zhu9587cd62012-10-17 11:58:37 -070069 , m_ccnxHandle(new CcnxWrapper ())
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 Afanasyevce001692013-07-14 11:34:41 -070078 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070079{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070080#ifndef NS3_MODULE
81 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070082
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070083 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070084 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070085
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070086 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070087 bind (&SyncLogic::sendSyncInterest, this),
88 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070089#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080090}
91
Zhenkai Zhub9f19592012-10-16 14:27:38 -070092SyncLogic::SyncLogic (const std::string &syncPrefix,
93 LogicPerBranchCallback onUpdateBranch)
94 : m_state (new FullState)
95 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
96 , m_syncPrefix (syncPrefix)
97 , m_onUpdateBranch (onUpdateBranch)
98 , m_perBranch(true)
Zhenkai Zhu9587cd62012-10-17 11:58:37 -070099 , m_ccnxHandle(new CcnxWrapper())
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700100#ifndef NS3_MODULE
101 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
102 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
103 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
104#else
105 , m_rangeUniformRandom (200,1000)
106 , m_reexpressionJitter (10,500)
107#endif
Alexander Afanasyevce001692013-07-14 11:34:41 -0700108 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700109{
110#ifndef NS3_MODULE
111 // In NS3 module these functions are moved to StartApplication method
112
113 m_ccnxHandle->setInterestFilter (m_syncPrefix,
114 bind (&SyncLogic::respondSyncInterest, this, _1));
115
116 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
117 bind (&SyncLogic::sendSyncInterest, this),
118 REEXPRESSING_INTEREST);
119#endif
120}
121
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800122SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800123{
Alexander Afanasyev60126e02012-10-05 16:54:42 -0700124 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700125}
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700126
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700127#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700128void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700129SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700130{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700131 m_ccnxHandle->SetNode (GetNode ());
132 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700133
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700134 m_ccnxHandle->setInterestFilter (m_syncPrefix,
135 bind (&SyncLogic::respondSyncInterest, this, _1));
136
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700137 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700138 bind (&SyncLogic::sendSyncInterest, this),
139 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700140}
141
142void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700143SyncLogic::StopApplication ()
144{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700145 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700146 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700147 m_scheduler.cancel (REEXPRESSING_INTEREST);
148 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700149}
150#endif
151
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800152void
153SyncLogic::stop()
154{
155 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
156 m_scheduler.cancel (REEXPRESSING_INTEREST);
157 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
158}
159
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700160/**
161 * Two types of intersts
162 *
163 * Normal name: .../<hash>
164 * Recovery name: .../recovery/<hash>
165 */
166boost::tuple<DigestConstPtr, std::string>
167SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700168{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700169 BOOST_ASSERT (name.find (m_syncPrefix) == 0);
170
171 string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700172 if (hash[0] == '/')
173 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700174 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700175
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700176 size_t pos = hash.find ('/');
177 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700178 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700179 interestType = hash.substr (0, pos);
180 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700181 }
182
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700183 _LOG_TRACE (hash << ", " << interestType);
184
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700185 DigestPtr digest = make_shared<Digest> ();
186 istringstream is (hash);
187 is >> *digest;
188
189 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700190}
191
192void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700193SyncLogic::respondSyncInterest (const string &name)
194{
195 try
196 {
197 _LOG_TRACE ("<< I " << name);
198
199 DigestConstPtr digest;
200 string type;
201 tie (digest, type) = convertNameToDigestAndType (name);
202
203 if (type == "normal") // kind of ineffective...
204 {
205 processSyncInterest (name, digest);
206 }
207 else if (type == "recovery")
208 {
209 processSyncRecoveryInterest (name, digest);
210 }
211 }
212 catch (Error::DigestCalculationError &e)
213 {
214 _LOG_TRACE ("Something fishy happened...");
215 // log error. ignoring it for now, later we should log it
216 return ;
217 }
218}
219
220void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700221SyncLogic::respondSyncData (const std::string &name, const char *wireData, size_t len)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700222{
223 try
224 {
225 _LOG_TRACE ("<< D " << name);
226
227 DigestConstPtr digest;
228 string type;
229 tie (digest, type) = convertNameToDigestAndType (name);
230
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700231 if (type == "normal")
232 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700233 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700234 }
235 else
236 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700237 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700238 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700239 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700240 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700241 }
242 catch (Error::DigestCalculationError &e)
243 {
244 _LOG_TRACE ("Something fishy happened...");
245 // log error. ignoring it for now, later we should log it
246 return;
247 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700248}
249
250
251void
252SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700253{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700254 DigestConstPtr rootDigest;
255 {
256 recursive_mutex::scoped_lock lock (m_stateMutex);
257 rootDigest = m_state->getDigest();
258 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700259
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700260 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700261 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700262 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700263
264 SyncStateMsg ssm;
265 {
266 recursive_mutex::scoped_lock lock (m_stateMutex);
267 ssm << (*m_state);
268 }
269 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700270 return;
271 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700272
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700273 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700274 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700275 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700276 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700277 return;
278 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700279
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700280 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
281
282 if (stateInDiffLog != m_log.end ())
283 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700284 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700285
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700286 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700287 return;
288 }
289
290 if (!timedProcessing)
291 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700292 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700293 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700294 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700295 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
296 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700297 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700298
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700299 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
300 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700301
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700302 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
303 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700304 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700305 }
306 else
307 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700308 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700309
310 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700311 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700312 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800313}
314
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800315void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700316SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800317{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700318 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700319 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700320
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700321 try
322 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700323
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700324 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700325
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700326 ownInterestSatisfied = (name == m_outstandingInterestName);
327
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700328 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700329 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700330 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700331 {
332 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700333 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700334 }
335 msg >> diff;
336
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700337 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700338 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700339 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700340 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
341 BOOST_ASSERT (diffLeaf != 0);
342
343 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700344 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700345 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700346 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700347
348 bool inserted = false;
349 bool updated = false;
350 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700351 {
352 recursive_mutex::scoped_lock lock (m_stateMutex);
353 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
354 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800355
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700356 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700357 {
358 diffLog->update (info, seq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700359 if (!oldSeq.isValid())
360 {
361 oldSeq = SeqNo(seq.getSession(), 0);
362 }
363 else
364 {
365 ++oldSeq;
366 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700367 // there is no need for application to process update on forwarder node
368 if (info->toString() != forwarderPrefix)
369 {
370 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700371 if (m_perBranch)
372 {
373 ostringstream interestName;
374 interestName << mdi.prefix << "/" << mdi.high.getSession() << "/" << mdi.high.getSeq();
375 m_onUpdateBranch(interestName.str());
376 }
377 else
378 {
379 v.push_back(mdi);
380 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700381 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700382 }
383 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700384 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700385 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700386 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700387 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700388 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700389 diffLog->remove (info);
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700390 if (!m_perBranch)
391 {
392 m_onRemove (info->toString ());
393 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700394 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700395 }
396 else
397 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700398 }
399 }
400
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700401 if (!v.empty())
402 {
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700403 if (!m_perBranch)
404 {
405 m_onUpdate(v);
406 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700407 }
408
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700409 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700410 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700411 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700412 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700413 _LOG_TRACE ("Something really fishy happened during state decoding " <<
414 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700415 diffLog.reset ();
416 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700417 }
418
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700419 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
420 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700421 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700422 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700423
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700424 // this is kind of wrong
425 // 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 -0700426
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700427 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700428 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700429 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700430 bind (&SyncLogic::sendSyncInterest, this),
431 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700432 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800433}
434
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800435void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700436SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800437{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700438
439 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
440
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700441 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700442 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700443 _LOG_TRACE ("Could not find " << *digest << " in digest log");
444 return;
445 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700446
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700447 SyncStateMsg ssm;
448 {
449 recursive_mutex::scoped_lock lock (m_stateMutex);
450 ssm << (*m_state);
451 }
452 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700453}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700454
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700455void
456SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
457{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700458 DiffStatePtr fullStateLog = make_shared<DiffState> ();
459 {
460 recursive_mutex::scoped_lock lock (m_stateMutex);
461 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
462 {
463 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
464 /// @todo Impose limit on how many state info should be send out
465 }
466 }
467
468 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800469 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700470 uint32_t counter = 0;
471 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700472 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700473 Interest interest = m_syncInterestTable.pop ();
474
475 if (!interest.m_unknown)
476 {
477 _LOG_TRACE (">> D " << interest.m_name);
478 sendSyncData (interest.m_name, interest.m_digest, diffLog);
479 }
480 else
481 {
482 _LOG_TRACE (">> D (unknown)" << interest.m_name);
483 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
484 }
485 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700486 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700487 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700488 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700489 catch (Error::InterestTableIsEmpty &e)
490 {
491 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800492 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800493}
494
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800495void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700496SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700497{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700498 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700499 if (m_log.size () > 0)
500 {
501 m_log.get<sequenced> ().front ()->setNext (diffLog);
502 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700503 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 -0700504 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700505 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700506}
507
508void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700509SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
510{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700511 DiffStatePtr diff;
512 {
513 //cout << "Add local names" <<endl;
514 recursive_mutex::scoped_lock lock (m_stateMutex);
515 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700516
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700517 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700518
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700519 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700520 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700521
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700522 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700523
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700524 diff = make_shared<DiffState>();
525 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700526 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700527 }
528
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700529 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
530 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700531}
532
533void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700534SyncLogic::remove(const string &prefix)
535{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700536 DiffStatePtr diff;
537 {
538 recursive_mutex::scoped_lock lock (m_stateMutex);
539 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700540 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700541
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700542 // increment the sequence number for the forwarder node
543 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700544
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700545 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
546 SeqNo seqNo (0);
547 if (item != m_state->getLeaves ().end ())
548 {
549 seqNo = (*item)->getSeq ();
550 ++seqNo;
551 }
552 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700553
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700554 diff = make_shared<DiffState>();
555 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700556 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700557
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700558 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700559 }
560
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700561 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700562}
563
564void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800565SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800566{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800567 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800568
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700569 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700570 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700571
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700572 os << m_syncPrefix << "/" << *m_state->getDigest();
573 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700574 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700575 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700576
577 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700578 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700579 bind (&SyncLogic::sendSyncInterest, this),
580 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700581
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700582 m_ccnxHandle->sendInterest (os.str (),
583 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800584}
585
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700586void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700587SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
588{
589 ostringstream os;
590 os << m_syncPrefix << "/recovery/" << *digest;
591 _LOG_TRACE (">> I " << os.str ());
592
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700593 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
594 m_recoveryRetransmissionInterval <<= 1;
595
596 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
597 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
598 {
599 m_scheduler.schedule (nextRetransmission,
600 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
601 REEXPRESSING_RECOVERY_INTEREST);
602 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700603
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700604 m_ccnxHandle->sendInterest (os.str (),
605 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700606}
607
608
609void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700610SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
611{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700612 SyncStateMsg msg;
613 msg << (*state);
614 sendSyncData(name, digest, msg);
615}
616
617// pass in state msg instead of state, so that there is no need to lock the state until
618// this function returns
619void
620SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm)
621{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700622 _LOG_TRACE (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700623 int size = ssm.ByteSize();
624 char *wireData = new char[size];
625 ssm.SerializeToArray(wireData, size);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700626 m_ccnxHandle->publishRawData (name,
627 wireData,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700628 size,
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700629 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700630 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700631
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700632 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700633 bool satisfiedOwnInterest = false;
634 {
635 recursive_mutex::scoped_lock lock (m_stateMutex);
636 satisfiedOwnInterest = (m_outstandingInterestName == name);
637 }
638
639 if (satisfiedOwnInterest)
640 {
641 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
642
643 m_scheduler.cancel (REEXPRESSING_INTEREST);
644 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
645 bind (&SyncLogic::sendSyncInterest, this),
646 REEXPRESSING_INTEREST);
647 }
648}
649
Zhenkai Zhue5660932012-06-04 15:25:20 -0700650string
651SyncLogic::getRootDigest()
652{
653 ostringstream os;
654 recursive_mutex::scoped_lock lock (m_stateMutex);
655 os << *m_state->getDigest();
656 return os.str();
657}
658
Alexander Afanasyev80237382012-10-04 10:20:47 -0700659size_t
660SyncLogic::getNumberOfBranches () const
661{
662 recursive_mutex::scoped_lock lock (m_stateMutex);
663 return m_state->getLeaves ().size ();
664}
665
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700666void
667SyncLogic::printState () const
668{
669 recursive_mutex::scoped_lock lock (m_stateMutex);
670
671 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
672 {
673 std::cout << *leaf << std::endl;
674 }
675}
676
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700677std::map<std::string, bool>
678SyncLogic::getBranchPrefixes() const
679{
680 recursive_mutex::scoped_lock lock (m_stateMutex);
681
682 std::map<std::string, bool> m;
683
684 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
685 {
686 std::string prefix = leaf->getInfo()->toString();
687 // do not return forwarder prefix
688 if (prefix != forwarderPrefix)
689 {
690 m.insert(pair<std::string, bool>(prefix, false));
691 }
692 }
693
694 return m;
695}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700696
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800697}