blob: 596ad9423f09225df606f6c1f4f6856c8eb957b9 [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 Afanasyev3aaea672012-10-05 15:25:11 -070068 , m_ccnxHandle(CcnxWrapper::Create ())
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 Afanasyev60126e02012-10-05 16:54:42 -070093 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070094 m_ccnxHandle.reset ();
Alexander Afanasyev60126e02012-10-05 16:54:42 -070095 // m_scheduler.cancel (REEXPRESSING_INTEREST);
96 // m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
97 // sleep (m_syncInterestReexpress+1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070098}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070099
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700100#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700101void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700102SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700103{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700104 m_ccnxHandle->SetNode (GetNode ());
105 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700106
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700107 m_ccnxHandle->setInterestFilter (m_syncPrefix,
108 bind (&SyncLogic::respondSyncInterest, this, _1));
109
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700110 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700111 bind (&SyncLogic::sendSyncInterest, this),
112 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700113}
114
115void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700116SyncLogic::StopApplication ()
117{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700118 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700119 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700120 m_scheduler.cancel (REEXPRESSING_INTEREST);
121 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700122}
123#endif
124
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700125/**
126 * Two types of intersts
127 *
128 * Normal name: .../<hash>
129 * Recovery name: .../recovery/<hash>
130 */
131boost::tuple<DigestConstPtr, std::string>
132SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700133{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700134 BOOST_ASSERT (name.find (m_syncPrefix) == 0);
135
136 string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700137 if (hash[0] == '/')
138 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700139 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700140
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700141 size_t pos = hash.find ('/');
142 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700143 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700144 interestType = hash.substr (0, pos);
145 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700146 }
147
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700148 _LOG_TRACE (hash << ", " << interestType);
149
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700150 DigestPtr digest = make_shared<Digest> ();
151 istringstream is (hash);
152 is >> *digest;
153
154 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700155}
156
157void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700158SyncLogic::respondSyncInterest (const string &name)
159{
160 try
161 {
162 _LOG_TRACE ("<< I " << name);
163
164 DigestConstPtr digest;
165 string type;
166 tie (digest, type) = convertNameToDigestAndType (name);
167
168 if (type == "normal") // kind of ineffective...
169 {
170 processSyncInterest (name, digest);
171 }
172 else if (type == "recovery")
173 {
174 processSyncRecoveryInterest (name, digest);
175 }
176 }
177 catch (Error::DigestCalculationError &e)
178 {
179 _LOG_TRACE ("Something fishy happened...");
180 // log error. ignoring it for now, later we should log it
181 return ;
182 }
183}
184
185void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700186SyncLogic::respondSyncData (const std::string &name, const char *wireData, size_t len)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700187{
188 try
189 {
190 _LOG_TRACE ("<< D " << name);
191
192 DigestConstPtr digest;
193 string type;
194 tie (digest, type) = convertNameToDigestAndType (name);
195
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700196 if (type == "normal")
197 {
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700198 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700199 }
200 else
201 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700202 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700203 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700204 processSyncData (name, digest, wireData, len);
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700205 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700206 }
207 catch (Error::DigestCalculationError &e)
208 {
209 _LOG_TRACE ("Something fishy happened...");
210 // log error. ignoring it for now, later we should log it
211 return;
212 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700213}
214
215
216void
217SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700218{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700219 DigestConstPtr rootDigest;
220 {
221 recursive_mutex::scoped_lock lock (m_stateMutex);
222 rootDigest = m_state->getDigest();
223 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700224
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700225 // Special case when state is not empty and we have received request with zero-root digest
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700226 if (digest->isZero () && !rootDigest->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700227 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700228
229 SyncStateMsg ssm;
230 {
231 recursive_mutex::scoped_lock lock (m_stateMutex);
232 ssm << (*m_state);
233 }
234 sendSyncData (name, digest, ssm);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700235 return;
236 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700237
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700238 if (*rootDigest == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700239 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700240 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700241 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700242 return;
243 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700244
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700245 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
246
247 if (stateInDiffLog != m_log.end ())
248 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700249 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700250
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700251 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700252 return;
253 }
254
255 if (!timedProcessing)
256 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700257 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700258 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700259 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700260 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
261 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700262 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700263
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700264 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
265 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700266
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700267 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
268 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700269 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700270 }
271 else
272 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700273 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700274
275 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700276 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700277 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800278}
279
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800280void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700281SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800282{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700283 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700284 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700285
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700286 try
287 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700288
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700289 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700290
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700291 ownInterestSatisfied = (name == m_outstandingInterestName);
292
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700293 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700294 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700295 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700296 {
297 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700298 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700299 }
300 msg >> diff;
301
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700302 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700303 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700304 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700305 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
306 BOOST_ASSERT (diffLeaf != 0);
307
308 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700309 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700310 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700311 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700312
313 bool inserted = false;
314 bool updated = false;
315 SeqNo oldSeq;
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700316 {
317 recursive_mutex::scoped_lock lock (m_stateMutex);
318 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
319 }
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800320
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700321 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700322 {
323 diffLog->update (info, seq);
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700324 //m_onUpdate (info->toString (), seq, oldSeq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700325 if (!oldSeq.isValid())
326 {
327 oldSeq = SeqNo(seq.getSession(), 0);
328 }
329 else
330 {
331 ++oldSeq;
332 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700333 // there is no need for application to process update on forwarder node
334 if (info->toString() != forwarderPrefix)
335 {
336 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
337 v.push_back(mdi);
338 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700339 }
340 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700341 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700342 {
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700343 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700344 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700345 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700346 diffLog->remove (info);
347 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700348 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700349 }
350 else
351 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700352 }
353 }
354
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700355 if (!v.empty())
356 {
357 m_onUpdate(v);
358 }
359
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700360 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700361 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700362 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700363 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700364 _LOG_TRACE ("Something really fishy happened during state decoding " <<
365 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700366 diffLog.reset ();
367 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700368 }
369
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700370 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
371 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700372 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700373 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700374
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700375 // this is kind of wrong
376 // 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 -0700377
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700378 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700379 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700380 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700381 bind (&SyncLogic::sendSyncInterest, this),
382 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700383 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800384}
385
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800386void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700387SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800388{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700389
390 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
391
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700392 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700393 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700394 _LOG_TRACE ("Could not find " << *digest << " in digest log");
395 return;
396 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700397
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700398 SyncStateMsg ssm;
399 {
400 recursive_mutex::scoped_lock lock (m_stateMutex);
401 ssm << (*m_state);
402 }
403 sendSyncData (name, digest, ssm);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700404}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700405
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700406void
407SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
408{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700409 DiffStatePtr fullStateLog = make_shared<DiffState> ();
410 {
411 recursive_mutex::scoped_lock lock (m_stateMutex);
412 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
413 {
414 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
415 /// @todo Impose limit on how many state info should be send out
416 }
417 }
418
419 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800420 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700421 uint32_t counter = 0;
422 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700423 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700424 Interest interest = m_syncInterestTable.pop ();
425
426 if (!interest.m_unknown)
427 {
428 _LOG_TRACE (">> D " << interest.m_name);
429 sendSyncData (interest.m_name, interest.m_digest, diffLog);
430 }
431 else
432 {
433 _LOG_TRACE (">> D (unknown)" << interest.m_name);
434 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
435 }
436 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700437 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700438 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700439 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700440 catch (Error::InterestTableIsEmpty &e)
441 {
442 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800443 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800444}
445
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800446void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700447SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700448{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700449 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700450 if (m_log.size () > 0)
451 {
452 m_log.get<sequenced> ().front ()->setNext (diffLog);
453 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700454 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 -0700455 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700456 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700457}
458
459void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700460SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
461{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700462 DiffStatePtr diff;
463 {
464 //cout << "Add local names" <<endl;
465 recursive_mutex::scoped_lock lock (m_stateMutex);
466 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700467
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700468 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700469
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700470 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700471 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700472
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700473 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700474
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700475 diff = make_shared<DiffState>();
476 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700477 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700478 }
479
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700480 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
481 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700482}
483
484void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700485SyncLogic::remove(const string &prefix)
486{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700487 DiffStatePtr diff;
488 {
489 recursive_mutex::scoped_lock lock (m_stateMutex);
490 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700491 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700492
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700493 // increment the sequence number for the forwarder node
494 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700495
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700496 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
497 SeqNo seqNo (0);
498 if (item != m_state->getLeaves ().end ())
499 {
500 seqNo = (*item)->getSeq ();
501 ++seqNo;
502 }
503 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700504
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700505 diff = make_shared<DiffState>();
506 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700507 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700508
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700509 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700510 }
511
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700512 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700513}
514
515void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800516SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800517{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800518 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800519
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700520 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700521 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700522
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700523 os << m_syncPrefix << "/" << *m_state->getDigest();
524 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700525 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700526 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700527
528 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700529 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700530 bind (&SyncLogic::sendSyncInterest, this),
531 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700532
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700533 m_ccnxHandle->sendInterest (os.str (),
534 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800535}
536
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700537void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700538SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
539{
540 ostringstream os;
541 os << m_syncPrefix << "/recovery/" << *digest;
542 _LOG_TRACE (">> I " << os.str ());
543
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700544 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
545 m_recoveryRetransmissionInterval <<= 1;
546
547 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
548 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
549 {
550 m_scheduler.schedule (nextRetransmission,
551 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
552 REEXPRESSING_RECOVERY_INTEREST);
553 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700554
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700555 m_ccnxHandle->sendInterest (os.str (),
556 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700557}
558
559
560void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700561SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
562{
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700563 SyncStateMsg msg;
564 msg << (*state);
565 sendSyncData(name, digest, msg);
566}
567
568// pass in state msg instead of state, so that there is no need to lock the state until
569// this function returns
570void
571SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm)
572{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700573 _LOG_TRACE (">> D " << name);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700574 int size = ssm.ByteSize();
575 char *wireData = new char[size];
576 ssm.SerializeToArray(wireData, size);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700577 m_ccnxHandle->publishRawData (name,
578 wireData,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700579 size,
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700580 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700581 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700582
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700583 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700584 bool satisfiedOwnInterest = false;
585 {
586 recursive_mutex::scoped_lock lock (m_stateMutex);
587 satisfiedOwnInterest = (m_outstandingInterestName == name);
588 }
589
590 if (satisfiedOwnInterest)
591 {
592 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
593
594 m_scheduler.cancel (REEXPRESSING_INTEREST);
595 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
596 bind (&SyncLogic::sendSyncInterest, this),
597 REEXPRESSING_INTEREST);
598 }
599}
600
Zhenkai Zhue5660932012-06-04 15:25:20 -0700601string
602SyncLogic::getRootDigest()
603{
604 ostringstream os;
605 recursive_mutex::scoped_lock lock (m_stateMutex);
606 os << *m_state->getDigest();
607 return os.str();
608}
609
Alexander Afanasyev80237382012-10-04 10:20:47 -0700610size_t
611SyncLogic::getNumberOfBranches () const
612{
613 recursive_mutex::scoped_lock lock (m_stateMutex);
614 return m_state->getLeaves ().size ();
615}
616
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700617void
618SyncLogic::printState () const
619{
620 recursive_mutex::scoped_lock lock (m_stateMutex);
621
622 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
623 {
624 std::cout << *leaf << std::endl;
625 }
626}
627
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700628std::map<std::string, bool>
629SyncLogic::getBranchPrefixes() const
630{
631 recursive_mutex::scoped_lock lock (m_stateMutex);
632
633 std::map<std::string, bool> m;
634
635 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
636 {
637 std::string prefix = leaf->getInfo()->toString();
638 // do not return forwarder prefix
639 if (prefix != forwarderPrefix)
640 {
641 m.insert(pair<std::string, bool>(prefix, false));
642 }
643 }
644
645 return m;
646}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700647
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800648}