blob: 7b45e38fe433bff7696781e55be4982d2b6d788b [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
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080024#include "sync-diff-leaf.h"
25#include "sync-full-leaf.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070026#include "sync-log.h"
27
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080028#include <boost/make_shared.hpp>
29#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030#include <boost/lexical_cast.hpp>
31#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080032#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080033
34using namespace std;
35using namespace boost;
36
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070037#ifndef _STANDALONE
38INIT_LOGGER ("SyncLogic");
39#endif
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070040
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041namespace Sync
42{
43
Alexander Afanasyev750d1872012-03-12 15:33:56 -070044SyncLogic::SyncLogic (const std::string &syncPrefix,
45 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070046 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080047 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070048 , m_onUpdate (onUpdate)
49 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070050 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070051#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -070052 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070053 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,80))
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070054#else
55 , m_rangeUniformRandom (20,80)
56#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057{
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070058#ifdef _STANDALONE
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070059#ifdef _DEBUG
60#ifdef HAVE_LOG4CXX
61 // _LOG_FUNCTION (syncPrefix);
62 static int id = 0;
63 staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id));
64 id ++;
65#endif
66#endif
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070067#endif
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070068
69 _LOG_DEBUG ("syncPrefix");
70
71#ifndef NS3_MODULE
72 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070073
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070074 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070075 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070076
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070077 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070078 bind (&SyncLogic::sendSyncInterest, this),
79 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070080#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080081}
82
Alexander Afanasyevc1030192012-03-08 22:21:28 -080083SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080084{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070085 // _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070086 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080087
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070088 m_ccnxHandle.reset ();
89}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070090
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070091#ifdef NS3_MODULE
92void
93SyncLogic::StartApplication ()
94{
95 m_ccnxHandle->SetNode (GetNode ());
96 m_ccnxHandle->StartApplication ();
97
98 m_ccnxHandle->setInterestFilter (m_syncPrefix,
99 bind (&SyncLogic::respondSyncInterest, this, _1));
100
101 m_scheduler.schedule (TIME_SECONDS (0),
102 bind (&SyncLogic::sendSyncInterest, this),
103 REEXPRESSING_INTEREST);
104}
105
106void
107SyncLogic::StopApplication ()
108{
Alexander Afanasyev434f1612012-04-21 21:19:15 -0700109 m_ccnxHandle->clearInterestFilter (m_syncPrefix);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700110 m_ccnxHandle->StopApplication ();
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700111 m_scheduler.cancel (REEXPRESSING_INTEREST);
112 m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -0700113}
114#endif
115
116
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700117void
118SyncLogic::respondSyncInterest (const string &interest)
119{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700120 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700121 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700122 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700123 // cout << "Received Sync Interest: " << hash << endl;
124
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700125 DigestPtr digest = make_shared<Digest> ();
126 try
127 {
128 istringstream is (hash);
129 is >> *digest;
130 }
131 catch (Error::DigestCalculationError &e)
132 {
133 // log error. ignoring it for now, later we should log it
134 return;
135 }
136
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700137 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700138}
139
140void
141SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
142{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700143 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700144
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700145 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700146 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700147 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700148 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
149
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700150 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700151 m_ccnxHandle->publishData (interestName + "/state",
152 lexical_cast<string> (m_state),
153 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700154 if (m_outstandingInterest == interestName)
155 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700156 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700157 bind (&SyncLogic::sendSyncInterest, this),
158 REEXPRESSING_INTEREST);
159 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700160 return;
161 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700162
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700163 if (*m_state.getDigest() == *digest)
164 {
165 // cout << interestName << "\n";
166 if (digest->isZero ())
167 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700168 _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700169 m_syncInterestTable.insert (interestName + "/state");
170 }
171 else
172 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700173 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700174 m_syncInterestTable.insert (interestName);
175 }
176 return;
177 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700178
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700179 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
180
181 if (stateInDiffLog != m_log.end ())
182 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700183 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
184 // string state = lexical_cast<string> (*stateDiff);
185 // erase_all (state, "\n");
186 // _LOG_TRACE (">> D " << interestName << ", state: " << state);
187 // _LOG_DEBUG ("Log size: " << m_log.size ());
188
189 // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
190 // {
191 // string state = lexical_cast<string> (*ds);
192 // erase_all (state, "\n");
193 // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ());
194 // }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700195
196 m_syncInterestTable.remove (interestName);
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700197 _LOG_DEBUG (">> D" << interestName);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700198 m_ccnxHandle->publishData (interestName,
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700199 lexical_cast<string> (*stateDiff),
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700200 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700201 if (m_outstandingInterest == interestName)
202 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700203 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700204 bind (&SyncLogic::sendSyncInterest, this),
205 REEXPRESSING_INTEREST);
206 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700207 return;
208 }
209
210 if (!timedProcessing)
211 {
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700212 uint32_t waitDelay =
213#ifndef NS3_MODULE
214 m_rangeUniformRandom ()
215#else
216 m_rangeUniformRandom.GetValue ()
217#endif
218 ;
219
220 _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
221 m_scheduler.schedule (TIME_MILLISECONDS (waitDelay) /*from 20 to 100ms*/,
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700222 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
223 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700224
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700225 }
226 else
227 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700228 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700229
230 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700231 m_ccnxHandle->publishData (interestName + "/state",
232 lexical_cast<string> (m_state),
233 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700234
235 if (m_outstandingInterest == interestName)
236 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700237 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700238 bind (&SyncLogic::sendSyncInterest, this),
239 REEXPRESSING_INTEREST);
240 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700241 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800242}
243
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800244void
245SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800246{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700247 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700248 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700249
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700250 try
251 {
252 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700253
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700254 string last = name.substr(name.find_last_of("/") + 1);
255 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700256
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700257 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 -0700258 if (last == "state")
259 {
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700260 m_syncInterestTable.remove (name.substr(0, name.find_last_of("/")));
261
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700262 FullState full;
263 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700264 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700265 {
266 NameInfoConstPtr info = leaf->getInfo ();
267 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700268
269 bool inserted = false;
270 bool updated = false;
271 SeqNo oldSeq;
272 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800273
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700274 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700275 {
276 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700277 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700278 }
279 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700280 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700281 else
282 {
283 DiffState diff;
284 ss >> diff;
285 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
286 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700287 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
288 BOOST_ASSERT (diffLeaf != 0);
289
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700290 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700291 if (diffLeaf->getOperation() == UPDATE)
292 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700293 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800294
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700295 bool inserted = false;
296 bool updated = false;
297 SeqNo oldSeq;
298 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800299
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700300 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700301 {
302 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700303 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700304 }
305 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700306 else if (diffLeaf->getOperation() == REMOVE)
307 {
308 if (m_state.remove (info))
309 {
310 diffLog->remove (info);
311 m_onRemove (info->toString ());
312 }
313 }
314 else
315 {
316 BOOST_ASSERT (false); // just in case
317 }
318 }
319 }
320
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700321 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700322 }
323 catch (Error::SyncXmlDecodingFailure &e)
324 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700325 diffLog.reset ();
326 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700327 }
328
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700329 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700330 if (diffLog->getLeaves ().size () > 0)
331 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700332 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700333 bind (&SyncLogic::sendSyncInterest, this),
334 REEXPRESSING_INTEREST);
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700335 }
336 else
337 {
338 // should not reexpress the same interest. Need at least wait for data lifetime
339 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
340 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700341 // m_scheduler.schedule (posix_time::seconds (0),
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700342 m_scheduler.schedule (TIME_SECONDS (m_syncResponseFreshness) + TIME_MILLISECONDS (1),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700343 bind (&SyncLogic::sendSyncInterest, this),
344 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700345 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800346}
347
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800348void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700349SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800350{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800351 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700352 if (pis.size () > 0)
353 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700354 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700355 ss << *diffLog;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700356 bool satisfiedOwnInterest = false;
357
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700358 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
359 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700360 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700361 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700362
363 {
364 recursive_mutex::scoped_lock lock (m_stateMutex);
365 // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
366 satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
367 }
368 }
369
370 if (satisfiedOwnInterest)
371 {
372 _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
373 // we need to reexpress interest only if we satisfied our own interest
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700374 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700375 bind (&SyncLogic::sendSyncInterest, this),
376 REEXPRESSING_INTEREST);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700377 }
378 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800379}
380
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800381void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700382SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700383{
384 //cout << "Process Pending Interests" <<endl;
385 diffLog->setDigest (m_state.getDigest());
386 if (m_log.size () > 0)
387 {
388 m_log.get<sequenced> ().front ()->setNext (diffLog);
389 }
390 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
391 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700392 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700393 // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700394}
395
396void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700397SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
398{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700399 DiffStatePtr diff;
400 {
401 //cout << "Add local names" <<endl;
402 recursive_mutex::scoped_lock lock (m_stateMutex);
403 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700404
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700405 SeqNo seqN (session, seq);
406 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700407
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700408 _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
409
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700410 diff = make_shared<DiffState>();
411 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700412 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700413 }
414
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700415 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
416 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700417}
418
419void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700420SyncLogic::remove(const string &prefix)
421{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700422 DiffStatePtr diff;
423 {
424 recursive_mutex::scoped_lock lock (m_stateMutex);
425 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
426 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700427
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700428 diff = make_shared<DiffState>();
429 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700430
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700431 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700432 }
433
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700434 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700435}
436
437void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800438SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800439{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800440 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800441
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700442 {
443 // cout << "Sending Sync Interest" << endl;
444 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700445
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700446 os << m_syncPrefix << "/" << *m_state.getDigest();
447
448 _LOG_TRACE (">> I " << os.str ());
449
450 m_outstandingInterest = os.str ();
451 }
452
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800453 m_ccnxHandle->sendInterest (os.str (),
454 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700455
456 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700457 m_scheduler.schedule (TIME_SECONDS (4),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700458 bind (&SyncLogic::sendSyncInterest, this),
459 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800460}
461
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800462}