blob: b44c2e8f65818404b34e88e6672eef438d3634bc [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"
32
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080033#include <boost/make_shared.hpp>
34#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070035#include <boost/lexical_cast.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080036#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080037
38using namespace std;
39using namespace boost;
40
Chaoyi Bianf007f922012-04-09 20:05:37 -070041INIT_LOGGER ("SyncLogic");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070042
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070043#ifdef NS3_MODULE
44#define GET_RANDOM(var) var.GetValue ()
45#else
46#define GET_RANDOM(var) var ()
47#endif
48
49#define TIME_SECONDS_WITH_JITTER(sec) \
50 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
51
Alexander Afanasyev42612262012-05-05 10:58:37 -070052#define TIME_MILLISECONDS_WITH_JITTER(ms) \
53 (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070054
55
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080056namespace Sync
57{
58
Alexander Afanasyev750d1872012-03-12 15:33:56 -070059SyncLogic::SyncLogic (const std::string &syncPrefix,
60 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070061 LogicRemoveCallback onRemove)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070062 : m_state (new FullState)
63 , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
64 , m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070065 , m_onUpdate (onUpdate)
66 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070067 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyeva76010b2012-05-24 21:31:49 -070068 , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070069#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070070 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070071 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
72 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070073#else
Alexander Afanasyev55608612012-04-30 13:24:50 -070074 , m_rangeUniformRandom (200,1000)
Alexander Afanasyeva76010b2012-05-24 21:31:49 -070075 , m_reexpressionJitter (10,500)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070076#endif
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070077{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070078#ifndef NS3_MODULE
79 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070080
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070081 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070082 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070083
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070084 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070085 bind (&SyncLogic::sendSyncInterest, this),
86 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070087#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080088}
89
Alexander Afanasyevc1030192012-03-08 22:21:28 -080090SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080091{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070092 m_ccnxHandle.reset ();
93}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070094
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070095#ifdef NS3_MODULE
Alexander Afanasyev387ac952012-03-11 23:49:27 -070096void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070097SyncLogic::StartApplication ()
Alexander Afanasyev387ac952012-03-11 23:49:27 -070098{
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070099 m_ccnxHandle->SetNode (GetNode ());
100 m_ccnxHandle->StartApplication ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700101
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700102 m_ccnxHandle->setInterestFilter (m_syncPrefix,
103 bind (&SyncLogic::respondSyncInterest, this, _1));
104
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700105 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700106 bind (&SyncLogic::sendSyncInterest, this),
107 REEXPRESSING_INTEREST);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700108}
109
110void
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700111SyncLogic::StopApplication ()
112{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700113 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700114 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700115 m_scheduler.cancel (REEXPRESSING_INTEREST);
116 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700117}
118#endif
119
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700120/**
121 * Two types of intersts
122 *
123 * Normal name: .../<hash>
124 * Recovery name: .../recovery/<hash>
125 */
126boost::tuple<DigestConstPtr, std::string>
127SyncLogic::convertNameToDigestAndType (const std::string &name)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700128{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700129 BOOST_ASSERT (name.find (m_syncPrefix) == 0);
130
131 string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700132 if (hash[0] == '/')
133 hash = hash.substr (1, hash.size ()-1);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700134 string interestType = "normal";
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700135
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700136 size_t pos = hash.find ('/');
137 if (pos != string::npos)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700138 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700139 interestType = hash.substr (0, pos);
140 hash = hash.substr (pos + 1);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700141 }
142
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700143 _LOG_TRACE (hash << ", " << interestType);
144
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700145 DigestPtr digest = make_shared<Digest> ();
146 istringstream is (hash);
147 is >> *digest;
148
149 return make_tuple (digest, interestType);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700150}
151
152void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700153SyncLogic::respondSyncInterest (const string &name)
154{
155 try
156 {
157 _LOG_TRACE ("<< I " << name);
158
159 DigestConstPtr digest;
160 string type;
161 tie (digest, type) = convertNameToDigestAndType (name);
162
163 if (type == "normal") // kind of ineffective...
164 {
165 processSyncInterest (name, digest);
166 }
167 else if (type == "recovery")
168 {
169 processSyncRecoveryInterest (name, digest);
170 }
171 }
172 catch (Error::DigestCalculationError &e)
173 {
174 _LOG_TRACE ("Something fishy happened...");
175 // log error. ignoring it for now, later we should log it
176 return ;
177 }
178}
179
180void
181SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer)
182{
183 try
184 {
185 _LOG_TRACE ("<< D " << name);
186
187 DigestConstPtr digest;
188 string type;
189 tie (digest, type) = convertNameToDigestAndType (name);
190
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700191 if (type == "normal")
192 {
193 processSyncData (name, digest, dataBuffer);
194 }
195 else
196 {
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700197 // timer is always restarted when we schedule recovery
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700198 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
199 processSyncData (name, digest, dataBuffer);
200 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700201 }
202 catch (Error::DigestCalculationError &e)
203 {
204 _LOG_TRACE ("Something fishy happened...");
205 // log error. ignoring it for now, later we should log it
206 return;
207 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700208}
209
210
211void
212SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700213{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700214 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700215
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700216 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700217 if (digest->isZero () && !m_state->getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700218 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700219 sendSyncData (name, digest, m_state);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700220 return;
221 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700222
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700223 if (*m_state->getDigest() == *digest)
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700224 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700225 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700226 m_syncInterestTable.insert (digest, name, false);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700227 return;
228 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700229
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700230 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
231
232 if (stateInDiffLog != m_log.end ())
233 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700234 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700235
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700236 sendSyncData (name, digest, stateDiff);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700237 return;
238 }
239
240 if (!timedProcessing)
241 {
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700242 bool exists = m_syncInterestTable.insert (digest, name, true);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700243 if (exists) // somebody else replied, so restart random-game timer
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700244 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700245 _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
246 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700247 }
Alexander Afanasyev03793b42012-05-01 13:03:07 -0700248
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700249 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
250 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700251
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700252 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
253 bind (&SyncLogic::processSyncInterest, this, name, digest, true),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700254 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700255 }
256 else
257 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700258 _LOG_TRACE (" (timed processing)");
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700259
260 m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700261 sendSyncRecoveryInterests (digest);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700262 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800263}
264
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800265void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700266SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800267{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700268 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700269 bool ownInterestSatisfied = false;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700270
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700271 try
272 {
273 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700274
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700275 m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700276
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700277 ownInterestSatisfied = (name == m_outstandingInterestName);
278
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700279 DiffState diff;
280 istringstream ss (dataBuffer);
281 ss >> diff;
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700282 vector<MissingDataInfo> v;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700283 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700284 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700285 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
286 BOOST_ASSERT (diffLeaf != 0);
287
288 NameInfoConstPtr info = diffLeaf->getInfo();
289 if (diffLeaf->getOperation() == UPDATE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700290 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700291 SeqNo seq = diffLeaf->getSeq();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700292
293 bool inserted = false;
294 bool updated = false;
295 SeqNo oldSeq;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700296 tie (inserted, updated, oldSeq) = m_state->update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800297
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700298 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700299 {
300 diffLog->update (info, seq);
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700301 //m_onUpdate (info->toString (), seq, oldSeq);
302 MissingDataInfo mdi;
303 mdi.prefix = info->toString();
304 mdi.low = oldSeq;
305 mdi.high = seq;
306 v.push_back(mdi);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700307 }
308 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700309 else if (diffLeaf->getOperation() == REMOVE)
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700310 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700311 if (m_state->remove (info))
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700312 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700313 diffLog->remove (info);
314 m_onRemove (info->toString ());
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700315 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700316 }
317 else
318 {
319 BOOST_ASSERT (false); // just in case
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700320 }
321 }
322
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700323 if (!v.empty())
324 {
325 m_onUpdate(v);
326 }
327
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700328 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700329 }
330 catch (Error::SyncXmlDecodingFailure &e)
331 {
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700332 _LOG_TRACE ("Something really fishy happened during state decoding " <<
333 diagnostic_information (e));
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700334 diffLog.reset ();
335 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700336 }
337
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -0700338 if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
339 ownInterestSatisfied)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700340 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700341 // Do it only if everything went fine and state changed
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700342
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700343 // this is kind of wrong
344 // 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 -0700345
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700346 // if state has changed, then it is safe to express a new interest
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700347 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700348 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700349 bind (&SyncLogic::sendSyncInterest, this),
350 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700351 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800352}
353
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800354void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700355SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800356{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700357 recursive_mutex::scoped_lock lock (m_stateMutex);
358
359 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
360
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700361 if (stateInDiffLog == m_log.end ())
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700362 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700363 _LOG_TRACE ("Could not find " << *digest << " in digest log");
364 return;
365 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700366
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700367 sendSyncData (name, digest, m_state);
368}
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700369
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700370void
371SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
372{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700373 DiffStatePtr fullStateLog = make_shared<DiffState> ();
374 {
375 recursive_mutex::scoped_lock lock (m_stateMutex);
376 BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
377 {
378 fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
379 /// @todo Impose limit on how many state info should be send out
380 }
381 }
382
383 try
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800384 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700385 uint32_t counter = 0;
386 while (m_syncInterestTable.size () > 0)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700387 {
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700388 Interest interest = m_syncInterestTable.pop ();
389
390 if (!interest.m_unknown)
391 {
392 _LOG_TRACE (">> D " << interest.m_name);
393 sendSyncData (interest.m_name, interest.m_digest, diffLog);
394 }
395 else
396 {
397 _LOG_TRACE (">> D (unknown)" << interest.m_name);
398 sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
399 }
400 counter ++;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700401 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700402 _LOG_DEBUG ("Satisfied " << counter << " pending interests");
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700403 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700404 catch (Error::InterestTableIsEmpty &e)
405 {
406 // ok. not really an error
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800407 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800408}
409
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800410void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700411SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700412{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700413 diffLog->setDigest (m_state->getDigest());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700414 if (m_log.size () > 0)
415 {
416 m_log.get<sequenced> ().front ()->setNext (diffLog);
417 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700418 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 -0700419 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700420 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700421}
422
423void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700424SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
425{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700426 DiffStatePtr diff;
427 {
428 //cout << "Add local names" <<endl;
429 recursive_mutex::scoped_lock lock (m_stateMutex);
430 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700431
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700432 _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700433
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700434 SeqNo seqN (session, seq);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700435 m_state->update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700436
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700437 _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700438
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700439 diff = make_shared<DiffState>();
440 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700441 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700442 }
443
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700444 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
445 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700446}
447
448void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700449SyncLogic::remove(const string &prefix)
450{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700451 DiffStatePtr diff;
452 {
453 recursive_mutex::scoped_lock lock (m_stateMutex);
454 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700455 m_state->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700456
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700457 diff = make_shared<DiffState>();
458 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700459
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 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700464}
465
466void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800467SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800468{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800469 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800470
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700471 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700472 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700473
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700474 os << m_syncPrefix << "/" << *m_state->getDigest();
475 m_outstandingInterestName = os.str ();
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700476 _LOG_TRACE (">> I " << os.str ());
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700477 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700478
479 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700480 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700481 bind (&SyncLogic::sendSyncInterest, this),
482 REEXPRESSING_INTEREST);
Alexander Afanasyev42612262012-05-05 10:58:37 -0700483
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700484 m_ccnxHandle->sendInterestForString (os.str (),
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700485 bind (&SyncLogic::respondSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800486}
487
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700488void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700489SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
490{
491 ostringstream os;
492 os << m_syncPrefix << "/recovery/" << *digest;
493 _LOG_TRACE (">> I " << os.str ());
494
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700495 TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
496 m_recoveryRetransmissionInterval <<= 1;
497
498 m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
499 if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
500 {
501 m_scheduler.schedule (nextRetransmission,
502 bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
503 REEXPRESSING_RECOVERY_INTEREST);
504 }
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700505
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700506 m_ccnxHandle->sendInterestForString (os.str (),
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700507 bind (&SyncLogic::respondSyncData, this, _1, _2));
508}
509
510
511void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700512SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
513{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700514 _LOG_TRACE (">> D " << name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700515 // sending
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700516 m_ccnxHandle->publishStringData (name,
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700517 lexical_cast<string> (*state),
518 m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700519
Alexander Afanasyev282a10b2012-05-09 12:56:38 -0700520 // checking if our own interest got satisfied
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700521 bool satisfiedOwnInterest = false;
522 {
523 recursive_mutex::scoped_lock lock (m_stateMutex);
524 satisfiedOwnInterest = (m_outstandingInterestName == name);
525 }
526
527 if (satisfiedOwnInterest)
528 {
529 _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
530
531 m_scheduler.cancel (REEXPRESSING_INTEREST);
532 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
533 bind (&SyncLogic::sendSyncInterest, this),
534 REEXPRESSING_INTEREST);
535 }
536}
537
538
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800539}