blob: b77f29dd2b994b99aeb4be523632cb571a5ff947 [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 Afanasyev0ac399e2012-04-27 13:28:28 -070023#include <ns3/ccnx-pit.h>
24#include <ns3/ccnx.h>
25
Chaoyi Bian11f294f2012-03-08 14:28:06 -080026#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080027#include "sync-diff-leaf.h"
28#include "sync-full-leaf.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070029#include "sync-log.h"
30
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080031#include <boost/make_shared.hpp>
32#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070033#include <boost/lexical_cast.hpp>
34#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080035#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080036
37using namespace std;
38using namespace boost;
39
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070040#ifndef _STANDALONE
41INIT_LOGGER ("SyncLogic");
42#endif
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070043
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070044
45#ifdef NS3_MODULE
46#define GET_RANDOM(var) var.GetValue ()
47#else
48#define GET_RANDOM(var) var ()
49#endif
50
51#define TIME_SECONDS_WITH_JITTER(sec) \
52 (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
53
54#define TIME_MILLISECONDS_WITH_JITTER(sec) \
55 (TIME_MILLISECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
56
57
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080058namespace Sync
59{
60
Alexander Afanasyev750d1872012-03-12 15:33:56 -070061SyncLogic::SyncLogic (const std::string &syncPrefix,
62 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070063 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080064 : 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 Afanasyev73f7f9a2012-04-09 15:45:47 -070068#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070069 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070070 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (500,1000))
Alexander Afanasyev085742a2012-04-26 12:24:57 -070071 , m_reexpressionJitter (m_randomGenerator, uniform_int<> (0,100))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070072#else
Alexander Afanasyev55608612012-04-30 13:24:50 -070073 , m_rangeUniformRandom (200,1000)
74 , m_reexpressionJitter (100,500)
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070075#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080076{
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070077#ifdef _STANDALONE
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070078#ifdef _DEBUG
79#ifdef HAVE_LOG4CXX
80 // _LOG_FUNCTION (syncPrefix);
81 static int id = 0;
82 staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id));
83 id ++;
84#endif
85#endif
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070086#endif
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070087
88#ifndef NS3_MODULE
89 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070090
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070091 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070092 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070093
Alexander Afanasyev983b0e92012-04-26 12:44:18 -070094 m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070095 bind (&SyncLogic::sendSyncInterest, this),
96 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070097#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080098}
99
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800100SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800101{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700102 // _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700103 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800104
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700105 m_ccnxHandle.reset ();
106}
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700107
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700108#ifdef NS3_MODULE
109void
110SyncLogic::StartApplication ()
111{
112 m_ccnxHandle->SetNode (GetNode ());
113 m_ccnxHandle->StartApplication ();
114
115 m_ccnxHandle->setInterestFilter (m_syncPrefix,
116 bind (&SyncLogic::respondSyncInterest, this, _1));
117
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700118 m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700119 bind (&SyncLogic::sendSyncInterest, this),
120 REEXPRESSING_INTEREST);
121}
122
123void
124SyncLogic::StopApplication ()
125{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700126 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700127 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700128 m_scheduler.cancel (REEXPRESSING_INTEREST);
129 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700130}
131#endif
132
133
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700134void
135SyncLogic::respondSyncInterest (const string &interest)
136{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700137 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700138 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700139 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700140 // cout << "Received Sync Interest: " << hash << endl;
141
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700142 DigestPtr digest = make_shared<Digest> ();
143 try
144 {
145 istringstream is (hash);
146 is >> *digest;
147 }
148 catch (Error::DigestCalculationError &e)
149 {
150 // log error. ignoring it for now, later we should log it
151 return;
152 }
153
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700154 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700155}
156
157void
158SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
159{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700160 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700161
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700162 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700163 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700164 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700165 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
166
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700167 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700168 m_ccnxHandle->publishData (interestName + "/state",
169 lexical_cast<string> (m_state),
170 m_syncResponseFreshness);
171 return;
172 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700173
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700174 if (*m_state.getDigest() == *digest)
175 {
176 // cout << interestName << "\n";
177 if (digest->isZero ())
178 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700179 _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700180 m_syncInterestTable.insert (interestName + "/state");
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -0700181 // _LOG_TRACE (*GetNode ()->GetObject<ns3::Ccnx> ()->GetPit ());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700182 }
183 else
184 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700185 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700186 m_syncInterestTable.insert (interestName);
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -0700187 // _LOG_TRACE (*GetNode ()->GetObject<ns3::Ccnx> ()->GetPit ());
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700188 }
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -0700189
190 if (m_outstandingInterest == interestName)
191 {
192 _LOG_DEBUG ("Cancelling interest reexpressing 1");
193 // !!! important change !!!
194 m_scheduler.cancel (REEXPRESSING_INTEREST);
195 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
196 bind (&SyncLogic::sendSyncInterest, this),
197 REEXPRESSING_INTEREST);
198 }
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700199 return;
200 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700201
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700202 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
203
204 if (stateInDiffLog != m_log.end ())
205 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700206 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
207 // string state = lexical_cast<string> (*stateDiff);
208 // erase_all (state, "\n");
209 // _LOG_TRACE (">> D " << interestName << ", state: " << state);
210 // _LOG_DEBUG ("Log size: " << m_log.size ());
211
212 // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
213 // {
214 // string state = lexical_cast<string> (*ds);
215 // erase_all (state, "\n");
216 // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ());
217 // }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700218
219 m_syncInterestTable.remove (interestName);
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700220 _LOG_DEBUG (">> D" << interestName);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700221 m_ccnxHandle->publishData (interestName,
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700222 lexical_cast<string> (*stateDiff),
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700223 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700224 if (m_outstandingInterest == interestName)
225 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700226 m_scheduler.cancel (REEXPRESSING_INTEREST);
227 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700228 bind (&SyncLogic::sendSyncInterest, this),
229 REEXPRESSING_INTEREST);
230 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700231 return;
232 }
233
234 if (!timedProcessing)
235 {
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700236 UnknownDigestContainer::index<hashed>::type::iterator previousUnknownDigest = m_recentUnknownDigests.get<hashed> ().find (digest);
237 if (previousUnknownDigest != m_recentUnknownDigests.get<hashed> ().end ())
238 {
239 _LOG_DEBUG ("Digest is not in the log, but we have already seen it. (Don't do anything)");
240 }
241 else
242 {
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -0700243 // m_recentUnknownDigests.insert (DigestTime (digest, TIME_NOW + TIME_SECONDS (m_unknownDigestStoreTime)));
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700244
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700245 uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700246 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700247
248 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700249 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
250 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev1f8f0682012-04-26 13:00:14 -0700251
252 // just in case, re-express our interest (e.g., probably something bad happened)
253
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -0700254 // m_scheduler.cancel (REEXPRESSING_INTEREST);
255 // m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
256 // bind (&SyncLogic::sendSyncInterest, this),
257 // REEXPRESSING_INTEREST);
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700258 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700259 }
260 else
261 {
Alexander Afanasyev280882c2012-04-30 02:33:00 -0700262 // _LOG_TRACE (" (timed processing)");
263 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700264
Alexander Afanasyev280882c2012-04-30 02:33:00 -0700265 m_syncInterestTable.remove (interestName + "/state");
266 m_ccnxHandle->publishData (interestName + "/state",
267 lexical_cast<string> (m_state),
268 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700269
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -0700270 // exit (1);
271 // if (m_outstandingInterest == interestName)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700272 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700273 m_scheduler.cancel (REEXPRESSING_INTEREST);
274 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700275 bind (&SyncLogic::sendSyncInterest, this),
276 REEXPRESSING_INTEREST);
277 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700278 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800279}
280
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800281void
282SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800283{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700284 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700285 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700286
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700287 try
288 {
289 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700290
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700291 string last = name.substr(name.find_last_of("/") + 1);
292 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700293
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700294 m_syncInterestTable.remove (name); // just in case, remove both interests from our interest table. No reason to keep them there
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700295 if (last == "state")
296 {
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700297 m_syncInterestTable.remove (name.substr(0, name.find_last_of("/")));
298
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700299 FullState full;
300 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700301 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700302 {
303 NameInfoConstPtr info = leaf->getInfo ();
304 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700305
306 bool inserted = false;
307 bool updated = false;
308 SeqNo oldSeq;
309 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800310
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700311 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700312 {
313 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700314 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700315 }
316 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700317 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700318 else
319 {
320 DiffState diff;
321 ss >> diff;
322 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
323 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700324 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
325 BOOST_ASSERT (diffLeaf != 0);
326
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700327 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700328 if (diffLeaf->getOperation() == UPDATE)
329 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700330 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800331
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700332 bool inserted = false;
333 bool updated = false;
334 SeqNo oldSeq;
335 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800336
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700337 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700338 {
339 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700340 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700341 }
342 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700343 else if (diffLeaf->getOperation() == REMOVE)
344 {
345 if (m_state.remove (info))
346 {
347 diffLog->remove (info);
348 m_onRemove (info->toString ());
349 }
350 }
351 else
352 {
353 BOOST_ASSERT (false); // just in case
354 }
355 }
356 }
357
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700358 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700359 }
360 catch (Error::SyncXmlDecodingFailure &e)
361 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700362 diffLog.reset ();
363 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700364 }
365
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700366 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700367 if (diffLog->getLeaves ().size () > 0)
368 {
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700369 m_scheduler.cancel (REEXPRESSING_INTEREST);
370 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700371 bind (&SyncLogic::sendSyncInterest, this),
372 REEXPRESSING_INTEREST);
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700373 }
374 else
375 {
376 // should not reexpress the same interest. Need at least wait for data lifetime
377 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
378 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700379 m_scheduler.schedule (TIME_MILLISECONDS_WITH_JITTER (m_syncResponseFreshness),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700380 bind (&SyncLogic::sendSyncInterest, this),
381 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700382 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800383}
384
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800385void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700386SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800387{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800388 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700389 if (pis.size () > 0)
390 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700391 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700392 ss << *diffLog;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700393 bool satisfiedOwnInterest = false;
394
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700395 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
396 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700397 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700398 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700399
400 {
401 recursive_mutex::scoped_lock lock (m_stateMutex);
402 // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
403 satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
404 }
405 }
406
407 if (satisfiedOwnInterest)
408 {
409 _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
410 // we need to reexpress interest only if we satisfied our own interest
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700411 m_scheduler.cancel (REEXPRESSING_INTEREST);
412 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700413 bind (&SyncLogic::sendSyncInterest, this),
414 REEXPRESSING_INTEREST);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700415 }
416 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800417}
418
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800419void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700420SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700421{
422 //cout << "Process Pending Interests" <<endl;
423 diffLog->setDigest (m_state.getDigest());
424 if (m_log.size () > 0)
425 {
426 m_log.get<sequenced> ().front ()->setNext (diffLog);
427 }
428 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
429 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700430 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700431 // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700432}
433
434void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700435SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
436{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700437 DiffStatePtr diff;
438 {
439 //cout << "Add local names" <<endl;
440 recursive_mutex::scoped_lock lock (m_stateMutex);
441 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700442
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700443 SeqNo seqN (session, seq);
444 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700445
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700446 _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
447
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700448 diff = make_shared<DiffState>();
449 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700450 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700451 }
452
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700453 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
454 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700455}
456
457void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700458SyncLogic::remove(const string &prefix)
459{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700460 DiffStatePtr diff;
461 {
462 recursive_mutex::scoped_lock lock (m_stateMutex);
463 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
464 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700465
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700466 diff = make_shared<DiffState>();
467 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700468
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700469 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700470 }
471
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700472 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700473}
474
475void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800476SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800477{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800478 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800479
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700480 {
481 // cout << "Sending Sync Interest" << endl;
482 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700483
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700484 os << m_syncPrefix << "/" << *m_state.getDigest();
485
486 _LOG_TRACE (">> I " << os.str ());
487
488 m_outstandingInterest = os.str ();
489 }
490
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800491 m_ccnxHandle->sendInterest (os.str (),
492 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700493
494 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev983b0e92012-04-26 12:44:18 -0700495 m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700496 bind (&SyncLogic::sendSyncInterest, this),
497 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800498}
499
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800500}