blob: 234f059e3e06a53b009e03041cb71cdea9a53bf7 [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{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700219 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700220
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700221 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700222 if (digest->isZero () && !m_state->getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700223 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700224 sendSyncData (name, digest, m_state);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700225 return;
226 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700227
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700228 if (*m_state->getDigest() == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700229 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700230 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700231 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700232 return;
233 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700234
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700235 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
236
237 if (stateInDiffLog != m_log.end ())
238 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700239 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700240
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700241 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700242 return;
243 }
244
245 if (!timedProcessing)
246 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700247 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700248 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700249 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700250 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
251 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700252 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700253
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700254 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
255 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700256
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700257 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
258 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700259 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700260 }
261 else
262 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700263 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700264
265 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700266 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700267 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800268}
269
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800270void
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700271SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800272{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700273 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700274 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700275
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700276 try
277 {
278 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700279
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700280 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700281
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700282 ownInterestSatisfied = (name == m_outstandingInterestName);
283
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700284 DiffState diff;
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700285 SyncStateMsg msg;
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700286 if (!msg.ParseFromArray(wireData, len) || !msg.IsInitialized())
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700287 {
288 //Throw
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700289 BOOST_THROW_EXCEPTION (Error::SyncStateMsgDecodingFailure () );
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700290 }
291 msg >> diff;
292
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700293 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700294 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700295 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700296 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
297 BOOST_ASSERT (diffLeaf != 0);
298
299 NameInfoConstPtr info = diffLeaf->getInfo();
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700300 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700301 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700302 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700303
304 bool inserted = false;
305 bool updated = false;
306 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700307 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800308
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700309 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700310 {
311 diffLog->update (info, seq);
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700312 //m_onUpdate (info->toString (), seq, oldSeq);
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700313 if (!oldSeq.isValid())
314 {
315 oldSeq = SeqNo(seq.getSession(), 0);
316 }
317 else
318 {
319 ++oldSeq;
320 }
Zhenkai Zhuba3c1d12012-10-03 23:26:35 -0700321 // there is no need for application to process update on forwarder node
322 if (info->toString() != forwarderPrefix)
323 {
324 MissingDataInfo mdi = {info->toString(), oldSeq, seq};
325 v.push_back(mdi);
326 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700327 }
328 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700329 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700330 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700331 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700332 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700333 diffLog->remove (info);
334 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700335 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700336 }
337 else
338 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700339 }
340 }
341
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700342 if (!v.empty())
343 {
344 m_onUpdate(v);
345 }
346
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700347 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700348 }
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700349 catch (Error::SyncStateMsgDecodingFailure &e)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700350 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700351 _LOG_TRACE ("Something really fishy happened during state decoding " <<
352 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700353 diffLog.reset ();
354 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700355 }
356
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700357 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
358 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700359 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700360 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700361
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700362 // this is kind of wrong
363 // 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 -0700364
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700365 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700366 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700367 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700368 bind (&SyncLogic::sendSyncInterest, this),
369 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700370 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800371}
372
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800373void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700374SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800375{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700376 recursive_mutex::scoped_lock lock (m_stateMutex);
377
378 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
379
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700380 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700381 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700382 _LOG_TRACE ("Could not find " << *digest << " in digest log");
383 return;
384 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700385
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700386 sendSyncData (name, digest, m_state);
387}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700388
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700389void
390SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
391{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700392 DiffStatePtr fullStateLog = make_shared<DiffState> ();
393 {
394 recursive_mutex::scoped_lock lock (m_stateMutex);
395 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
396 {
397 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
398 /// @todo Impose limit on how many state info should be send out
399 }
400 }
401
402 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800403 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700404 uint32_t counter = 0;
405 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700406 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700407 Interest interest = m_syncInterestTable.pop ();
408
409 if (!interest.m_unknown)
410 {
411 _LOG_TRACE (">> D " << interest.m_name);
412 sendSyncData (interest.m_name, interest.m_digest, diffLog);
413 }
414 else
415 {
416 _LOG_TRACE (">> D (unknown)" << interest.m_name);
417 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
418 }
419 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700420 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700421 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700422 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700423 catch (Error::InterestTableIsEmpty &e)
424 {
425 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800426 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800427}
428
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800429void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700430SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700431{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700432 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700433 if (m_log.size () > 0)
434 {
435 m_log.get<sequenced> ().front ()->setNext (diffLog);
436 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700437 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 -0700438 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700439 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700440}
441
442void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700443SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
444{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700445 DiffStatePtr diff;
446 {
447 //cout << "Add local names" <<endl;
448 recursive_mutex::scoped_lock lock (m_stateMutex);
449 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700450
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700451 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700452
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700453 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700454 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700455
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700456 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700457
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700458 diff = make_shared<DiffState>();
459 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700460 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700461 }
462
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700463 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
464 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700465}
466
467void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700468SyncLogic::remove(const string &prefix)
469{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700470 DiffStatePtr diff;
471 {
472 recursive_mutex::scoped_lock lock (m_stateMutex);
473 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700474 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700475
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700476 // increment the sequence number for the forwarder node
477 NameInfoConstPtr forwarderInfo = StdNameInfo::FindOrCreate(forwarderPrefix);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700478
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700479 LeafContainer::iterator item = m_state->getLeaves ().find (forwarderInfo);
480 SeqNo seqNo (0);
481 if (item != m_state->getLeaves ().end ())
482 {
483 seqNo = (*item)->getSeq ();
484 ++seqNo;
485 }
486 m_state->update (forwarderInfo, seqNo);
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700487
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700488 diff = make_shared<DiffState>();
489 diff->remove(info);
Alexander Afanasyev7e1986f2012-10-04 10:21:10 -0700490 diff->update(forwarderInfo, seqNo);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700491
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700492 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700493 }
494
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700495 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700496}
497
498void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800499SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800500{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800501 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800502
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700503 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700504 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700505
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700506 os << m_syncPrefix << "/" << *m_state->getDigest();
507 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700508 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700509 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700510
511 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700512 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700513 bind (&SyncLogic::sendSyncInterest, this),
514 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700515
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700516 m_ccnxHandle->sendInterest (os.str (),
517 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800518}
519
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700520void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700521SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
522{
523 ostringstream os;
524 os << m_syncPrefix << "/recovery/" << *digest;
525 _LOG_TRACE (">> I " << os.str ());
526
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700527 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
528 m_recoveryRetransmissionInterval <<= 1;
529
530 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
531 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
532 {
533 m_scheduler.schedule (nextRetransmission,
534 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
535 REEXPRESSING_RECOVERY_INTEREST);
536 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700537
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700538 m_ccnxHandle->sendInterest (os.str (),
539 bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700540}
541
542
543void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700544SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
545{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700546 _LOG_TRACE (">> D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700547 // sending
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700548 SyncStateMsg ssm;
549 ssm << (*state);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700550 int size = ssm.ByteSize();
551 char *wireData = new char[size];
552 ssm.SerializeToArray(wireData, size);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700553 m_ccnxHandle->publishRawData (name,
554 wireData,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700555 size,
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700556 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Chaoyi Bian6665d5a2012-06-07 14:16:44 -0700557 delete []wireData;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700558
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700559 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700560 bool satisfiedOwnInterest = false;
561 {
562 recursive_mutex::scoped_lock lock (m_stateMutex);
563 satisfiedOwnInterest = (m_outstandingInterestName == name);
564 }
565
566 if (satisfiedOwnInterest)
567 {
568 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
569
570 m_scheduler.cancel (REEXPRESSING_INTEREST);
571 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
572 bind (&SyncLogic::sendSyncInterest, this),
573 REEXPRESSING_INTEREST);
574 }
575}
576
Zhenkai Zhue5660932012-06-04 15:25:20 -0700577string
578SyncLogic::getRootDigest()
579{
580 ostringstream os;
581 recursive_mutex::scoped_lock lock (m_stateMutex);
582 os << *m_state->getDigest();
583 return os.str();
584}
585
Alexander Afanasyev80237382012-10-04 10:20:47 -0700586size_t
587SyncLogic::getNumberOfBranches () const
588{
589 recursive_mutex::scoped_lock lock (m_stateMutex);
590 return m_state->getLeaves ().size ();
591}
592
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700593void
594SyncLogic::printState () const
595{
596 recursive_mutex::scoped_lock lock (m_stateMutex);
597
598 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
599 {
600 std::cout << *leaf << std::endl;
601 }
602}
603
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700604std::map<std::string, bool>
605SyncLogic::getBranchPrefixes() const
606{
607 recursive_mutex::scoped_lock lock (m_stateMutex);
608
609 std::map<std::string, bool> m;
610
611 BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
612 {
613 std::string prefix = leaf->getInfo()->toString();
614 // do not return forwarder prefix
615 if (prefix != forwarderPrefix)
616 {
617 m.insert(pair<std::string, bool>(prefix, false));
618 }
619 }
620
621 return m;
622}
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700623
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800624}